|
@@ -0,0 +1,219 @@
|
|
|
+package com.lc.utils;
|
|
|
+
|
|
|
+import java.lang.reflect.Constructor;
|
|
|
+import java.lang.reflect.Field;
|
|
|
+import java.lang.reflect.ParameterizedType;
|
|
|
+import java.lang.reflect.Type;
|
|
|
+import java.sql.Connection;
|
|
|
+import java.sql.PreparedStatement;
|
|
|
+import java.sql.ResultSet;
|
|
|
+import java.sql.SQLException;
|
|
|
+import java.util.ArrayList;
|
|
|
+import java.util.List;
|
|
|
+
|
|
|
+/**
|
|
|
+ * ClassName: BaseDao
|
|
|
+ *
|
|
|
+ * @Author 爱扣钉-陈晨
|
|
|
+ * @Create 2024/1/28 14:30
|
|
|
+ * @Version 1.0
|
|
|
+ */
|
|
|
+public class BaseDAO<T> {
|
|
|
+
|
|
|
+ static Connection connection = null;
|
|
|
+ //获取连接
|
|
|
+ static {
|
|
|
+ //获取连接
|
|
|
+ try {
|
|
|
+ connection = DButils.getConnection();
|
|
|
+ } catch (SQLException e) {
|
|
|
+ throw new RuntimeException(e);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ public int insert(T t) throws Exception {
|
|
|
+ Class<T> clazz = (Class<T>) t.getClass();
|
|
|
+
|
|
|
+ String sql = "insert into " + getTableName(clazz) + "( "+getFiledSql(clazz)+" ) values ( ";
|
|
|
+
|
|
|
+ Field[] declaredFields = clazz.getDeclaredFields();
|
|
|
+ int length = declaredFields.length;
|
|
|
+ for (int i = 0; i < length; i++) {
|
|
|
+ if (i== length - 1){
|
|
|
+ sql+= " ? );";
|
|
|
+ }else{
|
|
|
+ sql+= " ?,";
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ PreparedStatement preparedStatement = connection.prepareStatement(sql);
|
|
|
+
|
|
|
+ for (int i = 1; i <= declaredFields.length; i++) {
|
|
|
+ declaredFields[i-1].setAccessible(true);
|
|
|
+
|
|
|
+ preparedStatement.setObject(i,declaredFields[i-1].get(t));
|
|
|
+ }
|
|
|
+
|
|
|
+ return preparedStatement.executeUpdate();
|
|
|
+ }
|
|
|
+
|
|
|
+ public int update(T t) throws Exception {
|
|
|
+ Class<T> clazz = (Class<T>) t.getClass();
|
|
|
+
|
|
|
+ String sql = " update " + getTableName(clazz) + " set ";
|
|
|
+
|
|
|
+ Field[] declaredFields = clazz.getDeclaredFields();
|
|
|
+
|
|
|
+ for (Field declaredField : declaredFields) {
|
|
|
+ String filedName = filedName(declaredField);
|
|
|
+ if (!declaredField.getName().equals("id")){
|
|
|
+ sql+= " " + filedName + " = ? , ";
|
|
|
+ }
|
|
|
+ }
|
|
|
+ sql = sql.substring(0, sql.length() - 2); // 去除最后一个逗号和空格
|
|
|
+
|
|
|
+ sql+= " where id = ? ";
|
|
|
+
|
|
|
+ PreparedStatement preparedStatement = connection.prepareStatement(sql);
|
|
|
+
|
|
|
+ int index = 0;
|
|
|
+ for (Field declaredField : declaredFields) {
|
|
|
+ if (!declaredField.getName().equals("id")){
|
|
|
+ declaredField.setAccessible(true);
|
|
|
+ preparedStatement.setObject(index,declaredField.get(t));
|
|
|
+ }
|
|
|
+ index++;
|
|
|
+ }
|
|
|
+ Field id = clazz.getDeclaredField("id");
|
|
|
+ id.setAccessible(true);
|
|
|
+ preparedStatement.setObject(index++,id.get(t));
|
|
|
+
|
|
|
+ return preparedStatement.executeUpdate();
|
|
|
+ }
|
|
|
+
|
|
|
+ public int del(int id) throws Exception {
|
|
|
+ Class<T> clazzClass = getClazzClass();
|
|
|
+ String sql = " delete from " + getTableName(clazzClass) + " where id = ? ";
|
|
|
+ PreparedStatement preparedStatement = connection.prepareStatement(sql);
|
|
|
+ preparedStatement.setObject(1,id);
|
|
|
+ return preparedStatement.executeUpdate();
|
|
|
+ }
|
|
|
+
|
|
|
+ public List<T> getAll() {
|
|
|
+ //this 子类
|
|
|
+ try {
|
|
|
+ Class<T> clazz = getClazzClass();
|
|
|
+ //sql
|
|
|
+ String sql = "select " + getFiledSql(clazz) + " from " + getTableName(clazz);
|
|
|
+
|
|
|
+ //连接 DButils
|
|
|
+ Connection connection = DButils.getConnection();
|
|
|
+ PreparedStatement preparedStatement = connection.prepareStatement(sql);
|
|
|
+ //结果
|
|
|
+ ResultSet rs = preparedStatement.executeQuery();
|
|
|
+
|
|
|
+ List<T> list = new ArrayList<>();
|
|
|
+
|
|
|
+ while (rs.next()){
|
|
|
+ Constructor<T> constructor = clazz.getConstructor();
|
|
|
+ T t = constructor.newInstance();
|
|
|
+ //反射完成赋值
|
|
|
+ Field[] declaredFields = clazz.getDeclaredFields();
|
|
|
+ for (Field declaredField : declaredFields) {
|
|
|
+ declaredField.setAccessible(true);
|
|
|
+ declaredField.set(t,rs.getObject(filedName(declaredField)));
|
|
|
+ }
|
|
|
+ list.add(t);
|
|
|
+ }
|
|
|
+
|
|
|
+ return list;
|
|
|
+
|
|
|
+ } catch (Exception e) {
|
|
|
+ throw new RuntimeException(e);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ public T getId(int id) {
|
|
|
+ //this 子类
|
|
|
+ try {
|
|
|
+ Class<T> clazz = getClazzClass();
|
|
|
+
|
|
|
+ //sql
|
|
|
+ String sql = "select " + getFiledSql(clazz) + " from " + getTableName(clazz) + " where id = ? ";
|
|
|
+
|
|
|
+ //连接 DButils
|
|
|
+ Connection connection = DButils.getConnection();
|
|
|
+ PreparedStatement preparedStatement = connection.prepareStatement(sql);
|
|
|
+ //设置参数
|
|
|
+ preparedStatement.setInt(1,id);
|
|
|
+ //结果
|
|
|
+ ResultSet rs = preparedStatement.executeQuery();
|
|
|
+ //反射
|
|
|
+ return getT(rs, clazz);
|
|
|
+
|
|
|
+ } catch (Exception e) {
|
|
|
+ throw new RuntimeException(e);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ public T getT(ResultSet rs , Class<T> clazz) throws Exception{
|
|
|
+
|
|
|
+ Constructor<T> constructor = clazz.getConstructor();
|
|
|
+ T t = constructor.newInstance();
|
|
|
+
|
|
|
+ while (rs.next()){
|
|
|
+ //反射完成赋值
|
|
|
+ Field[] declaredFields = clazz.getDeclaredFields();
|
|
|
+ for (Field declaredField : declaredFields) {
|
|
|
+ declaredField.setAccessible(true);
|
|
|
+ declaredField.set(t,rs.getObject(filedName(declaredField)));
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return t;
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ private String getTableName(Class<T> clazz) {
|
|
|
+ //获取表名
|
|
|
+ if (clazz.isAnnotationPresent(Table.class)){
|
|
|
+ return clazz.getAnnotation(Table.class).value();
|
|
|
+ }else {
|
|
|
+ return clazz.getSimpleName();
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ private String getFiledSql(Class<T> clazz){
|
|
|
+ String filedSql = "";
|
|
|
+ //获取字段
|
|
|
+ Field[] fields = clazz.getDeclaredFields();
|
|
|
+ for (Field field : fields) {
|
|
|
+ //获取字段名
|
|
|
+ filedSql += filedName(field) + " , ";
|
|
|
+ }
|
|
|
+ filedSql = filedSql.substring(0,filedSql.lastIndexOf(","));
|
|
|
+
|
|
|
+ return filedSql;
|
|
|
+ }
|
|
|
+
|
|
|
+ public String filedName(Field field){
|
|
|
+ if (field.isAnnotationPresent(Column.class)){
|
|
|
+ //有注解
|
|
|
+ return field.getAnnotation(Column.class).filed();
|
|
|
+ }else{
|
|
|
+ //没有注解
|
|
|
+ return field.getName();
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ public Class<T> getClazzClass() throws ClassNotFoundException {
|
|
|
+ Class clazz = this.getClass();
|
|
|
+ //获取父类泛型类型
|
|
|
+ ParameterizedType genericSuperclass = (ParameterizedType) clazz.getGenericSuperclass();
|
|
|
+ //泛型
|
|
|
+ Type cla = genericSuperclass.getActualTypeArguments()[0];
|
|
|
+ //javabean的class
|
|
|
+ Class<T> typeClazz = (Class<T>) Class.forName(cla.getTypeName());
|
|
|
+ return typeClazz;
|
|
|
+ }
|
|
|
+}
|