add setCaseInsensitive

This commit is contained in:
Looly
2020-03-16 23:19:04 +08:00
parent 3a579171c1
commit 87167a07d0
27 changed files with 296 additions and 217 deletions

View File

@@ -1,15 +1,6 @@
package cn.hutool.db;
import java.io.Serializable;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.Collection;
import java.util.List;
import java.util.Map;
import javax.sql.DataSource;
import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.db.dialect.Dialect;
import cn.hutool.db.handler.BeanListHandler;
import cn.hutool.db.handler.EntityHandler;
@@ -24,6 +15,14 @@ import cn.hutool.db.sql.SqlExecutor;
import cn.hutool.db.sql.SqlUtil;
import cn.hutool.db.sql.Wrapper;
import javax.sql.DataSource;
import java.io.Serializable;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.Collection;
import java.util.List;
import java.util.Map;
/**
* 抽象数据库操作类<br>
* 通过给定的数据源执行给定SQL或者给定数据源和方言执行相应的CRUD操作<br>
@@ -39,6 +38,10 @@ public abstract class AbstractDb implements Serializable {
* 是否支持事务
*/
protected Boolean isSupportTransaction = null;
/**
* 是否大小写不敏感(默认大小写不敏感)
*/
protected boolean caseInsensitive = DbUtil.caseInsensitiveGlobal;
protected SqlConnRunner runner;
// ------------------------------------------------------- Constructor start
@@ -81,7 +84,7 @@ public abstract class AbstractDb implements Serializable {
* @since 3.1.1
*/
public List<Entity> query(String sql, Map<String, Object> params) throws SQLException {
return query(sql, new EntityListHandler(), params);
return query(sql, new EntityListHandler(this.caseInsensitive), params);
}
/**
@@ -94,7 +97,7 @@ public abstract class AbstractDb implements Serializable {
* @since 3.1.1
*/
public List<Entity> query(String sql, Object... params) throws SQLException {
return query(sql, new EntityListHandler(), params);
return query(sql, new EntityListHandler(this.caseInsensitive), params);
}
/**
@@ -121,7 +124,7 @@ public abstract class AbstractDb implements Serializable {
* @throws SQLException SQL执行异常
*/
public Entity queryOne(String sql, Object... params) throws SQLException {
return query(sql, new EntityHandler(), params);
return query(sql, new EntityHandler(this.caseInsensitive), params);
}
/**
@@ -171,9 +174,9 @@ public abstract class AbstractDb implements Serializable {
/**
* 支持占位符的查询例如select * from table where field1=:name1
*
* @param <T> 结果集需要处理的对象类型
* @param sql 查询语句,使用参数名占位符,例如:name
* @param rsh 结果集处理对象
* @param <T> 结果集需要处理的对象类型
* @param sql 查询语句,使用参数名占位符,例如:name
* @param rsh 结果集处理对象
* @param paramMap 参数
* @return 结果对象
* @throws SQLException SQL执行异常
@@ -429,7 +432,7 @@ public abstract class AbstractDb implements Serializable {
* @throws SQLException SQL执行异常
*/
public Entity get(Entity where) throws SQLException {
return find(where.getFieldNames(), where, new EntityHandler());
return find(where.getFieldNames(), where, new EntityHandler(this.caseInsensitive));
}
// ------------------------------------------------------------- Get end
@@ -466,7 +469,7 @@ public abstract class AbstractDb implements Serializable {
* @since 4.5.16
*/
public List<Entity> find(Collection<String> fields, Entity where) throws SQLException {
return find(fields, where, EntityListHandler.create());
return find(fields, where, new EntityListHandler(this.caseInsensitive));
}
/**
@@ -502,7 +505,7 @@ public abstract class AbstractDb implements Serializable {
* @throws SQLException SQL执行异常
*/
public <T> T find(Entity where, RsHandler<T> rsh, String... fields) throws SQLException {
return find(CollectionUtil.newArrayList(fields), where, rsh);
return find(CollUtil.newArrayList(fields), where, rsh);
}
/**
@@ -515,7 +518,7 @@ public abstract class AbstractDb implements Serializable {
* @since 3.2.1
*/
public List<Entity> find(Entity where) throws SQLException {
return find(where.getFieldNames(), where, EntityListHandler.create());
return find(where.getFieldNames(), where, new EntityListHandler(this.caseInsensitive));
}
/**
@@ -549,8 +552,8 @@ public abstract class AbstractDb implements Serializable {
* 查询数据列表,返回所有字段<br>
* 查询条件为多个key value对表示默认key = value如果使用其它条件可以使用where.put("key", " &gt; 1")value也可以传Condition对象key被忽略
*
* @param <T> Bean类型
* @param where 条件实体类(包含表名)
* @param <T> Bean类型
* @param where 条件实体类(包含表名)
* @param beanClass 返回的对象类型
* @return 数据对象列表
* @throws SQLException SQL执行异常
@@ -595,7 +598,7 @@ public abstract class AbstractDb implements Serializable {
*/
public List<Entity> findBy(String tableName, Condition... wheres) throws SQLException {
final Query query = new Query(wheres, tableName);
return find(query, EntityListHandler.create());
return find(query, new EntityListHandler(this.caseInsensitive));
}
/**
@@ -695,7 +698,7 @@ public abstract class AbstractDb implements Serializable {
* @since 3.2.2
*/
public List<Entity> pageForEntityList(Entity where, Page page) throws SQLException {
return page(where, page, EntityListHandler.create());
return page(where, page, new EntityListHandler(this.caseInsensitive));
}
/**
@@ -808,6 +811,17 @@ public abstract class AbstractDb implements Serializable {
// ---------------------------------------------------------------------------- Getters and Setters start
/**
* 设置是否在结果中忽略大小写<br>
* 如果忽略则在Entity中调用getXXX时字段值忽略大小写默认忽略
*
* @param caseInsensitive 否在结果中忽略大小写
* @since 5.2.4
*/
public void setCaseInsensitive(boolean caseInsensitive) {
this.caseInsensitive = caseInsensitive;
}
/**
* 获取{@link SqlConnRunner}
*

View File

@@ -1,16 +1,13 @@
package cn.hutool.db;
import java.sql.SQLException;
import java.util.Arrays;
import java.util.List;
import javax.sql.DataSource;
import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.db.ds.DSFactory;
import cn.hutool.db.handler.EntityHandler;
import cn.hutool.db.handler.EntityListHandler;
import javax.sql.DataSource;
import java.sql.SQLException;
import java.util.Arrays;
import java.util.List;
/**
* 数据访问层模板<br>
@@ -189,7 +186,7 @@ public class DaoTemplate {
}
final Entity where = Entity.create(tableName).set(primaryKeyField, pk);
final Entity record = (Entity) entity.clone();
final Entity record = entity.clone();
record.remove(primaryKeyField);
return db.update(record, where);
@@ -241,7 +238,7 @@ public class DaoTemplate {
* @throws SQLException SQL执行异常
*/
public Entity get(Entity where) throws SQLException {
return db.find(null, fixEntity(where), new EntityHandler());
return db.get(fixEntity(where));
}
//------------------------------------------------------------- Get end
@@ -276,7 +273,7 @@ public class DaoTemplate {
* @throws SQLException SQL执行异常
*/
public List<Entity> find(Entity where) throws SQLException {
return db.find(null, fixEntity(where), new EntityListHandler());
return db.find(null, fixEntity(where));
}
/**
@@ -294,7 +291,7 @@ public class DaoTemplate {
if(false == "select".equals(selectKeyword)){
sql = "SELECT * FROM " + this.tableName + " " + sql;
}
return db.query(sql, new EntityListHandler(), params);
return db.query(sql, params);
}
/**

View File

@@ -24,16 +24,20 @@ import cn.hutool.setting.Setting;
/**
* 数据库操作工具类
*
*
* @author Luxiaolei
*
*/
public final class DbUtil {
private final static Log log = LogFactory.get();
/**
* 是否大小写不敏感(默认大小写不敏感)
*/
protected static boolean caseInsensitiveGlobal = true;
/**
* 实例化一个新的SQL运行对象
*
*
* @param dialect 数据源
* @return SQL执行类
*/
@@ -43,7 +47,7 @@ public final class DbUtil {
/**
* 实例化一个新的SQL运行对象
*
*
* @param ds 数据源
* @return SQL执行类
*/
@@ -53,7 +57,7 @@ public final class DbUtil {
/**
* 实例化一个新的SQL运行对象
*
*
* @param conn 数据库连接对象
* @return SQL执行类
*/
@@ -63,7 +67,7 @@ public final class DbUtil {
/**
* 实例化一个新的SQL运行对象使用默认数据源
*
*
* @return SQL执行类
* @deprecated 请使用 {@link #use()}
*/
@@ -74,7 +78,7 @@ public final class DbUtil {
/**
* 实例化一个新的SQL运行对象
*
*
* @param ds 数据源
* @return SQL执行类
* @deprecated 请使用 {@link #use(DataSource)}
@@ -86,8 +90,8 @@ public final class DbUtil {
/**
* 实例化一个新的SQL运行对象
*
* @param ds 数据源
*
* @param ds 数据源
* @param dialect SQL方言
* @return SQL执行类
* @deprecated 请使用 {@link #use(DataSource, Dialect)}
@@ -99,7 +103,7 @@ public final class DbUtil {
/**
* 实例化一个新的Db使用默认数据源
*
*
* @return SQL执行类
*/
public static Db use() {
@@ -108,7 +112,7 @@ public final class DbUtil {
/**
* 实例化一个新的Db对象
*
*
* @param ds 数据源
* @return SQL执行类
*/
@@ -118,8 +122,8 @@ public final class DbUtil {
/**
* 实例化一个新的SQL运行对象
*
* @param ds 数据源
*
* @param ds 数据源
* @param dialect SQL方言
* @return SQL执行类
*/
@@ -129,7 +133,7 @@ public final class DbUtil {
/**
* 新建数据库会话,使用默认数据源
*
*
* @return 数据库会话
*/
public static Session newSession() {
@@ -138,7 +142,7 @@ public final class DbUtil {
/**
* 新建数据库会话
*
*
* @param ds 数据源
* @return 数据库会话
*/
@@ -149,7 +153,7 @@ public final class DbUtil {
/**
* 连续关闭一系列的SQL相关对象<br>
* 这些对象必须按照顺序关闭,否则会出错。
*
*
* @param objsToClose 需要关闭的对象
*/
@SuppressWarnings("ConstantConditions")
@@ -183,7 +187,7 @@ public final class DbUtil {
/**
* 获得默认数据源
*
*
* @return 默认数据源
*/
public static DataSource getDs() {
@@ -192,7 +196,7 @@ public final class DbUtil {
/**
* 获取指定分组的数据源
*
*
* @param group 分组
* @return 数据源
*/
@@ -202,7 +206,7 @@ public final class DbUtil {
/**
* 获得JNDI数据源
*
*
* @param jndiName JNDI名称
* @return 数据源
*/
@@ -217,7 +221,7 @@ public final class DbUtil {
/**
* 获得JNDI数据源
*
*
* @param jndiName JNDI名称
* @return 数据源
*/
@@ -231,6 +235,7 @@ public final class DbUtil {
/**
* 从配置文件中读取SQL打印选项
*
* @param setting 配置文件
* @since 4.1.7
*/
@@ -250,14 +255,25 @@ public final class DbUtil {
/**
* 设置全局配置是否通过debug日志显示SQL
*
* @param isShowSql 是否显示SQL
* @param isFormatSql 是否格式化显示的SQL
*
* @param isShowSql 是否显示SQL
* @param isFormatSql 是否格式化显示的SQL
* @param isShowParams 是否打印参数
* @param level SQL打印到的日志等级
* @param level SQL打印到的日志等级
* @since 4.1.7
*/
public static void setShowSqlGlobal(boolean isShowSql, boolean isFormatSql, boolean isShowParams, Level level) {
SqlLog.INSTANCE.init(isShowSql, isFormatSql, isShowParams, level);
}
/**
* 设置全局是否在结果中忽略大小写<br>
* 如果忽略则在Entity中调用getXXX时字段值忽略大小写默认忽略
*
* @param caseInsensitive 否在结果中忽略大小写
* @since 5.2.4
*/
public static void setCaseInsensitiveGlobal(boolean caseInsensitive) {
caseInsensitiveGlobal = caseInsensitive;
}
}

View File

@@ -1,6 +1,6 @@
package cn.hutool.db;
import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.lang.Assert;
import cn.hutool.core.map.MapUtil;
import cn.hutool.core.util.ArrayUtil;
@@ -18,6 +18,7 @@ import cn.hutool.db.sql.SqlUtil;
import cn.hutool.db.sql.Wrapper;
import javax.sql.DataSource;
import java.io.Serializable;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
@@ -31,8 +32,14 @@ import java.util.List;
*
* @author Luxiaolei
*/
public class SqlConnRunner {
public class SqlConnRunner implements Serializable {
private static final long serialVersionUID = 1L;
private Dialect dialect;
/**
* 是否大小写不敏感(默认大小写不敏感)
*/
protected boolean caseInsensitive = DbUtil.caseInsensitiveGlobal;
/**
* 实例化一个新的SQL运行对象
@@ -98,7 +105,7 @@ public class SqlConnRunner {
*/
public int insert(Connection conn, Entity record) throws SQLException {
checkConn(conn);
if (CollectionUtil.isEmpty(record)) {
if (CollUtil.isEmpty(record)) {
throw new SQLException("Empty entity provided!");
}
PreparedStatement ps = null;
@@ -185,7 +192,7 @@ public class SqlConnRunner {
*/
public List<Object> insertForGeneratedKeys(Connection conn, Entity record) throws SQLException {
checkConn(conn);
if (CollectionUtil.isEmpty(record)) {
if (CollUtil.isEmpty(record)) {
throw new SQLException("Empty entity provided!");
}
@@ -210,7 +217,7 @@ public class SqlConnRunner {
*/
public Long insertForGeneratedKey(Connection conn, Entity record) throws SQLException {
checkConn(conn);
if (CollectionUtil.isEmpty(record)) {
if (CollUtil.isEmpty(record)) {
throw new SQLException("Empty entity provided!");
}
@@ -235,7 +242,7 @@ public class SqlConnRunner {
*/
public int del(Connection conn, Entity where) throws SQLException {
checkConn(conn);
if (CollectionUtil.isEmpty(where)) {
if (CollUtil.isEmpty(where)) {
//不允许做全表删除
throw new SQLException("Empty entity provided!");
}
@@ -262,10 +269,10 @@ public class SqlConnRunner {
*/
public int update(Connection conn, Entity record, Entity where) throws SQLException {
checkConn(conn);
if (CollectionUtil.isEmpty(record)) {
if (CollUtil.isEmpty(record)) {
throw new SQLException("Empty entity provided!");
}
if (CollectionUtil.isEmpty(where)) {
if (CollUtil.isEmpty(where)) {
//不允许做全表更新
throw new SQLException("Empty where provided!");
}
@@ -342,7 +349,7 @@ public class SqlConnRunner {
* @throws SQLException SQL执行异常
*/
public <T> T find(Connection conn, Entity where, RsHandler<T> rsh, String... fields) throws SQLException {
return find(conn, CollectionUtil.newArrayList(fields), where, rsh);
return find(conn, CollUtil.newArrayList(fields), where, rsh);
}
/**
@@ -355,7 +362,7 @@ public class SqlConnRunner {
* @since 3.2.1
*/
public List<Entity> find(Connection conn, Entity where) throws SQLException {
return find(conn, where.getFieldNames(), where, EntityListHandler.create());
return find(conn, where.getFieldNames(), where, new EntityListHandler(this.caseInsensitive));
}
/**
@@ -367,7 +374,7 @@ public class SqlConnRunner {
* @throws SQLException SQL执行异常
*/
public List<Entity> findAll(Connection conn, Entity where) throws SQLException {
return find(conn, where, EntityListHandler.create());
return find(conn, where, new EntityListHandler(this.caseInsensitive));
}
/**
@@ -505,7 +512,7 @@ public class SqlConnRunner {
checkConn(conn);
final int count = count(conn, where);
PageResultHandler pageResultHandler = PageResultHandler.create(new PageResult<>(page, numPerPage, count));
final PageResultHandler pageResultHandler = new PageResultHandler(new PageResult<>(page, numPerPage, count), this.caseInsensitive);
return this.page(conn, fields, where, page, numPerPage, pageResultHandler);
}
@@ -525,14 +532,14 @@ public class SqlConnRunner {
//查询全部
if (null == page) {
List<Entity> entityList = this.find(conn, fields, where, new EntityListHandler());
List<Entity> entityList = this.find(conn, fields, where, new EntityListHandler(DbUtil.caseInsensitiveGlobal));
final PageResult<Entity> pageResult = new PageResult<>(0, entityList.size(), entityList.size());
pageResult.addAll(entityList);
return pageResult;
}
final int count = count(conn, where);
PageResultHandler pageResultHandler = PageResultHandler.create(new PageResult<>(page.getPageNumber(), page.getPageSize(), count));
PageResultHandler pageResultHandler = new PageResultHandler(new PageResult<>(page.getPageNumber(), page.getPageSize(), count), this.caseInsensitive);
return this.page(conn, fields, where, page, pageResultHandler);
}
@@ -553,6 +560,17 @@ public class SqlConnRunner {
//---------------------------------------------------------------------------- Getters and Setters end
/**
* 设置是否在结果中忽略大小写<br>
* 如果忽略则在Entity中调用getXXX时字段值忽略大小写默认忽略
*
* @param caseInsensitive 否在结果中忽略大小写
* @since 5.2.4
*/
public void setCaseInsensitive(boolean caseInsensitive) {
this.caseInsensitive = caseInsensitive;
}
/**
* @return SQL方言
*/

View File

@@ -1,5 +1,15 @@
package cn.hutool.db.handler;
import cn.hutool.core.bean.BeanDesc.PropDesc;
import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.convert.Convert;
import cn.hutool.core.lang.Assert;
import cn.hutool.core.util.ArrayUtil;
import cn.hutool.core.util.ReflectUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.core.util.TypeUtil;
import cn.hutool.db.Entity;
import java.lang.reflect.Method;
import java.lang.reflect.Type;
import java.sql.ResultSet;
@@ -11,16 +21,6 @@ import java.util.Collection;
import java.util.List;
import java.util.Map;
import cn.hutool.core.bean.BeanDesc.PropDesc;
import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.convert.Convert;
import cn.hutool.core.lang.Assert;
import cn.hutool.core.util.ArrayUtil;
import cn.hutool.core.util.ReflectUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.core.util.TypeUtil;
import cn.hutool.db.Entity;
/**
* 数据结果集处理辅助类
*

View File

@@ -8,35 +8,53 @@ import cn.hutool.db.PageResult;
/**
* 分页结果集处理类 处理出的结果为PageResult
* @author loolly
*
* @author loolly
*/
public class PageResultHandler implements RsHandler<PageResult<Entity>>{
public class PageResultHandler implements RsHandler<PageResult<Entity>> {
private static final long serialVersionUID = -1474161855834070108L;
private PageResult<Entity> pageResult;
/**
* 是否大小写不敏感
*/
private boolean caseInsensitive;
/**
* 创建一个 EntityHandler对象<br>
* 结果集根据给定的分页对象查询数据库,填充结果
*
* @param pageResult 分页结果集空对象
* @return EntityHandler对象
*/
public static PageResultHandler create(PageResult<Entity> pageResult) {
return new PageResultHandler(pageResult);
}
/**
* 构造<br>
* 结果集根据给定的分页对象查询数据库,填充结果
* 结果集根据给定的分页对象查询数据库,填充结果
*
* @param pageResult 分页结果集空对象
*/
public PageResultHandler(PageResult<Entity> pageResult) {
this(pageResult, false);
}
/**
* 构造<br>
* 结果集根据给定的分页对象查询数据库,填充结果
*
* @param pageResult 分页结果集空对象
* @param caseInsensitive 是否大小写不敏感
*/
public PageResultHandler(PageResult<Entity> pageResult, boolean caseInsensitive) {
this.pageResult = pageResult;
this.caseInsensitive = caseInsensitive;
}
@Override
public PageResult<Entity> handle(ResultSet rs) throws SQLException {
return HandleHelper.handleRs(rs, pageResult);
return HandleHelper.handleRs(rs, pageResult, this.caseInsensitive);
}
}

View File

@@ -10,9 +10,6 @@ import org.junit.Test;
import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.lang.Console;
import cn.hutool.core.thread.ThreadUtil;
import cn.hutool.db.Db;
import cn.hutool.db.DbRuntimeException;
import cn.hutool.db.Entity;
import cn.hutool.db.handler.EntityListHandler;
/**