# 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});
  	
  }


