手写ORM框架:O(Object 对象) R(Relative 关系) M(Mapping 映射)对象关系映射。把数据中的表 映射为java中的实体类,表中的一条记录映射为java实体类对象。表中的列映射实体的属性 。
手写ORM框架使用的技术:泛型 注解 反射
1.新建maven工程,配置环境
<!--引入mysql配置文件-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.31</version>
</dependency>
2.创建连接数据库的工具类
package com.hmq.util; import java.io.InputStream; import java.sql.Connection; import java.sql.DriverManager; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.util.Properties; public class DbUtil { //硬编码 private static String driverName=""; private static String url=""; private static String username=""; private static String password=""; //1.获取连接对象 public static Connection getConnection() throws Exception{ Class.forName(driverName); Connection connection = DriverManager.getConnection(url, username, password); return connection; } static{ try { //静态代码块 ----随着类的加载而被加载,而且只会被加载一次 InputStream inputStream = ClassLoader.getSystemResourceAsStream("db.properties"); Properties properties = new Properties(); properties.load(inputStream);//try 抛出load 的异常 driverName = properties.getProperty("jdbc.driverName"); url = properties.getProperty("jdbc.url"); username = properties.getProperty("jdbc.username"); password = properties.getProperty("jdbc.password"); }catch (Exception e){ System.out.println("名字为db.properties的属性文件不存在"); e.printStackTrace(); } } //2.关闭资源 public static void closeAll(Connection connection, PreparedStatement ps, ResultSet rs){ try{ if (rs!=null){ rs.close(); } if (ps!=null){ ps.close(); } if (connection!=null){ connection.close(); } }catch (Exception e){ e.printStackTrace(); } } }
db.properties
jdbc.driverName=com.mysql.cj.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/qy165?serverTimezone=Asia/Shanghai
jdbc.username=root
jdbc.password=123456
3.创建一个父类BaseDao
一个子类DAO继承BaseDao类后,无需再写任何sql语句,而就可以完成单表的crud操作
比如:StudentDao extends BaseDao DeptDao extends BaseDao
BaseDao 对所有表的CRUD操作都能通用
package com.hmq.dao;
import com.hmq.ananotation.TableField;
import com.hmq.ananotation.TableId;
import com.hmq.ananotation.TableName;
import com.hmq.util.DbUtil;
import java.lang.reflect.Field;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.util.ArrayList;
import java.util.List;
public class BaseDao<T>{
//通用的添加操作
//通用的添加sql语句:insert into 表名 (列名,列名...) values(值,值...)
public int insert(T t) throws Exception{
StringBuffer sql = new StringBuffer("insert into ");//注意空格
//根据对象获取Class反射类
Class<?> aClass = t.getClass();
//获取反射类上的注解对象
TableName annotation = aClass.getAnnotation(TableName.class);
//表名
String tableName=aClass.getSimpleName();
if (annotation!=null){
tableName=annotation.value();
}
sql.append(tableName);
//获取列名
Field[] declaredFields = aClass.getDeclaredFields();
//列名
List<String> columns = new ArrayList<String>();
List<String> values = new ArrayList<String>();
for (Field field:declaredFields){
String name = field.getName();//属性名一样时
TableId annotation2 = field.getAnnotation(TableId.class);
if (annotation2!=null||name.equals("id")){
continue;
}
//属性名不一样时
TableField annotation1 = field.getAnnotation(TableField.class);
if (annotation1!=null){
name=annotation1.value();
}
field.setAccessible(true);
Object o = field.get(t);
columns.add(name);
values.add("'"+o+"'");//id name loc values ('10','政治部','郑州')
}
String columnNames = columns.toString().replace("[","(").replace("]",")");//将打印的'[]'换成'()'
String columnValues = values.toString().replace("[","(").replace("]",")");
sql.append(columnNames);
sql.append(" values ");
sql.append(columnValues);
//执行sql
Connection connection = DbUtil.getConnection();//获取连接对象
PreparedStatement ps = connection.prepareStatement(sql.toString());//拿到ps对象
int i = ps.executeUpdate();
return i;
}
//修改方法
//sql:update 表名 set 列名=值,列名=值....where 主键名=值;
public int update(T t) throws Exception {
StringBuffer sql = new StringBuffer(" update ");
//获取实体类的反射类
Class<?> aClass = t.getClass();
//获取反射类上指定的注解对象
TableName annotation = aClass.getAnnotation(TableName.class);
String tableName = aClass.getSimpleName();
if (annotation != null) {
tableName = annotation.value();
}
sql.append(tableName + " set ");
//获取所有的Field
Field[] declaredFields = aClass.getDeclaredFields();
String where = " where ";
for (Field field : declaredFields) {
field.setAccessible(true);
String name = field.getName();
TableId tableId = field.getAnnotation(TableId.class);
if (name.equals("id")) {
where = where +" id='"+field.get(t)+"'";
continue;
}
if (tableId!=null){
where = where + tableId.value()+" '"+field.get(t)+"'";
continue;
}
TableField tableField = field.getAnnotation(TableField.class);
if (tableField != null) {
name = tableField.value();
}
String value = "'" + field.get(t) + "'";
sql.append(name + "=" + value + ",");
}
System.out.println(where);
String sql2 = sql.toString().substring(0,sql.length()-1)+where;
System.out.println(sql2);
return 0;
}
}