• 0811KQC—注解反射—orm框架(增删改查)


    0811KQC

    在这里插入图片描述

    
    @MyEmpAnno(id=1,name="张三",sex="男",age=16,method = "introduce")
    public class Student {
        private Integer id;
        private String name;
        private String sex;
        private Integer age;
    
        public Student() {
        }
    
        public Student(Integer id, String name, String sex, Integer age) {
            this.id = id;
            this.name = name;
            this.sex = sex;
            this.age = age;
        }
    
        public void introduce(){
            System.out.println("id:"+id+"name:"+name+"sex:"+sex+"age"+age);
        }
    
        public Integer getId() {
            return id;
        }
    
        public void setId(Integer id) {
            this.id = id;
        }
    
        public String getName() {
            return name;
        }
    
        public void setName(String name) {
            this.name = name;
        }
    
        public String getSex() {
            return sex;
        }
    
        public void setSex(String sex) {
            this.sex = sex;
        }
    
        public Integer getAge() {
            return age;
        }
    
        public void setAge(Integer age) {
            this.age = age;
        }
    
        @Override
        public String toString() {
            return "Student{" +
                    "id=" + id +
                    ", name='" + name + '\'' +
                    ", sex='" + sex + '\'' +
                    ", age=" + age +
                    '}';
        }
    }
    
    
    • 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
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    import java.lang.annotation.ElementType;
    import java.lang.annotation.Retention;
    import java.lang.annotation.RetentionPolicy;
    import java.lang.annotation.Target;
    
    @Target({ElementType.TYPE,ElementType.METHOD})
    @Retention(RetentionPolicy.RUNTIME)
    public @interface MyEmpAnno {
        int id(); //姓名
        String name(); //性别
        String sex(); //年龄
        String method(); //定义将要执行的方法
    
        int age();
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    import java.lang.annotation.Annotation;
    import java.lang.reflect.Field;
    import java.lang.reflect.InvocationTargetException;
    import java.lang.reflect.Method;
    
    public class Test1 {
        public static void main(String[] args) throws ClassNotFoundException, IllegalAccessException, InstantiationException, NoSuchFieldException, NoSuchMethodException, InvocationTargetException {
            Class cls = Class.forName("Student");
    
            Annotation[] as =  cls.getAnnotations();
            for (Annotation annotation : as){
                if(annotation instanceof MyEmpAnno){
                    MyEmpAnno myEmpAnno = (MyEmpAnno) annotation;
    //                int id(); //编号
    //                String name(); //名字
    //                String sex(); //性别
    //                int age(); //年龄
    //                String method(); //定义将要执行的方法
    
                    int id = myEmpAnno.id();
                    String name = myEmpAnno.name();
                    String sex = myEmpAnno.sex();
                    String method = myEmpAnno.method();
                    int age = myEmpAnno.age();
                    Object obj = cls.newInstance();
    
                    Field f1= cls.getDeclaredField("id");
                    f1.setAccessible(true);
                    f1.set(obj,id);
    
                    Field f2 = cls.getDeclaredField("name");
                    f2.setAccessible(true);
                    f2.set(obj,name);
    
                    Field f3 = cls.getDeclaredField("sex");
                    f3.setAccessible(true);
                    f3.set(obj,sex);
    
                    Field f4 = cls.getDeclaredField("age");
                    f4.setAccessible(true);
                    f4.set(obj,age);
    
                    System.out.println(obj);
    
                    //调用指定方法
                    Method m = cls.getDeclaredMethod(method);
                    m.invoke(obj);
    
                    break;
                }
            }
    
        }
    }
    
    
    • 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

    在这里插入图片描述

    1.使用反射获取注解,设置对象值

    Student:

    package com.test2;
    
    
    @MyStuAnno(value="aaa",name="张三",sex="男",nums = {6,7,8})
    public class Student {
        private Integer studentId;
        private String name;
        private String sex;
        private Integer age;
    
        public Student() {
        }
    
        public Student(Integer studentId, String name, String sex, Integer age) {
            this.studentId = studentId;
            this.name = name;
            this.sex = sex;
            this.age = age;
        }
    
        public Integer getStudentId() {
            return studentId;
        }
    
        public void setStudentId(Integer studentId) {
            this.studentId = studentId;
        }
    
        public String getName() {
            return name;
        }
    
        public void setName(String name) {
            this.name = name;
        }
    
        public String getSex() {
            return sex;
        }
    
        public void setSex(String sex) {
            this.sex = sex;
        }
    
        public Integer getAge() {
            return age;
        }
    
        public void setAge(Integer age) {
            this.age = age;
        }
    
        @Override
        public String toString() {
            return "Student{" +
                    "studentId=" + studentId +
                    ", name='" + name + '\'' +
                    ", sex='" + sex + '\'' +
                    ", age=" + age +
                    '}';
        }
    }
    
    
    • 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
    • 59
    • 60
    • 61
    • 62
    • 63

    MyStuAnno:

    package com.test2;
    
    import java.lang.annotation.ElementType;
    import java.lang.annotation.Retention;
    import java.lang.annotation.RetentionPolicy;
    import java.lang.annotation.Target;
    
    @Target({ElementType.TYPE,ElementType.METHOD})
    @Retention(RetentionPolicy.RUNTIME)
    public @interface MyStuAnno {
        //定义注解的姓名属性
        //当一个注解属性没有默认值,并且不是value属性的时候,使用的时候必须带上属性名赋值
        //String name();
    
        //default:可以为属性设置默认值
        //String name() default "张三";
    
        String name();
        String sex();
        //value:是一个特殊的属性,设置该属性的时候,可以省略名字,
        //      但是和其他属性一起使用的时候,也要加属性名
        String value() default "";
    
        //注解中的数组属性
        int[] nums() default {1,2,3,4};
    
    }
    
    
    • 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

    2.orm框架:实现数据的增删改查

    在这里插入图片描述

    创建数据库:

    #创建数据库
    create database 70811_db
    #设置字符集
    default character set utf8mb4
    #设置编码方式
    default collate utf8mb4_general_ci
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6

    在这里插入图片描述

    创建数据表:

    #创建表
    create table emp
    (
    	empId int primary key auto_increment,
    	name varchar(20),
    	sex varchar(20),
    	birthday date
    );
    
    select * from emp;
    
    insert into emp
    (name,sex,birthday)
    select '张三','男','2000-01-01' union
    select '李四','女','2002-01-01';
    
    drop table orders;
    
    create table orders
    (
    	orderId int primary key auto_increment,
    	customerId int,
    	reciverName varchar(20),
    	reciverAddress varchar(200),
    	recivarPhone varchar(11),
    	money decimal(5,1),
    	orderTime datetime
    );
    
    select * from orders;
    
    insert into orders
    (customerId,reciverName,reciverAddress,recivarPhone,money,orderTime)
    select 1,'张三','河南','12312312311',10,now() union
    select 2,'李四','河北','12312312312',20,now() union
    select 3,'王五','山东','12312312313',30,now();
    
    • 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

    在这里插入图片描述
    在这里插入图片描述

    代码:

    在这里插入图片描述

    整体布局:

    在这里插入图片描述

    util:

    BaseDAO:

    package orm.util;
    
    import javax.sql.DataSource;
    import java.io.IOException;
    import java.io.InputStream;
    import java.io.InputStreamReader;
    import java.sql.*;
    import java.util.*;
    
    public class BaseDAO {
    
    	//四大金刚
    	//驱动类
    	private static final String DRIVER="com.mysql.cj.jdbc.Driver";
    	//连接地址
    	private static final String URL="jdbc:mysql://localhost:3306/70811_db?useSSL=false&characterEncoding=utf8&serverTimezone=Asia/Shanghai";
    	//用户名
    	private static final String USER="root";
    	//密码
    	private static final String PASSWORD="123456";
    
    
    
    	//获取连接
    	public static Connection getConnection(){
    
    		Connection con = null;
    
    		try{
    			//加载驱动类
    			Class.forName(DRIVER);
    			//获取连接
    			con = DriverManager.getConnection(URL,USER,PASSWORD);
    			
    		}catch(Exception ex){
    			ex.printStackTrace();
    		}
    
    		return con;
    	}
    
    	//关闭数据库对象
    	public static void closeAll(Connection con,Statement st,ResultSet rs){
    		
    		if(rs!=null){
    			try{
    				rs.close();
    			}catch(Exception ex){
    				ex.printStackTrace();
    			}
    			
    		}
    
    		if(st!=null){
    
    			try{
    				st.close();
    			}catch(Exception ex){
    				ex.printStackTrace();
    			}
    			
    		}
    
    		if(con!=null){
    			try{
    				con.close();
    			}catch(Exception ex){
    				ex.printStackTrace();
    			}
    			
    		}
    
    	}
    
    
    	//通用设置参数方法
    	public static void setParams(PreparedStatement pst,Object[] params){
    
    		if(params==null){
    			return;
    		}
    
    		for(int i=0;i> executeQuery(String sql,Object[] params) {
    
    		List> rows = new ArrayList<>();
    
    		Connection con = null;
    		PreparedStatement pst = null;
    		ResultSet rs = null;
    
    		try{
    			//获取连接	
    			con = getConnection();			
    			//获取命令对象
    			pst = con.prepareStatement(sql);
    			//设置参数
    			setParams(pst,params);
    			//执行查询
    			rs = pst.executeQuery();
    
    			//通过rs获取结果集的结构信息
    			ResultSetMetaData rsmd =  rs.getMetaData();
    			//获取结果集的列数
    			int colCount = rsmd.getColumnCount();
    
    			//遍历查询结果,并封装到List中
    			while(rs.next()){
    				//用Map存储当前行的各个列数据
    				Map map = new HashMap<>();
    				//循环获取每一列的信息
    				for(int i=1;i<=colCount;i++){
    					//获取列名(使用rsmd)
    					String colName = rsmd.getColumnLabel(i);
    					//获取列值(使用rs)
    					Object colVal = rs.getObject(i);
    					//将当前列存储到map中
    					map.put(colName,colVal);								
    				}
    				
    				//将遍历的当前行的数据存储到List中
    				rows.add(map);
    							
    			}
    
    
    		}catch(Exception ex){
    			ex.printStackTrace();
    		}finally{
    			closeAll(con,pst,rs);
    		}
    		
    		return rows;
    
    	}
    
    }
    
    • 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
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94
    • 95
    • 96
    • 97
    • 98
    • 99
    • 100
    • 101
    • 102
    • 103
    • 104
    • 105
    • 106
    • 107
    • 108
    • 109
    • 110
    • 111
    • 112
    • 113
    • 114
    • 115
    • 116
    • 117
    • 118
    • 119
    • 120
    • 121
    • 122
    • 123
    • 124
    • 125
    • 126
    • 127
    • 128
    • 129
    • 130
    • 131
    • 132
    • 133
    • 134
    • 135
    • 136
    • 137
    • 138
    • 139
    • 140
    • 141
    • 142
    • 143
    • 144
    • 145
    • 146
    • 147
    • 148
    • 149
    • 150
    • 151
    • 152
    • 153
    • 154
    • 155
    • 156
    • 157
    • 158
    • 159
    • 160
    • 161
    • 162
    • 163
    • 164
    • 165
    • 166
    • 167
    • 168
    • 169
    • 170
    • 171
    • 172
    • 173
    • 174
    • 175
    • 176
    • 177
    • 178
    • 179
    • 180
    • 181
    • 182
    • 183
    • 184
    • 185
    • 186
    • 187
    • 188
    • 189
    • 190
    • 191
    • 192
    • 193
    • 194
    • 195
    • 196
    • 197
    • 198
    • 199
    • 200
    • 201
    • 202
    • 203
    • 204

    entity:

    Emp:

    package orm.entity;
    
    import orm.anno.Table;
    import orm.anno.TableColumn;
    import orm.anno.TableId;
    
    import java.util.Date;
    
    @Table("emp")
    public class Emp {
        //标记该列是主键列,并且是自增的
        @TableId(isAutoGen = true)
        //标记该属性对应数据表的中列的信息
        @TableColumn(colName = "empId",colType = "int",colSize = 11)
        private Integer empId;
    
        @TableColumn(colName = "name",colType = "varchar",colSize = 20)
        private String name;
    
        @TableColumn(colName = "sex",colType = "char",colSize = 1)
        private String sex;
    
        @TableColumn(colName = "birthday",colType = "date",colSize = 8)
        private Date birthday;
    
        public Emp() {
        }
    
        public Emp(Integer empId, String name, String sex, Date birthday) {
            this.empId = empId;
            this.name = name;
            this.sex = sex;
            this.birthday = birthday;
        }
    
        public Integer getEmpId() {
            return empId;
        }
    
        public void setEmpId(Integer empId) {
            this.empId = empId;
        }
    
        public String getName() {
            return name;
        }
    
        public void setName(String name) {
            this.name = name;
        }
    
        public String getSex() {
            return sex;
        }
    
        public void setSex(String sex) {
            this.sex = sex;
        }
    
        public Date getBirthday() {
            return birthday;
        }
    
        public void setBirthday(Date birthday) {
            this.birthday = birthday;
        }
    
        @Override
        public String toString() {
            return "Emp{" +
                    "empId=" + empId +
                    ", name='" + name + '\'' +
                    ", sex='" + sex + '\'' +
                    ", birthday=" + birthday +
                    '}';
        }
    }
    
    
    • 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
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78

    Orders :

    package orm.entity;
    
    import orm.anno.Table;
    import orm.anno.TableColumn;
    import orm.anno.TableId;
    
    import java.util.Date;
    
    @Table("orders")
    public class Orders {
        @TableId(isAutoGen = true)
        @TableColumn(colName = "orderId",colType = "int",colSize = 11)
        private Integer orderId;
        @TableColumn(colName = "customerId",colType = "int",colSize = 11)
        private Integer customerId;
        private String reciverName;
        private String reciverAddress;
        private String reciverPhone;
        private Double money;
        private Date orderTime;
    
        public Orders() {
        }
    
        public Orders(Integer orderId, Integer customerId, String reciverName, String reciverAddress, String reciverPhone, Double money, Date orderTime) {
            this.orderId = orderId;
            this.customerId = customerId;
            this.reciverName = reciverName;
            this.reciverAddress = reciverAddress;
            this.reciverPhone = reciverPhone;
            this.money = money;
            this.orderTime = orderTime;
        }
    
        public Integer getOrderId() {
            return orderId;
        }
    
        public void setOrderId(Integer orderId) {
            this.orderId = orderId;
        }
    
        public Integer getCustomerId() {
            return customerId;
        }
    
        public void setCustomerId(Integer customerId) {
            this.customerId = customerId;
        }
    
        public String getReciverName() {
            return reciverName;
        }
    
        public void setReciverName(String reciverName) {
            this.reciverName = reciverName;
        }
    
        public String getReciverAddress() {
            return reciverAddress;
        }
    
        public void setReciverAddress(String reciverAddress) {
            this.reciverAddress = reciverAddress;
        }
    
        public String getReciverPhone() {
            return reciverPhone;
        }
    
        public void setReciverPhone(String reciverPhone) {
            this.reciverPhone = reciverPhone;
        }
    
        public Double getMoney() {
            return money;
        }
    
        public void setMoney(Double money) {
            this.money = money;
        }
    
        public Date getOrderTime() {
            return orderTime;
        }
    
        public void setOrderTime(Date orderTime) {
            this.orderTime = orderTime;
        }
    
        @Override
        public String toString() {
            return "Orders{" +
                    "orderId=" + orderId +
                    ", customerId=" + customerId +
                    ", reciverName='" + reciverName + '\'' +
                    ", reciverAddress='" + reciverAddress + '\'' +
                    ", reciverPhone='" + reciverPhone + '\'' +
                    ", money=" + money +
                    ", orderTime=" + orderTime +
                    '}';
        }
    }
    
    
    • 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
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94
    • 95
    • 96
    • 97
    • 98
    • 99
    • 100
    • 101
    • 102
    • 103
    • 104

    dao:

    IGenericDAO:

    package orm.dao;
    
    import java.util.List;
    import java.util.Map;
    
    public interface IGenericDAO {
        List> listAll(Class clz);
        int insert(T t);
        int update(T t);
        int delete(Integer id);
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12

    GenericDAOImpl:

    package orm.dao.impl;
    
    import orm.anno.Table;
    import orm.anno.TableColumn;
    import orm.dao.IGenericDAO;
    import orm.dao.old.IEmpDAO;
    import orm.util.BaseDAO;
    
    import java.lang.reflect.Field;
    import java.util.List;
    import java.util.Map;
    
    /**
     * @author: hy
     * @create: 2022-08-11 10:28:41
     */
    public class GenericDAOImpl implements IGenericDAO {
    
        //通过反射获取拼接的sql
        public String getQuerySql(Class clz){
            //定义sql
            StringBuilder sql = new StringBuilder();
    
            //使用clz通过反射,获取对应类型上标记的注解(类的注解@Table,字段注解@TableCoumn)
            String tableName = "";
            if(clz.isAnnotationPresent(Table.class)){
                Table t =  clz.getDeclaredAnnotation(Table.class);
                //获取注解上定义的表名
                tableName = t.value();
    
                StringBuilder cols = new StringBuilder();
    
                //获取该实体类下的所有字段
                Field[] fields = clz.getDeclaredFields();
                //遍历字段,读取字段上的注解
                for (Field f : fields){
                    //判断字段上有没有TableColumn注解
                    if(f.isAnnotationPresent(TableColumn.class)){
                        //获取字段上注解
                        TableColumn tc = f.getDeclaredAnnotation(TableColumn.class);
                        //获取该注解定义的列名
                        String colName = tc.colName();
                        //拼接列名
                        cols.append(colName+",");
                    }
                }
    
                //拼接查询的列名,结构如下:  empId,name,sex,birthday
                String colsRes = cols.substring(0,cols.length()-1);
                //拼接完整的sql
                sql.append("select ").append(colsRes).append(" from ").append(tableName);
            }
    
            return sql.toString();
        }
    	
    	//查找
        @Override
        public List> listAll(Class clz) {
            String sql = getQuerySql(clz);
            return BaseDAO.executeQuery(sql,null);
        }
    
           //通过反射获取拼接的sql
        public String getInsertSql(Class clz){
            //定义sql
            StringBuilder sql = new StringBuilder();
    
            //使用clz通过反射,获取对应类型上标记的注解(类的注解@Table,字段注解@TableCoumn)
            String tableName = "";
            if(clz.isAnnotationPresent(Table.class)){
                Table t =  clz.getDeclaredAnnotation(Table.class);
                //获取注解上定义的表名
                tableName = t.value();
    
                StringBuilder cols = new StringBuilder();
    
                //获取该实体类下的所有字段
                Field[] fields = clz.getDeclaredFields();
                //遍历字段,读取字段上的注解
                for (Field f : fields){
                    //判断字段上有TableColumn注解,且没有TableId注释
                    if(f.isAnnotationPresent(TableColumn.class) && !f.isAnnotationPresent(TableId.class)){
                        //获取字段上注解
                        TableColumn tc = f.getDeclaredAnnotation(TableColumn.class);
                        //获取该注解定义的列名
                        String colName = tc.colName();
                        //拼接列名
                        cols.append(colName+",");
                    }
                }
    
                //拼接查询的列名,结构如下:  name,sex,birthday
                String colsRes = cols.substring(0,cols.length()-1);
                //拼接完整的sql
                sql.append("insert into ").append(tableName).append("(").append(colsRes).append(")").append(" values ").append(" (?,?,?) ");
            }
    
            System.out.println("sql.toString():"+sql.toString());
            return sql.toString();
        }
    
        //插入
        @Override
        public int insert(T t) {
            List list=new ArrayList<>();
    
            //定义sql
            Class itCls = t.getClass();
            String sql = getInsertSql(itCls);
            Field[] fields=itCls.getDeclaredFields();
            for(Field f:fields){
                try {
                    if(f.isAnnotationPresent(TableColumn.class) && !f.isAnnotationPresent(TableId.class)){
                        f.setAccessible(true);
                        list.add(f.get(t));
                    }
    
                } catch (IllegalAccessException e) {
                    e.printStackTrace();
                }
            }
    
            System.out.println("list.toArray()2:"+list.toArray());
            return BaseDAO.executeUpdate(sql,list.toArray());
        }
    
        //通过反射获取拼接的sql
        public String getUpdateSql(Class clz){
            //定义sql
            StringBuilder sql = new StringBuilder();
    
            //使用clz通过反射,获取对应类型上标记的注解(类的注解@Table,字段注解@TableCoumn)
            String tableName = "";
            if(clz.isAnnotationPresent(Table.class)){
                Table t =  clz.getDeclaredAnnotation(Table.class);
                //获取注解上定义的表名
                tableName = t.value();
    
                StringBuilder cols = new StringBuilder();   //name,sex,birthday
                StringBuilder cols1 = new StringBuilder(); //  empId
    
                //获取该实体类下的所有字段
                Field[] fields = clz.getDeclaredFields();
                //遍历字段,读取字段上的注解
                for (Field f : fields){
                    //判断字段上有TableColumn注解,且没有TableId注释
                    if(f.isAnnotationPresent(TableColumn.class) && !f.isAnnotationPresent(TableId.class)){
                        //获取字段上注解
                        TableColumn tc = f.getDeclaredAnnotation(TableColumn.class);
                        //获取该注解定义的列名
                        String colName = tc.colName();
                        //拼接列名
                        cols.append(colName+"=?,");
                    }
                    //判断字段上有没有TableId注释
                    if(f.isAnnotationPresent(TableColumn.class) && f.isAnnotationPresent(TableId.class)){
                        //获取字段上注解
                        TableColumn tc = f.getDeclaredAnnotation(TableColumn.class);
                        //获取该注解定义的列名
                        String colName = tc.colName();
                        //拼接列名
                        cols1.append(colName+"=?");
                    }
                }
    
                //拼接查询的列名,结构如下:  name,sex,birthday  empId
                String colsRes = cols.substring(0,cols.length()-1);
                //拼接完整的sql
    
    //            update goods
    //            set price = 0.1
    //            where goodsId = 1111
    
                sql.append("update ").append(tableName).append(" set ").append(colsRes).append(" where ").append(cols1);
            }
    
            System.out.println("sql.toString():"+sql.toString());
            return sql.toString();
        }
    
        //更改
        @Override
        public int update(T t) {
            List list=new ArrayList();
            Object b = null; //局部变量需要赋初值
    
            Class uecls=t.getClass();
            String sql=getUpdateSql(uecls);
    
            Field[] fields=uecls.getDeclaredFields();
            for(Field f:fields){
                try {
                    if(f.isAnnotationPresent(TableColumn.class) && !f.isAnnotationPresent(TableId.class)){
                        f.setAccessible(true);
                        System.out.println(f.get(t));
                        list.add(f.get(t));
                    }
                    if(f.isAnnotationPresent(TableColumn.class) && f.isAnnotationPresent(TableId.class)){
                        f.setAccessible(true);
                        b=f.get(t);
                        //System.out.println(f.get(t));
                    }
    
                } catch (IllegalAccessException e) {
                    e.printStackTrace();
                }
    
            }
    
    //        for(Field f:fields){
    //            try {
    //                if(f.isAnnotationPresent(TableColumn.class) && f.isAnnotationPresent(TableId.class)){
    //                    f.setAccessible(true);
    //                    list.add(f.get(t));
    //                    System.out.println(f.get(t));
    //                }
    //
    //            } catch (IllegalAccessException e) {
    //                e.printStackTrace();
    //            }
    //
    //        }
    
            list.add(b);
            System.out.println("list.toArray()3:"+ Arrays.toString(list.toArray()));
            return BaseDAO.executeUpdate(sql,list.toArray());
    
        }
    
        //删除
        @Override
        public int delete(Integer id) {
            String sql=" delete from emp " +
                    "        where empId =?";
            Object[] params={id};
            return BaseDAO.executeUpdate(sql,params);
        }
    }
    
    
    • 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
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94
    • 95
    • 96
    • 97
    • 98
    • 99
    • 100
    • 101
    • 102
    • 103
    • 104
    • 105
    • 106
    • 107
    • 108
    • 109
    • 110
    • 111
    • 112
    • 113
    • 114
    • 115
    • 116
    • 117
    • 118
    • 119
    • 120
    • 121
    • 122
    • 123
    • 124
    • 125
    • 126
    • 127
    • 128
    • 129
    • 130
    • 131
    • 132
    • 133
    • 134
    • 135
    • 136
    • 137
    • 138
    • 139
    • 140
    • 141
    • 142
    • 143
    • 144
    • 145
    • 146
    • 147
    • 148
    • 149
    • 150
    • 151
    • 152
    • 153
    • 154
    • 155
    • 156
    • 157
    • 158
    • 159
    • 160
    • 161
    • 162
    • 163
    • 164
    • 165
    • 166
    • 167
    • 168
    • 169
    • 170
    • 171
    • 172
    • 173
    • 174
    • 175
    • 176
    • 177
    • 178
    • 179
    • 180
    • 181
    • 182
    • 183
    • 184
    • 185
    • 186
    • 187
    • 188
    • 189
    • 190
    • 191
    • 192
    • 193
    • 194
    • 195
    • 196
    • 197
    • 198
    • 199
    • 200
    • 201
    • 202
    • 203
    • 204
    • 205
    • 206
    • 207
    • 208
    • 209
    • 210
    • 211
    • 212
    • 213
    • 214
    • 215
    • 216
    • 217
    • 218
    • 219
    • 220
    • 221
    • 222
    • 223
    • 224
    • 225
    • 226
    • 227
    • 228
    • 229
    • 230
    • 231
    • 232
    • 233
    • 234
    • 235
    • 236
    • 237
    • 238
    • 239
    • 240

    old:
    IEmpDAO:

    package orm.dao.old;
    
    import orm.entity.Emp;
    
    import java.util.List;
    import java.util.Map;
    
    public interface IEmpDAO {
        List> listAll();
        int insert(Emp emp);
        int update(Emp emp);
        int delete(Integer empId);
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14

    IOrdersDAO:

    package orm.dao.old;
    
    import orm.entity.Emp;
    import orm.entity.Orders;
    
    import java.util.List;
    import java.util.Map;
    
    public interface IOrdersDAO {
        List> listAll();
        int insert(Orders orders);
        int update(Orders orders);
        int delete(Integer orderId);
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15

    EmpDAOImpl:

    package orm.dao.old.impl;
    
    import orm.dao.old.IEmpDAO;
    import orm.entity.Emp;
    
    import java.util.List;
    import java.util.Map;
    
    /**
     * @author: hy
     * @create: 2022-08-11 10:24:36
     */
    public class EmpDAOImpl  implements IEmpDAO {
        @Override
        public List> listAll() {
            return null;
        }
    
        @Override
        public int insert(Emp emp) {
            return 0;
        }
    
        @Override
        public int update(Emp emp) {
            return 0;
        }
    
        @Override
        public int delete(Integer empId) {
            return 0;
        }
    }
    
    
    • 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

    OrdersDAOImpl:

    package orm.dao.old.impl;
    
    import orm.dao.old.IOrdersDAO;
    import orm.entity.Orders;
    
    import java.util.List;
    import java.util.Map;
    
    /**
     * @author: hy
     * @create: 2022-08-11 10:24:56
     */
    public class OrdersDAOImpl implements IOrdersDAO {
        @Override
        public List> listAll() {
            return null;
        }
    
        @Override
        public int insert(Orders orders) {
            return 0;
        }
    
        @Override
        public int update(Orders orders) {
            return 0;
        }
    
        @Override
        public int delete(Integer orderId) {
            return 0;
        }
    }
    
    
    • 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

    anno:

    Table :

    package orm.anno;
    
    import java.lang.annotation.ElementType;
    import java.lang.annotation.Retention;
    import java.lang.annotation.RetentionPolicy;
    import java.lang.annotation.Target;
    
    @Target(ElementType.TYPE)
    @Retention(RetentionPolicy.RUNTIME)
    public @interface Table { //标记表名
        //设置实体对应的数据库表名
        String value();
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14

    TableColumn:

    package orm.anno;
    
    import java.lang.annotation.ElementType;
    import java.lang.annotation.Retention;
    import java.lang.annotation.RetentionPolicy;
    import java.lang.annotation.Target;
    
    @Target(ElementType.FIELD)
    @Retention(RetentionPolicy.RUNTIME)
    public @interface TableColumn { //标记普通列
        //列名
        String colName();
        //列的类型
        String colType();
        //列的长度
        int colSize();
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18

    TableId:

    package orm.anno;
    
    import java.lang.annotation.ElementType;
    import java.lang.annotation.Retention;
    import java.lang.annotation.RetentionPolicy;
    import java.lang.annotation.Target;
    
    /**
     * @author: hy
     * @create: 2022-08-11 10:33:51
     */
    @Target(ElementType.FIELD)
    @Retention(RetentionPolicy.RUNTIME)
    public @interface TableId { //用于定义主键列
        //是否自增
        boolean isAutoGen();
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18

    test:

    Test:

    package orm.test;
    
    import orm.dao.IGenericDAO;
    import orm.dao.impl.GenericDAOImpl;
    import orm.entity.Emp;
    import orm.entity.Orders;
    
    
    import java.sql.Date;
    import java.util.List;
    import java.util.Map;
    
    import static java.util.Date.*;
    
    public class Test {
        public static void main(String[] args) {
    //        IEmpDAO empDAO = new EmpDAOImpl();
    //        empDAO.listAll();
            IGenericDAO dao = new GenericDAOImpl();
    
            //查找
            List> empList =  dao.listAll(Emp.class);
            System.out.println(empList);
    
            //添加
    //        dao.insert(new Emp());
            Emp emp=new Emp("赵六","男", Date.valueOf("1990-09-09"));
            int it=dao.insert(emp);
            if(it==1){
                System.out.println("插入成功!");
            }
    
            //更新
    //        dao.update(new Emp());
            Emp emp1=new Emp(2,"刘七","男", Date.valueOf("1998-09-09"));
            int ue=dao.update(emp1);
            if(ue==1){
                System.out.println("修改成功!");
            }
    
            //删除
    //        dao.delete(10);
            int de=dao.delete(1);
            if(de==1){
                System.out.println("删除成功!");
            }
    
    
            //查询orders (orders的增删改查和emp的思路是相同的。直接拿来用即可。删除的时候需要修改一下列名。略)
            IGenericDAO dao2 = new GenericDAOImpl<>();
    
            List> ordersList =  dao2.listAll(Orders.class);
            System.out.println(ordersList);
    //        dao2.insert(new Orders());
    //        dao2.update(new Orders());
    //        dao2.delete(1);
        }
    }
    
    • 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

    运行结果如下:

    emp和orders的查询:
    在这里插入图片描述
    emp的增删改查和orders的查询:

    在这里插入图片描述
    增删改查之后,emp数据表中的数据:
    在这里插入图片描述

  • 相关阅读:
    NOIP2018 普及组复赛题解
    React UseMemo源码分析
    性能优化:编译器优化选项 -O2/-O3 究竟有多强大?
    3D抓取算法的介绍
    rk3399 linux4.19 ubuntu mpv播放概率性内核崩溃在vop_crtc_atomic_flush
    iOS QQ登录SDK升级后报错Duplicate interface definition for class ‘TencentOAuth‘修复
    成功解决AttributeError: ‘PngImageFile‘ object has no attribute ‘imshow‘
    [数据结构与算法] 线性表之单链表初始化详解
    netty系列之:channel,ServerChannel和netty中的实现
    【云原生之kubernetes实战】使用helm在k8s集群下部署DataEase可视化分析平台
  • 原文地址:https://blog.csdn.net/Liu_wen_wen/article/details/126287660