1.学习的视频:JDBC封装2(直播)

    2.学习的内容
    1)策略模式(Strategy,定义了一组算法,将每个算法都封装起来,并且使它们之间可以互换。(流程都一样,只有一点不一样)
    结构:1.类的方法 用来规定流程(接口 参数)
    2.类的方法 提供策略
    补充:策略这个词应该怎么理解,打个比方说,我们出门的时候会选择不同的出行方式,比如骑自行车、坐公交、坐火车、坐飞机、坐火箭等等,这些出行方式,每一种都是一个策略。
    2)映射关系(mapping ),用于实现面向对象编程语言里不同类型系统的数据之间的转换。映射关系一般指对象关系映射,对象关系映射成功运用在不同的面向对象持久层中。
    3)泛型,用来规定数据类型
    注意: 泛型 造型
    在类或接口描述的时候 可以使用某种符号来表示一个未知的类型
    在类型使用的时候 需要一个具体类型来代替
    注意: 泛型需要使用引用数据类型来代替
    1.泛型类 2.泛型接口 3.泛型方法 4.方法参数泛型限制
    例: T —>第一个T指的是定义,方法中用了一个泛型,第二个T是返回值的类型
    4)匿名内部类,可以在定义一个类的同时对其进行实例化。它与局部类很相似,不同的是它没有类名,如果某个局部类只需要用一次,那么就可以使用匿名内部类
    3.没学会的内容:工厂模式的使用方式以及场景

    4.代码练习
    1)单条SQL语句查询

    1. // 设计一个方法 可以执行任何一个表格的任何一个单条查询
    2. // SQL Object[]
    3. // 返回值--对象--通用 Object 或者 <T>
    4. public <T>T superSelect(String sql,RowMapper rm,Object... objs) {
    5. T obj = null;
    6. try {
    7. Class.forName(className);//加载类 类中的静态元素就执行啦
    8. Connection conn = DriverManager.getConnection(url, user, password);
    9. //创建状态参数
    10. PreparedStatement pstat = conn.prepareStatement(sql);
    11. //将SQL语句中的 "?" 赋值
    12. for(int i=0;i<obj.length;i++) {
    13. pstat.setObject(i+1,obj[i]);
    14. }
    15. ResultSet rs = pstat.executeQuery();
    16. /**
    17. * 若查询多条SQL语句则将if改为while
    18. * 并将返回值类型改为list集合 <T> List<T>
    19. */
    20. //将查询语句拿出来 并存入
    21. if(rs.next()){
    22. obj = rm.mapperRow(rs);//执行策略模式
    23. //利用map集合-->可读性不好
    24. // if (rs.next()) {// 获取结果集中所有的列
    25. // ResultSetMetaData rsmd = rs.getMetaData();
    26. // int count = rsmd.getColumnCount();//获取列的个数
    27. // for (int i = 0; i < count; i++) {//遍历列
    28. // HashMap<String, Object> map = new HashMap();
    29. // String key = rsmd.getColumnName(i);
    30. // Object value = rs.getObject(key);
    31. // map.put(key, value);
    32. // }
    33. }
    34. rs.close();
    35. pstat.close();
    36. conn.close();
    37. } catch (ClassNotFoundException e) {
    38. e.printStackTrace();
    39. } catch (SQLException e) {
    40. e.printStackTrace();
    41. }
    42. return obj;
    43. }

    2)考试系统题目存储与读取

    1. package domain;
    2. public class Question {
    3. private String title;
    4. private String answer;
    5. public Question(){}
    6. public Question(String title, String answer) {
    7. this.title = title;
    8. this.answer = answer;
    9. }
    10. public String getTitle() {
    11. return title;
    12. }
    13. public void setTitle(String title) {
    14. this.title = title;
    15. }
    16. public String getAnswer() {
    17. return answer;
    18. }
    19. public void setAnswer(String answer) {
    20. this.answer = answer;
    21. }
    22. //重写Question类中的 equals hashCode -->目的去掉重复元素
    23. @Override//检测方法是否错误
    24. public int hashCode(){//根据题目算的
    25. String thisTitle = this.title.substring(0,this.title.indexOf("<br>"));
    26. return thisTitle.hashCode();//String的hashcode
    27. }
    28. @Override
    29. public boolean equals(Object obj){
    30. if(this==obj){//判断地址是否一致
    31. return true;
    32. }//判断是否是同一个类型
    33. if(obj instanceof Question){//类型不同-->造型
    34. Question anotherQuestion = (Question)obj;
    35. String thisTitle = this.title.substring(0,this.title.indexOf("<br>"));
    36. String anotherTitle = anotherQuestion.title.substring(0,anotherQuestion.title.indexOf("<br>"));
    37. if(thisTitle.equals(anotherTitle)){
    38. return true;//同一个题干
    39. }
    40. }
    41. return false;
    42. }
    43. }
    1. public class QuestionService {
    2. //在Service层里需要一个底层dao的支持
    3. private QuestionDao dao = MySpring.getBean("dao.QuestionDao");
    4. //设计一个方法 获取随机产生的试卷 试卷题目数量
    5. public ArrayList<Question> getPaper(int count){
    6. return dao.getPaper(count);
    7. }
    8. }
    1. public class QuestionDao {
    2. //获取缓存对象
    3. private QuestionFileReader reader = MySpring.getBean("util.QuestionFileReader");
    4. //由于Set集合不能随机抽取,是无序的,所以将缓存集合改为list集合
    5. private ArrayList<Question> questionBank = new ArrayList(reader.getQuestionBox());
    6. //读取缓存 生成题库
    7. public ArrayList<Question> getPaper(int count){
    8. HashSet<Question> paper = new HashSet<>();//用来存储最终的试卷题目
    9. while(paper.size()!=count){
    10. Random r = new Random();// 题库的总长度
    11. int index = r.nextInt(this.questionBank.size());//随机产生的一个题目索引位置
    12. paper.add(this.questionBank.get(index));
    13. }// 再构建回去
    14. return new ArrayList<Question>(paper);
    15. }
    16. }
    1. public class QuestionFileReader {
    2. //程序执行时 将文件中的所有题目 缓存到集合里-->缓存流
    3. private HashSet<Question> questionBox = new HashSet<>();
    4. {//
    5. try {
    6. BufferedReader reader = new BufferedReader(new FileReader("src//dbfile//Question.txt"));
    7. String message = reader.readLine();//每一次读取一行 title#answer
    8. while(message!=null){
    9. String[] values = message.split("#");
    10. questionBox.add(new Question(values[0],values[1]));
    11. message = reader.readLine();
    12. }
    13. reader.close();
    14. } catch (Exception e) {
    15. e.printStackTrace();
    16. }
    17. }
    18. //将集合里的返回
    19. public HashSet<Question> getQuestionBox(){
    20. return questionBox;
    21. }
    22. }

    总结
    通过今天的学习,了解到策略模式的使用方式以及场景,对于优化JDBC—>查询SQL语句方法,解决连接数据库的代码冗余有了一定的掌握,并且更深层次认识MVC分层架构思想,今天上课学习的代码虽然能看懂,但是在理解以及编码方面还有欠缺,emmm稀碎的基础,多多练习吧