mybatis 批量操作

2019-11-11  本文已影响0人  wyh1791

大纲

本文旨在分享一下mybatis批量操作的实现方式

同时分享一个对代码生成工具优化改进, 改进点如下

1. 数据库实体对象以DO结尾
2. 数据库接口以Dao结尾
3. 数据库接口模板函数规范化
  insert, 
  upsert,
  update, 
  selectByPrimaryKey, 
  updateByPrimaryKey, 
  deleteByPrimaryKey, 
  listByParams, 
  batchInsert, 
  batchUpdate, 
  batchUpsert 

mybatis批量操作

批量插入

接口

    /**
     * 批量插入数据
     *
     * 缺陷: list中, 每个对象是否为空需要保持一致,  按照第一个对象中字段是否为空判断, 非空才进行插入
     *
     * @param list 插入列表
     * @return 成功数量
     */
    int batchInsert(@Param("list") List<T> list);

生成xml


03cfb487a4af4b6aa3dbe6338b93435e_image.png

批量更新

接口

    /**
     * 批量更新
     *
     * 缺陷: list中, 每个对象是否为空需要保持一致,  按照第一个对象中字段是否为空判断, 非空才进行更新
     *
     * @param list 跟新列表  批量数量建议小于1000, 数量太大销量会很低
     * @return 成功数量
     */
    int batchUpdate(@Param("list") List<T> list);

生成xml


12be8956e4984572a43947f9e30bef80_image.png

批量插入或更新

接口

    /**
     * 批量插入数据, 主键重复进行更新
     *
     * 缺陷: list中, 每个对象是否为空需要保持一致,  按照第一个对象中字段是否为空判断, 非空才进行插入/更新
     *
     * @param list 插入列表
     * @return 成功数量
     */
    int batchUpsert(@Param("list") List<T> list);

生成xml


9af502d666964811916109adcf4bb2c6_image.png

mybatis码生成工具

详见git仓库 git仓库地址

目录

ca97e73e702149e7a8d436955c4aa848_image.png

数据库接口抽象基类

/**
 * 基础类 MAPPER
 */
public interface IBaseDao<T> {

    /**
     * 插入记录
     * @param obj DO对象
     * @return 成功数量
     */
    int insert(T obj);

    /**
     * description: 插入数据,  主键重复则更新数据
     *
     * @author wyh
     * @date 2019/10/9 11:41
     * @param obj DO对象
     * @return 成功数量
     */
    int upsert(T obj);

    /**
     * 物理删除记录
     * @param seq 主键
     * @return 成功数量
     */
    <K> int deleteByPrimaryKey(@Param("id") K seq);

    /**
     * 更新记录(有效字段,即非空字段)
     * @param obj DO对象
     * @return 修改成功数量
     */
    int updateByPrimaryKey(T obj);

    /**
     * 根据主键 返回记录
     * @param seq 主键
     * @return 查询数据
     */
    <K> T selectByPrimaryKey(@Param("id") K seq);

    /**
     * 根据 条件返回记录
     * @param params 参数map
     * @return 数据列表
     */
    List<T> listByParams(Map<String, Object> params);


    /**
     * 批量插入数据
     *
     * 缺陷: list中, 每个对象是否为空需要保持一致,  按照第一个对象中字段是否为空判断, 非空才进行插入
     *
     * @param list 插入列表
     * @return 成功数量
     */
    int batchInsert(@Param("list") List<T> list);


    /**
     * 批量插入数据, 主键重复进行更新
     *
     * 缺陷: list中, 每个对象是否为空需要保持一致,  按照第一个对象中字段是否为空判断, 非空才进行插入/更新
     *
     * @param list 插入列表
     * @return 成功数量
     */
    int batchUpsert(@Param("list") List<T> list);


    /**
     * 批量更新
     *
     * 缺陷: list中, 每个对象是否为空需要保持一致,  按照第一个对象中字段是否为空判断, 非空才进行更新
     *
     * @param list 跟新列表  批量数量建议小于1000, 数量太大销量会很低
     * @return 成功数量
     */
    int batchUpdate(@Param("list") List<T> list);
}

样例

数据实体对象: BizEnumDO.java

/**
*@Author: wyh
*@Date: 2019-10-11
*/
package com.clubfactory.center.product;

import lombok.Data;
import lombok.experimental.Accessors;

import java.io.Serializable;
import java.util.Date;

/**
* @ClassName: BizEnum
* @Description:
* @author wyh
* @date 2019-10-11
*/
@Data
@Accessors(chain = true)
public class BizEnumDO implements Serializable{


    /**
     * 主键
     */
    private Integer id;

    /**
     * 创建时间
     */
    private Date createTime;

    /**
     * 更新时间
     */
    private Date updateTime;

    /**
     * 逻辑删除标示 1:删除
     */
    private Integer isDelete;

    /**
     * 枚举key
     */
    private String mainKey;

    /**
     * 枚举名称
     */
    private String mainValue;

    /**
     * 枚举子项key
     */
    private String itemKey;

    /**
     * 枚举子项value
     */
    private String itemValue;

    /**
     * 排序
     */
    private Integer sequence;

}




数据库接口: BizEnumDao.java

/**
*@Author: wyh
*@Date: 2019-10-09
*/
package com.clubfactory.center.product;
import com.clubfactory.center.product.BizEnumDO;
import org.springframework.stereotype.Repository;

/**
* @author wyh
* @date 2019-10-09
*/
@Repository
public interface BizEnumDao extends IBaseDao<BizEnumDO> {
    
}

数据库接口实现: BizEnumMapper-Manual.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.clubfactory.center.product.BizEnumDao">


</mapper>

数据库接口实现: BizEnumMapper.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.clubfactory.center.product.BizEnumDao">

    <resultMap id="BaseResultMap" type="com.clubfactory.center.product.BizEnumDO">
        <id property="id" column="ID"/><!--主键-->
        <result property="createTime" column="CREATE_TIME"/><!--创建时间-->
        <result property="updateTime" column="UPDATE_TIME"/><!--更新时间-->
        <result property="isDelete" column="IS_DELETE"/><!--逻辑删除标示 1:删除-->
        <result property="mainKey" column="MAIN_KEY"/><!--枚举key-->
        <result property="mainValue" column="MAIN_VALUE"/><!--枚举名称-->
        <result property="itemKey" column="ITEM_KEY"/><!--枚举子项key-->
        <result property="itemValue" column="ITEM_VALUE"/><!--枚举子项value-->
        <result property="sequence" column="SEQUENCE"/><!--排序-->
    </resultMap>

    <sql id="Base_Column_List">
        <trim suffixOverrides=",">
            ID,
            CREATE_TIME,
            UPDATE_TIME,
            IS_DELETE,
            MAIN_KEY,
            MAIN_VALUE,
            ITEM_KEY,
            ITEM_VALUE,
            SEQUENCE,
        </trim>
    </sql>


    <sql id="idCnd">
        <where>
            <if test="id!=null">ID=#{id,jdbcType=INTEGER}</if>
            <if test="id==null">1=0</if>
        </where>
    </sql>

    <insert id="insert" parameterType="com.clubfactory.center.product.BizEnumDO"
            keyProperty="id" useGeneratedKeys="true">
        INSERT INTO
        biz_enum
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="id!=null">ID,</if>
            <if test="createTime!=null">CREATE_TIME,</if>
            <if test="updateTime!=null">UPDATE_TIME,</if>
            <if test="isDelete!=null">IS_DELETE,</if>
            <if test="mainKey!=null">MAIN_KEY,</if>
            <if test="mainValue!=null">MAIN_VALUE,</if>
            <if test="itemKey!=null">ITEM_KEY,</if>
            <if test="itemValue!=null">ITEM_VALUE,</if>
            <if test="sequence!=null">SEQUENCE,</if>
        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="id!=null">#{id,jdbcType=INTEGER},</if>
            <if test="createTime!=null">#{createTime,jdbcType=TIMESTAMP},</if>
            <if test="updateTime!=null">#{updateTime,jdbcType=TIMESTAMP},</if>
            <if test="isDelete!=null">#{isDelete,jdbcType=TINYINT},</if>
            <if test="mainKey!=null">#{mainKey,jdbcType=VARCHAR},</if>
            <if test="mainValue!=null">#{mainValue,jdbcType=VARCHAR},</if>
            <if test="itemKey!=null">#{itemKey,jdbcType=VARCHAR},</if>
            <if test="itemValue!=null">#{itemValue,jdbcType=VARCHAR},</if>
            <if test="sequence!=null">#{sequence,jdbcType=TINYINT},</if>
        </trim>
    </insert>

    <insert id="upsert" parameterType="com.clubfactory.center.product.BizEnumDO"
            keyProperty="id" useGeneratedKeys="true">
        INSERT INTO
        biz_enum
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="id!=null">ID,</if>
            <if test="createTime!=null">CREATE_TIME,</if>
            <if test="updateTime!=null">UPDATE_TIME,</if>
            <if test="isDelete!=null">IS_DELETE,</if>
            <if test="mainKey!=null">MAIN_KEY,</if>
            <if test="mainValue!=null">MAIN_VALUE,</if>
            <if test="itemKey!=null">ITEM_KEY,</if>
            <if test="itemValue!=null">ITEM_VALUE,</if>
            <if test="sequence!=null">SEQUENCE,</if>
        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="id!=null">#{id,jdbcType=INTEGER},</if>
            <if test="createTime!=null">#{createTime,jdbcType=TIMESTAMP},</if>
            <if test="updateTime!=null">#{updateTime,jdbcType=TIMESTAMP},</if>
            <if test="isDelete!=null">#{isDelete,jdbcType=TINYINT},</if>
            <if test="mainKey!=null">#{mainKey,jdbcType=VARCHAR},</if>
            <if test="mainValue!=null">#{mainValue,jdbcType=VARCHAR},</if>
            <if test="itemKey!=null">#{itemKey,jdbcType=VARCHAR},</if>
            <if test="itemValue!=null">#{itemValue,jdbcType=VARCHAR},</if>
            <if test="sequence!=null">#{sequence,jdbcType=TINYINT},</if>
        </trim>
        ON DUPLICATE KEY UPDATE
        <trim suffixOverrides=",">
            <if test="createTime!=null">CREATE_TIME=#{createTime,jdbcType=TIMESTAMP},</if>
            <if test="updateTime!=null">UPDATE_TIME=#{updateTime,jdbcType=TIMESTAMP},</if>
            <if test="isDelete!=null">IS_DELETE=#{isDelete,jdbcType=TINYINT},</if>
            <if test="mainKey!=null">MAIN_KEY=#{mainKey,jdbcType=VARCHAR},</if>
            <if test="mainValue!=null">MAIN_VALUE=#{mainValue,jdbcType=VARCHAR},</if>
            <if test="itemKey!=null">ITEM_KEY=#{itemKey,jdbcType=VARCHAR},</if>
            <if test="itemValue!=null">ITEM_VALUE=#{itemValue,jdbcType=VARCHAR},</if>
            <if test="sequence!=null">SEQUENCE=#{sequence,jdbcType=TINYINT},</if>
        </trim>
    </insert>

    <delete id="deleteByPrimaryKey">
        DELETE FROM biz_enum
        <include refid="idCnd"/>
    </delete>

    <update id="updateByPrimaryKeySelective">
        UPDATE biz_enum
        <set>
            <if test="id!=null">ID=#{id,jdbcType=INTEGER},</if>
            <if test="createTime!=null">CREATE_TIME=#{createTime,jdbcType=TIMESTAMP},</if>
            <if test="updateTime!=null">UPDATE_TIME=#{updateTime,jdbcType=TIMESTAMP},</if>
            <if test="isDelete!=null">IS_DELETE=#{isDelete,jdbcType=TINYINT},</if>
            <if test="mainKey!=null">MAIN_KEY=#{mainKey,jdbcType=VARCHAR},</if>
            <if test="mainValue!=null">MAIN_VALUE=#{mainValue,jdbcType=VARCHAR},</if>
            <if test="itemKey!=null">ITEM_KEY=#{itemKey,jdbcType=VARCHAR},</if>
            <if test="itemValue!=null">ITEM_VALUE=#{itemValue,jdbcType=VARCHAR},</if>
            <if test="sequence!=null">SEQUENCE=#{sequence,jdbcType=TINYINT},</if>
        </set>
        <include refid="idCnd"/>
    </update>

    <select id="selectByPrimaryKey" resultMap="BaseResultMap">
        SELECT
        <include refid="Base_Column_List"/>
        FROM
        biz_enum
        <include refid="idCnd"/>
    </select>


    <sql id="where_clause">
        <where>
            <if test="id!=null">AND ID=#{id,jdbcType=INTEGER}</if>
            <if test="createTime!=null">AND CREATE_TIME=#{createTime,jdbcType=TIMESTAMP}</if>
            <if test="updateTime!=null">AND UPDATE_TIME=#{updateTime,jdbcType=TIMESTAMP}</if>
            <if test="isDelete!=null">AND IS_DELETE=#{isDelete,jdbcType=TINYINT}</if>
            <if test="mainKey!=null">AND MAIN_KEY=#{mainKey,jdbcType=VARCHAR}</if>
            <if test="mainValue!=null">AND MAIN_VALUE=#{mainValue,jdbcType=VARCHAR}</if>
            <if test="itemKey!=null">AND ITEM_KEY=#{itemKey,jdbcType=VARCHAR}</if>
            <if test="itemValue!=null">AND ITEM_VALUE=#{itemValue,jdbcType=VARCHAR}</if>
            <if test="sequence!=null">AND SEQUENCE=#{sequence,jdbcType=TINYINT}</if>
        </where>
    </sql>

    <select id="listByParams" resultMap="BaseResultMap">
        SELECT
        <include refid="Base_Column_List"/>
        FROM
        biz_enum
        <include refid="where_clause"/>
    </select>


    <insert id="batchInsert" parameterType="com.clubfactory.center.product.BizEnumDO"
            keyProperty="id" useGeneratedKeys="true">
        INSERT INTO
        biz_enum
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="list[0].id!=null">ID,</if>
            <if test="list[0].createTime!=null">CREATE_TIME,</if>
            <if test="list[0].updateTime!=null">UPDATE_TIME,</if>
            <if test="list[0].isDelete!=null">IS_DELETE,</if>
            <if test="list[0].mainKey!=null">MAIN_KEY,</if>
            <if test="list[0].mainValue!=null">MAIN_VALUE,</if>
            <if test="list[0].itemKey!=null">ITEM_KEY,</if>
            <if test="list[0].itemValue!=null">ITEM_VALUE,</if>
            <if test="list[0].sequence!=null">SEQUENCE,</if>
        </trim>
        values
        <foreach collection="list" item="item" separator=",">
            <trim prefix="(" suffix=")" suffixOverrides=",">
                <if test="list[0].id!=null">#{item.id,jdbcType=INTEGER},</if>
                <if test="list[0].createTime!=null">#{item.createTime,jdbcType=TIMESTAMP},</if>
                <if test="list[0].updateTime!=null">#{item.updateTime,jdbcType=TIMESTAMP},</if>
                <if test="list[0].isDelete!=null">#{item.isDelete,jdbcType=TINYINT},</if>
                <if test="list[0].mainKey!=null">#{item.mainKey,jdbcType=VARCHAR},</if>
                <if test="list[0].mainValue!=null">#{item.mainValue,jdbcType=VARCHAR},</if>
                <if test="list[0].itemKey!=null">#{item.itemKey,jdbcType=VARCHAR},</if>
                <if test="list[0].itemValue!=null">#{item.itemValue,jdbcType=VARCHAR},</if>
                <if test="list[0].sequence!=null">#{item.sequence,jdbcType=TINYINT},</if>
            </trim>
        </foreach>
    </insert>

    <insert id="batchUpsert" parameterType="com.clubfactory.center.product.BizEnumDO"
            keyProperty="id" useGeneratedKeys="true">
        INSERT INTO
        biz_enum
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="list[0].id!=null">ID,</if>
            <if test="list[0].createTime!=null">CREATE_TIME,</if>
            <if test="list[0].updateTime!=null">UPDATE_TIME,</if>
            <if test="list[0].isDelete!=null">IS_DELETE,</if>
            <if test="list[0].mainKey!=null">MAIN_KEY,</if>
            <if test="list[0].mainValue!=null">MAIN_VALUE,</if>
            <if test="list[0].itemKey!=null">ITEM_KEY,</if>
            <if test="list[0].itemValue!=null">ITEM_VALUE,</if>
            <if test="list[0].sequence!=null">SEQUENCE,</if>
        </trim>
        values
        <foreach collection="list" item="item" separator=",">
            <trim prefix="(" suffix=")" suffixOverrides=",">
                <if test="list[0].id!=null">#{item.id,jdbcType=INTEGER},</if>
                <if test="list[0].createTime!=null">#{item.createTime,jdbcType=TIMESTAMP},</if>
                <if test="list[0].updateTime!=null">#{item.updateTime,jdbcType=TIMESTAMP},</if>
                <if test="list[0].isDelete!=null">#{item.isDelete,jdbcType=TINYINT},</if>
                <if test="list[0].mainKey!=null">#{item.mainKey,jdbcType=VARCHAR},</if>
                <if test="list[0].mainValue!=null">#{item.mainValue,jdbcType=VARCHAR},</if>
                <if test="list[0].itemKey!=null">#{item.itemKey,jdbcType=VARCHAR},</if>
                <if test="list[0].itemValue!=null">#{item.itemValue,jdbcType=VARCHAR},</if>
                <if test="list[0].sequence!=null">#{item.sequence,jdbcType=TINYINT},</if>
            </trim>
        </foreach>
        ON DUPLICATE KEY UPDATE
        <trim suffixOverrides=",">
            <if test="list[0].createTime!=null">CREATE_TIME=values(CREATE_TIME),</if>
            <if test="list[0].updateTime!=null">UPDATE_TIME=values(UPDATE_TIME),</if>
            <if test="list[0].isDelete!=null">IS_DELETE=values(IS_DELETE),</if>
            <if test="list[0].mainKey!=null">MAIN_KEY=values(MAIN_KEY),</if>
            <if test="list[0].mainValue!=null">MAIN_VALUE=values(MAIN_VALUE),</if>
            <if test="list[0].itemKey!=null">ITEM_KEY=values(ITEM_KEY),</if>
            <if test="list[0].itemValue!=null">ITEM_VALUE=values(ITEM_VALUE),</if>
            <if test="list[0].sequence!=null">SEQUENCE=values(SEQUENCE),</if>
        </trim>
    </insert>

    <update id="batchUpdate" parameterType="com.clubfactory.center.product.BizEnumDO">
        update
        biz_enum
        <trim prefix="set" suffixOverrides=",">
            <if test="list[0].mainKey!=null">
                <trim prefix="MAIN_KEY = case" suffix="end,">
                    <foreach collection="list" item="item" index="index">
                        when id=${item.ID} then #{item.mainKey,jdbcType=VARCHAR}
                    </foreach>
                </trim>
            </if>
            <if test="list[0].mainValue!=null">
                <trim prefix="MAIN_VALUE = case" suffix="end,">
                    <foreach collection="list" item="item" index="index">
                        when id=${item.ID} then #{item.mainValue,jdbcType=VARCHAR}
                    </foreach>
                </trim>
            </if>
            <if test="list[0].itemKey!=null">
                <trim prefix="ITEM_KEY = case" suffix="end,">
                    <foreach collection="list" item="item" index="index">
                        when id=${item.ID} then #{item.itemKey,jdbcType=VARCHAR}
                    </foreach>
                </trim>
            </if>
            <if test="list[0].itemValue!=null">
                <trim prefix="ITEM_VALUE = case" suffix="end,">
                    <foreach collection="list" item="item" index="index">
                        when id=${item.ID} then #{item.itemValue,jdbcType=VARCHAR}
                    </foreach>
                </trim>
            </if>
        </trim>
        where id in
        <foreach collection="list" index="index" item="item" separator="," open="(" close=")">
            ${item.ID}
        </foreach>
    </update>

</mapper>


git仓库地址

上一篇下一篇

猜你喜欢

热点阅读