Explain the DAO class (database operation object)

Various relationships under the DAO class can be represented by a diagram.
Insert picture description here

1.DAO class introduction:

1. Introduction
1) DAO=DataBase Access Object; database operation object
2) Encapsulate the operation details for a table in a DAO class
3) All DAO classes in the project should be stored in the dao package
4) DAO class naming rules = Table name+Dao DeptDao/EmpDao

For example, such a table.
Insert picture description here
1. Entity class introduction:
an entity class is used to describe the structure of a table
2) The class name of the entity class should be the same as the table name
dept.frm-------- Dept.class
3) Attributes of the entity class The name should be the same as the anonymous field in the table
DEPTNO INT private Integer deptNo
DNAME VARCHAR private String dname
LOC VARCHAR private String loc
4) An instance object of an entity class is used to describe a row of data in the table file
deptno dname loc
10 Accounting xx
20 Sales BOSTON
Dept dept1 = new Dept(10,'Accounting','xx');
Dept dept2 = new Dept(20,'Sales','BOSTON')
2. Entity class function: Entity class reduces DAO class development difficulty
1. Effectively reduces The number of method parameters in the Dao class
reduces the difficulty of method development and use
2. Effectively reduces the operation difficulty of the Dao class and ResultSet

 * 实体类属性的数据类型必须都是高级引用类型,目的方便存储null值
 * 2021/1/23
public class Dept {

    private Integer deptNo ;
    private String  dname;
    private String  loc;

    public Integer getDeptNo() {
        return deptNo;

    public Dept setDeptNo(Integer deptNo) {
        this.deptNo = deptNo;
        return this;

    public String getDname() {
        return dname;

    public Dept setDname(String dname) {
        this.dname = dname;
        return this;

    public String getLoc() {
        return loc;

    public Dept setLoc(String loc) {
        this.loc = loc;
        return this;

    public Dept(Integer deptNo, String dname, String loc) {
        this.deptNo = deptNo;
        this.dname = dname;
        this.loc = loc;

    public Dept() {

Based on function package-JdbcUtil
1. What is a function: a
line of command is a function. For example, int a =10
2. JDBC development process:
1) Establish a connection channel
2) Create a transportation tool
3) Push SQL commands and get processing results
4 ) Destroy resources
3. The role of JdbcUtil reduces the difficulty of using JDBC technology when developing DAO classes


public class ReflectUtil {

    *  作用:动态生成INSERT语句
    *  insert语句结构:
    *  insert into  表    (字段名1,字段名2,字段名3) values(值1,值2)
    *  -----------  --    ---------------        ---------------
    *       1        2           3                     4
    *   问题1:如何能够得到表名
    *         实体类的类名应该与对应的表名相同
    *   问题2:如何得到表中字段名
    *         实体类的属性名应该与对应的表中字段名相同
    *   问题3:如何得到要插入的数据行
    *         一个实体类的实例对象封装表文件中一个数据行

    public String  createInsertSql(Object instance)throws Exception{

         String tableName = null;
         Class classManager = null;
         Field fieldArray[]=null;
         StringBuffer columns = new StringBuffer(" (");
         StringBuffer sql = new StringBuffer("insert into ");
         StringBuffer values = new StringBuffer(" values(");

         classManager= instance.getClass(); //得到实例对象,隶属的类文件的管理者
         fieldArray = classManager.getDeclaredFields();
        tableName = classManager.getSimpleName();

        for(Field field:fieldArray){

            String fieldName = field.getName();//得到管理的属性的名称,这个名称就是字段名
            if(!columns.toString().equals(" (")){

        for(Field field:fieldArray){
              Object value = field.get(instance);
              if(!values.toString().equals(" values(")){


        return sql.toString();

Guess you like

Origin blog.csdn.net/m0_45311187/article/details/113060007