MyBatis批量增删改查操作

转载自 MyBatis批量增删改查操作

1. 批量增加操作步骤

  • 在接口UserMapper中添加批量增加方法。
1
2
3
4
5
/** 
* 批量增加操作
* @param users
*/
public void batchInsertUsers(List<User> users);
  • 在User.xml中添加批量增加操作的配置。
1
2
3
4
5
6
7
<!-- 批量增加操作 -->  
<insert id="batchInsertUsers" parameterType="java.util.List">
insert into mhc_user(userName,password) values
<foreach collection="list" item="item" index="index" separator=",">
(#{item.userName},#{item.password})
</foreach>
</insert>

​ 由于批量增加的方法中参数为List,所以parameterType的值为java.util.List。

  • 创建批量操作的工具类BatchDataUtils,编写批量增加方法。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
/** 
* 批量增加操作
* @param users
*/
public static void batchInsertUsers(List<User> users){

SqlSessionFactory ssf = MyBatisUtil.getSqlSessionFactory();
SqlSession session = ssf.openSession();

try {
UserMapper userMapper = session.getMapper(UserMapper.class);
userMapper.batchInsertUsers(users);
session.commit();
} catch (Exception e) {
e.printStackTrace();
} finally {
MyBatisUtil.closeSession(session);
}
}

2. 批量删除操作步骤

  • 在接口UserMapper中添加删除增加方法。
1
2
3
4
5
/** 
* 批量删除操作
* @param ids
*/
public void batchDeleteUsers(List ids);
  • 在User.xml中添加批量增加操作的配置。
1
2
3
4
5
6
7
<!-- 批量删除操作 -->  
<delete id="batchDeleteUsers" parameterType="java.util.List">
delete from mhc_user where id in
<foreach collection="list" index="index" item="item" open="(" close=")" separator=",">
#{item}
</foreach>
</delete>

​ 由于批量删除的方法中参数为List,所以parameterType的值为java.util.List。

  • 在批量操作的工具类BatchDataUtils中编写批量删除方法。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
/** 
* 批量删除操作
* @param ids
*/
public static void batchDeleteUsers(List ids){
SqlSessionFactory ssf = MyBatisUtil.getSqlSessionFactory();
SqlSession session = ssf.openSession();

try {
UserMapper userMapper = session.getMapper(UserMapper.class);
userMapper.batchDeleteUsers(ids);
session.commit();
} catch (Exception e) {
e.printStackTrace();
} finally {
MyBatisUtil.closeSession(session);
}
}

3. 批量查询操作步骤

  • 在接口UserMapper中添加批量查询方法。
1
2
3
4
5
6
/** 
* 批量查询操作
* @param ids
* @return
*/
public List<User> batchSelectUsers(List ids);
  • 在User.xml中添加批量查询操作的配置。
1
2
3
4
5
6
7
8
<!-- 批量查询操作 -->  
<select id="batchSelectUsers" resultType="User">
select *
from mhc_user where id in
<foreach collection="list" index="index" item="item" open="(" separator="," close=")">
#{item}
</foreach>
</select>

​ 由于批量查询的方法的返回为List,所以resultType的值为User,即com.mahaochen.mybatis.domain.User。详见configuration.xml中。

1
2
3
4
<typeAliases>  
<!-- 注册实体Bean -->
<typeAlias type="com.mahaochen.mybatis.domain.User" alias="User"/>
</typeAliases>
  • 创建批量操作的工具类BatchDataUtils,编写批量查询方法。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
/** 
* 批量查询操作
* @param ids
* @return
*/
public static List<User> batchSelectUsers(List ids){
SqlSessionFactory ssf = MyBatisUtil.getSqlSessionFactory();
SqlSession session = ssf.openSession();
List<User> users = null;
try {
UserMapper userMapper = session.getMapper(UserMapper.class);
users = userMapper.batchSelectUsers(ids);
} catch (Exception e) {
e.printStackTrace();
} finally {
MyBatisUtil.closeSession(session);
}
return users;
}
}

4. 批量更新操作步骤

  • 在接口UserMapper中添加批量增加方法。
1
2
3
4
5
/** 
* 批量更新操作
* @param ids
*/
public void batchUpdateUsers(List users);
  • 在User.xml中添加批量更新操作的配置。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
<!-- 批量更新操作 -->  
<!-- FOR MySQL mysql需要数据库连接配置&allowMultiQueries=true
例如:jdbc:mysql://127.0.0.1:3306/mhc?allowMultiQueries=true -->
<update id="batchUpdateUsers" parameterType="java.util.List">
<foreach collection="list" item="item" index="index" open="" close="" separator=";">
update mhc_user
<set>
userName = #{item.userName}, password = #{item.password}
</set>
where id = #{item.id}
</foreach>
</update>

<!-- 【扩展知识】 FOR Oracle 有以下三种方式-->
<!-- 方式一 -->
<update id="batchUpdateUsers01" parameterType="java.util.List">
<foreach collection="list" item="item" index="index" open="begin" close=";end;" separator=";" >
update mhc_user
<set>
userName = #{item.userName}, password = #{item.password}
</set>
where id = #{item.id}
</foreach>
</update>
<!-- 方式二 -->
<update id="batchUpdateUsers02" parameterType="java.util.List">
<foreach collection="list" item="item" index="index" open="begin" close="end;" separator="" >
update mhc_user
<set>
userName = #{item.userName}, password = #{item.password}
</set>
where id = #{item.id};
</foreach>
</update>
<!-- 方式三 -->
<update id="batchUpdateUsers03" parameterType="java.util.List">
begin
<foreach collection="list" item="item" index="index" separator="" >
update mhc_user
<set>
userName = #{item.userName}, password = #{item.password}
</set>
where id = #{item.id};
</foreach>
end;
</update>

​ 由于批量更新的方法中参数为List,所以parameterType的值为java.util.List。

  • 创建批量的工具类
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
/** 
* 批量更新操作
* @param users
*/
public static void batchUpdateUsers(List users){
SqlSessionFactory ssf = MyBatisUtil.getSqlSessionFactory();
SqlSession session = ssf.openSession();

try {
UserMapper userMapper = session.getMapper(UserMapper.class);
userMapper.batchUpdateUsers(users);
session.commit();
} catch (Exception e) {
e.printStackTrace();
} finally {
MyBatisUtil.closeSession(session);
}
}
打赏
  • 版权声明: 本博客所有文章除特别声明外,著作权归作者所有。转载请注明出处!
  • Copyrights © 2015-2023 高行行
  • 访问人数: | 浏览次数:

请我喝杯咖啡吧~

支付宝
微信