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) {
e.printStackTrace();
} finally {
release( st, rs);
release(conn, st, rs);
}
return rs;
}
/**
* 关闭数据库连接(废弃)
* 废弃原因采用数据库连接池不用关闭连接
* 关闭数据库连接
* @param conn
* @param st
* @param rs
*/
@Deprecated
public void release(Connection conn, Statement st, ResultSet rs) {
if (rs != null) {
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.MapListHandler;
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.LoggerFactory;
@ -40,11 +40,14 @@ public class DataBaseHelper {
private static final String JDBC_PASSWORD;
static {
JDBC_DRIVER = DbConfigHelper.getJdbcDriver();
JDBC_URL = DbConfigHelper.getJdbcUrl();
JDBC_USER = DbConfigHelper.getJdbcUser();
JDBC_PASSWORD = DbConfigHelper.getJdbcPassword();
// JDBC_DRIVER = DbConfigHelper.getJdbcDriver();
// JDBC_URL = DbConfigHelper.getJdbcUrl();
// JDBC_USER = DbConfigHelper.getJdbcUser();
// JDBC_PASSWORD = DbConfigHelper.getJdbcPassword();
JDBC_DRIVER = null;
JDBC_URL = null;
JDBC_USER = null;
JDBC_PASSWORD = null;
DATA_SOURCE = new BasicDataSource();
DATA_SOURCE.setDriverClassName(JDBC_DRIVER);
DATA_SOURCE.setUrl(JDBC_URL);

@ -9,14 +9,15 @@ import java.util.Properties;
* @author qi
* 属性文件助手类
*/
public class DbConfigHelper {
public class DbConfigHelper implements DbConfigInterface {
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);
}
@ -26,7 +27,8 @@ public class DbConfigHelper {
*
* @return
*/
public static String getJdbcUrl() {
@Override
public String getJdbcUrl() {
return PropsUtil.getString(CONFIG_PROPS, DbConfigConstant.JDBC_URL);
}
@ -36,7 +38,8 @@ public class DbConfigHelper {
*
* @return
*/
public static String getJdbcUser() {
@Override
public String getJdbcUser() {
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);
}

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

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

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

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

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

@ -2,13 +2,13 @@ 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.annotation.Column;
import org.panda.jdbc.sql.builder.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.*;
import java.lang.reflect.Field;
public class JdbcUtilsTest {
@ -38,6 +38,14 @@ public class JdbcUtilsTest {
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();
AbstractDbBuiler abstractDbBuiler = dbBuilerFactory.getSqlBuilder(SqlConstant.DB_TYPE_MYSQL);

Loading…
Cancel
Save