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