ソースを参照

通用模块完善

develop
陈国强 6年前
コミット
066bd947d9
4個のファイルの変更545行の追加0行の削除
  1. +82
    -0
      code-generator/src/main/java/com/qmrz/generator/MapperFPGenerator.java
  2. +80
    -0
      code-generator/src/main/java/com/qmrz/generator/MapperFPRGenerator.java
  3. +96
    -0
      code-generator/src/main/resources/template/mapper_fp.ftl
  4. +287
    -0
      code-generator/src/main/resources/template/mapper_fpr.ftl

+ 82
- 0
code-generator/src/main/java/com/qmrz/generator/MapperFPGenerator.java ファイルの表示

@@ -0,0 +1,82 @@
package com.qmrz.generator;

import com.qmrz.exception.ABException;
import com.qmrz.service.TableService;
import com.qmrz.utils.CGHelper;
import com.qmrz.utils.Fn;
import com.qmrz.utils.MapUtil;
import com.qmrz.utils.SpringContextUtil;
import org.apache.commons.lang.StringUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
* Mapper生成
*/
public class MapperGenerator {
private TableService tableService;
private String dbName;

public MapperGenerator(String dbName) {
tableService = SpringContextUtil.getBean(TableService.class);
this.dbName = dbName;
}

public void generator() {
List<Map> tableList = tableService.getTableList(dbName);
List<Map> columnList = tableService.getColumnList(dbName);

Map<String, List<Map>> result = columnList.stream().collect(Collectors.groupingBy(map -> Fn.toString(map.get("table_name")), Collectors.toList()));

CGHelper cgHelper = new CGHelper("mapper2.ftl", dbName);//无同步代码版本
// CGHelper cgHelper = new CGHelper("mapper_sync.ftl", dbName);//有同步代码版本

tableList.forEach(item -> {
String table_name = item.get("table_name").toString();
String table_name2 = Fn.firstUpperCase(table_name);

List<Map> tmpColumnList = result.get(table_name);

List<String> orderByList = new ArrayList<>();
tmpColumnList.stream().filter(i -> {
String tmpColumnName = Fn.toString(i.get("column_name"));
if ("createtime".equals(tmpColumnName)) {
orderByList.add("t."+tmpColumnName + " desc");
return true;
}
return false;
}).findFirst();

StringBuilder primaryKeyName = new StringBuilder();

tmpColumnList.stream().filter(i -> {
if ("PRI".equals(Fn.toString(i.get("column_key")))) {
orderByList.add("t."+Fn.toString(i.get("column_name")) + " desc");
primaryKeyName.append(Fn.toString(i.get("column_name")));
return true;
}
return false;
}).findFirst();

if (primaryKeyName.length() == 0) {
throw new ABException("表 " + table_name + " 没有主键");
}

String orderBy = "";

if (orderByList.size() > 0) {
orderBy = "order by " + String.join(",", orderByList);
}

item.put("columnList", tmpColumnList);
item.put("dbname", dbName);
item.put("table_name2", table_name2);//首字母大写
item.put("orderBy", orderBy);
item.put("primaryKeyName", primaryKeyName.toString());
cgHelper.generator("mapper2", table_name2 + "Mapper.xml", item);
});
}
}

+ 80
- 0
code-generator/src/main/java/com/qmrz/generator/MapperFPRGenerator.java ファイルの表示

@@ -0,0 +1,80 @@
package com.qmrz.generator;

import com.qmrz.exception.ABException;
import com.qmrz.service.TableService;
import com.qmrz.utils.CGHelper;
import com.qmrz.utils.Fn;
import com.qmrz.utils.SpringContextUtil;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
* Mapper生成
*/
public class MapperFPGenerator {
private TableService tableService;
private String dbName;

public MapperFPGenerator(String dbName) {
tableService = SpringContextUtil.getBean(TableService.class);
this.dbName = dbName;
}

public void generator() {
List<Map> tableList = tableService.getTableList(dbName);
List<Map> columnList = tableService.getColumnList(dbName);

Map<String, List<Map>> result = columnList.stream().collect(Collectors.groupingBy(map -> Fn.toString(map.get("table_name")), Collectors.toList()));

CGHelper cgHelper = new CGHelper("mapper_fp.ftl", dbName);//无同步代码版本
// CGHelper cgHelper = new CGHelper("mapper_sync.ftl", dbName);//有同步代码版本

tableList.forEach(item -> {
String table_name = item.get("table_name").toString();
String table_name2 = Fn.firstUpperCase(table_name);

List<Map> tmpColumnList = result.get(table_name);

List<String> orderByList = new ArrayList<>();
tmpColumnList.stream().filter(i -> {
String tmpColumnName = Fn.toString(i.get("column_name"));
if ("createtime".equals(tmpColumnName)) {
orderByList.add("t."+tmpColumnName + " desc");
return true;
}
return false;
}).findFirst();

StringBuilder primaryKeyName = new StringBuilder();

tmpColumnList.stream().filter(i -> {
if ("PRI".equals(Fn.toString(i.get("column_key")))) {
orderByList.add("t."+Fn.toString(i.get("column_name")) + " desc");
primaryKeyName.append(Fn.toString(i.get("column_name")));
return true;
}
return false;
}).findFirst();

if (primaryKeyName.length() == 0) {
throw new ABException("表 " + table_name + " 没有主键");
}

String orderBy = "";

if (orderByList.size() > 0) {
orderBy = "order by " + String.join(",", orderByList);
}

item.put("columnList", tmpColumnList);
item.put("dbname", dbName);
item.put("table_name2", table_name2);//首字母大写
item.put("orderBy", orderBy);
item.put("primaryKeyName", primaryKeyName.toString());
cgHelper.generator("mapper_fp", table_name2 + "Mapper.xml", item);
});
}
}

+ 96
- 0
code-generator/src/main/resources/template/mapper_fp.ftl ファイルの表示

@@ -0,0 +1,96 @@
<?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">

<!--
${table_name}(${table_comment})
注意:分隔线以下 不增加业务,有新增自定义业务时加到分隔线上面
-->
<mapper namespace="com.qmrz.domain.mapper.${table_name2}Mapper">


<!-- ========================= 分隔线以下为生成的通用代码 ========================= -->
<!--
注意:
1、增加条件必须增加 _parameter.containsKey
2、修改字段手动更改此文件,不推荐重新生成后全部覆盖
-->

<!-- 通用查询条件 -->
<sql id="common_where">
<#list columnList as item>
<if test="_parameter.containsKey('where_${item.column_name}')">and t.${item.column_name} = ${"#"}{where_${item.column_name}}</if>
</#list>

<#list columnList as item>
<if test="_parameter.containsKey('like_${item.column_name}')">and t.${item.column_name} like concat('%',${"#"}{like_${item.column_name}},'%')</if>
</#list>

<if test="_parameter.containsKey('list_${primaryKeyName}')">
and t.${primaryKeyName} in
<foreach collection="list_${primaryKeyName}" item="item" open="(" separator="," close=")">
${"#"}{item}
</foreach>
</if>
</sql>
<!-- 通用查询记录 -->
<select id="selectList" resultType="java.util.LinkedHashMap">
select <#list columnList as item>t.${item.column_name}<#if item_has_next>,</#if></#list>
from ${table_name} t
<where>
<include refid="common_where" />
</where>
${orderBy}
<if test="_parameter.containsKey('__limit__')">
<#noparse>limit #{__limit__}</#noparse>
</if>
</select>

<!-- 通用查询记录总数 -->
<select id="selectCount" resultType="Integer">
select count(0) as cou from ${table_name} t
<where>
<include refid="common_where" />
</where>
</select>

<!-- 通用插入数据 -->
<insert id="insert">
insert into ${table_name}(
<trim suffixOverrides=",">
<#list columnList as item>
<if test="_parameter.containsKey('${item.column_name}')">${item.column_name},</if>
</#list>
</trim>)
values(
<trim suffixOverrides=",">
<#list columnList as item>
<if test="_parameter.containsKey('${item.column_name}')">${"#"}{${item.column_name}},</if>
</#list>
</trim>
)
</insert>

<!-- 通用更新数据 -->
<update id="update">
update ${table_name} t
<set>
<#list columnList as item>
<#if item.column_key?? && item.column_key!="PRI" || item.column_name?? && item.column_name != "id">
<if test="_parameter.containsKey('${item.column_name}')">t.${item.column_name} = ${"#"}{${item.column_name}},</if>
</#if>
</#list>
</set>
<where>
<include refid="common_where" />
</where>
</update>

<!-- 通用删除记录 -->
<delete id="delete">
delete t from ${table_name} t
<where>
<include refid="common_where" />
</where>
</delete>
</mapper>

+ 287
- 0
code-generator/src/main/resources/template/mapper_fpr.ftl ファイルの表示

@@ -0,0 +1,287 @@
<?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">

<!--
${table_name}(${table_comment})
注意:分隔线以下 不增加业务,有新增自定义业务时加到分隔线上面
-->
<mapper namespace="com.qmrz.domain.mapper.${table_name2}Mapper">

<select id="getList" resultType="java.util.LinkedHashMap">
select <#list columnList as item>t.${item.column_name}<#if item_has_next>,</#if></#list>
,zp2.name gldwname,zp3.name gldw2name,za.name xzqhname
,IFNULL(th.status,0) status

<!-- 已分派 -->
<if test='where_status=="1"'>
,th.fppoliceid,fpP1.fullname fppolicename
,th.jspoliceid,jsP1.fullname jspolicename
,th.fptime
,if(th.chpoliceid,1,0) chstatus, if(th.thpoliceid,1,0) thstatus
</if>

<!-- 2撤回 3退回 -->
<if test='where_status.contains("2") || where_status.contains("3")'>
,th.currpoliceid chthpoliceid,p.fullname chthpolicename
,th.operationtime chthtime
,if(th.chpoliceid,1,0) chstatus, if(th.thpoliceid,1,0) thstatus
,th.remarks
</if>

<!-- 已反馈 -->
<if test='where_status=="4"'>
,th.currpoliceid fkpoliceid,p.fullname fkpolicename
,th.operationtime fktime
,if(th.chpoliceid,1,0) chstatus, if(th.thpoliceid,1,0) thstatus
,th.memo,th.remarks, th.gldw fkgldw, fkzp2.name fkgldwname
</if>

<!-- 无需处理 -->
<if test='where_status=="5"'>
,th.currpoliceid wxclpoliceid,p.fullname wxclpolicename
,th.operationtime wxcltime
,if(th.chpoliceid,1,0) chstatus, if(th.thpoliceid,1,0) thstatus
,th.remarks
</if>

from ${table_name} t
left join zonepubinfo zp2 on zp2.pubcode = t.gldw <!-- 第4级 管辖单位(派出所代码) -->
left join zonepubinfo zp3 on zp3.pubcode = t.gldw2 <!-- 第5级 管辖单位(警务区代码) -->
left join zoneadmin za on za.id = t.xzqh <!-- 行政区划 -->
left join ${table_name}relation th on t.${primaryKeyName}=th.lnid
left join police p on p.id=th.currpoliceid <!-- 最近一次操作时的人员ID(分派时为接收人) -->

<!-- 已分派 -->
<if test='where_status=="1"'>
left join police fpP1 on fpP1.id=th.fppoliceid
left join police jsP1 on jsP1.id=th.jspoliceid
</if>

<!-- 已反馈 -->
<if test='where_status=="4"'>
left join zonepubinfo fkzp2 on fkzp2.pubcode = th.gldw and th.memo=4
</if>

<where>
<include refid="common_where" />

<!--类型区别 最新: 0 未分派 1已分派 2 撤回 3 退回 4 已反馈 5无需处理-->
<choose>
<when test='where_status=="0"'>
<!-- and th.status is null -->
and ifnull(th.status,0) = 0
</when>
<otherwise>
and th.status in ( ${'$'}{where_status} )
</otherwise>
</choose>

<!--app模糊搜索 -->
<if test="_parameter.containsKey('like_appkeyword')">
<!-- TODO 需处理
and CONCAT(t.ownername,'|',t.ownerphone,'|',t.houseaddr) like concat('%', ${'#'}{like_appkeyword} ,'%')
-->
</if>

<!--查询当日 或 历史-->
<if test="_parameter.containsKey('where_historytype')">
<!--当日-->
<if test=" where_historytype == 1 ">
and DATEDIFF(th.operationtime,now())=0
</if>
<!--历史-->
<if test=" where_historytype == 2 ">
<![CDATA[ and DATEDIFF(th.operationtime,now())<0 ]]>
</if>
</if>

<!--反馈时间查询-->
<if test="_parameter.containsKey('where_fkbegintime')">
<![CDATA[ and th.operationtime >= ${'#'}{where_fkbegintime} ]]>
</if>
<if test="_parameter.containsKey('where_fkendtime')">
<![CDATA[ and th.operationtime < ${'#'}{where_fkendtime} ]]>
</if>

<!--接受人 status=1 已分派 | 撤回人退回人 status in (2,3)撤回退回 | 反馈人 status=4 已反馈 5无需处理-->
<if test='where_status=="1"'>
<if test="_parameter.containsKey('like_fppolicename')">
and fpP1.fullname like concat('%',${'#'}{like_fppolicename},'%')
</if>
</if>

<choose>
<!-- 接收人 -->
<when test="_parameter.containsKey('like_jspolicename')">
and p.fullname like concat('%',${'#'}{like_jspolicename},'%')
</when>
<!-- 撤回人 -->
<when test="_parameter.containsKey('like_chpolicename')">
and p.fullname like concat('%',${'#'}{like_chpolicename},'%')
</when>
<!-- 退回人 -->
<when test="_parameter.containsKey('like_thpolicename')">
and p.fullname like concat('%',${'#'}{like_thpolicename},'%')
</when>
<!-- 撤回/退回人 -->
<when test="_parameter.containsKey('like_chthpolicename')">
and p.fullname like concat('%',${'#'}{like_chthpolicename},'%')
</when>
<!-- 反馈人 -->
<when test="_parameter.containsKey('like_fkpolicename')">
and p.fullname like concat('%',${'#'}{like_fkpolicename},'%')
</when>
<!-- 无需处理人 -->
<when test="_parameter.containsKey('like_wxclpolicename')">
and p.fullname like concat('%',${'#'}{like_wxclpolicename},'%')
</when>
</choose>

<if test="_parameter.containsKey('where_fppoliceid')">
and th.fppoliceid = ${'#'}{where_fppoliceid}
</if>

<choose>
<when test="_parameter.containsKey('where_jspoliceid')">
and th.currpoliceid = ${'#'}{where_jspoliceid}
</when>

<when test="_parameter.containsKey('where_chpoliceid')">
and th.currpoliceid = ${'#'}{where_chpoliceid}
</when>
<when test="_parameter.containsKey('where_thpoliceid')">
and th.currpoliceid = ${'#'}{where_thpoliceid}
</when>
<when test="_parameter.containsKey('where_chthpoliceid')">
and th.currpoliceid = ${'#'}{where_chthpoliceid}
</when>

<when test="_parameter.containsKey('where_fkpoliceid')">
and th.currpoliceid = ${'#'}{where_fkpoliceid}
</when>
<when test="_parameter.containsKey('where_wxclpoliceid')">
and th.currpoliceid = ${'#'}{where_wxclpoliceid}
</when>
</choose>

<!--反馈结果-->
<if test="_parameter.containsKey('where_memo')">
and th.memo = ${'#'}{where_memo}
</if>

<!--过滤出选中-->
<if test="_parameter.containsKey('where_idlist')">
and t.${primaryKeyName} in (
<foreach item="item" index="index" collection="where_idlist" separator="," >
${'#'}{item}
</foreach>
)
</if>

<!-- 系统数据权限条件 登入信息过滤 不能加前缀 where_ -->
<if test="_parameter.containsKey('policeid')">
and th.currpoliceid= ${'#'}{policeid}
</if>
<if test="_parameter.containsKey('gldw2List')">
and t.gldw2 in (
<foreach item="item" index="index" collection="gldw2List" separator=",">
${'#'}{item}
</foreach>
)
</if>
</where>

<if test='where_status == "0"'>
${orderBy}
</if>
<if test='where_status != "0"'>
order by th.operationtime desc , t.${primaryKeyName} desc
</if>
</select>

<!-- ========================= 分隔线以下为生成的通用代码 ========================= -->
<!--
注意:
1、增加条件必须增加 _parameter.containsKey
2、修改字段手动更改此文件,不推荐重新生成后全部覆盖
-->

<!-- 通用查询条件 -->
<sql id="common_where">
<#list columnList as item>
<if test="_parameter.containsKey('where_${item.column_name}')">and t.${item.column_name} = ${"#"}{where_${item.column_name}}</if>
</#list>

<#list columnList as item>
<if test="_parameter.containsKey('like_${item.column_name}')">and t.${item.column_name} like concat('%',${"#"}{like_${item.column_name}},'%')</if>
</#list>

<if test="_parameter.containsKey('list_${primaryKeyName}')">
and t.${primaryKeyName} in
<foreach collection="list_${primaryKeyName}" item="item" open="(" separator="," close=")">
${"#"}{item}
</foreach>
</if>
</sql>
<!-- 通用查询记录 -->
<select id="selectList" resultType="java.util.LinkedHashMap">
select <#list columnList as item>t.${item.column_name}<#if item_has_next>,</#if></#list>
from ${table_name} t
<where>
<include refid="common_where" />
</where>
${orderBy}
<if test="_parameter.containsKey('__limit__')">
<#noparse>limit #{__limit__}</#noparse>
</if>
</select>

<!-- 通用查询记录总数 -->
<select id="selectCount" resultType="Integer">
select count(0) as cou from ${table_name} t
<where>
<include refid="common_where" />
</where>
</select>

<!-- 通用插入数据 -->
<insert id="insert">
insert into ${table_name}(
<trim suffixOverrides=",">
<#list columnList as item>
<if test="_parameter.containsKey('${item.column_name}')">${item.column_name},</if>
</#list>
</trim>)
values(
<trim suffixOverrides=",">
<#list columnList as item>
<if test="_parameter.containsKey('${item.column_name}')">${"#"}{${item.column_name}},</if>
</#list>
</trim>
)
</insert>

<!-- 通用更新数据 -->
<update id="update">
update ${table_name} t
<set>
<#list columnList as item>
<#if item.column_key?? && item.column_key!="PRI" || item.column_name?? && item.column_name != "id">
<if test="_parameter.containsKey('${item.column_name}')">t.${item.column_name} = ${"#"}{${item.column_name}},</if>
</#if>
</#list>
</set>
<where>
<include refid="common_where" />
</where>
</update>

<!-- 通用删除记录 -->
<delete id="delete">
delete t from ${table_name} t
<where>
<include refid="common_where" />
</where>
</delete>
</mapper>

読み込み中…
キャンセル
保存