From bef3c8decade8258cd6cfe1cd8e055b0e1ac1d37 Mon Sep 17 00:00:00 2001 From: qi Date: Mon, 26 Aug 2019 17:18:33 +0800 Subject: [PATCH] =?UTF-8?q?=E6=9B=B4=E6=96=B0=E9=83=A8=E5=88=86=E5=8A=9F?= =?UTF-8?q?=E8=83=BD=E4=BB=A3=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/org/panda/beans/util/BeanUtil.java | 19 ++-- .../java/org/panda/beans/entity/IdEntity.java | 24 +++++ .../beans/entity/IdEntityStrengthen.java | 46 +++++++++ .../java/org/panda/beans/entity/SysUser.java | 50 +--------- .../org/panda/beans/util/BeanUtilTest.java | 13 ++- .../main/java/org/panda/jdbc/JdbcLoad.java | 38 ++++++++ .../main/java/org/panda/jdbc/JdbcUtils.java | 95 +++++++------------ .../org/panda/jdbc/annotation/Column.java | 9 +- .../java/org/panda/jdbc/annotation/Table.java | 27 ++++++ .../java/org/panda/jdbc/sql/MysqlBuilder.java | 14 +-- .../factory/impl/DefaultDbBuilerFactory.java | 1 + .../test/java/org/panda/entity/IdEntity.java | 24 +++++ .../org/panda/entity/IdEntityStrengthen.java | 46 +++++++++ .../test/java/org/panda/entity/SysUser.java | 3 + .../java/org/panda/jdbc/JdbcUtilsTest.java | 59 ++++++++++++ pom.xml | 2 +- 16 files changed, 333 insertions(+), 137 deletions(-) create mode 100644 panda-beans/src/test/java/org/panda/beans/entity/IdEntity.java create mode 100644 panda-beans/src/test/java/org/panda/beans/entity/IdEntityStrengthen.java create mode 100644 panda-jdbc/src/main/java/org/panda/jdbc/JdbcLoad.java create mode 100644 panda-jdbc/src/main/java/org/panda/jdbc/annotation/Table.java create mode 100644 panda-jdbc/src/test/java/org/panda/entity/IdEntity.java create mode 100644 panda-jdbc/src/test/java/org/panda/entity/IdEntityStrengthen.java create mode 100644 panda-jdbc/src/test/java/org/panda/jdbc/JdbcUtilsTest.java diff --git a/panda-beans/src/main/java/org/panda/beans/util/BeanUtil.java b/panda-beans/src/main/java/org/panda/beans/util/BeanUtil.java index b657f2b..c972353 100644 --- a/panda-beans/src/main/java/org/panda/beans/util/BeanUtil.java +++ b/panda-beans/src/main/java/org/panda/beans/util/BeanUtil.java @@ -26,18 +26,15 @@ public class BeanUtil { * @param clazz */ public static List getAllField(Class clazz){ - Field[] fields = getField(clazz); - Field[] superFields = getSuperField(clazz); - List fieldList = new ArrayList<>(fields.length+superFields.length); - if (superFields!=null&&superFields.length>0){ - for (Field field:superFields) { - fieldList.add(field); - } - } - if (fields!=null&&fields.length>0){ - for (Field field:fields){ + + final List fieldList = new ArrayList<>(); + Class currentClass = clazz; + while (currentClass!=null){ + final Field[] fields = currentClass.getDeclaredFields(); + for (final Field field:fields){ fieldList.add(field); } + currentClass = currentClass.getSuperclass(); } return fieldList; } @@ -53,7 +50,7 @@ public class BeanUtil { } /** - * 获取父类的属性 + * 获取上一级父类的属性 * @param clazz * @return */ diff --git a/panda-beans/src/test/java/org/panda/beans/entity/IdEntity.java b/panda-beans/src/test/java/org/panda/beans/entity/IdEntity.java new file mode 100644 index 0000000..1b6a543 --- /dev/null +++ b/panda-beans/src/test/java/org/panda/beans/entity/IdEntity.java @@ -0,0 +1,24 @@ +package org.panda.beans.entity; + +public class IdEntity { + + protected Long id; + + protected Integer status; + + public Long getId() { + return id; + } + + public void setId(Long id) { + this.id = id; + } + + public Integer getStatus() { + return status; + } + + public void setStatus(Integer status) { + this.status = status; + } +} diff --git a/panda-beans/src/test/java/org/panda/beans/entity/IdEntityStrengthen.java b/panda-beans/src/test/java/org/panda/beans/entity/IdEntityStrengthen.java new file mode 100644 index 0000000..6f5b6d0 --- /dev/null +++ b/panda-beans/src/test/java/org/panda/beans/entity/IdEntityStrengthen.java @@ -0,0 +1,46 @@ +package org.panda.beans.entity; + +import java.util.Date; + +public class IdEntityStrengthen extends IdEntity { + + protected Date createTime; + + protected Long createUserId; + + protected Date updateTime; + + protected Long updateUserId; + + public Date getCreateTime() { + return createTime; + } + + public void setCreateTime(Date createTime) { + this.createTime = createTime; + } + + public Long getCreateUserId() { + return createUserId; + } + + public void setCreateUserId(Long createUserId) { + this.createUserId = createUserId; + } + + public Date getUpdateTime() { + return updateTime; + } + + public void setUpdateTime(Date updateTime) { + this.updateTime = updateTime; + } + + public Long getUpdateUserId() { + return updateUserId; + } + + public void setUpdateUserId(Long updateUserId) { + this.updateUserId = updateUserId; + } +} diff --git a/panda-beans/src/test/java/org/panda/beans/entity/SysUser.java b/panda-beans/src/test/java/org/panda/beans/entity/SysUser.java index 7434b91..e3ba0ce 100644 --- a/panda-beans/src/test/java/org/panda/beans/entity/SysUser.java +++ b/panda-beans/src/test/java/org/panda/beans/entity/SysUser.java @@ -7,34 +7,11 @@ import java.util.Date; * @Description: * @Date: Create in 10:25 AM 2019/2/28 */ -public class SysUser { +public class SysUser extends IdEntityStrengthen { - private Long id; private String name; - private Date createTime; - - private Date updateTime; - - public SysUser() { - } - - public SysUser(Long id, String name, Date createTime, Date updateTime) { - this.id = id; - this.name = name; - this.createTime = createTime; - this.updateTime = updateTime; - } - - public Long getId() { - return id; - } - - public void setId(Long id) { - this.id = id; - } - public String getName() { return name; } @@ -43,29 +20,4 @@ public class SysUser { this.name = name; } - public Date getCreateTime() { - return createTime; - } - - public void setCreateTime(Date createTime) { - this.createTime = createTime; - } - - public Date getUpdateTime() { - return updateTime; - } - - public void setUpdateTime(Date updateTime) { - this.updateTime = updateTime; - } - - @Override - public String toString() { - return "SysUser{" + - "id=" + id + - ", name='" + name + '\'' + - ", createTime=" + createTime + - ", updateTime=" + updateTime + - '}'; - } } diff --git a/panda-beans/src/test/java/org/panda/beans/util/BeanUtilTest.java b/panda-beans/src/test/java/org/panda/beans/util/BeanUtilTest.java index 4fabc7f..48ea725 100644 --- a/panda-beans/src/test/java/org/panda/beans/util/BeanUtilTest.java +++ b/panda-beans/src/test/java/org/panda/beans/util/BeanUtilTest.java @@ -4,8 +4,10 @@ import org.junit.Test; import org.panda.beans.entity.SysUser; import java.lang.reflect.Constructor; +import java.lang.reflect.Field; import java.lang.reflect.Method; import java.util.Date; +import java.util.List; import static org.junit.Assert.*; @@ -21,8 +23,7 @@ public class BeanUtilTest { SysUser sysUser = new SysUser(); sysUser.setId(1L); sysUser.setName("liangqi"); - sysUser.setCreateTime(new Date()); - sysUser.setUpdateTime(new Date()); + Class clazz = sysUser.getClass(); Method[] methods = clazz.getMethods(); @@ -36,9 +37,13 @@ public class BeanUtilTest { public void test(){ // SysUser sysUser = BeanUtil.instantiateClass(SysUser.class); - SysUser sysUser = BeanUtil.instantiateClass(SysUser.class,1L,"qi",new Date(),new Date()); - System.out.println(sysUser); + // SysUser sysUser = BeanUtil.instantiateClass(SysUser.class,1L,"qi",new Date(),new Date()); + // System.out.println(sysUser); // System.out.println(sysUser); + List fieldList = BeanUtil.getAllField(SysUser.class); + fieldList.forEach(field -> { + System.out.println(field.getName()); + }); } } \ No newline at end of file diff --git a/panda-jdbc/src/main/java/org/panda/jdbc/JdbcLoad.java b/panda-jdbc/src/main/java/org/panda/jdbc/JdbcLoad.java new file mode 100644 index 0000000..1d1348b --- /dev/null +++ b/panda-jdbc/src/main/java/org/panda/jdbc/JdbcLoad.java @@ -0,0 +1,38 @@ +package org.panda.jdbc; + +import org.panda.jdbc.helper.DbConfigInterface; + +public class JdbcLoad { + + private String driver = null; + + private String url = null; + + private String username = null; + + private String password = null; + + public JdbcLoad(String driver, String url, String username, String password) { + this.driver = driver; + this.url = url; + this.username = username; + this.password = password; + loadDriver(); + } + + public JdbcLoad(DbConfigInterface dbConfigInterface){ + this.driver = dbConfigInterface.getJdbcDriver(); + this.url = dbConfigInterface.getJdbcUrl(); + this.username =dbConfigInterface.getJdbcUser(); + this.password =dbConfigInterface.getJdbcPassword(); + loadDriver(); + } + + private void loadDriver(){ + try { + Class.forName(this.driver); + } catch (ClassNotFoundException e) { + e.printStackTrace(); + } + } +} diff --git a/panda-jdbc/src/main/java/org/panda/jdbc/JdbcUtils.java b/panda-jdbc/src/main/java/org/panda/jdbc/JdbcUtils.java index b7957d3..d844ebe 100644 --- a/panda-jdbc/src/main/java/org/panda/jdbc/JdbcUtils.java +++ b/panda-jdbc/src/main/java/org/panda/jdbc/JdbcUtils.java @@ -22,53 +22,8 @@ public class JdbcUtils { */ private static final ThreadLocal CONNECTION_HOLDER = new ThreadLocal<>(); - private String driver = null; - - private String url = null; - - private String username = null; - - private String password = null; - private DataSource dataSource; - public JdbcUtils(String driver, String url, String username, String password) { - this.driver = driver; - this.url = url; - this.username = username; - this.password = password; - loadDriver(); - } - - public JdbcUtils(DbConfigInterface dbConfigInterface){ - this.driver = dbConfigInterface.getJdbcDriver(); - this.url = dbConfigInterface.getJdbcUrl(); - this.username =dbConfigInterface.getJdbcUser(); - this.password =dbConfigInterface.getJdbcPassword(); - loadDriver(); - } - - private void loadDriver(){ - try { - Class.forName(this.driver); - } catch (ClassNotFoundException e) { - e.printStackTrace(); - } - } - - @Deprecated - public JdbcUtils(Properties prop) { - this.driver = PropsUtil.getString(prop, "jdbc.driver"); - this.url = PropsUtil.getString(prop, "jdbc.url"); - this.username = PropsUtil.getString(prop, "jdbc.user"); - this.password = PropsUtil.getString(prop, "jdbc.password"); - try { - Class.forName(driver); - } catch (ClassNotFoundException e) { - e.printStackTrace(); - } - } - public JdbcUtils(DataSource dataSource){ this.dataSource = dataSource; } @@ -87,17 +42,6 @@ public class JdbcUtils { } catch (SQLException e) { e.printStackTrace(); } - }else { - conn = CONNECTION_HOLDER.get(); - if (conn == null) { - try { - conn = DriverManager.getConnection(url, username, password); - } catch (SQLException e) { - e.printStackTrace(); - } finally { - CONNECTION_HOLDER.set(conn); - } - } } return conn; } @@ -119,7 +63,7 @@ public class JdbcUtils { } catch (SQLException e) { e.printStackTrace(); } finally { - release(conn, null, null); + release( null, null); } return dbInfo; @@ -156,7 +100,7 @@ public class JdbcUtils { } catch (SQLException e) { e.printStackTrace(); } finally { - release(conn, null, rs); + release( null, rs); } return tableNameList; } @@ -185,7 +129,7 @@ public class JdbcUtils { } catch (SQLException e) { e.printStackTrace(); } finally { - release(conn, st, rs); + release( st, rs); } } @@ -218,7 +162,7 @@ public class JdbcUtils { } catch (SQLException e) { e.printStackTrace(); } finally { - release(conn, st, rs); + release( st, rs); } } @@ -246,18 +190,19 @@ public class JdbcUtils { } catch (SQLException e) { e.printStackTrace(); } finally { - release(conn, st, rs); + release( st, rs); } return rs; } /** - * 关闭数据库连接 - * + * 关闭数据库连接(废弃) + * 废弃原因:采用数据库连接池,不用关闭连接 * @param conn * @param st * @param rs */ + @Deprecated public void release(Connection conn, Statement st, ResultSet rs) { if (rs != null) { try { @@ -284,5 +229,29 @@ public class JdbcUtils { } } + /** + * 关闭数据库连接 + * @param st + * @param rs (结果集) + */ + public void release( Statement st, ResultSet rs) { + if (rs != null) { + try { + rs.close(); + } catch (SQLException e) { + e.printStackTrace(); + } + rs = null; + } + if (st != null) { + try { + st.close(); + } catch (SQLException e) { + e.printStackTrace(); + } + } + + } + } diff --git a/panda-jdbc/src/main/java/org/panda/jdbc/annotation/Column.java b/panda-jdbc/src/main/java/org/panda/jdbc/annotation/Column.java index 5348861..3dd4158 100644 --- a/panda-jdbc/src/main/java/org/panda/jdbc/annotation/Column.java +++ b/panda-jdbc/src/main/java/org/panda/jdbc/annotation/Column.java @@ -4,6 +4,7 @@ import java.lang.annotation.*; /** * 列名详情注解 + * @author qi */ @Target(ElementType.FIELD) @Retention(RetentionPolicy.RUNTIME) @@ -20,6 +21,10 @@ public @interface Column { * 字段长度 * @return */ - int length() default 100; - + int length() default 50; + /** + * 是否为空 false:NOT NULL :true:NULL + * 默认 true + */ + boolean isNull() default true; } diff --git a/panda-jdbc/src/main/java/org/panda/jdbc/annotation/Table.java b/panda-jdbc/src/main/java/org/panda/jdbc/annotation/Table.java new file mode 100644 index 0000000..d82c4c6 --- /dev/null +++ b/panda-jdbc/src/main/java/org/panda/jdbc/annotation/Table.java @@ -0,0 +1,27 @@ +package org.panda.jdbc.annotation; + +import java.lang.annotation.*; + +/** + * @author qi + * 数据库表注解 + * + */ +@Target(ElementType.TYPE) +@Retention(RetentionPolicy.RUNTIME) +@Inherited +public @interface Table { + /** + * ENGINE 设置存储引擎 + * 类型: ISAM MyISAM HEAP InnoDB + * 相关链接:https://www.cnblogs.com/zhangjinghe/p/7599988.html + * @return + */ + String engine() default "InnoDB"; + + /** + * CHARSET 设置编码 + * @return + */ + String charset() default "utf-8"; +} diff --git a/panda-jdbc/src/main/java/org/panda/jdbc/sql/MysqlBuilder.java b/panda-jdbc/src/main/java/org/panda/jdbc/sql/MysqlBuilder.java index 87d939b..97fe5e7 100644 --- a/panda-jdbc/src/main/java/org/panda/jdbc/sql/MysqlBuilder.java +++ b/panda-jdbc/src/main/java/org/panda/jdbc/sql/MysqlBuilder.java @@ -22,9 +22,10 @@ public class MysqlBuilder extends AbstractDbBuiler { static { dataTypeMap.put("class java.lang.String","varchar"); - dataTypeMap.put("class java.util.Date","date"); + dataTypeMap.put("class java.util.Date","datetime"); dataTypeMap.put("class java.lang.Integer","int"); - dataTypeMap.put("class java.lang.Long","int"); + dataTypeMap.put("class java.lang.Long","bigint"); + dataTypeMap.put("int","int"); } @@ -101,9 +102,8 @@ public class MysqlBuilder extends AbstractDbBuiler { //表名 String tableName = BeanSqlUtil.beanNameToTableName(clazz); sql.append(tableName); - sql.append("("); - sql.append("id bigint auto_increment not null,"); - Field[] fields = BeanUtil.getField(clazz); + sql.append("\n("); + sql.append("id bigint auto_increment not null,\n"); List> mapList= BeanUtil.getFieldInfo(BeanUtil.getField(clazz)); for (Map map:mapList){ String dataTypeStr = dataTypeMap.get(map.get("fieldType")); @@ -116,9 +116,9 @@ public class MysqlBuilder extends AbstractDbBuiler { sql.append("(100)"); } } - sql.append(","); + sql.append(",\n"); } - sql.append("primary key(id)"); + sql.append("primary key(id)\n"); sql.append(")ENGINE=Innodb DEFAULT CHARSET=utf8;"); return sql.toString(); diff --git a/panda-jdbc/src/main/java/org/panda/jdbc/sql/factory/impl/DefaultDbBuilerFactory.java b/panda-jdbc/src/main/java/org/panda/jdbc/sql/factory/impl/DefaultDbBuilerFactory.java index 219d3bb..6da25bd 100644 --- a/panda-jdbc/src/main/java/org/panda/jdbc/sql/factory/impl/DefaultDbBuilerFactory.java +++ b/panda-jdbc/src/main/java/org/panda/jdbc/sql/factory/impl/DefaultDbBuilerFactory.java @@ -24,6 +24,7 @@ public class DefaultDbBuilerFactory implements DbBuilerFactory { case SqlConstant.DB_TYPE_ORACLE: abstractDbBuiler = new OracleBuiler(); break; + default: break; } return abstractDbBuiler; } diff --git a/panda-jdbc/src/test/java/org/panda/entity/IdEntity.java b/panda-jdbc/src/test/java/org/panda/entity/IdEntity.java new file mode 100644 index 0000000..3b90d7e --- /dev/null +++ b/panda-jdbc/src/test/java/org/panda/entity/IdEntity.java @@ -0,0 +1,24 @@ +package org.panda.entity; + +public class IdEntity { + + protected Long id; + + protected Integer status; + + public Long getId() { + return id; + } + + public void setId(Long id) { + this.id = id; + } + + public Integer getStatus() { + return status; + } + + public void setStatus(Integer status) { + this.status = status; + } +} diff --git a/panda-jdbc/src/test/java/org/panda/entity/IdEntityStrengthen.java b/panda-jdbc/src/test/java/org/panda/entity/IdEntityStrengthen.java new file mode 100644 index 0000000..1fd4a52 --- /dev/null +++ b/panda-jdbc/src/test/java/org/panda/entity/IdEntityStrengthen.java @@ -0,0 +1,46 @@ +package org.panda.entity; + +import java.util.Date; + +public class IdEntityStrengthen extends IdEntity { + + protected Date createTime; + + protected Long createUserId; + + protected Date updateTime; + + protected Long updateUserId; + + public Date getCreateTime() { + return createTime; + } + + public void setCreateTime(Date createTime) { + this.createTime = createTime; + } + + public Long getCreateUserId() { + return createUserId; + } + + public void setCreateUserId(Long createUserId) { + this.createUserId = createUserId; + } + + public Date getUpdateTime() { + return updateTime; + } + + public void setUpdateTime(Date updateTime) { + this.updateTime = updateTime; + } + + public Long getUpdateUserId() { + return updateUserId; + } + + public void setUpdateUserId(Long updateUserId) { + this.updateUserId = updateUserId; + } +} diff --git a/panda-jdbc/src/test/java/org/panda/entity/SysUser.java b/panda-jdbc/src/test/java/org/panda/entity/SysUser.java index b5a9826..082172a 100644 --- a/panda-jdbc/src/test/java/org/panda/entity/SysUser.java +++ b/panda-jdbc/src/test/java/org/panda/entity/SysUser.java @@ -1,7 +1,10 @@ package org.panda.entity; +import org.panda.jdbc.annotation.Table; + import java.util.Date; +@Table public class SysUser { private Long id; diff --git a/panda-jdbc/src/test/java/org/panda/jdbc/JdbcUtilsTest.java b/panda-jdbc/src/test/java/org/panda/jdbc/JdbcUtilsTest.java new file mode 100644 index 0000000..7ee9a18 --- /dev/null +++ b/panda-jdbc/src/test/java/org/panda/jdbc/JdbcUtilsTest.java @@ -0,0 +1,59 @@ +package org.panda.jdbc; + +import org.junit.Test; +import org.panda.entity.SysUser; +import org.panda.jdbc.annotation.Table; +import org.panda.jdbc.sql.AbstractDbBuiler; +import org.panda.jdbc.sql.SqlConstant; +import org.panda.jdbc.sql.factory.DbBuilerFactory; +import org.panda.jdbc.sql.factory.impl.DefaultDbBuilerFactory; + +import static org.junit.Assert.*; + +public class JdbcUtilsTest { + + @Test + public void getConnection() { + } + + @Test + public void getDBInfo() { + } + + @Test + public void getAllTable() { + } + + @Test + public void update() { + } + + @Test + public void beatchUpdate() { + + } + + @Test + public void createTable() { + + Class clzz = SysUser.class; + + + DbBuilerFactory dbBuilerFactory = new DefaultDbBuilerFactory(); + AbstractDbBuiler abstractDbBuiler = dbBuilerFactory.getSqlBuilder(SqlConstant.DB_TYPE_MYSQL); + String createSql = abstractDbBuiler.createTableStr(SysUser.class); + System.out.println(createSql); + } + + @Test + public void query() { + } + + @Test + public void release() { + } + + @Test + public void release1() { + } +} \ No newline at end of file diff --git a/pom.xml b/pom.xml index 290fcc6..bfb52ec 100644 --- a/pom.xml +++ b/pom.xml @@ -21,7 +21,7 @@ 1.7.12 1.2.17 - 1.0.0 + 1.0.1