jdbc 模块功能扩展

master
星期八 5 years ago
parent baacdea833
commit 64b09091d6
  1. 7
      panda-jdbc/src/main/java/org/panda/jdbc/JdbcUtils.java
  2. 61
      panda-jdbc/src/main/java/org/panda/jdbc/TableUtil.java
  3. 15
      panda-jdbc/src/main/java/org/panda/jdbc/helper/DataBaseHelper.java
  4. 14
      panda-jdbc/src/main/java/org/panda/jdbc/helper/DbConfigHelper.java
  5. 18
      panda-jdbc/src/main/java/org/panda/jdbc/mode/Column.java
  6. 3
      panda-jdbc/src/main/java/org/panda/jdbc/sql/BeanSqlUtil.java
  7. 1
      panda-jdbc/src/main/java/org/panda/jdbc/sql/builder/MysqlBuilder.java
  8. 3
      panda-jdbc/src/main/java/org/panda/jdbc/sql/factory/impl/DefaultDbBuilerFactory.java
  9. 2
      panda-jdbc/src/test/java/org/panda/entity/SysUser.java
  10. 14
      panda-jdbc/src/test/java/org/panda/jdbc/JdbcUtilsTest.java

@ -190,19 +190,18 @@ public class JdbcUtils {
} catch (SQLException e) { } catch (SQLException e) {
e.printStackTrace(); e.printStackTrace();
} finally { } finally {
release( st, rs); release(conn, st, rs);
} }
return rs; return rs;
} }
/** /**
* 关闭数据库连接(废弃) * 关闭数据库连接
* 废弃原因采用数据库连接池不用关闭连接
* @param conn * @param conn
* @param st * @param st
* @param rs * @param rs
*/ */
@Deprecated
public void release(Connection conn, Statement st, ResultSet rs) { public void release(Connection conn, Statement st, ResultSet rs) {
if (rs != null) { if (rs != null) {
try { try {

@ -0,0 +1,61 @@
package org.panda.jdbc;
import org.panda.beans.util.BeanUtil;
import org.panda.jdbc.mode.Column;
import org.panda.jdbc.mode.Table;
import org.panda.jdbc.sql.BeanSqlUtil;
import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.List;
/**
* @Author: qi
* @Description:
* @Date: Create in 8:44 AM 2019/11/2
*/
public class TableUtil {
/**
* class 对象转换成 tablle对象
* @param clazz
* @return
*/
public Table beanToTable(Class clazz){
Table table = new Table();
String tableName = BeanSqlUtil.beanNameToTableName(clazz);
table.setTableName(tableName);
List<Field> fields = BeanUtil.getAllField(clazz);
List<Column> columnList = new ArrayList<>();
fields.forEach(field -> {
Column column = fieldToColumn(field);
columnList.add(column);
});
table.setColumns(columnList);
return table;
}
/**
* 对象属性转换成 表列
* @param field
* @return
*/
public Column fieldToColumn(Field field){
Column column = new Column();
if (field.isAnnotationPresent(org.panda.jdbc.annotation.Column.class)){
org.panda.jdbc.annotation.Column annotationColumn = field.getAnnotation(org.panda.jdbc.annotation.Column.class);
int length = annotationColumn.length();
String defalutValue = annotationColumn.defaultValue();
boolean isNull = annotationColumn.isNull();
column.setColumnSize(length);
column.setColumnDef(defalutValue);
column.setColumnName(BeanSqlUtil.caseToHump(field.getName()));
column.setNull(isNull);
}
return column;
}
}

@ -6,7 +6,7 @@ import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler; import org.apache.commons.dbutils.handlers.BeanListHandler;
import org.apache.commons.dbutils.handlers.MapListHandler; import org.apache.commons.dbutils.handlers.MapListHandler;
import org.panda.beans.util.BeanUtil; import org.panda.beans.util.BeanUtil;
import org.panda.jdbc.sql.AbstractDbBuiler; import org.panda.jdbc.sql.builder.AbstractDbBuiler;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
@ -40,11 +40,14 @@ public class DataBaseHelper {
private static final String JDBC_PASSWORD; private static final String JDBC_PASSWORD;
static { static {
JDBC_DRIVER = DbConfigHelper.getJdbcDriver(); // JDBC_DRIVER = DbConfigHelper.getJdbcDriver();
JDBC_URL = DbConfigHelper.getJdbcUrl(); // JDBC_URL = DbConfigHelper.getJdbcUrl();
JDBC_USER = DbConfigHelper.getJdbcUser(); // JDBC_USER = DbConfigHelper.getJdbcUser();
JDBC_PASSWORD = DbConfigHelper.getJdbcPassword(); // JDBC_PASSWORD = DbConfigHelper.getJdbcPassword();
JDBC_DRIVER = null;
JDBC_URL = null;
JDBC_USER = null;
JDBC_PASSWORD = null;
DATA_SOURCE = new BasicDataSource(); DATA_SOURCE = new BasicDataSource();
DATA_SOURCE.setDriverClassName(JDBC_DRIVER); DATA_SOURCE.setDriverClassName(JDBC_DRIVER);
DATA_SOURCE.setUrl(JDBC_URL); DATA_SOURCE.setUrl(JDBC_URL);

@ -9,14 +9,15 @@ import java.util.Properties;
* @author qi * @author qi
* 属性文件助手类 * 属性文件助手类
*/ */
public class DbConfigHelper { public class DbConfigHelper implements DbConfigInterface {
private static final Properties CONFIG_PROPS = PropsUtil.loadProps(DbConfigConstant.CONFIG_FILE); private static final Properties CONFIG_PROPS = PropsUtil.loadProps(DbConfigConstant.CONFIG_FILE);
/** /**
* 获取数据库驱动 * 获取数据库驱动
*/ */
public static String getJdbcDriver() { @Override
public String getJdbcDriver() {
return PropsUtil.getString(CONFIG_PROPS, DbConfigConstant.JDBC_DRIVER); return PropsUtil.getString(CONFIG_PROPS, DbConfigConstant.JDBC_DRIVER);
} }
@ -26,7 +27,8 @@ public class DbConfigHelper {
* *
* @return * @return
*/ */
public static String getJdbcUrl() { @Override
public String getJdbcUrl() {
return PropsUtil.getString(CONFIG_PROPS, DbConfigConstant.JDBC_URL); return PropsUtil.getString(CONFIG_PROPS, DbConfigConstant.JDBC_URL);
} }
@ -36,7 +38,8 @@ public class DbConfigHelper {
* *
* @return * @return
*/ */
public static String getJdbcUser() { @Override
public String getJdbcUser() {
return PropsUtil.getString(CONFIG_PROPS, DbConfigConstant.JDBC_USER); return PropsUtil.getString(CONFIG_PROPS, DbConfigConstant.JDBC_USER);
} }
@ -44,7 +47,8 @@ public class DbConfigHelper {
/** /**
* 获取数据库密码 * 获取数据库密码
*/ */
public static String getJdbcPassword() { @Override
public String getJdbcPassword() {
return CONFIG_PROPS.getProperty(DbConfigConstant.JDBC_PASSWORD); return CONFIG_PROPS.getProperty(DbConfigConstant.JDBC_PASSWORD);
} }

@ -50,6 +50,10 @@ public class Column {
* 备注 * 备注
*/ */
private String remarks; private String remarks;
/**
* 是否允许为null defaultValue:true
*/
private Boolean isNull;
public String getColumnName() { public String getColumnName() {
return columnName; return columnName;
@ -83,13 +87,11 @@ public class Column {
this.remarks = remarks; this.remarks = remarks;
} }
@Override public Boolean getNull() {
public String toString() { return isNull;
return "Column{" + }
"columnName='" + columnName + '\'' +
", columnSize=" + columnSize + public void setNull(Boolean aNull) {
", columnDef='" + columnDef + '\'' + isNull = aNull;
", remarks='" + remarks + '\'' +
'}';
} }
} }

@ -15,6 +15,9 @@ import java.util.Map;
*/ */
public class BeanSqlUtil { public class BeanSqlUtil {
/**
* 驼峰下滑线
*/
public static final String SQL_CHARACTER = "_"; public static final String SQL_CHARACTER = "_";
/** /**

@ -84,6 +84,7 @@ public class MysqlBuilder extends AbstractDbBuiler {
} }
}); });
} }
//创建表
map.forEach((key,value)->{ map.forEach((key,value)->{
sql.append(createTableStr(value)); sql.append(createTableStr(value));
}); });

@ -17,8 +17,7 @@ public class DefaultDbBuilerFactory implements DbBuilerFactory {
public AbstractDbBuiler getSqlBuilder(String dbType) { public AbstractDbBuiler getSqlBuilder(String dbType) {
AbstractDbBuiler abstractDbBuiler = null; AbstractDbBuiler abstractDbBuiler = null;
switch (dbType){ switch (dbType){
case SqlConstant case SqlConstant.DB_TYPE_MYSQL:
.DB_TYPE_MYSQL:
abstractDbBuiler = new MysqlBuilder(); abstractDbBuiler = new MysqlBuilder();
break; break;
case SqlConstant.DB_TYPE_ORACLE: case SqlConstant.DB_TYPE_ORACLE:

@ -1,5 +1,6 @@
package org.panda.entity; package org.panda.entity;
import org.panda.jdbc.annotation.Column;
import org.panda.jdbc.annotation.Table; import org.panda.jdbc.annotation.Table;
import java.util.Date; import java.util.Date;
@ -9,6 +10,7 @@ public class SysUser {
private Long id; private Long id;
@Column(defaultValue = "qi",length = 120,isNull = false)
private String name; private String name;
private Date createTime; private Date createTime;

@ -2,13 +2,13 @@ package org.panda.jdbc;
import org.junit.Test; import org.junit.Test;
import org.panda.entity.SysUser; import org.panda.entity.SysUser;
import org.panda.jdbc.annotation.Table; import org.panda.jdbc.annotation.Column;
import org.panda.jdbc.sql.AbstractDbBuiler; import org.panda.jdbc.sql.builder.AbstractDbBuiler;
import org.panda.jdbc.sql.SqlConstant; import org.panda.jdbc.sql.SqlConstant;
import org.panda.jdbc.sql.factory.DbBuilerFactory; import org.panda.jdbc.sql.factory.DbBuilerFactory;
import org.panda.jdbc.sql.factory.impl.DefaultDbBuilerFactory; import org.panda.jdbc.sql.factory.impl.DefaultDbBuilerFactory;
import static org.junit.Assert.*; import java.lang.reflect.Field;
public class JdbcUtilsTest { public class JdbcUtilsTest {
@ -38,6 +38,14 @@ public class JdbcUtilsTest {
Class clzz = SysUser.class; Class clzz = SysUser.class;
Field[] fields = clzz.getDeclaredFields();
for (Field field1:fields){
if (field1.isAnnotationPresent(Column.class)){
Column column = field1.getAnnotation(Column.class);
System.out.println(column);
}
// System.out.println(column.length());
}
DbBuilerFactory dbBuilerFactory = new DefaultDbBuilerFactory(); DbBuilerFactory dbBuilerFactory = new DefaultDbBuilerFactory();
AbstractDbBuiler abstractDbBuiler = dbBuilerFactory.getSqlBuilder(SqlConstant.DB_TYPE_MYSQL); AbstractDbBuiler abstractDbBuiler = dbBuilerFactory.getSqlBuilder(SqlConstant.DB_TYPE_MYSQL);

Loading…
Cancel
Save