# template_config

name: entity
file_name: ${context.javaBeanName}.java
content:
  package ${context.packageName}.entity;

  #if(${table.hasDateField})
  import java.util.Date;
  #end
  #if(${table.hasLocalDateField})
  import java.time.LocalDate;
  #end
  #if(${table.hasLocalDateTimeField})
  import java.time.LocalDateTime;
  #end
  #if(${table.hasBigDecimalField})
  import java.math.BigDecimal;
  #end

  #if( "${table.comment}" != "" )
  /**
   * ${table.comment}
   */
  #end
  public class ${context.javaBeanName} {
  #foreach($column in $columns)
  #if( "${column.comment}" != "" )
  	/** ${column.comment} */
  #end
  	private ${column.javaTypeBox} ${column.javaFieldName};
  #end

  #foreach(${column} in ${columns})
  	public void set${column.javaFieldNameUF}(${column.javaTypeBox} ${column.javaFieldName}) {
  		this.${column.javaFieldName} = ${column.javaFieldName};
  	}
  	
  	public ${column.javaTypeBox} get${column.javaFieldNameUF}() {
  		return this.${column.javaFieldName};
  	}
  	
  #end

  	@Override
      public boolean equals(Object o) {
          if (this == o) { return true; }
          if (o == null || getClass() != o.getClass()) {return false;}
          ${context.javaBeanName} that = (${context.javaBeanName}) o;
          return ${pk.javaFieldName}.equals(that.${pk.javaFieldName});
      }

      @Override
      public int hashCode() {
          return java.util.Objects.hash(${pk.javaFieldName});
      }
      
      @Override
      public String toString() {
          return "${context.javaBeanName}{" +
  #foreach(${column} in ${columns})
  		#if($velocityCount == 1)
  		"${column.javaFieldName}=" + ${column.javaFieldName} +
  		#else
  		",${column.javaFieldName}='" + ${column.javaFieldName} + "'" + 
  		#end
  #end
                  '}';
      }
  	
  }


name: mybatis
file_name: ${context.javaBeanName}Mapper.xml
content:
  #set($jq="$")
  <?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="${context.packageName}.mapper.${context.javaBeanName}Mapper">
  	<resultMap id="BaseResultMap" type="${context.packageName}.entity.${context.javaBeanName}">
      #foreach($column in $columns)    
          <result column="${column.columnName}" property="${column.javaFieldName}" jdbcType="${column.mybatisJdbcType}" />
  	#end
  	</resultMap>  
      
      <!-- 表字段 -->
      <sql id="baseColumns">
      #foreach($column in $columns)
      #if($velocityCount > 1),#end t.${column.columnName}
      #end
      </sql> 
      
      <!-- 查询全部 -->
      <select id="listAll" resultMap="BaseResultMap">
  		SELECT         
          	<include refid="baseColumns" />
  		FROM ${table.tableName} t
  	</select>
   
   	<!-- 根据主键获取单条记录 -->
      <select id="getById" resultMap="BaseResultMap" parameterType="${pk.javaTypeBox}">
  		SELECT         
          	<include refid="baseColumns" />
  		FROM ${table.tableName} t
  		WHERE ${pk.columnName} = #{${pk.javaFieldName}}
  	</select>

  	<!-- 插入全部字段 -->
      <insert id="insert" parameterType="${context.packageName}.entity.${context.javaBeanName}"
  		keyProperty="${context.javaPkName}" keyColumn="${context.pkName}" useGeneratedKeys="true"
      >
  	INSERT INTO ${table.tableName}
      <trim prefix="(" suffix=")" suffixOverrides=",">	 
      #foreach($column in $columns) 
      	#if(!${column.isIdentityPk}) 
      	${column.columnName},
      	#end          
      #end
      </trim>
      <trim prefix="VALUES (" suffix=")" suffixOverrides=",">            
      #foreach($column in $columns) 
      	#if(!${column.isIdentityPk})                     
          #{${column.javaFieldName}},                   
      	#end          
      #end
      </trim>
  	</insert>
      
      <!-- 插入不为NULL的字段 -->
      <insert id="insertIgnoreNull" parameterType="${context.packageName}.entity.${context.javaBeanName}"
          keyProperty="${pk.javaFieldName}" keyColumn="${context.pkName}" useGeneratedKeys="true"
          >
          INSERT INTO ${table.tableName}    
          <trim prefix="(" suffix=")" suffixOverrides=",">	 

              #foreach($column in $columns) 
                  #if(!${column.isIdentityPk}) 
                      <if test="${column.javaFieldName} != null">
                     ${column.columnName},
                      </if>
                  #end          
              #end
          </trim>
          <trim prefix="VALUES (" suffix=")" suffixOverrides=",">            
              #foreach($column in $columns) 
                  #if(!${column.isIdentityPk})  
                      <if test="${column.javaFieldName} != null" >
                      #{${column.javaFieldName}},                    
                      </if>
                  #end          
              #end
          </trim>
      </insert>

  	<!-- 更新,更新全部字段 -->
      <update id="update" parameterType="${context.packageName}.entity.${context.javaBeanName}">
      UPDATE ${table.tableName}
       <set>		
       #foreach($column in $columns) 
           #if(!${column.isPk})               
           ${column.columnName}=#{${column.javaFieldName}},        
           #end          
       #end
       </set>	
      WHERE ${pk.columnName} = #{${pk.javaFieldName}}
      </update>  
      
      
      <!-- 更新不为NULL的字段 -->
      <update id="updateIgnoreNull" parameterType="${context.packageName}.entity.${context.javaBeanName}">
      UPDATE ${table.tableName}
      <set>
      	#foreach($column in $columns) 
              #if(!${column.isPk})  
                  <if test="${column.javaFieldName} != null" >
                  ${column.columnName}=#{${column.javaFieldName}},                 
                  </if>
              #end          
          #end
      </set>
      WHERE ${pk.columnName} = #{${pk.javaFieldName}}
      </update>

  		
  	<!-- 根据主键删除记录 -->
  	<delete id="delete" parameterType="${context.packageName}.entity.${context.javaBeanName}">
  		UPDATE ${table.tableName}
  		SET is_deleted=1
  		WHERE ${pk.columnName} = #{${pk.javaFieldName}}
  	</delete>


  </mapper>


name: dao
file_name: ${context.javaBeanName}Mapper.java
content:
  package ${context.packageName}.mapper;

  import java.util.List;
  import org.apache.ibatis.annotations.Mapper;
  import ${context.packageName}.entity.${context.javaBeanName};

  @Mapper
  public interface ${context.javaBeanName}Mapper {

  	/**
       * 查询所有记录
       *
       * @return 返回集合，没有返回空List
       */
  	List<${context.javaBeanName}> listAll();


  	/**
       * 根据主键查询
       *
       * @param id 主键
       * @return 返回记录，没有返回null
       */
  	${context.javaBeanName} getById(${pk.javaTypeBox} ${pk.javaFieldName});
  	
  	/**
       * 新增，插入所有字段
       *
       * @param ${context.javaBeanNameLF} 新增的记录
       * @return 返回影响行数
       */
  	int insert(${context.javaBeanName} ${context.javaBeanNameLF});
  	
  	/**
       * 新增，忽略null字段
       *
       * @param ${context.javaBeanNameLF} 新增的记录
       * @return 返回影响行数
       */
  	int insertIgnoreNull(${context.javaBeanName} ${context.javaBeanNameLF});
  	
  	/**
       * 修改，修改所有字段
       *
       * @param ${context.javaBeanNameLF} 修改的记录
       * @return 返回影响行数
       */
  	int update(${context.javaBeanName} ${context.javaBeanNameLF});
  	
  	/**
       * 修改，忽略null字段
       *
       * @param ${context.javaBeanNameLF} 修改的记录
       * @return 返回影响行数
       */
  	int updateIgnoreNull(${context.javaBeanName} ${context.javaBeanNameLF});
  	
  	/**
       * 删除记录
       *
       * @param ${context.javaBeanNameLF} 待删除的记录
       * @return 返回影响行数
       */
  	int delete(${context.javaBeanName} ${context.javaBeanNameLF});
  	
  }


name: vue
file_name: ${context.javaBeanNameLF}.vue
content:
  #set($jq="$")
  <template>
    <div class="app-container">
      <el-form :inline="true" :model="searchFormData" class="demo-form-inline" size="mini">
        #foreach($column in $columns)
        	#if(!${column.isPk})
          <el-form-item
        	#if( "${column.comment}" != "" )
          label="${column.comment}"
          #else
          label="${column.javaFieldName}"
          #end
        >
        	<el-input v-model="searchFormData.${column.javaFieldName}" :clearable="true" placeholder="${column.comment}" style="width: 250px;" />
        </el-form-item>
          #end
        #end
        <el-form-item>
          <el-button type="primary" icon="el-icon-search" @click="loadTable">查询</el-button>
        </el-form-item>
      </el-form>
      <el-button type="primary" size="mini" icon="el-icon-plus" style="margin-bottom: 10px;" @click="onAdd">新增</el-button>
      <el-table
        :data="pageInfo.rows"
        border
        highlight-current-row
      >
      #foreach($column in $columns)
        <el-table-column
          prop="${column.javaFieldName}"
          #if( "${column.comment}" != "" )
          label="${column.comment}"
          #else
          label="${column.javaFieldName}"
          #end
        />
       #end
       <el-table-column>
          <template slot-scope="scope">
            <el-button type="text" size="mini" @click="onTableUpdate(scope.row)">修改</el-button>
            <el-button type="text" size="mini" @click="onTableDelete(scope.row)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
      <el-pagination
        background
        style="margin-top: 5px"
        :current-page="searchFormData.pageIndex"
        :page-size="searchFormData.pageSize"
        :page-sizes="[5, 10, 20, 40]"
        :total="pageInfo.total"
        layout="total, sizes, prev, pager, next"
        @size-change="onSizeChange"
        @current-change="onPageIndexChange"
      />
      <!--dialog-->
      <el-dialog
        :title="dialogTitle"
        :visible.sync="dialogVisible"
        :close-on-click-modal="false"
        @close="resetForm('dialogForm')"
      >
        <el-form
          ref="dialogForm"
          :rules="dialogFormRules"
          :model="dialogFormData"
          label-width="120px"
          size="mini"
        >
        #foreach($column in $columns)
        	#if(!${column.isPk})
          <el-form-item
        	prop="${column.javaFieldName}"
          #if( "${column.comment}" != "" )
          label="${column.comment}"
          #else
          label="${column.javaFieldName}"
          #end
         >
            <el-input v-model="dialogFormData.${column.javaFieldName}" />
          </el-form-item>
          #end      
        #end
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button @click="dialogVisible = false">取 消</el-button>
          <el-button type="primary" @click="onDialogSave">保 存</el-button>
        </div>
      </el-dialog>
    </div>
  </template>

  <script>
  import axios from 'axios'
  // 创建axios实例
  const client = axios.create({
    baseURL: process.env.VUE_APP_BASE_API, // api 的 base_url
    timeout: 60000 // 请求超时时间,60秒
  })
  export default {
    data() {    
      return {
        searchFormData: {
          #foreach($column in $columns)
          ${column.javaFieldName}: '',
          #end
          pageIndex: 1,
          pageSize: 10
        },
        pageInfo: {
          rows: [],
          total: 0
        },
        dialogVisible: false,
        dialogTitle: '',
        dialogFormData: {
          #foreach($column in $columns)
          #if($velocityCount > 1),#end ${column.javaFieldName}: ''
          #end
        },
        dialogFormRules: {        
          #foreach($column in $columns)
          #if($velocityCount > 1),#end ${column.javaFieldName}: [
            { required: true, message: '不能为空', trigger: 'blur' }
          ]
          #end
        }
      }
    },
    created() {
      this.loadTable()
    },
    methods: {
      loadTable() {
        this.post('/${context.javaBeanNameLF}/list', this.searchFormData, resp => {
          this.pageInfo.rows = resp.data
        })
      },
      onTableUpdate(row) {
        this.dialogTitle = '修改'
        this.dialogVisible = true
        this.${jq}nextTick(() => {
          Object.assign(this.dialogFormData, row)
        })
      },
      onTableDelete(row) {
        this.confirm(`确认要删除该记录吗？`, function(done) {
          const data = {
            ${pk.javaFieldName}: row.${pk.javaFieldName}
          }
          this.post('/${context.javaBeanNameLF}/del', data, () => {
            done()
            this.tip('删除成功')
            this.loadTable()
          })
        })
      },
      onDialogSave() {
        this.${jq}refs.dialogForm.validate((valid) => {
          if (valid) {
            const uri = this.dialogFormData.${pk.javaFieldName} ? '/${context.javaBeanNameLF}/update' : '/${context.javaBeanNameLF}/add'
            this.post(uri, this.dialogFormData, () => {
              this.dialogVisible = false
              this.loadTable()
            })
          }
        })
      },
      onSizeChange(size) {
        this.searchFormData.pageSize = size
        this.loadTable()
      },
      onAdd() {
        this.dialogTitle = '新增'
        this.dialogVisible = true
        this.dialogFormData.${pk.javaFieldName} = 0
      },
      onPageIndexChange(pageIndex) {
        this.searchFormData.pageIndex = pageIndex
        this.loadTable()
      },
      /**
       * 请求接口
       * @param uri uri
       * @param data 请求数据
       * @param callback 成功时回调
       * @param errorCallback 错误时回调
       */
      post(uri, data, callback, errorCallback) {
        const that = this
        client.post(uri, data).then(function(response) {
          const resp = response.data
          const code = resp.code
          if (code === '0') { // 成功
            callback && callback.call(that, resp)
          } else {
            that.${jq}message.error(resp.msg)
          }
        }).catch(function(error) {
          console.error(error)
          errorCallback && errorCallback(error)
          that.${jq}message.error(error.message)
        })
      }
    }
  }
  </script>


