MyBatis中批量插入数据库与批量更新数据库的XML代码写法

55 篇文章 0 订阅
45 篇文章 0 订阅
MyBatis中批量插入数据库与批量更新数据库的XML代码写法

下面以MySQL为例讲解。

一、批量插入(insert)

DAO层接口定义:

int insertBatch(@Param("arrtest")TestEntity[] arrTest);

XML文件配置:

<insert id="insertBatch">
  insert into TEST (STATE, TESTID, TYPE, COMMENT, STUID, PRTSN, BTIME, ETIME)
  values
  <foreach collection="arrtest" item="item" index="index" separator="," >
    (#{item.state,jdbcType=TINYINT}, #{item.testid,jdbcType=BIGINT}, #{item.type,jdbcType=TINYINT},
    #{item.comment,jdbcType=VARCHAR}, #{item.stuid,jdbcType=BIGINT}, #{item.prtsn,jdbcType=INTEGER}, NOW(), NOW())
  </foreach>
</insert>
二、批量更新(update)
比较批量插入,批量更新要复杂些。
1. 普通写法
这种写法一条记录update一次,性能比较差,容易造成阻塞,不建议使用!

XML代码:

<update id="updateBatch"  parameterType="java.util.List">  
    <foreach collection="list" item="item" index="index" open="" close="" separator=";">
        update TEST
        <set>
            NAME=${item.name}
        </set>
        where TESTID = ${item.testid}
    </foreach>      
</update>
2. 使用“case when”语法来实现这个功能
这种写法执行一次update,性能好,效率高,推荐使用。

XML代码:

update TEST
    SET NAME = case id 
        when 1 then 'name1'
        when 2 then 'name2'
        when 3 then 'name3'
    end, 
    TITLE = case id 
        when 1 then 'new title 1'
        when 2 then 'new title 2'
        when 3 then 'new title 3'
    end
where TESTID in (1,2,3)
这条SQL语句的意思是,如果TESTID为1,则NAME的值为name1,TITLE的值为“new title 1”,依此类推。

下面是在MyBatis中的代码实现。

DAO层接口定义:

int updateBatchByPrimaryKeySelective(@Param("arrtest")TestEntity[] arrTest);

XML文件配置:

<update id="updateBatchByPrimaryKeySelective">
    update TEST
    <trim prefix="set" suffixOverrides=",">

      <trim prefix="STATE = case" suffix="end,">
        <foreach collection="arrtest" item="item" index="index">
          <choose>
            <when test="item.state != null">
              when TESTID = #{item.testid} then #{item.state}
            </when>
            <otherwise>
              when TESTID = #{item.testid} then STATE
            </otherwise>
          </choose>
        </foreach>
      </trim>

      <trim prefix="NAME = case" suffix="end," >
        <foreach collection="arrtest" item="item" index="index">
          <choose>
            <when test="item.name != null">
              when TESTID = #{item.testid} then #{item.name}
            </when>
            <otherwise>
              when TESTID = #{item.testid} then NAME
            </otherwise>
          </choose>
        </foreach>
      </trim>

      <trim prefix="BTIME = case" suffix="end," >
        <foreach collection="arrtest" item="item" index="index">
          <choose>
            <when test="item.btime != null">
              when TESTID = #{item.testid} then #{item.btime}
            </when>
            <otherwise>
              when TESTID = #{item.testid} then BTIME
            </otherwise>
          </choose>
        </foreach>
      </trim>
    </trim>
    where
    <foreach collection="arrtest" separator="or" item="item" index="index">
      TESTID = #{item.testid}
    </foreach>
  </update>


Ok,完毕!

  • 5
    点赞
  • 26
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
在Java使用MyBatis进行批量插入达梦数据库,你可以按照以下步骤进行操作: 1. 首先,确保你已经正确配置了MyBatis和达梦数据库的依赖。 2. 创建一个Mapper接口,用于定义插入数据的方法。例如,你可以创建一个名为`UserMapper`的接口,并在其添加一个批量插入用户的方法。 ```java public interface UserMapper { void insertUsers(List<User> users); } ``` 3. 创建一个对应的Mapper XML文件,用于实现具体的SQL操作。在该文件,你可以使用MyBatis的foreach标签来实现批量插入。 ```xml <!-- UserMapper.xml --> <mapper namespace="com.example.mapper.UserMapper"> <insert id="insertUsers" parameterType="java.util.List"> INSERT INTO user (id, name, age) VALUES <foreach collection="list" item="user" separator=","> (#{user.id}, #{user.name}, #{user.age}) </foreach> </insert> </mapper> ``` 4. 在Java代码,通过MyBatis的SqlSession来调用Mapper接口定义的方法。 ```java SqlSessionFactory sqlSessionFactory = ...; // 初始化SqlSessionFactory SqlSession sqlSession = sqlSessionFactory.openSession(); UserMapper userMapper = sqlSession.getMapper(UserMapper.class); List<User> userList = new ArrayList<>(); // 添加要插入的用户数据到userList userMapper.insertUsers(userList); sqlSession.commit(); sqlSession.close(); ``` 这样,就可以使用MyBatis实现批量插入达梦数据库了。得在配置文件正确配置数据库连接信息和Mapper接口的扫描路径。希望对你有所帮助!如有其他问题,请继续提问。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值