add YamlUtil

This commit is contained in:
Looly
2021-09-24 15:29:56 +08:00
parent ba00f03026
commit 7b6593fe68
43 changed files with 798 additions and 1433 deletions

View File

@@ -14,45 +14,61 @@ import java.util.List;
* 此模板用于简化对指定表的操作,简化的操作如下:<br>
* 1、在初始化时指定了表名CRUD操作时便不需要表名<br>
* 2、在初始化时指定了主键某些需要主键的操作便不需要指定主键类型
* @author Looly
*
* @author Looly
*/
public class DaoTemplate {
/** 表名 */
/**
* 表名
*/
protected String tableName;
/** 本表的主键字段请在子类中覆盖或构造方法中指定默认为id */
/**
* 本表的主键字段请在子类中覆盖或构造方法中指定默认为id
*/
protected String primaryKeyField = "id";
/** SQL运行器 */
/**
* SQL运行器
*/
protected Db db;
//--------------------------------------------------------------- Constructor start
/**
* 构造此构造需要自定义SqlRunner主键默认为id
*
* @param tableName 数据库表名
*/
public DaoTemplate(String tableName) {
this(tableName, (String)null);
this(tableName, (String) null);
}
/**
* 构造,使用默认的池化连接池,读取默认配置文件的空分组,适用于只有一个数据库的情况
* @param tableName 数据库表名
*
* @param tableName 数据库表名
* @param primaryKeyField 主键字段名
*/
public DaoTemplate(String tableName, String primaryKeyField) {
this(tableName, primaryKeyField, DSFactory.get());
}
/**
* 构造
*
* @param tableName 表
* @param ds 数据源
*/
public DaoTemplate(String tableName, DataSource ds) {
this(tableName, null, ds);
}
/**
* 构造
* @param tableName 表名
*
* @param tableName 表名
* @param primaryKeyField 主键字段名
* @param ds 数据源
* @param ds 数据源
*/
public DaoTemplate(String tableName, String primaryKeyField, DataSource ds) {
this(tableName, primaryKeyField, Db.use(ds));
@@ -60,13 +76,14 @@ public class DaoTemplate {
/**
* 构造
* @param tableName 表名
*
* @param tableName 表名
* @param primaryKeyField 主键字段名
* @param db Db对象
* @param db Db对象
*/
public DaoTemplate(String tableName, String primaryKeyField, Db db) {
this.tableName = tableName;
if(StrUtil.isNotBlank(primaryKeyField)){
if (StrUtil.isNotBlank(primaryKeyField)) {
this.primaryKeyField = primaryKeyField;
}
this.db = db;
@@ -74,8 +91,10 @@ public class DaoTemplate {
//--------------------------------------------------------------- Constructor end
//------------------------------------------------------------- Add start
/**
* 添加
*
* @param entity 实体对象
* @return 插入行数
* @throws SQLException SQL执行异常
@@ -86,6 +105,7 @@ public class DaoTemplate {
/**
* 添加
*
* @param entity 实体对象
* @return 主键列表
* @throws SQLException SQL执行异常
@@ -96,6 +116,7 @@ public class DaoTemplate {
/**
* 添加
*
* @param entity 实体对象
* @return 自增主键
* @throws SQLException SQL执行异常
@@ -106,11 +127,12 @@ public class DaoTemplate {
//------------------------------------------------------------- Add end
//------------------------------------------------------------- Delete start
/**
* 删除
* @param <T> 主键类型
*
* @param pk 主键
* @param <T> 主键类型
* @param pk 主键
* @return 删除行数
* @throws SQLException SQL执行异常
*/
@@ -124,7 +146,7 @@ public class DaoTemplate {
/**
* 删除
*
* @param <T> 主键类型
* @param <T> 主键类型
* @param field 字段名
* @param value 字段值
* @return 删除行数
@@ -141,7 +163,7 @@ public class DaoTemplate {
/**
* 删除
*
* @param <T> 主键类型
* @param <T> 主键类型
* @param where 删除条件当条件为空时返回0防止误删全表
* @return 删除行数
* @throws SQLException SQL执行异常
@@ -155,14 +177,16 @@ public class DaoTemplate {
//------------------------------------------------------------- Delete end
//------------------------------------------------------------- Update start
/**
* 按照条件更新
*
* @param record 更新的内容
* @param where 条件
* @param where 条件
* @return 更新条目数
* @throws SQLException SQL执行异常
*/
public int update(Entity record, Entity where) throws SQLException{
public int update(Entity record, Entity where) throws SQLException {
if (MapUtil.isEmpty(record)) {
return 0;
}
@@ -171,6 +195,7 @@ public class DaoTemplate {
/**
* 更新
*
* @param entity 实体对象,必须包含主键
* @return 更新行数
* @throws SQLException SQL执行异常
@@ -194,6 +219,7 @@ public class DaoTemplate {
/**
* 增加或者更新实体
*
* @param entity 实体,当包含主键时更新,否则新增
* @return 新增或更新条数
* @throws SQLException SQL执行异常
@@ -204,11 +230,12 @@ public class DaoTemplate {
//------------------------------------------------------------- Update end
//------------------------------------------------------------- Get start
/**
* 根据主键获取单个记录
*
* @param <T> 主键类型
* @param pk 主键值
* @param pk 主键值
* @return 记录
* @throws SQLException SQL执行异常
*/
@@ -220,7 +247,7 @@ public class DaoTemplate {
* 根据某个字段(最好是唯一字段)查询单个记录<br>
* 当有多条返回时,只显示查询到的第一条
*
* @param <T> 字段值类型
* @param <T> 字段值类型
* @param field 字段名
* @param value 字段值
* @return 记录
@@ -243,10 +270,11 @@ public class DaoTemplate {
//------------------------------------------------------------- Get end
//------------------------------------------------------------- Find start
/**
* 根据某个字段值查询结果
*
* @param <T> 字段值类型
* @param <T> 字段值类型
* @param field 字段名
* @param value 字段值
* @return 记录
@@ -258,6 +286,7 @@ public class DaoTemplate {
/**
* 查询当前表的所有记录
*
* @return 记录
* @throws SQLException SQL执行异常
*/
@@ -281,14 +310,14 @@ public class DaoTemplate {
* SQL语句可以是非完整SQL语句可以只提供查询的条件部分例如WHERE部分<br>
* 此方法会自动补全SELECT * FROM [tableName] 部分,这样就无需关心表名,直接提供条件即可
*
* @param sql SQL语句
* @param sql SQL语句
* @param params SQL占位符中对应的参数
* @return 记录
* @throws SQLException SQL执行异常
*/
public List<Entity> findBySql(String sql, Object... params) throws SQLException {
String selectKeyword = StrUtil.subPre(sql.trim(), 6).toLowerCase();
if(false == "select".equals(selectKeyword)){
if (false == "select".equals(selectKeyword)) {
sql = "SELECT * FROM " + this.tableName + " " + sql;
}
return db.query(sql, params);
@@ -297,13 +326,13 @@ public class DaoTemplate {
/**
* 分页
*
* @param where 条件
* @param page 分页对象
* @param where 条件
* @param page 分页对象
* @param selectFields 查询的字段列表
* @return 分页结果集
* @throws SQLException SQL执行异常
*/
public PageResult<Entity> page(Entity where, Page page, String... selectFields) throws SQLException{
public PageResult<Entity> page(Entity where, Page page, String... selectFields) throws SQLException {
return db.page(Arrays.asList(selectFields), fixEntity(where), page);
}
@@ -311,11 +340,11 @@ public class DaoTemplate {
* 分页
*
* @param where 条件
* @param page 分页对象
* @param page 分页对象
* @return 分页结果集
* @throws SQLException SQL执行异常
*/
public PageResult<Entity> page(Entity where, Page page) throws SQLException{
public PageResult<Entity> page(Entity where, Page page) throws SQLException {
return db.page(fixEntity(where), page);
}
@@ -326,7 +355,7 @@ public class DaoTemplate {
* @return 数量
* @throws SQLException SQL执行异常
*/
public long count(Entity where) throws SQLException{
public long count(Entity where) throws SQLException {
return db.count(fixEntity(where));
}
@@ -337,20 +366,21 @@ public class DaoTemplate {
* @return 是否存在
* @throws SQLException SQL执行异常
*/
public boolean exist(Entity where) throws SQLException{
public boolean exist(Entity where) throws SQLException {
return this.count(where) > 0;
}
//------------------------------------------------------------- Find end
/**
* 修正Entity对象避免null和填充表名
*
* @param entity 实体类
* @return 修正后的实体类
*/
private Entity fixEntity(Entity entity){
if(null == entity){
private Entity fixEntity(Entity entity) {
if (null == entity) {
entity = Entity.create(tableName);
}else if(StrUtil.isBlank(entity.getTableName())){
} else if (StrUtil.isBlank(entity.getTableName())) {
entity.setTableName(tableName);
}
return entity;

View File

@@ -0,0 +1,34 @@
package cn.hutool.db;
import cn.hutool.core.collection.ListUtil;
import cn.hutool.core.io.FileUtil;
import cn.hutool.core.util.StrUtil;
import org.junit.Ignore;
import org.junit.Test;
import java.sql.ResultSet;
import java.sql.SQLException;
public class PicTransferTest {
@Test
@Ignore
public void findTest() throws SQLException {
Db.use().find(
ListUtil.of("NAME", "TYPE", "GROUP", "PIC"),
Entity.create("PIC_INFO").set("TYPE", 1),
rs -> {
while(rs.next()){
save(rs);
}
return null;
}
);
}
private static void save(ResultSet rs) throws SQLException{
String destDir = "d:/test/pic";
String path = StrUtil.format("{}/{}-{}.jpg", destDir, rs.getString("NAME"), rs.getString("GROUP"));
FileUtil.writeFromStream(rs.getBlob("PIC").getBinaryStream(), path);
}
}