前文我们介绍了MyBatis基本的增删该查操作,本文介绍批量的增删改查操作。前文地址:http://blog.csdn.net/mahoking/article/details/43673741
批量增加操作步骤
1. 在接口UserMapper中添加批量增加方法。
/**
* 批量增加操作
* @param users
*/
public void batchInsertUsers(List<User> users);
2.在User.xml中添加批量增加操作的配置。
<!-- 批量增加操作 -->
<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。
3. 创建批量操作的工具类BatchDataUtils,编写批量增加方法。
/**
* 批量增加操作
* @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);
}
}
批量删除操作步骤
1. 在接口UserMapper中添加删除增加方法。
/**
* 批量删除操作
* @param ids
*/
public void batchDeleteUsers(List ids);
2.在User.xml中添加批量增加操作的配置。
<!-- 批量删除操作 -->
<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。
3. 在批量操作的工具类BatchDataUtils中编写批量删除方法。
/**
* 批量删除操作
* @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);
}
}
批量查询操作步骤
1. 在接口UserMapper中添加批量查询方法。
/**
* 批量查询操作
* @param ids
* @return
*/
public List<User> batchSelectUsers(List ids);
2.在User.xml中添加批量查询操作的配置。
<!-- 批量查询操作 -->
<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<User>,所以resultType的值为User,即com.mahaochen.mybatis.domain.User。详见configuration.xml中。
<typeAliases>
<!-- 注册实体Bean -->
<typeAlias type="com.mahaochen.mybatis.domain.User" alias="User"/>
</typeAliases>
3. 创建批量操作的工具类BatchDataUtils,编写批量查询方法。
/**
* 批量查询操作
* @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;
}
}
批量更细操作步骤
1. 在接口UserMapper中添加批量增加方法。
/**
* 批量更新操作
* @param ids
*/
public void batchUpdateUsers(List users);
2.在User.xml中添加批量更新操作的配置。
<!-- 批量更新操作 -->
<!-- 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。
3. 创建批量操作的工具类BatchDataUtils,编写批量更新方法。
/**
* 批量更新操作
* @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);
}
}