How do I use Mybatis Generator

  1. Beginning Buzz

Mybatis Generator is the official code generator provided by Mybatis.I've used two versions of different styles: targetRuntime="MyBatis3DynamicSql", and targetRuntime="MyBatis3", which is just about the MyBatis3 type.

2. Native Insufficiency

1. No Paging

2. Data queries cannot specify that all returned columns can only be returned (select *)

3. There is no non-null judgment, andXXXEquals(xxx) runs null pointer judgment when XXX is null, here I think XXX is null and skipped automatically, the condition is not valid

4. Multiple File Generations Overlapping Version 1.3.7 seems to solve the problem

5. Cannot include or exclude return columns like 2

6. Can't group

7. Form operation is slightly complicated SQL is not easy to assemble

3. How to Solve

1. No Paging

PageHelper takes it too seriously, defines the BaseExampe class and gives paging properties, redefines the XXXMapper.xml file generation logic, and adds a limit parameter after selectByExampe

2. Data queries cannot specify that all returned columns can only be returned (select *)

Similarly, add column collections in BaseExample, pass column collections to XXXMapper.xml generation logic

3. There is no non-null judgment, andXXXEquals(xxx) runs null pointer judgment when XXX is null, here I think XXX is null and skipped automatically, the condition is not valid

Redefine Example's production logic, add the createCriteriaInternal(boolean skillNull) method, and add Condition skips when skillNull has a true value of null

4. Multiple File Generations Overlapping Version 1.3.7 seems to solve the problem

Use version 1.3.7 or above

5. Cannot include or exclude return columns like 2

BaseExample adds setIncludeColumns(String... columns) or setExcludeColumns(String... columns) methods

6. Can't group

BaseExample defines grouping information

7. Form operation is slightly complicated SQL is not easy to assemble

Modify XXXExample generation logic to add the addCriterion(String condition) method

4. Other issues

1. Why not use PageHelper paging

PageHelper is too full, too much thought, and too heavy for a paging function.PageHelper primarily blocks the operation of querying counts. Developers just look up list s, and counts are automatically brought in.

It doesn't really take a lot of time for developers to get counts senselessly.Simply bring in the automatic count query logic when querying the list

    /**
     * Paging Query
     *
     * @param baseExample Query Example
     * @param baseMapper  Query Mapper
     * @param <Model>     Return Entity
     * @param <Example>   Query Entities
     */
    public static <PK, Model extends BaseModel<PK>, Example extends BaseExample> Page<Model> selectByPage(Example baseExample, BaseMapper<Model, Example, PK> baseMapper) {
        checkCurrentPageAndPageSize(baseExample);
        BiFunction<Example, BaseMapper<Model, Example, PK>, Page<Model>> biFunction = (example, mapper) -> {
            Long count = mapper.countByExampleSelective(example);
            if (count == 0) {
                return emptyPage(example);
            }
            List<Model> list;
            list = mapper.selectByExampleSelective(example);
            Page<Model> page = new Page<>();
            page.setCurrentPage(example.getCurrentPage());
            page.setPageSize(example.getPageSize());
            page.setData(list);
            page.setTotalRow(count);
            return page;
        };
        return biFunction.apply(baseExample, baseMapper);
    }

2. How to customize Example, Mapper, XMl generation logic There are many online tutorials, which are not explained in detail here.Instance DEMO source pasted here for reference

  1. BaseExample
@Data
public abstract class BaseExample {

    /**
     * Current Page
     */
    private int currentPage;
    /**
     * pagesize
     */
    private int pageSize;
    /**
     * Paging Start Position
     */
    private Integer start;
    /**
     * Query Return Column
     */
    private List<String> columns;
    /**
     * Grouping Columns
     */
    private List<String> groups;

    /**
     * Grouping Conditions
     */
    private String having;

    /**
     * Calculate Paging Start Position
     */
    private void configStart() {
        if (currentPage == 0 || pageSize == 0) {
            return;
        }
        int result = currentPage - 1 > 0 ? currentPage - 1 : 0;
        this.start = result * pageSize;
    }


    public abstract List<String> getColumnList();

    public void setIncludeColumns(String... columns) {
        if (Objects.nonNull(columns)) {
            List<String> collect = Arrays.stream(columns).filter(this::isNotEmpty).collect(Collectors.toList());
            setColumns(collect);
        }
    }

    public void setOrderByClause(String orderByClause) {

    }


    private boolean equalsDate(String field) {
        if (StringUtils.equals(ModelConst.CREATE_DATE, field)) {
            return true;
        }
        return StringUtils.equals(ModelConst.EDIT_DATE, field);
    }


    private boolean equalsId(String field) {
        return StringUtils.equals(ModelConst.ID, field);
    }


    private boolean equalsIdAndDate(String field) {
        if (StringUtils.equals(ModelConst.CREATE_DATE, field)) {
            return true;
        }
        if (StringUtils.equals(ModelConst.EDIT_DATE, field)) {
            return true;
        }
        return StringUtils.equals(ModelConst.ID, field);
    }

    public void setExcludeColumns(String... excludeCols) {
        List<String> columnList = getColumnList();
        CollectionUtils.remove(columnList, excludeCols);
        setColumns(columnList);
    }

    public void excludeDateColumn() {
        excludeDateColumn(new String[]{});
    }

    public void excludeDateColumn(String... excludeCols) {
        List<String> columnList = getColumnList();
        List<String> collect = columnList.stream().filter(item -> !equalsDate(item)).collect(Collectors.toList());
        CollectionUtils.remove(collect, excludeCols);
        setColumns(collect);
    }

    public void excludeIdColumn() {
        excludeIdColumn(new String[]{});
    }

    public void excludeIdColumn(String... excludeCols) {
        List<String> columnList = getColumnList();
        List<String> collect = columnList.stream().filter(item -> !equalsId(item)).collect(Collectors.toList());
        CollectionUtils.remove(collect, excludeCols);
        setColumns(collect);
    }


    public void excludeIdAndDateColumn() {
        excludeIdAndDateColumn(new String[]{});
    }

    public void excludeIdAndDateColumn(String... excludeCols) {
        List<String> columnList = getColumnList();
        List<String> collect = columnList.stream().filter(item -> !equalsIdAndDate(item)).collect(Collectors.toList());
        CollectionUtils.remove(collect, excludeCols);
        setColumns(collect);
    }

    public void setGroups(String... groups) {
        if (Objects.nonNull(groups)) {
            List<String> collect = Arrays.stream(groups).collect(Collectors.toList());
            setGroups(collect);
        }
    }


    /**
     * Calculate start position when getting
     */
    public Integer getStart() {
        configStart();
        return start;
    }

    public List<String> getColumns() {
        return columns;
    }

    public void setColumns(List<String> columns) {
        this.columns = columns;
    }

    public List<String> getGroups() {
        return groups;
    }

    public void setGroups(List<String> groups) {
        this.groups = groups;
    }

    private boolean isNotEmpty(String string) {
        if (Objects.isNull(string)) {
            return false;
        }
        return string.length() > 0;
    }

  1. BaseMapper.java

public interface BaseMapper<Model extends BaseModel<T>, ModelExample extends BaseExample, T> {

    /**
     * Total number of queries based on Example
     *
     * @param example Example
     * @return Total number of bars
     */
    long countByExampleSelective(ModelExample example);

    /**
     * Delete records based on Example
     *
     * @param example Example
     * @return Number of records deleted
     */
    int deleteByExample(ModelExample example);

    /**
     * Delete records based on primary key
     *
     * @param id Primary key
     * @return Number of records deleted
     */
    int deleteByPrimaryKey(T id);

    /**
     * Add Entities
     *
     * @param record entity
     * @return Number of records added
     */
    int insert(Model record);

    /**
     * Add Entities (Exclusion Control)
     *
     * @param record entity
     * @return Number of records added
     */
    int insertSelective(Model record);

    /**
     * Query by Example
     *
     * @param example Example
     * @return Set of eligible entities
     */
    List<Model> selectByExampleSelective(ModelExample example);

    /**
     * Return records based on primary key
     *
     * @param id      Primary key
     * @param columns Entity Columns
     * @return entity
     */
    Model selectByPrimaryKey(@Param("id") T id, @Param("columns") List<String> columns);

    /**
     * Modify entity according to Example (exclude nulls)
     *
     * @param record  entity
     * @param example Example
     * @return Number of records modified
     */
    int updateByExampleSelective(@Param("record") Model record, @Param("example") ModelExample example);

    /**
     * Modify entity according to Example
     *
     * @param record  entity
     * @param example Example
     * @return Number of records modified
     */
    int updateByExample(@Param("record") Model record, @Param("example") ModelExample example);

    /**
     * Modify based on entity primary key (exclude nulls)
     *
     * @param record entity
     * @return Number of records modified
     */
    int updateByPrimaryKeySelective(Model record);

    /**
     * Modify based on entity primary key
     *
     * @param record entity
     * @return Number of records modified
     */
    int updateByPrimaryKey(Model record);

    /**
     * Bulk Insert (exclude empty)
     *
     * @param records Entity Collection
     * @return Number of records added
     */
    int insertBatchSelective(List<Model> records);

    /**
     * Bulk Modification (Exclude Empty)
     *
     * @param records Entity Collection
     * @return Number of records modified
     */
    int updateBatchByPrimaryKeySelective(List<Model> records);

}
  1. XXXMapper.xml
<?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="com.xxx.mapper.UserMapper">
  <resultMap id="BaseResultMap" type="com.xxx.dal.model.User">
      <id column="id" jdbcType="INTEGER" property="id"/>
      <result column="user_name" jdbcType="VARCHAR" property="userName"/>
      <result column="user_type" jdbcType="INTEGER" property="userType"/>
      <result column="user_code" jdbcType="VARCHAR" property="userCode"/>
      <result column="gmt_create" jdbcType="TIMESTAMP" property="gmtCreate"/>
      <result column="gmt_modified" jdbcType="TIMESTAMP" property="gmtModified"/>
      <result column="creator" jdbcType="VARCHAR" property="creator"/>
      <result column="editor" jdbcType="VARCHAR" property="editor"/>
      <result column="gs_id" jdbcType="INTEGER" property="gsId"/>
      <result column="nick_name" jdbcType="VARCHAR" property="nickName"/>
      <result column="status" jdbcType="INTEGER" property="status"/>
      <result column="sca_id" jdbcType="INTEGER" property="scaId"/>
      <result column="sca_name" jdbcType="VARCHAR" property="scaName"/>
      <result column="job_number_type" jdbcType="INTEGER" property="jobNumberType"/>
      <result column="settle_object_id" jdbcType="BIGINT" property="settleObjectId"/>
      <result column="settle_object_name" jdbcType="VARCHAR" property="settleObjectName"/>
      <result column="open_account_flag" jdbcType="INTEGER" property="openAccountFlag"/>
      <result column="ic_flag" jdbcType="INTEGER" property="icFlag"/>
      <result column="payment_mode_id" jdbcType="INTEGER" property="paymentModeId"/>
      <result column="payment_cycle_id" jdbcType="INTEGER" property="paymentCycleId"/>
  </resultMap>
  <sql id="Example_Where_Clause">
    <where>
      <foreach collection="oredCriteria" item="criteria" separator="or">
        <if test="criteria.valid">
          <trim prefix="(" prefixOverrides="and" suffix=")">
            <foreach collection="criteria.criteria" item="criterion">
              <choose>
                <when test="criterion.noValue">
                  and ${criterion.condition}
                </when>
                <when test="criterion.singleValue">
                  and ${criterion.condition} #{criterion.value}
                </when>
                <when test="criterion.betweenValue">
                  and ${criterion.condition} #{criterion.value} and #{criterion.secondValue}
                </when>
                <when test="criterion.listValue">
                  and ${criterion.condition}
                  <foreach close=")" collection="criterion.value" item="listItem" open="(" separator=",">
                    #{listItem}
                  </foreach>
                </when>
              </choose>
            </foreach>
          </trim>
        </if>
      </foreach>
    </where>
  </sql>
  <sql id="Update_By_Example_Where_Clause">
    <where>
      <foreach collection="example.oredCriteria" item="criteria" separator="or">
        <if test="criteria.valid">
          <trim prefix="(" prefixOverrides="and" suffix=")">
            <foreach collection="criteria.criteria" item="criterion">
              <choose>
                <when test="criterion.noValue">
                  and ${criterion.condition}
                </when>
                <when test="criterion.singleValue">
                  and ${criterion.condition} #{criterion.value}
                </when>
                <when test="criterion.betweenValue">
                  and ${criterion.condition} #{criterion.value} and #{criterion.secondValue}
                </when>
                <when test="criterion.listValue">
                  and ${criterion.condition}
                  <foreach close=")" collection="criterion.value" item="listItem" open="(" separator=",">
                    #{listItem}
                  </foreach>
                </when>
              </choose>
            </foreach>
          </trim>
        </if>
      </foreach>
    </where>
  </sql>
  <sql id="Base_Column_List">
    id, user_name, user_type, user_code, gmt_create, gmt_modified, creator, editor, gs_id, 
    nick_name, status, sca_id, sca_name, job_number_type, settle_object_id, settle_object_name, 
    open_account_flag, ic_flag, payment_mode_id, payment_cycle_id
  </sql>
  <delete id="deleteByPrimaryKey" parameterType="java.lang.Integer">
    delete from yh_user
    where id = #{id,jdbcType=INTEGER}
  </delete>
  <delete id="deleteByExample" parameterType="com.xxx.example.UserExample">
    delete from yh_user
    <if test="_parameter != null">
      <include refid="Example_Where_Clause" />
    </if>
  </delete>
  <insert id="insert" parameterType="com.xxx.dal.model.User">
    insert into yh_user (id, user_name, user_type, 
      user_code, gmt_create, gmt_modified, 
      creator, editor, gs_id, 
      nick_name, status, sca_id, 
      sca_name, job_number_type, settle_object_id, 
      settle_object_name, open_account_flag, ic_flag, 
      payment_mode_id, payment_cycle_id)
    values (#{id,jdbcType=INTEGER}, #{userName,jdbcType=VARCHAR}, #{userType,jdbcType=INTEGER}, 
      #{userCode,jdbcType=VARCHAR}, #{gmtCreate,jdbcType=TIMESTAMP}, #{gmtModified,jdbcType=TIMESTAMP}, 
      #{creator,jdbcType=VARCHAR}, #{editor,jdbcType=VARCHAR}, #{gsId,jdbcType=INTEGER}, 
      #{nickName,jdbcType=VARCHAR}, #{status,jdbcType=INTEGER}, #{scaId,jdbcType=INTEGER}, 
      #{scaName,jdbcType=VARCHAR}, #{jobNumberType,jdbcType=INTEGER}, #{settleObjectId,jdbcType=BIGINT}, 
      #{settleObjectName,jdbcType=VARCHAR}, #{openAccountFlag,jdbcType=INTEGER}, #{icFlag,jdbcType=INTEGER}, 
      #{paymentModeId,jdbcType=INTEGER}, #{paymentCycleId,jdbcType=INTEGER})
  </insert>
  <insert id="insertSelective" parameterType="com.xxx.dal.model.User">
    insert into yh_user
    <trim prefix="(" suffix=")" suffixOverrides=",">
      <if test="id != null">
        id,
      </if>
      <if test="userName != null">
        user_name,
      </if>
      <if test="userType != null">
        user_type,
      </if>
      <if test="userCode != null">
        user_code,
      </if>
      <if test="gmtCreate != null">
        gmt_create,
      </if>
      <if test="gmtModified != null">
        gmt_modified,
      </if>
      <if test="creator != null">
        creator,
      </if>
      <if test="editor != null">
        editor,
      </if>
      <if test="gsId != null">
        gs_id,
      </if>
      <if test="nickName != null">
        nick_name,
      </if>
      <if test="status != null">
        status,
      </if>
      <if test="scaId != null">
        sca_id,
      </if>
      <if test="scaName != null">
        sca_name,
      </if>
      <if test="jobNumberType != null">
        job_number_type,
      </if>
      <if test="settleObjectId != null">
        settle_object_id,
      </if>
      <if test="settleObjectName != null">
        settle_object_name,
      </if>
      <if test="openAccountFlag != null">
        open_account_flag,
      </if>
      <if test="icFlag != null">
        ic_flag,
      </if>
      <if test="paymentModeId != null">
        payment_mode_id,
      </if>
      <if test="paymentCycleId != null">
        payment_cycle_id,
      </if>
    </trim>
    <trim prefix="values (" suffix=")" suffixOverrides=",">
      <if test="id != null">
        #{id,jdbcType=INTEGER},
      </if>
      <if test="userName != null">
        #{userName,jdbcType=VARCHAR},
      </if>
      <if test="userType != null">
        #{userType,jdbcType=INTEGER},
      </if>
      <if test="userCode != null">
        #{userCode,jdbcType=VARCHAR},
      </if>
      <if test="gmtCreate != null">
        #{gmtCreate,jdbcType=TIMESTAMP},
      </if>
      <if test="gmtModified != null">
        #{gmtModified,jdbcType=TIMESTAMP},
      </if>
      <if test="creator != null">
        #{creator,jdbcType=VARCHAR},
      </if>
      <if test="editor != null">
        #{editor,jdbcType=VARCHAR},
      </if>
      <if test="gsId != null">
        #{gsId,jdbcType=INTEGER},
      </if>
      <if test="nickName != null">
        #{nickName,jdbcType=VARCHAR},
      </if>
      <if test="status != null">
        #{status,jdbcType=INTEGER},
      </if>
      <if test="scaId != null">
        #{scaId,jdbcType=INTEGER},
      </if>
      <if test="scaName != null">
        #{scaName,jdbcType=VARCHAR},
      </if>
      <if test="jobNumberType != null">
        #{jobNumberType,jdbcType=INTEGER},
      </if>
      <if test="settleObjectId != null">
        #{settleObjectId,jdbcType=BIGINT},
      </if>
      <if test="settleObjectName != null">
        #{settleObjectName,jdbcType=VARCHAR},
      </if>
      <if test="openAccountFlag != null">
        #{openAccountFlag,jdbcType=INTEGER},
      </if>
      <if test="icFlag != null">
        #{icFlag,jdbcType=INTEGER},
      </if>
      <if test="paymentModeId != null">
        #{paymentModeId,jdbcType=INTEGER},
      </if>
      <if test="paymentCycleId != null">
        #{paymentCycleId,jdbcType=INTEGER},
      </if>
    </trim>
  </insert>
  <update id="updateByExampleSelective" parameterType="map">
    update yh_user
    <set>
      <if test="record.id != null">
        id = #{record.id,jdbcType=INTEGER},
      </if>
      <if test="record.userName != null">
        user_name = #{record.userName,jdbcType=VARCHAR},
      </if>
      <if test="record.userType != null">
        user_type = #{record.userType,jdbcType=INTEGER},
      </if>
      <if test="record.userCode != null">
        user_code = #{record.userCode,jdbcType=VARCHAR},
      </if>
      <if test="record.gmtCreate != null">
        gmt_create = #{record.gmtCreate,jdbcType=TIMESTAMP},
      </if>
      <if test="record.gmtModified != null">
        gmt_modified = #{record.gmtModified,jdbcType=TIMESTAMP},
      </if>
      <if test="record.creator != null">
        creator = #{record.creator,jdbcType=VARCHAR},
      </if>
      <if test="record.editor != null">
        editor = #{record.editor,jdbcType=VARCHAR},
      </if>
      <if test="record.gsId != null">
        gs_id = #{record.gsId,jdbcType=INTEGER},
      </if>
      <if test="record.nickName != null">
        nick_name = #{record.nickName,jdbcType=VARCHAR},
      </if>
      <if test="record.status != null">
        status = #{record.status,jdbcType=INTEGER},
      </if>
      <if test="record.scaId != null">
        sca_id = #{record.scaId,jdbcType=INTEGER},
      </if>
      <if test="record.scaName != null">
        sca_name = #{record.scaName,jdbcType=VARCHAR},
      </if>
      <if test="record.jobNumberType != null">
        job_number_type = #{record.jobNumberType,jdbcType=INTEGER},
      </if>
      <if test="record.settleObjectId != null">
        settle_object_id = #{record.settleObjectId,jdbcType=BIGINT},
      </if>
      <if test="record.settleObjectName != null">
        settle_object_name = #{record.settleObjectName,jdbcType=VARCHAR},
      </if>
      <if test="record.openAccountFlag != null">
        open_account_flag = #{record.openAccountFlag,jdbcType=INTEGER},
      </if>
      <if test="record.icFlag != null">
        ic_flag = #{record.icFlag,jdbcType=INTEGER},
      </if>
      <if test="record.paymentModeId != null">
        payment_mode_id = #{record.paymentModeId,jdbcType=INTEGER},
      </if>
      <if test="record.paymentCycleId != null">
        payment_cycle_id = #{record.paymentCycleId,jdbcType=INTEGER},
      </if>
    </set>
    <if test="_parameter != null">
      <include refid="Update_By_Example_Where_Clause" />
    </if>
  </update>
  <update id="updateByExample" parameterType="map">
    update yh_user
    set id = #{record.id,jdbcType=INTEGER},
      user_name = #{record.userName,jdbcType=VARCHAR},
      user_type = #{record.userType,jdbcType=INTEGER},
      user_code = #{record.userCode,jdbcType=VARCHAR},
      gmt_create = #{record.gmtCreate,jdbcType=TIMESTAMP},
      gmt_modified = #{record.gmtModified,jdbcType=TIMESTAMP},
      creator = #{record.creator,jdbcType=VARCHAR},
      editor = #{record.editor,jdbcType=VARCHAR},
      gs_id = #{record.gsId,jdbcType=INTEGER},
      nick_name = #{record.nickName,jdbcType=VARCHAR},
      status = #{record.status,jdbcType=INTEGER},
      sca_id = #{record.scaId,jdbcType=INTEGER},
      sca_name = #{record.scaName,jdbcType=VARCHAR},
      job_number_type = #{record.jobNumberType,jdbcType=INTEGER},
      settle_object_id = #{record.settleObjectId,jdbcType=BIGINT},
      settle_object_name = #{record.settleObjectName,jdbcType=VARCHAR},
      open_account_flag = #{record.openAccountFlag,jdbcType=INTEGER},
      ic_flag = #{record.icFlag,jdbcType=INTEGER},
      payment_mode_id = #{record.paymentModeId,jdbcType=INTEGER},
      payment_cycle_id = #{record.paymentCycleId,jdbcType=INTEGER}
    <if test="_parameter != null">
      <include refid="Update_By_Example_Where_Clause" />
    </if>
  </update>
  <update id="updateByPrimaryKeySelective" parameterType="com.xxx.dal.model.User">
    update yh_user
    <set>
      <if test="userName != null">
        user_name = #{userName,jdbcType=VARCHAR},
      </if>
      <if test="userType != null">
        user_type = #{userType,jdbcType=INTEGER},
      </if>
      <if test="userCode != null">
        user_code = #{userCode,jdbcType=VARCHAR},
      </if>
      <if test="gmtCreate != null">
        gmt_create = #{gmtCreate,jdbcType=TIMESTAMP},
      </if>
      <if test="gmtModified != null">
        gmt_modified = #{gmtModified,jdbcType=TIMESTAMP},
      </if>
      <if test="creator != null">
        creator = #{creator,jdbcType=VARCHAR},
      </if>
      <if test="editor != null">
        editor = #{editor,jdbcType=VARCHAR},
      </if>
      <if test="gsId != null">
        gs_id = #{gsId,jdbcType=INTEGER},
      </if>
      <if test="nickName != null">
        nick_name = #{nickName,jdbcType=VARCHAR},
      </if>
      <if test="status != null">
        status = #{status,jdbcType=INTEGER},
      </if>
      <if test="scaId != null">
        sca_id = #{scaId,jdbcType=INTEGER},
      </if>
      <if test="scaName != null">
        sca_name = #{scaName,jdbcType=VARCHAR},
      </if>
      <if test="jobNumberType != null">
        job_number_type = #{jobNumberType,jdbcType=INTEGER},
      </if>
      <if test="settleObjectId != null">
        settle_object_id = #{settleObjectId,jdbcType=BIGINT},
      </if>
      <if test="settleObjectName != null">
        settle_object_name = #{settleObjectName,jdbcType=VARCHAR},
      </if>
      <if test="openAccountFlag != null">
        open_account_flag = #{openAccountFlag,jdbcType=INTEGER},
      </if>
      <if test="icFlag != null">
        ic_flag = #{icFlag,jdbcType=INTEGER},
      </if>
      <if test="paymentModeId != null">
        payment_mode_id = #{paymentModeId,jdbcType=INTEGER},
      </if>
      <if test="paymentCycleId != null">
        payment_cycle_id = #{paymentCycleId,jdbcType=INTEGER},
      </if>
    </set>
    where id = #{id,jdbcType=INTEGER}
  </update>
  <update id="updateByPrimaryKey" parameterType="com.xxx.dal.model.User">
    update yh_user
    set user_name = #{userName,jdbcType=VARCHAR},
      user_type = #{userType,jdbcType=INTEGER},
      user_code = #{userCode,jdbcType=VARCHAR},
      gmt_create = #{gmtCreate,jdbcType=TIMESTAMP},
      gmt_modified = #{gmtModified,jdbcType=TIMESTAMP},
      creator = #{creator,jdbcType=VARCHAR},
      editor = #{editor,jdbcType=VARCHAR},
      gs_id = #{gsId,jdbcType=INTEGER},
      nick_name = #{nickName,jdbcType=VARCHAR},
      status = #{status,jdbcType=INTEGER},
      sca_id = #{scaId,jdbcType=INTEGER},
      sca_name = #{scaName,jdbcType=VARCHAR},
      job_number_type = #{jobNumberType,jdbcType=INTEGER},
      settle_object_id = #{settleObjectId,jdbcType=BIGINT},
      settle_object_name = #{settleObjectName,jdbcType=VARCHAR},
      open_account_flag = #{openAccountFlag,jdbcType=INTEGER},
      ic_flag = #{icFlag,jdbcType=INTEGER},
      payment_mode_id = #{paymentModeId,jdbcType=INTEGER},
      payment_cycle_id = #{paymentCycleId,jdbcType=INTEGER}
    where id = #{id,jdbcType=INTEGER}
  </update>
    <select id="selectByExampleSelective" parameterType="com.xxx.example.UserExample"
            resultMap="BaseResultMap">
    select
    <choose>
      <when test="columns==null or columns.size()==0">
        <include refid="Base_Column_List"/>
      </when>
      <otherwise>
        <foreach collection="columns" item="column" separator=",">
          ${column}
        </foreach>
      </otherwise>
    </choose>
    from yh_user
    <if test="_parameter != null">
        <include refid="Example_Where_Clause"/>
    </if>
    <if test="groups !=null and groups.size()>0">
        group by
      <foreach collection="groups" index="index" item="item" separator=",">
        ${item}
      </foreach>
      <if test="having != null">
        having ${having}
      </if>
    </if>
    <if test="orderByClause != null">
      order by ${orderByClause}
    </if>
    <if test="start != null and start > -1">
      limit ${start},${pageSize}
    </if>
  </select>
    <select id="countByExampleSelective" parameterType="com.xxx.example.UserExample"
            resultType="java.lang.Long">
    select count(*) from ( select
    <choose>
      <when test="columns==null or columns.size()==0">
        <include refid="Base_Column_List"/>
      </when>
      <otherwise>
        <foreach collection="columns" item="column" separator=",">
          ${column}
        </foreach>
      </otherwise>
    </choose>
    from yh_user
    <if test="_parameter != null">
        <include refid="Example_Where_Clause"/>
    </if>
    <if test="groups !=null and groups.size()>0">
        group by
      <foreach collection="groups" index="index" item="item" separator=",">
        ${item}
      </foreach>
      <if test="having != null">
        having ${having}
      </if>
    </if>
    ) Temp
  </select>
  <insert id="insertBatchSelective" parameterType="java.util.List">
    insert into yh_user
    <trim prefix="(" suffix=")" suffixOverrides=",">
      <if test="list[0].id!=null">
        id,
      </if>
      <if test="list[0].userName!=null">
        user_name,
      </if>
      <if test="list[0].userType!=null">
        user_type,
      </if>
      <if test="list[0].userCode!=null">
        user_code,
      </if>
      <if test="list[0].gmtCreate!=null">
        gmt_create,
      </if>
      <if test="list[0].gmtModified!=null">
        gmt_modified,
      </if>
      <if test="list[0].creator!=null">
        creator,
      </if>
      <if test="list[0].editor!=null">
        editor,
      </if>
      <if test="list[0].gsId!=null">
        gs_id,
      </if>
      <if test="list[0].nickName!=null">
        nick_name,
      </if>
      <if test="list[0].status!=null">
        status,
      </if>
      <if test="list[0].scaId!=null">
        sca_id,
      </if>
      <if test="list[0].scaName!=null">
        sca_name,
      </if>
      <if test="list[0].jobNumberType!=null">
        job_number_type,
      </if>
      <if test="list[0].settleObjectId!=null">
        settle_object_id,
      </if>
      <if test="list[0].settleObjectName!=null">
        settle_object_name,
      </if>
      <if test="list[0].openAccountFlag!=null">
        open_account_flag,
      </if>
      <if test="list[0].icFlag!=null">
        ic_flag,
      </if>
      <if test="list[0].paymentModeId!=null">
        payment_mode_id,
      </if>
      <if test="list[0].paymentCycleId!=null">
        payment_cycle_id,
      </if>
    </trim>
      values
    <foreach collection="list" index="index" item="item" separator=",">
      <trim prefix=" (" suffix=")" suffixOverrides=",">
        <if test="item.id!=null">
          #{item.id,jdbcType=INTEGER},
        </if>
        <if test="item.userName!=null">
          #{item.userName,jdbcType=VARCHAR},
        </if>
        <if test="item.userType!=null">
          #{item.userType,jdbcType=INTEGER},
        </if>
        <if test="item.userCode!=null">
          #{item.userCode,jdbcType=VARCHAR},
        </if>
        <if test="item.gmtCreate!=null">
          #{item.gmtCreate,jdbcType=TIMESTAMP},
        </if>
        <if test="item.gmtModified!=null">
          #{item.gmtModified,jdbcType=TIMESTAMP},
        </if>
        <if test="item.creator!=null">
          #{item.creator,jdbcType=VARCHAR},
        </if>
        <if test="item.editor!=null">
          #{item.editor,jdbcType=VARCHAR},
        </if>
        <if test="item.gsId!=null">
          #{item.gsId,jdbcType=INTEGER},
        </if>
        <if test="item.nickName!=null">
          #{item.nickName,jdbcType=VARCHAR},
        </if>
        <if test="item.status!=null">
          #{item.status,jdbcType=INTEGER},
        </if>
        <if test="item.scaId!=null">
          #{item.scaId,jdbcType=INTEGER},
        </if>
        <if test="item.scaName!=null">
          #{item.scaName,jdbcType=VARCHAR},
        </if>
        <if test="item.jobNumberType!=null">
          #{item.jobNumberType,jdbcType=INTEGER},
        </if>
        <if test="item.settleObjectId!=null">
          #{item.settleObjectId,jdbcType=BIGINT},
        </if>
        <if test="item.settleObjectName!=null">
          #{item.settleObjectName,jdbcType=VARCHAR},
        </if>
        <if test="item.openAccountFlag!=null">
          #{item.openAccountFlag,jdbcType=INTEGER},
        </if>
        <if test="item.icFlag!=null">
          #{item.icFlag,jdbcType=INTEGER},
        </if>
        <if test="item.paymentModeId!=null">
          #{item.paymentModeId,jdbcType=INTEGER},
        </if>
        <if test="item.paymentCycleId!=null">
          #{item.paymentCycleId,jdbcType=INTEGER},
        </if>
      </trim>
    </foreach>
  </insert>
  <update id="updateBatchByPrimaryKeySelective" parameterType="java.util.List">
    <foreach collection="list" index="index" item="item" separator=";">
      update yh_user
      <set>
        <if test="item.userName!=null">
          user_name=#{item.userName,jdbcType=VARCHAR},
        </if>
        <if test="item.userType!=null">
          user_type=#{item.userType,jdbcType=INTEGER},
        </if>
        <if test="item.userCode!=null">
          user_code=#{item.userCode,jdbcType=VARCHAR},
        </if>
        <if test="item.gmtCreate!=null">
          gmt_create=#{item.gmtCreate,jdbcType=TIMESTAMP},
        </if>
        <if test="item.gmtModified!=null">
          gmt_modified=#{item.gmtModified,jdbcType=TIMESTAMP},
        </if>
        <if test="item.creator!=null">
          creator=#{item.creator,jdbcType=VARCHAR},
        </if>
        <if test="item.editor!=null">
          editor=#{item.editor,jdbcType=VARCHAR},
        </if>
        <if test="item.gsId!=null">
          gs_id=#{item.gsId,jdbcType=INTEGER},
        </if>
        <if test="item.nickName!=null">
          nick_name=#{item.nickName,jdbcType=VARCHAR},
        </if>
        <if test="item.status!=null">
          status=#{item.status,jdbcType=INTEGER},
        </if>
        <if test="item.scaId!=null">
          sca_id=#{item.scaId,jdbcType=INTEGER},
        </if>
        <if test="item.scaName!=null">
          sca_name=#{item.scaName,jdbcType=VARCHAR},
        </if>
        <if test="item.jobNumberType!=null">
          job_number_type=#{item.jobNumberType,jdbcType=INTEGER},
        </if>
        <if test="item.settleObjectId!=null">
          settle_object_id=#{item.settleObjectId,jdbcType=BIGINT},
        </if>
        <if test="item.settleObjectName!=null">
          settle_object_name=#{item.settleObjectName,jdbcType=VARCHAR},
        </if>
        <if test="item.openAccountFlag!=null">
          open_account_flag=#{item.openAccountFlag,jdbcType=INTEGER},
        </if>
        <if test="item.icFlag!=null">
          ic_flag=#{item.icFlag,jdbcType=INTEGER},
        </if>
        <if test="item.paymentModeId!=null">
          payment_mode_id=#{item.paymentModeId,jdbcType=INTEGER},
        </if>
        <if test="item.paymentCycleId!=null">
          payment_cycle_id=#{item.paymentCycleId,jdbcType=INTEGER},
        </if>
      </set>
        where
      id = #{item.id,jdbcType=INTEGER}
    </foreach>
  </update>
  <select id="selectByPrimaryKey" parameterType="java.util.Map" resultMap="BaseResultMap">
    select
    <choose>
      <when test="columns==null or columns.size()==0">
        <include refid="Base_Column_List"/>
      </when>
      <otherwise>
        <foreach collection="columns" item="column" separator=",">
          ${column}
        </foreach>
      </otherwise>
    </choose>
    from yh_user
      where id = #{id,jdbcType=INTEGER}
  </select>
</mapper>
  1. Next time explain how to customize the build logic and develop IDEA plug-in integration MBG

  2. Useful to enjoy rice

Tags: Programming SQL Java Mybatis xml

Posted on Fri, 10 Apr 2020 17:11:29 -0700 by Vbabiy