Skip to content

Commit 838de41

Browse files
committed
拆分CRUD
1 parent eb2d835 commit 838de41

File tree

25 files changed

+596
-462
lines changed

25 files changed

+596
-462
lines changed

hsweb-web-bean/src/main/java/org/hsweb/web/bean/po/GenericPo.java

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,6 @@ public boolean equals(Object obj) {
6161
/**
6262
* 创建一个主键
6363
*
64-
* @return
6564
*/
6665
public static String createUID() {
6766
return MD5.encode(UUID.randomUUID().toString());
@@ -75,15 +74,6 @@ public void setProperties(Map<String, Object> properties) {
7574
this.properties = properties;
7675
}
7776

78-
@Override
79-
public Object clone() throws CloneNotSupportedException {
80-
Field[] fields = this.getClass().getDeclaredFields();
81-
for (int i = 0; i < fields.length; i++) {
82-
83-
}
84-
return super.clone();
85-
}
86-
8777
public interface Property {
8878
/**
8979
* 主键
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package org.hsweb.web.dao;
2+
3+
/**
4+
* @author zhouhao
5+
*/
6+
public interface CRUMapper<Po, Pk> extends InsertMapper<Po>, QueryMapper<Po, Pk>, UpdateMapper<Po> {
7+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package org.hsweb.web.dao;
2+
3+
import org.hsweb.web.bean.common.DeleteParam;
4+
5+
/**
6+
* @author zhouhao
7+
*/
8+
public interface DeleteMapper {
9+
int delete(DeleteParam param);
10+
}
Lines changed: 1 addition & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -1,74 +1,8 @@
11
package org.hsweb.web.dao;
22

3-
import org.hsweb.web.bean.common.DeleteParam;
4-
import org.hsweb.web.bean.common.InsertParam;
5-
import org.hsweb.web.bean.common.QueryParam;
6-
import org.hsweb.web.bean.common.UpdateParam;
7-
8-
import java.util.List;
9-
103
/**
114
* 通用dao,定义常用的增删改查操作。其他daoMapper接口继承此接口,则无需再定义这些方法
12-
* <p>
13-
* Created by zh.sqy@qq.com on 2015-07-20 0020.
145
*/
15-
public interface GenericMapper<Po, Pk> {
16-
/**
17-
* 根据参数添加一条数据
18-
*
19-
* @param param 参数对象
20-
* @return 添加后生成的主键
21-
*/
22-
int insert(InsertParam<Po> param);
23-
24-
/**
25-
* 根据条件删除数据
26-
*
27-
* @param param 主键
28-
* @return 影响记录数
29-
*/
30-
int delete(DeleteParam param);
31-
32-
/**
33-
* 修改记录信息
34-
*
35-
* @param data 要修改的对象
36-
* @return 影响记录数
37-
*/
38-
int update(UpdateParam<Po> data);
39-
40-
/**
41-
* 根据条件集合查询记录,支持分页,排序。
42-
* <br/>查询条件支持 类似$LIKE,$IN 表达式查询,如传入 name$LIKE 则进行name字段模糊查询
43-
* <br/>$LIKE -->模糊查询 (只支持字符)
44-
* <br/>$START -->以?开始 (只支持字符 和数字)
45-
* <br/>$END -->以?结尾 (只支持字符 和数字)
46-
* <br/>$IN -->in查询,参数必须为List实现,传入类似 1,2,3 是非法的
47-
* <br/>$GT -->大于 (只支持 数字和日期)
48-
* <br/>$LT -->小于 (只支持 数字和日期)
49-
* <br/>$NOT -->不等于
50-
* <br/>$NOTNULL -->值不为空
51-
* <br/>$ISNULL -->值为空
52-
* <br/>所有操作支持取反
53-
*
54-
* @param param 查询参数
55-
* @return 查询结果
56-
*/
57-
List<Po> select(QueryParam param);
58-
59-
/**
60-
* 查询记录总数,用于分页等操作。查询条件同 {@link GenericMapper#select}
61-
*
62-
* @param param 查询参数
63-
* @return 查询结果,实现mapper中的sql应指定默认值,否则可能抛出异常
64-
*/
65-
int total(QueryParam param);
6+
public interface GenericMapper<Po, Pk> extends QueryMapper<Po, Pk>, UpdateMapper<Po>, InsertMapper<Po>, DeleteMapper {
667

67-
/**
68-
* 根据主键查询记录
69-
*
70-
* @param pk 主键
71-
* @return 查询结果
72-
*/
73-
Po selectByPk(Pk pk);
748
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package org.hsweb.web.dao;
2+
3+
import org.hsweb.web.bean.common.InsertParam;
4+
5+
/**
6+
* @author zhouhao
7+
*/
8+
public interface InsertMapper<Po> {
9+
int insert(InsertParam<Po> param);
10+
}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
package org.hsweb.web.dao;
2+
3+
import org.hsweb.web.bean.common.QueryParam;
4+
5+
import java.util.List;
6+
7+
/**
8+
* @author zhouhao
9+
*/
10+
public interface QueryMapper<Po, Pk> {
11+
/**
12+
* 根据条件集合查询记录,支持分页,排序。
13+
* <br/>查询条件支持 类似$LIKE,$IN 表达式查询,如传入 name$LIKE 则进行name字段模糊查询
14+
* <br/>$LIKE -->模糊查询 (只支持字符)
15+
* <br/>$START -->以?开始 (只支持字符 和数字)
16+
* <br/>$END -->以?结尾 (只支持字符 和数字)
17+
* <br/>$IN -->in查询,参数必须为List实现,传入类似 1,2,3 是非法的
18+
* <br/>$GT -->大于 (只支持 数字和日期)
19+
* <br/>$LT -->小于 (只支持 数字和日期)
20+
* <br/>$NOT -->不等于
21+
* <br/>$NOTNULL -->值不为空
22+
* <br/>$ISNULL -->值为空
23+
* <br/>所有操作支持取反
24+
*
25+
* @param param 查询参数
26+
* @return 查询结果
27+
*/
28+
List<Po> select(QueryParam param);
29+
30+
/**
31+
* 查询记录总数,用于分页等操作。查询条件同 {@link GenericMapper#select}
32+
*
33+
* @param param 查询参数
34+
* @return 查询结果,实现mapper中的sql应指定默认值,否则可能抛出异常
35+
*/
36+
int total(QueryParam param);
37+
38+
/**
39+
* 根据主键查询记录
40+
*
41+
* @param pk 主键
42+
* @return 查询结果
43+
*/
44+
Po selectByPk(Pk pk);
45+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package org.hsweb.web.dao;
2+
3+
import org.hsweb.web.bean.common.UpdateParam;
4+
5+
/**
6+
* @author zhouhao
7+
*/
8+
public interface UpdateMapper<Po> {
9+
/**
10+
* 修改记录信息
11+
*
12+
* @param data 要修改的对象
13+
* @return 影响记录数
14+
*/
15+
int update(UpdateParam<Po> data);
16+
}

hsweb-web-dao/hsweb-web-dao-mybatis/src/test/java/org/hsweb/web/mybatis/SpringApplication.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@
3737
@Configuration
3838
@EnableAutoConfiguration
3939
@ComponentScan("org.hsweb.web")
40-
@MapperScan("org.hsweb.web.dao")
40+
@MapperScan({"org.hsweb.web.dao", "org.hsweb.web.mybatis.mappers"})
4141
public class SpringApplication {
4242

4343
@Bean

hsweb-web-dao/hsweb-web-dao-mybatis/src/test/java/org/hsweb/web/mybatis/user/UserMapperTest.java

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717
package org.hsweb.web.mybatis.user;
1818

1919
import org.hsweb.ezorm.core.dsl.Query;
20-
import org.hsweb.ezorm.rdb.render.dialect.Dialect;
2120
import org.hsweb.web.bean.common.DeleteParam;
2221
import org.hsweb.web.bean.common.InsertParam;
2322
import org.hsweb.web.bean.common.QueryParam;
@@ -31,12 +30,9 @@
3130
import org.junit.Test;
3231
import org.springframework.beans.factory.annotation.Autowired;
3332

34-
import java.util.Arrays;
3533
import java.util.Collections;
3634
import java.util.Date;
3735

38-
import static org.hsweb.ezorm.rdb.render.dialect.Dialect.TermTypeMapper.sql;
39-
4036
/**
4137
* @author zhouhao
4238
*/
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
package org.hsweb.web.service;
2+
3+
import org.hsweb.ezorm.core.dsl.Delete;
4+
import org.hsweb.web.bean.common.DeleteParam;
5+
import org.hsweb.web.dao.DeleteMapper;
6+
import org.hsweb.web.dao.GenericMapper;
7+
8+
/**
9+
* @author zhouhao
10+
*/
11+
public interface DeleteService<Pk> {
12+
/**
13+
* 根据主键删除记录
14+
*
15+
* @param pk 主键
16+
* @return 影响记录数
17+
*/
18+
int delete(Pk pk);
19+
20+
Delete createDelete();
21+
22+
/**
23+
* 指定一个dao映射接口,接口需继承{@link GenericMapper}创建dsl数据删除操作对象<br>
24+
* 可通过返回的Update对象进行dsl方式操作如:<br>
25+
* <code>
26+
* createDelete(userMapper).where("id",1).exec();
27+
* </code>
28+
*
29+
* @param mapper dao映射结构
30+
* @return {@link Delete}
31+
* @see Delete
32+
* @see org.hsweb.ezorm.core.Conditional
33+
* @since 2.2
34+
*/
35+
static Delete createDelete(DeleteMapper mapper) {
36+
Delete update = new Delete();
37+
update.setParam(new DeleteParam());
38+
update.setExecutor(param -> mapper.delete(((DeleteParam) param)));
39+
return update;
40+
}
41+
42+
/**
43+
* 自定义一个删除执行器。创建dsl数据删除操作对象
44+
*
45+
* @param executor 执行器
46+
* @return {@link Delete}
47+
* @since 2.2
48+
*/
49+
static Delete createDelete(Delete.Executor<DeleteParam> executor) {
50+
Delete update = new Delete();
51+
update.setParam(new DeleteParam());
52+
update.setExecutor(param -> executor.doExecute(((DeleteParam) param)));
53+
return update;
54+
}
55+
56+
}

0 commit comments

Comments
 (0)