提交 e5e32f5d 编写于 作者: zhouweidong's avatar zhouweidong

update readme

上级 f5b75d40
...@@ -23,7 +23,7 @@ iBiz的实体模型简要说明,然后Mybatis-Plus的特性(功能)简要 ...@@ -23,7 +23,7 @@ iBiz的实体模型简要说明,然后Mybatis-Plus的特性(功能)简要
## MyBatis-Plus实现CURD ## MyBatis-Plus实现CURD
我们将通过搭建 [MyBatis-Plus官方快速入门示例](https://mp.baomidou.com/guide/quick-start.html) 来阐述 iBizSys 整合 mybatis-plus 的过程,在此之前,我们假设您已经: 我们将通过搭建一个简单的Demo来阐述 iBizSys 整合 mybatis-plus 的过程,在此之前,我们假设您已经:
- **熟悉iBizSys实体建立** - **熟悉iBizSys实体建立**
...@@ -34,51 +34,127 @@ iBiz的实体模型简要说明,然后Mybatis-Plus的特性(功能)简要 ...@@ -34,51 +34,127 @@ iBiz的实体模型简要说明,然后Mybatis-Plus的特性(功能)简要
## 建立实体 ## 建立实体
建立实体的过程其实就是对应[MyBatis-Plus官方快速入门示例](https://mp.baomidou.com/guide/quick-start.html) 创建数据表的过程,iBizSys将通过管理数据模型的方式来实现对业务表的管理。 iBizSys将通过管理数据模型的方式来实现对业务表的管理 [查看更多实体建立小知识](http://bbs.ibizlab.cn/)
![produce](img/createEntity0.png) ![produce](img/quickstart/createEntity.png)
![produce](img/createEntity2.png)
## 建立模板 ## 建立模板
由于MyBatis-Plus 示例中使用Spring Boot作为工程项目,所以本示例中也将使用Spring Boot作为工程项目。 由于MyBatis-Plus 示例中使用Spring Boot作为工程项目,所以本示例中也将使用Spring Boot作为工程项目。
## 依赖模板 搭建 SpringBoot 项目环境可通过以下两种方式进行
创建一个pom.xml的模板文件【pom.xml.ftl】 >在iBizSys模板仓库中fork SpringBoot模板,基于SpringBoot的基础上增加 Mybatis-Plus 的模板配置 `推荐使用`
![produce](img/pom_ftl.png) >自行搭建 SpringBoot 模板环境,并引入 Mybatis-Plus 相关的模板配置
## 配置模板: 本示例采用fork的方式获取SpringBoot的模板 [查看更多模板仓库的小知识](http://bbs.ibizlab.cn/)
创建一个SpringBoot yml配置文件的模板文件【application.yml.ftl】,并在配置文件中添加 H2 数据库的相关配置 `编写模板方式`
![produce](img/yml.png) >在线编辑:`iBizSys` 提供模板在线编辑工具,供大家更高效、便捷的编写模板 [查看iBizSys在线编辑工具](http://bbs.ibizlab.cn/)
## 启动类模板: >离线编辑:由于`iBizSys`的模板是放在`git`仓库中进行托管,用户可在离线环境下编写模板,编辑完成后再将模板上传至`git`中即可。
创建一个SpringBoot启动类的模板文件【%PUBPRJ%Main.java.ftl】, 文件名参数%PUBPRJ% 为动态参数,代表的是系统名称 ### 依赖模板:
![produce](img/AppMain_ftl.png) `pom.xml.ftl` 中引入 Mybatis-Plus 所需的相关依赖
## 实体类模板: ```java
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.0.6</version>
</dependency>
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
</dependency>
```
### 配置模板:
创建一个实体类模板文件【%DE%.java.ftl】 , 文件名参数 %DE% 为动态参数,代表的是实体代码名称。iBizSys发布器在发布成果物时,将为系统内的实体模型生成其对应的实体类,以实体的代码名称作为类名 `application.yml.ftl`中添加 H2 数据库的相关配置
```java
spring:
datasource:
username: test
password: 'test'
url: jdbc:h2:mem:demo;
driver-class-name: org.h2.Driver
schema: classpath:db/schema-h2.sql
data: classpath:db/data-h2.sql
```
![produce](img/de_ftl.png)
## Mapper类模板: ### 启动类模板:
创建一个Mapper类的模板文件【%DE%Mapper.java.ftl】 , 文件名参数 %DE% 为动态参数,代表的是实体代码名称。 在 启动类` %PUBPRJ%Main.java.ftl `中,添加 `@MapperScan` 注解,扫描 Mapper 文件夹。模板中含 ` % % 、${ } `均为动态参数 [查看模板参数](http://bbs.ibizlab.cn/)
```java
@SpringBootApplication
@MapperScan("${pub.getPKGCodeName()}.*.mapper")
public class ${pub.getCodeName()?lower_case}Main{
public static void main(String[] args) {
SpringApplication.run(${pub.getCodeName()?lower_case}Main.class, args);
}
}
```
![produce](img/mapper_ftl.png) ### 实体类模板:
## 测试类模板: 实体类模板将循环输出该实体的所有属性,配置 `@TableName` `@TableId` 注解,用于指定该实体所映射的数据库表及主键属性,以下为调整后的实体类相关的模板代码:
```java
@TableName(value = "${item.getTableName()}")
@Data
public class ${item.codeName}{
<#list item.getPSDEFields() as defield>
<#if defield.isKeyDEField()>
@TableId(value= "${defield.getName()?lower_case}",type=IdType.UUID)//指定主键生成策略
</#if>
private ${srfjavatype(defield.stdDataType)} ${defield.codeName?lower_case};
</#list>
}
```
### Mapper类模板:
创建一个Mapper类的模板文件【%DE%Mapper.java.ftl】
```java
public interface ${item.getCodeName()}Mapper extends BaseMapper<${item.getCodeName()}>{
}
```
创建一个测试类的模板文件【QuickStartTest.java.ftl】 ## 预览成果物
![produce](img/testMain.png) iBizSys提供了即时预览,可以让您在编写模板的同时预览到最终成果物 [查看更多模板编写小技巧](http://bbs.ibizlab.cn/)
>图中左侧为模板的成果物
>图中间为模板代码
>图右侧为动态运行时,可通过动态运行时查看模板所需参数
![produce](img/quickstart/result_preview_de.png)
## 发布模板 ## 发布模板
...@@ -92,45 +168,93 @@ iBiz的实体模型简要说明,然后Mybatis-Plus的特性(功能)简要 ...@@ -92,45 +168,93 @@ iBiz的实体模型简要说明,然后Mybatis-Plus的特性(功能)简要
![produce](img/publishSystem.png) ![produce](img/publishSystem.png)
## 查看成果物 ## 查看最终成果物
### 依赖
## 依赖 ![produce](img/quickstart/result_pom.png)
![produce](img/result_pom.png) ### 配置
## 配置 ![produce](img/quickstart/result_yml.png)
![produce](img/result_yml.png) ### 启动类
## 启动类 ![produce](img/quickstart/result_appMain.png)
![produce](img/result_main.png) ### 实体类
## 实体类 ![produce](img/quickstart/result_de.png)
![produce](img/result_de.png) ### Mapper类
## Mapper类 ![produce](img/quickstart/result_mapper.png)
![produce](img/result_mapper.png)
### 开始使用
## 开始使用
运行测试类,进行功能测试:
![produce](img/result_testMain.png) 添加`DBET`实体建表的schema脚本:
```sql
create table IF NOT EXISTS T_DBET
(
dbetid VARCHAR2(100) not null,
createman VARCHAR2(60),
updateman VARCHAR2(60),
createdate DATE,
dbetname VARCHAR2(200),
updatedate DATE
);
```
添加`DBET`实体测试数据的schema脚本
```sql
INSERT INTO T_DBET (dbetid, dbetname) VALUES
(1, '这是第一条实体数据'),
(2, '这是第二条实体数据'),
(3, '这是第三条实体数据'),
(4, '这是第四条实体数据'),
(5, '这是第五条实体数据');
```
添加测试类,进行功能测试,测试类相关代码如下:
```java
@RunWith(SpringRunner.class)
@SpringBootTest(classes = demoMain.class)
public class QuickStartTest {
@Autowired
private DBETMapper dbetMapper;
@Test
public void testMain() {
System.out.println(("----- selectAll method test ------"));
List<DBET> userList = dbetMapper.selectList(null);
Assert.assertEquals(5, userList.size());
userList.forEach(System.out::println);
}
}
```
控制台输出: 控制台输出:
``` ```
----- selectAll method test ------ ----- selectAll method test ------
CATEGORY(createdate=null, updateman=null, createman=null, categoryname=社会, categoryid=1, updatedate=null) DBET(dbetname=这是第一条实体数据, createdate=null, createman=null, dbetid=1, updateman=null, updatedate=null)
CATEGORY(createdate=null, updateman=null, createman=null, categoryname=军事, categoryid=2, updatedate=null) DBET(dbetname=这是第二条实体数据, createdate=null, createman=null, dbetid=2, updateman=null, updatedate=null)
CATEGORY(createdate=null, updateman=null, createman=null, categoryname=历史, categoryid=3, updatedate=null) DBET(dbetname=这是第三条实体数据, createdate=null, createman=null, dbetid=3, updateman=null, updatedate=null)
CATEGORY(createdate=null, updateman=null, createman=null, categoryname=计算机, categoryid=4, updatedate=null) DBET(dbetname=这是第四条实体数据, createdate=null, createman=null, dbetid=4, updateman=null, updatedate=null)
CATEGORY(createdate=null, updateman=null, createman=null, categoryname=农业, categoryid=5, updatedate=null) DBET(dbetname=这是第五条实体数据, createdate=null, createman=null, dbetid=5, updateman=null, updatedate=null)
``` ```
...@@ -140,80 +264,168 @@ CATEGORY(createdate=null, updateman=null, createman=null, categoryname=农业, c ...@@ -140,80 +264,168 @@ CATEGORY(createdate=null, updateman=null, createman=null, categoryname=农业, c
iBizSys可以帮助您快速搭建并使用您所搭建的技术模板来生产您的项目,想要了解更多iBizSys模板生产体系?那就继续往下看吧! iBizSys可以帮助您快速搭建并使用您所搭建的技术模板来生产您的项目,想要了解更多iBizSys模板生产体系?那就继续往下看吧!
## MyBatis-Plus实现关系映射 ## MyBatis-Plus实现关系映射
通过第一章的学习,您已经成功搭建了一个基于(SpringBoot+MyBatis-Plus)的项目,实现了对数据表的CURD功能。但我们日常项目经常使用到关系型数据库,经常会遇到(1:N、N:N)关系场景,本章就向大家介绍iBizSys实体关系。 通过第一章的学习,您已经成功搭建了一个基于(SpringBoot+MyBatis-Plus)的项目,实现了对数据表的CURD功能。但我们日常项目经常使用到关系型数据库,经常会遇到(1:N、N:N)关系场景,本章就向大家介绍iBizSys实体关系。
## 建立实体1:N关系
建立两个实体,分别为出版社【PUBLISHER】、书籍【BOOK】,实体关系为: 出版社 1:N 书籍 ### 建立实体1:N关系
建立`ONEET``MANYET`实体,实体关系为: `ONEET 1:N MANYET`
实体关系如下图所示:[了解更多实体关系建立小知识](http://bbs.ibizlab.cn/)
![produce](img/relations/der1-n.png)
![produce](img/relations/create_relation.png) ### 模板支持1:N关系
![produce](img/relations/create_relation2.png) #### Mapper文件模板
## 模板支持1:N关系 增加Mapper.xml.ftl,用于映射该实体`主从关系`的查询结果集`resultMap`,相关模板代码如下:
增加Mapper.xml.ftl,用于映射Mybatis的查询结果集【resultMap】 ```java
![produce](img/relations/result_map.png) <#ibiztemplate>
TARGET=PSDATAENTITY
</#ibiztemplate>
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="${pub.getPKGCodeName()}.${item.getPSSystemModule().codeName?lower_case}.mapper.${item.codeName}Mapper">
<#comment>暴露明细数据查询mapper给主关系调用</#comment>
<#if item.getMinorPSDERs?? && item.getMinorPSDERs()??>
<#list item.getMinorPSDERs() as MinorPSDER>
<#assign MajorEntity = MinorPSDER.getMajorPSDataEntity()>
<#assign MajorkeyField = MajorEntity.getKeyPSDEField()>
<#assign MajorField = MinorPSDER.getPSPickupDEField()>
<#list item.getAllPSDEDataQueries() as singleQuery>
<#list singleQuery.getAllPSDEDataQueryCodes() as dedqcode>
<select id="selectBy${MajorField.getName()?lower_case}" resultMap="${de.codeName}ResultMap">
<![CDATA[ select m1.* from( select t1.* from (
${srfjavasqlcode('${dedqcode.getQueryCode()}')}
)t1 ) m1 where ${MajorField.getName()?lower_case}=<#noparse>#{</#noparse>${MajorkeyField.codeName?lower_case}<#noparse>}</#noparse>
]]>
</select>
</#list>
<#break>
</#list>
</#list>
</#if>
<#comment>mybatis返回结果映射</#comment>
<resultMap id="${de.codeName}ResultMap" type="${pub.getPKGCodeName()}.${item.getPSSystemModule().codeName?lower_case}.domain.${item.codeName}" autoMapping="true">
<id property="${item.getKeyPSDEField().codeName?lower_case}" column="${item.getKeyPSDEField().getName()?lower_case}" /><!--主键字段映射-->
<#if item.getMinorPSDERs?? && item.getMinorPSDERs()??>
<#list item.getMinorPSDERs() as MinorPSDER>
<#assign MajorField = MinorPSDER.getPSPickupDEField()>
<result property="${MajorField.codeName?lower_case}" column="${MajorField.getName()?lower_case}" /><!--关系字段映射-->
</#list>
</#if>
<#comment>1N关系中,在子实体中创建父实体的实例对象</#comment>
<#if item.getMinorPSDERs?? && item.getMinorPSDERs()??>
<#list item.getMinorPSDERs() as MinorPSDER>
<#assign MajorEntity = MinorPSDER.getMajorPSDataEntity()>
<association property="${MinorPSDER.getCodeName()?lower_case}" javaType="${pub.getPKGCodeName()}.${MajorEntity.getPSSystemModule().codeName?lower_case}.domain.${MajorEntity.codeName}" column="${MajorField.getName()?lower_case}" select="${pub.getPKGCodeName()}.${MajorEntity.getPSSystemModule().codeName?lower_case}.mapper.${MajorEntity.codeName}Mapper.selectById" fetchType="lazy"></association>
</#list>
</#if>
<#comment>1N关系中,在父实体中创建子实体的List集合</#comment>
<#if item.getMajorPSDERs?? && item.getMajorPSDERs()??>
<#list item.getMajorPSDERs() as MajorPSDER>
<#assign MinorEntity = MajorPSDER.getMinorPSDataEntity()>
<#assign MajorDerField = MajorPSDER.getPSPickupDEField()>
<collection property="${MajorPSDER.getMinorCodeName()?lower_case}" ofType="${pub.getPKGCodeName()}.${MinorEntity.getPSSystemModule().codeName?lower_case}.domain.${MinorEntity.codeName}" column="${item.getKeyPSDEField().getName()?lower_case}" select="${pub.getPKGCodeName()}.${MinorEntity.getPSSystemModule().codeName?lower_case}.mapper.${MinorEntity.codeName}Mapper.selectBy${MajorDerField.getName()?lower_case}" fetchType="lazy"></collection>
</#list>
</#if>
</resultMap>
</mapper>
实体类关联【resultMap】及发布1:N关联属性 ```
#### 实体类模板
![produce](img/relations/de_resultmap.png) 在实体类中关联 `resultMap`,并发布出该实体主从关系对象,相关模板代码如下:
```java
@TableName(value = "${item.getTableName()}",resultMap = "${item.codeName}ResultMap")
@Data
public class ${item.codeName}{
<#comment>输出当前实体1:N主实体</#comment>
<#if item.getMinorPSDERs?? && item.getMinorPSDERs()??>
<#list item.getMinorPSDERs() as MinorPSDER>
<#assign MajorEntity = MinorPSDER.getMajorPSDataEntity()>
@TableField(exist = false)//关系主表数据
private ${MajorEntity.getCodeName()} ${MinorPSDER.getCodeName()?lower_case};
</#list>
</#if>
<#comment>输出当前实体1:N子实体</#comment>
<#if item.getMajorPSDERs?? && item.getMajorPSDERs()??>
<#list item.getMajorPSDERs() as MajorPSDER>
<#assign MinorEntity = MajorPSDER.getMinorPSDataEntity()>
@TableField(exist = false)//关系子表数据
private List <${MinorEntity.getCodeName()}> ${MajorPSDER.getMinorCodeName()?lower_case};
</#list>
</#if>
}
```
## 开始使用 ## 开始使用
添加出版社【PUBLISHER】、书籍【BOOK】实体建表的schema脚本 添加主实体`ONEET`、关系实体`MANYET`实体建表的schema脚本
```sql ```sql
create table IF NOT EXISTS T_PUBLISHER create table IF NOT EXISTS T_ONEET
( (
publisherid VARCHAR2(100) not null, oneetid VARCHAR2(100) not null,
oneetname VARCHAR2(200),
createman VARCHAR2(60), createman VARCHAR2(60),
updateman VARCHAR2(60), updateman VARCHAR2(60),
createdate DATE, createdate DATE,
publishername VARCHAR2(200),
updatedate DATE updatedate DATE
); );
create table IF NOT EXISTS T_BOOK create table IF NOT EXISTS T_MANYET
( (
bookid VARCHAR2(100) not null, manyetid VARCHAR2(100) not null,
manyetname VARCHAR2(200),
oneetid VARCHAR2(200),
createman VARCHAR2(60), createman VARCHAR2(60),
updateman VARCHAR2(60), updateman VARCHAR2(60),
createdate DATE, createdate DATE,
bookname VARCHAR2(200), updatedate DATE
updatedate DATE,
publisherid VARCHAR2(200),
price NUMBER,
pubyearisbn VARCHAR2(200)
); );
``` ```
添加出版社【PUBLISHER】、书籍【BOOK】实体测试数据的schema脚本 添加主实体`ONEET`、关系实体`MANYET`实体测试数据的schema脚本
```sql ```sql
INSERT INTO T_PUBLISHER (publisherid, publishername) VALUES INSERT INTO T_ONEET (oneetid, oneetname) VALUES
(1, '清华大学出版社'), (1, '主实体1'),
(2, '北京大学出版社'); (2, '主实体2');
INSERT INTO T_MANYET (manyetid, manyetname,oneetid) VALUES
INSERT INTO T_BOOK (bookid, bookname,publisherid,pubyearisbn) VALUES (1, '关系实体数据1',1),
(1, 'java程序设计',1,'2017-12'), (2, '关系实体数据2',1),
(2, 'c语言程序设计',1,'2018-12'), (3, '关系实体数据3',2);
(3, 'python从入门到精通',2,'2019-2');
``` ```
添加测试类进行功能测试,测试类代码如下:
运行测试类
```java ```java
...@@ -221,14 +433,14 @@ INSERT INTO T_BOOK (bookid, bookname,publisherid,pubyearisbn) VALUES ...@@ -221,14 +433,14 @@ INSERT INTO T_BOOK (bookid, bookname,publisherid,pubyearisbn) VALUES
@SpringBootTest(classes = demoMain.class) @SpringBootTest(classes = demoMain.class)
public class RelationsTest { public class RelationsTest {
@Autowired @Autowired
private PUBLISHERMapper publisherMapper; private ONEETMapper oneetMapper;
@Test @Test
public void testMain() { public void testMain() {
System.out.println(("----- selectAll method test ------")); System.out.println(("----- selectAll method test ------"));
List<PUBLISHER> userList = publisherMapper.selectList(null); List<ONEET> oneets = oneetMapper.selectList(null);
for(PUBLISHER publisher: userList){ for(ONEET oneet: oneets){
System.out.println(String.format("[%s]出版社共出版的书籍数为[%s]",publisher.getPublishername(),publisher.getBooks().size())); System.out.println(String.format("[%s]关联的关系实体数量为[%s]",oneet.getOneetname(),oneet.getManyetobjs().size()));
} }
} }
} }
...@@ -237,12 +449,12 @@ public class RelationsTest { ...@@ -237,12 +449,12 @@ public class RelationsTest {
控制台输出: 控制台输出:
从测试结果可以看出,在查询主数据【出版社】时,可以获取到子数据【书籍】对象,从而实现关联查询。 从测试结果可以看出,在查询主数据时,可以获取到子数据`Manyetobjs`对象,从而实现关联查询。
``` ```
----- selectAll method test ------ ----- selectAll method test ------
[清华大学出版社]出版社共出版的书籍数为[2] [主实体1]关联的关系实体数量为[2]
[北京大学出版社]出版社共出版的书籍数为[1] [主实体2]关联的关系实体数量为[1]
``` ```
...@@ -260,73 +472,184 @@ public class RelationsTest { ...@@ -260,73 +472,184 @@ public class RelationsTest {
## 配置实体搜索项 ## 配置实体搜索项
建立实体搜索项,支持通过SEX作为条件进行查询。 建立实体搜索项,支持通过SEX作为条件进行查询[查看更多实体搜索项配置小技巧](http://bbs.ibizlab.cn/)
![produce](img/dycond/search_cond.png)
## 模板支持实体搜索项
### 实体搜索项模板
模板中为每个实体发布实体搜索项 `searchfilter`,搜索条件包含:`模糊匹配``等于``大于等于``小于等于`···,模板代码如下:
```java
@Data
public class ${item.getCodeName()}SearchFilter{
private QueryWrapper<${item.getCodeName()}> selectCond;
public ${item.getCodeName()}SearchFilter(){
this.selectCond=new QueryWrapper<${item.getCodeName()}>();
}
/**
* 输出实体搜索项
*/
<#list item.getPSDEFields() as defield>
<#list defield.getAllPSDEFSearchModes() as formitem>
private ${srfjavatype(formitem.getPSDEField().stdDataType)} ${formitem.getName()?lower_case};//[${defield.getLogicName()}]
public void set${formitem.getName()?lower_case?cap_first}(${srfjavatype(formitem.getPSDEField().stdDataType)} ${formitem.getName()?lower_case}) {
this.${formitem.getName()?lower_case} = ${formitem.getName()?lower_case};
if(!StringUtils.isEmpty(this.${formitem.getName()?lower_case})){
<#if formitem.getValueOp() == "LIKE">
this.selectCond.like("${formitem.getPSDEField().getName()?lower_case}", ${formitem.getName()?lower_case});
<#elseif formitem.getValueOp() == "LEFTLIKE">
this.selectCond.likeLeft("${formitem.getPSDEField().getName()?lower_case}", ${formitem.getName()?lower_case});
<#elseif formitem.getValueOp() == "RIGHTLIKE">
this.selectCond.likeRight("${formitem.getPSDEField().getName()?lower_case}", ${formitem.getName()?lower_case});
<#elseif formitem.getValueOp() == "EQ">
this.selectCond.eq("${formitem.getPSDEField().getName()?lower_case}", ${formitem.getName()?lower_case});
<#elseif formitem.getValueOp() == "NOTEQ">
this.selectCond.ne("${formitem.getPSDEField().getName()?lower_case}", ${formitem.getName()?lower_case});
<#elseif formitem.getValueOp() == "GT">
this.selectCond.gt("${formitem.getPSDEField().getName()?lower_case}", ${formitem.getName()?lower_case});
<#elseif formitem.getValueOp() == "GTANDEQ">
this.selectCond.ge("${formitem.getPSDEField().getName()?lower_case}", ${formitem.getName()?lower_case});
<#elseif formitem.getValueOp() == "LT">
this.selectCond.lt("${formitem.getPSDEField().getName()?lower_case}", ${formitem.getName()?lower_case});
<#elseif formitem.getValueOp() == "LTANDEQ">
this.selectCond.le("${formitem.getPSDEField().getName()?lower_case}", ${formitem.getName()?lower_case});
</#if>
}
}
</#list>
</#list>
}
```
### 实体服务对象模板
实体服务类对象中,发布`实体数据集`相关模板代码,数据集接收`searchfilter`作为过滤参数,并将`searchfilter`作为参数传递 `mapper` 实现动态查询
![produce](img/dycond/cond1.png) ```java
@Service
public class ${item.codeName}ServiceImpl extends ServiceImpl<${item.codeName}Mapper, ${item.codeName}> implements ${item.codeName}Service{
@Resource
private ${item.codeName}Mapper ${item.codeName?lower_case}Mapper;
<#if item.getAllPSDEDataSets()??>
<#list item.getAllPSDEDataSets() as dedataset>
public List<${item.codeName}> list${dedataset.getCodeName()}(${item.codeName}SearchFilter filter) {
return ${item.codeName?lower_case}Mapper.search${dedataset.getCodeName()}(filter,filter.getSelectCond());
}
</#list>
</#if>
}
```
![produce](img/dycond/cond2.png) ### Mapper类模板
## 模板拼接动态条件 Mapper类提供接口供service调用,调用mapper.xml
为每个实体发布搜索项【searchfilter】 ```java
![produce](img/dycond/searchfilter.png) public interface ${item.getCodeName()}Mapper extends BaseMapper<${item.getCodeName()}>{
发布实体服务对象 <#if item.getAllPSDEDataSets()??>
<#list item.getAllPSDEDataSets() as dedataset>
List<${item.getCodeName()}> search${dedataset.getCodeName()}(@Param("srf") ${item.getCodeName()}SearchFilter searchfilter,@Param("ew") Wrapper<${item.getCodeName()}> wrapper) ;
</#list>
</#if>
![produce](img/dycond/IService.png) }
发布实体服务对象实现类,用于构造【searchfilter】,并调用mapper进行数据查询
![produce](img/dycond/serviceImpl.png) ```
### Mapper文件模板
Mapper类提供接口供service调用,接收服务对象中传递过来的【searchfilter】 根据 `Mapper类` 中传递过来的搜索条件,动态拼接查询条件
![produce](img/dycond/mapper.png) ```java
解析Mapper类searfilter中的wrapper,动态拼接查询条件 <#comment>实体数据查询</#comment>
<select id="search${singleQuery.getCodeName()}" resultMap="${de.codeName}ResultMap">
<![CDATA[select t1.* from (
${srfjavasqlcode('${dedqcode.getQueryCode()}')}
)t1]]>
<where><if test="ew!=null and ew.sqlSegment!=null and !ew.emptyOfWhere"><#noparse>${ew.sqlSegment}</#noparse></if></where>
<if test="ew!=null and ew.sqlSegment!=null and ew.emptyOfWhere"><#noparse>${ew.sqlSegment}</#noparse></if>
</select>
![produce](img/dycond/mapper_xml.png)
```
## 开始使用 ## 开始使用
添加作者【ACTOR】实体建表的schema脚本 添加`DBET`实体建表的schema脚本:
```sql ```sql
create table IF NOT EXISTS T_ACTOR create table IF NOT EXISTS T_DBET
( (
actorid VARCHAR2(100) not null, dbetid VARCHAR2(100) not null,
actorname VARCHAR2(200), dbetname VARCHAR2(200),
sex VARCHAR2(200),
createman VARCHAR2(60), createman VARCHAR2(60),
updateman VARCHAR2(60), updateman VARCHAR2(60),
createdate DATE, createdate DATE,
updatedate DATE, updatedate DATE
pseudonym VARCHAR2(200),
sex VARCHAR2(200),
school VARCHAR2(200),
education VARCHAR2(200),
birthday VARCHAR2(200)
); );
``` ```
添加`DBET`实体测试数据的schema脚本
```sql ```sql
INSERT INTO T_ACTOR (actorid, actorname,sex) VALUES INSERT INTO T_DBET (dbetid, dbetname,sex) VALUES
(1, '张三','男'), (1, '这是第一条实体数据','男'),
(2, '李四','女'), (2, '这是第二条实体数据','男'),
(3, '王五','男'); (3, '这是第三条实体数据','男'),
(4, '这是第四条实体数据','女'),
(5, '这是第五条实体数据','女');
```
添加测试类,测试类相关代码如下:
```java
@RunWith(SpringRunner.class)
@SpringBootTest(classes = demoMain.class)
public class DyCondTest {
@Autowired
private DBETService dbetService;
@Test
public void testMain() {
System.out.println(("----- selectAll method test ------"));
DBETSearchFilter dbetSearchFilter=new DBETSearchFilter();
dbetSearchFilter.setN_sex_eq("男");
List<DBET> dbets = dbetService.listDefault(dbetSearchFilter);
dbets.forEach(System.out::println);
}
}
``` ```
运行测试类,控制台输出: 控制台输出:
已经能够根据实体搜索项【searchfilter】中设定的条件,过滤出 SEX=“男” 的数据 可以看出已经能够根据实体搜索项【searchfilter】中设定的条件,过滤出 SEX=“男” 的数据
``` ```
----- selectAll method test ------ ----- selectAll method test ------
ACTOR(actorname=张三, updateman=null, createdate=null, actorid=1, createman=null, updatedate=null, pseudonym=null, sex=男, school=null, education=null, birthday=null) DBET(dbetname=这是第一条实体数据, createdate=null, createman=null, dbetid=1, updateman=null, updatedate=null, sex=男)
ACTOR(actorname=王五, updateman=null, createdate=null, actorid=3, createman=null, updatedate=null, pseudonym=null, sex=男, school=null, education=null, birthday=null) DBET(dbetname=这是第二条实体数据, createdate=null, createman=null, dbetid=2, updateman=null, updatedate=null, sex=男)
DBET(dbetname=这是第三条实体数据, createdate=null, createman=null, dbetid=3, updateman=null, updatedate=null, sex=男)
``` ```
\ No newline at end of file
Markdown 格式
0% or
您添加了 0 到此讨论。请谨慎行事。
先完成此消息的编辑!
想要评论请 注册