更新部分功能代码

master
星期八 5 years ago
parent 7b16d69578
commit bef3c8deca
  1. 19
      panda-beans/src/main/java/org/panda/beans/util/BeanUtil.java
  2. 24
      panda-beans/src/test/java/org/panda/beans/entity/IdEntity.java
  3. 46
      panda-beans/src/test/java/org/panda/beans/entity/IdEntityStrengthen.java
  4. 50
      panda-beans/src/test/java/org/panda/beans/entity/SysUser.java
  5. 13
      panda-beans/src/test/java/org/panda/beans/util/BeanUtilTest.java
  6. 38
      panda-jdbc/src/main/java/org/panda/jdbc/JdbcLoad.java
  7. 95
      panda-jdbc/src/main/java/org/panda/jdbc/JdbcUtils.java
  8. 9
      panda-jdbc/src/main/java/org/panda/jdbc/annotation/Column.java
  9. 27
      panda-jdbc/src/main/java/org/panda/jdbc/annotation/Table.java
  10. 14
      panda-jdbc/src/main/java/org/panda/jdbc/sql/MysqlBuilder.java
  11. 1
      panda-jdbc/src/main/java/org/panda/jdbc/sql/factory/impl/DefaultDbBuilerFactory.java
  12. 24
      panda-jdbc/src/test/java/org/panda/entity/IdEntity.java
  13. 46
      panda-jdbc/src/test/java/org/panda/entity/IdEntityStrengthen.java
  14. 3
      panda-jdbc/src/test/java/org/panda/entity/SysUser.java
  15. 59
      panda-jdbc/src/test/java/org/panda/jdbc/JdbcUtilsTest.java
  16. 2
      pom.xml

@ -26,18 +26,15 @@ public class BeanUtil {
* @param clazz
*/
public static List<Field> getAllField(Class clazz){
Field[] fields = getField(clazz);
Field[] superFields = getSuperField(clazz);
List<Field> 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<Field> 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
*/

@ -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;
}
}

@ -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;
}
}

@ -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 +
'}';
}
}

@ -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<Field> fieldList = BeanUtil.getAllField(SysUser.class);
fieldList.forEach(field -> {
System.out.println(field.getName());
});
}
}

@ -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();
}
}
}

@ -22,53 +22,8 @@ public class JdbcUtils {
*/
private static final ThreadLocal<Connection> 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();
}
}
}
}

@ -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;
}

@ -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";
}

@ -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<Map<String,String>> mapList= BeanUtil.getFieldInfo(BeanUtil.getField(clazz));
for (Map<String,String> 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();

@ -24,6 +24,7 @@ public class DefaultDbBuilerFactory implements DbBuilerFactory {
case SqlConstant.DB_TYPE_ORACLE:
abstractDbBuiler = new OracleBuiler();
break;
default: break;
}
return abstractDbBuiler;
}

@ -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;
}
}

@ -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;
}
}

@ -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;

@ -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() {
}
}

@ -21,7 +21,7 @@
<slf4j.version>1.7.12</slf4j.version>
<log4j.version>1.2.17</log4j.version>
<!-- 项目版本号 -->
<project.version>1.0.0</project.version>
<project.version>1.0.1</project.version>
</properties>
<dependencies>

Loading…
Cancel
Save