<?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.yun.mybatis.mapper.AccountMapper">
<sql id="BaseColumnName">
accountId, activateCash
</sql>
<sql id="InsertBaseColumnName">
activateCash, accountId, userCode, state
</sql>
<!-- 查询 -->
<select id="getAccount" resultType="com.yun.mybatis.entity.Account">
select
<include refid="BaseColumnName" />
from account
<where>
<if test="accountId != null and accountId !=''">
AND accountId=#{accountId}
</if>
</where>
</select>
<!-- 更新 -->
<update id="updateAccount" parameterType="com.yun.mybatis.entity.Account">
update account
<set>
<if test="activateCash != null and activateCash !=''">
activateCash=#{activateCash},
</if>
</set>
<where>
<if test="accountId != null and accountId !=''">
AND accountId=#{accountId}
</if>
</where>
</update>
<!-- 插入 -->
<insert id="insertAccount" parameterType="com.yun.mybatis.entity.Account">
insert into account
<trim prefix="(" suffix=")" suffixOverrides=",">
<if test="activateCash != null and activateCash !=''">
activateCash,
</if>
<if test="accountId != null and accountId !=''">
accountId,
</if>
<if test="userCode != null and userCode !=''">
userCode,
</if>
<if test="state != null and state !=''">
state,
</if>
</trim>
<trim prefix="values (" suffix=")" suffixOverrides=",">
<if test="activateCash != null and activateCash !=''">
#{activateCash},
</if>
<if test="accountId != null and accountId !=''">
#{accountId},
</if>
<if test="userCode != null and userCode !=''">
#{userCode},
</if>
<if test="state != null and state !=''">
#{state},
</if>
</trim>
</insert>
<!-- 批量查询 -->
<select id="selectAccountList" resultType="com.yun.mybatis.entity.Account">
select
<include refid="BaseColumnName" />
from account
<where>
<foreach collection="array" item="acc" index="idx" separator=","
open="accountId in (" close=")">
#{acc}
</foreach>
</where>
</select>
<!-- 批量插入 -->
<insert id="insertAccountList">
insert into account
(<include refid="InsertBaseColumnName"/>)
<foreach collection="list" item="acc" index="idx" separator=","
open="values">
<trim prefix="(" suffix=")" suffixOverrides=",">
<if test="acc.activateCash != null and acc.activateCash !=''">
#{acc.activateCash},
</if>
<if test="acc.accountId != null and acc.accountId !=''">
#{acc.accountId},
</if>
<if test="acc.userCode != null and acc.userCode !=''">
#{acc.userCode},
</if>
<if test="acc.state != null and acc.state !=''">
#{acc.state},
</if>
</trim>
</foreach>
</insert>
</mapper>
mybatis批量操作executor
@Test
public void testInsertExecutor() {
try {
SqlSession session = sessionFactory.openSession(ExecutorType.BATCH, true);
//SqlSession session = sessionFactory.openSession(true);
AccountMapper mapper = session.getMapper(AccountMapper.class);
Account account = new Account();
account.setAccountId("1234");
account.setUserCode("dfdfdf1234");
account.setState("00");
account.setActivateCash(11.1);
int num = mapper.insertAccount(account);
Account accountTwo = new Account();
accountTwo.setAccountId("5678");
accountTwo.setUserCode("dfdfdf5678");
accountTwo.setState("00");
accountTwo.setActivateCash(11.1);
int numTwo = mapper.insertAccount(accountTwo);
session.commit();
session.close();
} catch (Exception e) {
e.printStackTrace();
}
}
原生批量更新batch
@Test
public void testBatchJdbc() {
Connection conn;
Statement stat;
//ResultSet result;
try {
Class.forName("com.mysql.jdbc.Driver");
conn = DriverManager.getConnection(DB_URL, USER, PASS);
conn.setAutoCommit(false);
stat = conn.createStatement();
String sql = "update account set activateCash = 89401 where accountId = '32323232323232'";
String sql2 = "update account set activateCash = 100 where accountId = '123'";
stat.addBatch(sql);
stat.addBatch(sql2);
int[] result = stat.executeBatch();
for(int rel : result) {
System.out.println(rel);
}
conn.commit();
stat.close();
conn.close();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}