DAO模式的作用
隔离业务逻辑代码和数据访问代码
隔离不同数据库的实现
DAO设计模式的组成
DAO模式的组成部分
DAO接口
DAO实现类
实体类
数据库连接和关闭工具类
** 示例代码:**
对学生表实现逻辑与数据分离
1.建立propertise文件记录数据库连接
1
2
3
4
5
6
7 1//jdbc驱动
2driver=com.mysql.cj.jdbc.Driver
3//数据库及服务器
4url=jdbc:mysql://localhost:3306/study?serverTimezone=Asia/Shanghai&useUnicode=true&characterEncoding=UTF-8&useSSL=false
5user=root
6pwd=123456
7
2.建立student实体类:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49 1package com.jdbc_stu.dao_design.vo;
2
3import java.sql.Timestamp;
4
5public class Student {
6 private String sno;
7 private String sname;
8 private String ssex;
9 private Timestamp sbirthday;
10 private String sclass;
11 public String getSno() {
12 return sno;
13 }
14 public void setSno(String sno) {
15 this.sno = sno;
16 }
17 public String getSname() {
18 return sname;
19 }
20 public void setSname(String sname) {
21 this.sname = sname;
22 }
23 public String getSsex() {
24 return ssex;
25 }
26 public void setSsex(String ssex) {
27 this.ssex = ssex;
28 }
29 public Timestamp getSbirthday() {
30 return sbirthday;
31 }
32 public void setSbirthday(Timestamp sbirthday) {
33 this.sbirthday = sbirthday;
34 }
35 public String getSclass() {
36 return sclass;
37 }
38 public void setSclass(String sclass) {
39 this.sclass = sclass;
40 }
41 @Override
42 public String toString() {
43 return "Student [sno=" + sno + ", sname=" + sname + ", ssex=" + ssex + ", sbirthday=" + sbirthday + ", sclass="
44 + sclass + "]";
45 }
46
47}
48
49
3.建立工具类Util:
建立链接关闭方法
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45 1package com.jdbc_stu.dao_design.util;
2
3import java.io.FileInputStream;
4import java.io.IOException;
5import java.io.InputStream;
6import java.sql.Connection;
7import java.sql.DriverManager;
8import java.sql.SQLException;
9import java.util.Properties;
10
11import com.util.Util;
12
13public class DbUtil {
14 public static void main(String[] args) throws ClassNotFoundException, SQLException, IOException {
15 getConnection();
16 }
17 //关闭数据库相关链接
18 public static void closeSql(AutoCloseable...stream){
19 for (AutoCloseable closeable : stream) {
20 try {
21 closeable.close();
22 } catch (Exception e) {
23 // TODO Auto-generated catch block
24 e.printStackTrace();
25 }
26 }
27 }
28 //获取链接
29 public static Connection getConnection() throws ClassNotFoundException, SQLException, IOException{
30 //从文件中读取配置信息
31 Properties pro = new Properties();
32 //该方法是将将properties内的内容读取出来
33 InputStream is = DbUtil.class.getResourceAsStream("/db.properties");
34 pro.load(is);
35 //关闭资源
36 Util.closeStream(is);
37 //加载驱动
38 Class.forName(pro.getProperty("driver"));
39 //建立数据库链接
40 Connection con = DriverManager.getConnection(pro.getProperty("url"),pro.getProperty("user"), pro.getProperty("pwd"));
41 return con;
42 }
43}
44
45
basedao设计建立BaseDao
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58 1package com.jdbc_stu.dao_design.util;
2
3import java.lang.reflect.Field;
4import java.sql.Connection;
5import java.sql.PreparedStatement;
6import java.sql.ResultSet;
7import java.util.ArrayList;
8import java.util.List;
9
10public class BaseDao {
11 /*basedao:
12 *queryAll
13 *适用于查询所有表的内容
14 */
15 //创建方法泛型,形参sql语句,对象
16 public static <T>List<T> queryAll(String sql,Class<T> clazz){
17 Connection con = null;
18 PreparedStatement ps = null;
19 ResultSet rs = null;
20 List<T> list = new ArrayList<T>();
21 try {
22 //调用方法建立数据库链接
23 con = DbUtil.getConnection();
24 //执行查询语句
25 ps = con.prepareStatement(sql);
26 //获取结果
27 rs = ps.executeQuery();
28 //遍历拿出数据
29 while(rs.next()){
30 //生成用于存储数据的对象
31 Object obj = clazz.newInstance();
32 //获取对象的属性列表
33 Field[] fields = clazz.getDeclaredFields();
34 //遍历,为每一个属性赋值
35 for (Field field : fields) {
36 //获取属性名
37 String fName = field.getName();
38 //从结果集中获取该属性名对应字段的值
39 Object value = rs.getObject(fName);
40 //将值放到对象里
41 field.setAccessible(true);//将属性的访问权限暂时改为public
42 field.set(obj, value);
43 }
44 list.add((T) obj);
45 }
46
47
48 } catch (Exception e) {
49 // TODO Auto-generated catch block
50 e.printStackTrace();
51 }finally{
52 DbUtil.closeSql(con,rs,ps);
53 }
54 return list;
55 }
56
57}
58
解疑链接:new和newInstance联系与区别
4.建立测试
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 1package com.jdbc_stu.dao_design.dao;
2
3
4import java.util.List;
5
6
7import com.jdbc_stu.dao_design.util.DbUtil;
8import com.jdbc_stu.dao_design.vo.Student;
9
10public class StudentDao {
11 public static void main(String[] args) {
12 List<Student> list = dao.queryAll("select sname,ssex from student");
13 System.out.println(list);
14 }
15}
16