1.学习的视频:JDBC封装2(直播)
2.学习的内容:
1)策略模式(Strategy),定义了一组算法,将每个算法都封装起来,并且使它们之间可以互换。(流程都一样,只有一点不一样)
结构:1.类的方法 用来规定流程(接口 参数)
2.类的方法 提供策略
补充:策略这个词应该怎么理解,打个比方说,我们出门的时候会选择不同的出行方式,比如骑自行车、坐公交、坐火车、坐飞机、坐火箭等等,这些出行方式,每一种都是一个策略。
2)映射关系(mapping ),用于实现面向对象编程语言里不同类型系统的数据之间的转换。映射关系一般指对象关系映射,对象关系映射成功运用在不同的面向对象持久层中。
3)泛型,用来规定数据类型
注意: 泛型 造型
在类或接口描述的时候 可以使用某种符号来表示一个未知的类型
在类型使用的时候 需要一个具体类型来代替
注意: 泛型需要使用引用数据类型来代替
1.泛型类 2.泛型接口 3.泛型方法 4.方法参数泛型限制
例:
4)匿名内部类,可以在定义一个类的同时对其进行实例化。它与局部类很相似,不同的是它没有类名,如果某个局部类只需要用一次,那么就可以使用匿名内部类
3.没学会的内容:工厂模式的使用方式以及场景
4.代码练习
1)单条SQL语句查询
// 设计一个方法 可以执行任何一个表格的任何一个单条查询// SQL Object[]// 返回值--对象--通用 Object 或者 <T>public <T>T superSelect(String sql,RowMapper rm,Object... objs) {T obj = null;try {Class.forName(className);//加载类 类中的静态元素就执行啦Connection conn = DriverManager.getConnection(url, user, password);//创建状态参数PreparedStatement pstat = conn.prepareStatement(sql);//将SQL语句中的 "?" 赋值for(int i=0;i<obj.length;i++) {pstat.setObject(i+1,obj[i]);}ResultSet rs = pstat.executeQuery();/*** 若查询多条SQL语句则将if改为while* 并将返回值类型改为list集合 <T> List<T>*///将查询语句拿出来 并存入if(rs.next()){obj = rm.mapperRow(rs);//执行策略模式//利用map集合-->可读性不好// if (rs.next()) {// 获取结果集中所有的列// ResultSetMetaData rsmd = rs.getMetaData();// int count = rsmd.getColumnCount();//获取列的个数// for (int i = 0; i < count; i++) {//遍历列// HashMap<String, Object> map = new HashMap();// String key = rsmd.getColumnName(i);// Object value = rs.getObject(key);// map.put(key, value);// }}rs.close();pstat.close();conn.close();} catch (ClassNotFoundException e) {e.printStackTrace();} catch (SQLException e) {e.printStackTrace();}return obj;}
2)考试系统题目存储与读取
package domain;public class Question {private String title;private String answer;public Question(){}public Question(String title, String answer) {this.title = title;this.answer = answer;}public String getTitle() {return title;}public void setTitle(String title) {this.title = title;}public String getAnswer() {return answer;}public void setAnswer(String answer) {this.answer = answer;}//重写Question类中的 equals hashCode -->目的去掉重复元素@Override//检测方法是否错误public int hashCode(){//根据题目算的String thisTitle = this.title.substring(0,this.title.indexOf("<br>"));return thisTitle.hashCode();//String的hashcode}@Overridepublic boolean equals(Object obj){if(this==obj){//判断地址是否一致return true;}//判断是否是同一个类型if(obj instanceof Question){//类型不同-->造型Question anotherQuestion = (Question)obj;String thisTitle = this.title.substring(0,this.title.indexOf("<br>"));String anotherTitle = anotherQuestion.title.substring(0,anotherQuestion.title.indexOf("<br>"));if(thisTitle.equals(anotherTitle)){return true;//同一个题干}}return false;}}
public class QuestionService {//在Service层里需要一个底层dao的支持private QuestionDao dao = MySpring.getBean("dao.QuestionDao");//设计一个方法 获取随机产生的试卷 试卷题目数量public ArrayList<Question> getPaper(int count){return dao.getPaper(count);}}
public class QuestionDao {//获取缓存对象private QuestionFileReader reader = MySpring.getBean("util.QuestionFileReader");//由于Set集合不能随机抽取,是无序的,所以将缓存集合改为list集合private ArrayList<Question> questionBank = new ArrayList(reader.getQuestionBox());//读取缓存 生成题库public ArrayList<Question> getPaper(int count){HashSet<Question> paper = new HashSet<>();//用来存储最终的试卷题目while(paper.size()!=count){Random r = new Random();// 题库的总长度int index = r.nextInt(this.questionBank.size());//随机产生的一个题目索引位置paper.add(this.questionBank.get(index));}// 再构建回去return new ArrayList<Question>(paper);}}
public class QuestionFileReader {//程序执行时 将文件中的所有题目 缓存到集合里-->缓存流private HashSet<Question> questionBox = new HashSet<>();{//try {BufferedReader reader = new BufferedReader(new FileReader("src//dbfile//Question.txt"));String message = reader.readLine();//每一次读取一行 title#answerwhile(message!=null){String[] values = message.split("#");questionBox.add(new Question(values[0],values[1]));message = reader.readLine();}reader.close();} catch (Exception e) {e.printStackTrace();}}//将集合里的返回public HashSet<Question> getQuestionBox(){return questionBox;}}
总结:
通过今天的学习,了解到策略模式的使用方式以及场景,对于优化JDBC—>查询SQL语句方法,解决连接数据库的代码冗余有了一定的掌握,并且更深层次认识MVC分层架构思想,今天上课学习的代码虽然能看懂,但是在理解以及编码方面还有欠缺,emmm稀碎的基础,多多练习吧
