赞
踩
目录
平时一直使用PDManer对业务进行数据库表建模,建完模后可以自动生成基础的业务代码,以 mybatis 为例,生成 Entity,Mapper,Mapper.xml 代码。
PDManer里有默认的代码模板,不过在实际开发中需要根据实际情况做调整,便需要自定义代码模板。
记录下实际项目中用过的 mybatis 代码模板,以便以后复用。
- {{ var today=new Date();
- var fullYear=today.getFullYear();
- var month=today.getMonth() + 1;
- var days=today.getDate();
-
- var pkVarName = "id";
- var pkDataType = "String";
- it.entity.fields.forEach(function(field){
- if(field.primaryKey){
- pkVarName = field.defKey,false;
- pkDataType = field["type"];
- return;
- }
- });
-
- var pkgName = it.entity.env.base.nameSpace;
- var beanClass = it.entity.env.base.codeRoot;
- var beanVarName = beanClass.charAt(0).toLowerCase()+beanClass.slice(1);
- var serviceClass = beanClass+'Service';
- var serviceVarName= beanVarName+'Service';
-
- }}package {{=pkgName}}.mapper;
- $blankline
- import java.util.List;
- import org.apache.ibatis.annotations.Mapper;
- import org.apache.ibatis.annotations.Param;
- import {{=pkgName}}.beans.entity.{{=beanClass}};
- $blankline
-
- /**
- * {{=it.entity.defName}};({{=it.entity.defKey}})表数据库访问层
- * @author : name
- * @date : {{=fullYear}}-{{=month}}-{{=days}}
- */
- @Mapper
- public interface {{=beanClass}}Mapper{
- //↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓模板生成的基础方法↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓
- /**
- * 通过ID查询单条数据
- *
- * @param {{=pkVarName}} 主键
- * @return 实例对象
- */
- {{=beanClass}} selectById({{=pkDataType}} {{=pkVarName}});
-
- /**
- * 查询列表
- *
- * @param {{=beanVarName}} 查询条件
- * @return 对象列表
- */
- List<{{=beanClass}}> selectList({{=beanClass}} {{=beanVarName}});
-
- /**
- * 统计总行数
- *
- * @param {{=beanVarName}} 查询条件
- * @return 总行数
- */
- long count({{=beanClass}} {{=beanVarName}});
-
- /**
- * 新增数据
- *
- * @param {{=beanVarName}} 实例对象
- * @return 影响行数
- */
- int insert({{=beanClass}} {{=beanVarName}});
-
- /**
- * 批量新增数据
- *
- * @param entities List<{{=beanClass}}> 实例对象列表
- * @return 影响行数
- */
- int insertBatch(@Param("entities") List<{{=beanClass}}> entities);
-
- /**
- * 批量新增或按主键更新数据
- *
- * @param entities List<{{=beanClass}}> 实例对象列表
- * @return 影响行数
- */
- int insertOrUpdateBatch(@Param("entities") List<{{=beanClass}}> entities);
-
- /**
- * 更新数据
- *
- * @param {{=beanVarName}} 实例对象
- * @return 影响行数
- */
- int update({{=beanClass}} {{=beanVarName}});
-
- /**
- * 通过主键删除数据
- *
- * @param {{=pkVarName}} 主键
- * @return 影响行数
- */
- int deleteById({{=pkDataType}} {{=pkVarName}});
- //↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑模板生成的基础方法↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑
- $blankline
- //↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓自定义方法↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓
- $blankline
- $blankline
- $blankline
- //↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑自定义方法↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑
- }
- {{ var today=new Date();
- var fullYear=today.getFullYear();
- var month=today.getMonth() + 1;
- var days=today.getDate();
-
- var pkVarName = "id";
- var pkDataType = "String";
- var pkField = "id";
- it.entity.fields.forEach(function(field){
- if(field.primaryKey){
- pkField = field.defKey;
- pkVarName = field.defKey;
- pkDataType = field["type"];
- return;
- }
- });
-
- var pkgName = it.entity.env.base.nameSpace;
- var beanClass = it.entity.env.base.codeRoot;
- var beanVarName = beanClass.charAt(0).toLowerCase()+beanClass.slice(1);
- var serviceClass = beanClass+'Service';
- var serviceVarName= beanVarName+'Service';
-
- }}<?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="{{=pkgName}}.mapper.{{=beanClass}}Mapper">
- <resultMap type="{{=pkgName}}.beans.entity.{{=beanClass}}" id="{{=beanClass}}Map">
- {{~it.entity.fields:field:index}}
- <result property="{{=it.func.camel(field.defKey,false)}}" column="{{=field.defKey}}"/>
- {{~}}
- </resultMap>
- $blankline
- <!--↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓模板生成的基础方法↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓-->
- <!-- 通过ID查询单条数据 -->
- <select id="selectById" resultMap="{{=beanClass}}Map">
- select
- {{=it.entity.fields.map(function(e,i){
- if (e.defKey == 'desc' || e.defKey == 'type' || e.defKey == 'key' || e.defKey == 'level') return '`'+e.defKey+'`';
- return e.defKey}).join(',')}}
- from {{=it.entity.defKey}}
- where {{=pkField}} = #{{{=pkVarName}}} and delete_flag = 0
- </select>
- $blankline
- <!--查询列表-->
- <select id="selectList" resultMap="{{=beanClass}}Map">
- select
- {{=it.entity.fields.map(function(e,i){
- if (e.defKey == 'desc' || e.defKey == 'type' || e.defKey == 'key' || e.defKey == 'level') return '`'+e.defKey+'`';
- return e.defKey}).join(',')}}
- from {{=it.entity.defKey}}
- <where>
- delete_flag = 0
- {{~it.entity.fields:field:index}}
- {{?field.defKey != 'delete_flag'}}<if test={{?field.type == 'Integer'}}"{{=it.func.camel(field.defKey,false)}} != null"{{??}}"{{=it.func.camel(field.defKey,false)}} != null and {{=it.func.camel(field.defKey,false)}} != ''"{{?}}>
- and {{?field.defKey == 'desc' || field.defKey == 'type' || field.defKey == 'key'}}`{{=field.defKey}}`{{??}}{{=field.defKey}}{{?}} = #{{{=it.func.camel(field.defKey,false)}}}
- </if>{{?}}
- {{~}}
- </where>
- </select>
- $blankline
- <!--统计总行数-->
- <select id="count" resultType="java.lang.Long">
- select count(1)
- from {{=it.entity.defKey}}
- <where>
- delete_flag = 0
- {{~it.entity.fields:field:index}}
- {{?field.defKey != 'delete_flag'}}<if test={{?field.type == 'Integer'}}"{{=it.func.camel(field.defKey,false)}} != null"{{??}}"{{=it.func.camel(field.defKey,false)}} != null and {{=it.func.camel(field.defKey,false)}} != ''"{{?}}>
- and {{?field.defKey == 'desc' || field.defKey == 'type' || field.defKey == 'key' || field.defKey == 'level'}}`{{=field.defKey}}`{{??}}{{=field.defKey}}{{?}} = #{{{=it.func.camel(field.defKey,false)}}}
- </if>{{?}}
- {{~}}
- </where>
- </select>
- $blankline
- <!--新增数据-->
- <insert id="insert" keyProperty="{{=pkField}}" useGeneratedKeys="true">
- insert into {{=it.entity.defKey}}({{=it.entity.fields.map(function(e,i){
- if (e.defKey == 'desc' || e.defKey == 'type' || e.defKey == 'key' || e.defKey == 'level') return '`'+e.defKey+'`';
- return e.defKey}).join(',')}})
- values ({{=it.entity.fields.map(function(e,i){
- if (e.defKey == 'create_date' || e.defKey == 'update_date') return 'now()';
- if (e.defKey == 'delete_flag') return '0';
- return '#{'+it.func.camel(e.defKey,false)+'}'}).join(',')}})
- </insert>
- $blankline
- <!-- 批量新增数据 -->
- <insert id="insertBatch" keyProperty="{{=pkField}}" useGeneratedKeys="true">
- insert into {{=it.entity.defKey}}({{=it.entity.fields.map(function(e,i){
- if (e.defKey == 'desc' || e.defKey == 'type' || e.defKey == 'key' || e.defKey == 'level') return '`'+e.defKey+'`';
- return e.defKey}).join(',')}})
- values
- <foreach collection="entities" item="entity" separator=",">
- ({{=it.entity.fields.map(function(e,i){
- if (e.defKey == 'create_date' || e.defKey == 'update_date') return 'now()';
- if (e.defKey == 'delete_flag') return '0';
- return '#{entity.'+it.func.camel(e.defKey,false)+'}'}).join(',')}})
- </foreach>
- </insert>
- $blankline
- <!-- 批量新增或按主键更新数据 -->
- <insert id="insertOrUpdateBatch" keyProperty="{{=pkField}}" useGeneratedKeys="true">
- insert into {{=it.entity.defKey}}({{=it.entity.fields.map(function(e,i){
- if (e.defKey == 'desc' || e.defKey == 'type' || e.defKey == 'key' || e.defKey == 'level') return '`'+e.defKey+'`';
- return e.defKey}).join(',')}})
- values
- <foreach collection="entities" item="entity" separator=",">
- ({{=it.entity.fields.map(function(e,i){
- if (e.defKey == 'create_date' || e.defKey == 'update_date') return 'now()';
- if (e.defKey == 'delete_flag') return '0';
- return '#{entity.'+it.func.camel(e.defKey,false)+'}'}).join(',')}})
- </foreach>
- on duplicate key update
- {{=it.entity.fields.filter(function(e){
- if (e.defKey == 'create_date' || e.defKey == 'delete_flag')
- return false;
- return true;
- }).map(function(e,i){
- if (e.defKey == 'update_date')
- return 'update_date=now()';
- if (e.defKey == 'desc' || e.defKey == 'type' || e.defKey == 'key' || e.defKey == 'level')
- return '`' + e.defKey + '`' + '=values('+e.defKey+')' ;
- return e.defKey + '=values('+it.func.camel(e.defKey,false)+')';}).join(',\n\t\t')
- }}
- </insert>
- $blankline
- <!-- 更新数据 -->
- <update id="update">
- update {{=it.entity.defKey}}
- <set>
- {{~it.entity.fields:field:index}}
- {{?field.defKey != 'create_date' && field.defKey != 'update_date'}}<if test={{?field.type == 'Integer'}}"{{=it.func.camel(field.defKey,false)}} != null"{{??}}"{{=it.func.camel(field.defKey,false)}} != null and {{=it.func.camel(field.defKey,false)}} != ''"{{?}}>
- {{?field.defKey == 'desc' || field.defKey == 'type' || field.defKey == 'key' || field.defKey == 'level'}}`{{=field.defKey}}`{{??}}{{=field.defKey}}{{?}} = #{{{=it.func.camel(field.defKey,false)}}},
- </if>{{??}} {{continue;}} {{?}}
- {{~}}
- update_date=now()
- </set>
- where {{=pkField}} = #{{{=pkVarName}}}
- </update>
- $blankline
- <!--通过主键删除-->
- <delete id="deleteById">
- update {{=it.entity.defKey}} set update_date = now(), delete_flag = 1 where {{=pkField}} = #{{{=pkVarName}}}
- </delete>
- <!--↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑模板生成的基础方法↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑-->
- $blankline
- <!--↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓自定义方法↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓-->
- $blankline
- <!--↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑自定义方法↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑-->
- </mapper>
-
- {{ var today=new Date();
- var fullYear=today.getFullYear();
- var month=today.getMonth() + 1;
- var days=today.getDate();
-
- var pkVarName = "id";
- var pkDataType = "String";
- it.entity.fields.forEach(function(field){
- if(field.primaryKey){
- pkVarName = it.func.camel(field.defKey,false);
- pkDataType = field["type"];
- return;
- }
- });
-
- var pkgName = it.entity.env.base.nameSpace;
- var beanClass = it.entity.env.base.codeRoot;
- var beanVarName = beanClass.charAt(0).toLowerCase()+beanClass.slice(1);
- var serviceClass = beanClass+'Service';
- var serviceVarName= beanVarName+'Service';
-
- }}package {{=pkgName}}.beans.entity;
- $blankline
- import java.io.Serializable;
- import java.util.Date;
- $blankline
-
- /**
- * {{=it.entity.defName}};{{=it.entity.comment}}
- * @author : name
- * @date : {{=fullYear}}-{{=month}}-{{=days}}
- */
- public class {{=beanClass}} implements Serializable,Cloneable{
- {{~it.entity.fields:field:index}}
- /** {{=it.func.join(field.defName,field.comment,';')}} */
- private {{=field.type}} {{=it.func.camel(field.defKey,false)}} ;
- {{~}}
- $blankline
-
- {{~it.entity.fields:field:index}}
- /** {{=it.func.join(field.defName,field.comment,';')}} */
- public {{=field.type}} get{{=it.func.camel(field.defKey,true)}}(){
- return this.{{=it.func.camel(field.defKey,false)}};
- }
- /** {{=it.func.join(field.defName,field.comment,';')}} */
- public void set{{=it.func.camel(field.defKey,true)}}({{=field.type}} {{= it.func.camel(field.defKey,false) }}){
- this.{{=it.func.camel(field.defKey,false)}}={{=it.func.camel(field.defKey,false)}};
- }
- {{~}}
- }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。