MyBatis批量的增删改查

批量增加操作步骤

1. 在接口UserMapper中添加批量增加方法。

[java]  view plain  copy
  1. /** 
  2.     * 批量增加操作 
  3.     * @param users 
  4.     */  
  5.    public void batchInsertUsers(List<User> users);  

2.在User.xml中添加批量增加操作的配置。

[html]  view plain  copy
  1. <!-- 批量增加操作 -->  
  2.     <insert id="batchInsertUsers" parameterType="java.util.List">  
  3.         insert into mhc_user(userName,password) values  
  4.         <foreach collection="list" item="item" index="index" separator=",">  
  5.             (#{item.userName},#{item.password})  
  6.         </foreach>  
  7.     </insert>  

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

3. 创建批量操作的工具类BatchDataUtils,编写批量增加方法。

[java]  view plain  copy
  1. /** 
  2.      * 批量增加操作 
  3.      * @param users 
  4.      */  
  5.     public static void batchInsertUsers(List<User> users){  
  6.           
  7.         SqlSessionFactory ssf = MyBatisUtil.getSqlSessionFactory();  
  8.         SqlSession session = ssf.openSession();  
  9.           
  10.         try {  
  11.             UserMapper userMapper = session.getMapper(UserMapper.class);  
  12.             userMapper.batchInsertUsers(users);  
  13.             session.commit();  
  14.         } catch (Exception e) {  
  15.             e.printStackTrace();  
  16.         } finally {  
  17.             MyBatisUtil.closeSession(session);  
  18.         }  
  19.     }  

批量删除操作步骤

1. 在接口UserMapper中添加删除增加方法。

[java]  view plain  copy
  1. /** 
  2.      * 批量删除操作 
  3.      * @param ids 
  4.      */  
  5.     public void batchDeleteUsers(List ids);  

2.在User.xml中添加批量增加操作的配置。

[html]  view plain  copy
  1. <!-- 批量删除操作 -->  
  2.     <delete id="batchDeleteUsers" parameterType="java.util.List">  
  3.         delete from mhc_user where id in  
  4.         <foreach collection="list" index="index" item="item" open="(" close=")" separator=",">  
  5.             #{item}  
  6.         </foreach>  
  7.     </delete>  

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

3. 在批量操作的工具类BatchDataUtils中编写批量删除方法。

[java]  view plain  copy
  1. /** 
  2.      * 批量删除操作 
  3.      * @param ids 
  4.      */  
  5.     public static void batchDeleteUsers(List ids){  
  6.         SqlSessionFactory ssf = MyBatisUtil.getSqlSessionFactory();  
  7.         SqlSession session = ssf.openSession();  
  8.           
  9.         try {  
  10.             UserMapper userMapper = session.getMapper(UserMapper.class);  
  11.             userMapper.batchDeleteUsers(ids);  
  12.             session.commit();  
  13.         } catch (Exception e) {  
  14.             e.printStackTrace();  
  15.         } finally {  
  16.             MyBatisUtil.closeSession(session);  
  17.         }  
  18. }  

批量查询操作步骤

1. 在接口UserMapper中添加批量查询方法。

[java]  view plain  copy
  1. /** 
  2.     * 批量查询操作  
  3.     * @param ids 
  4.     * @return 
  5.     */  
  6.     public List<User> batchSelectUsers(List ids);  

2.在User.xml中添加批量查询操作的配置。

[html]  view plain  copy
  1. <!-- 批量查询操作 -->  
  2.     <select id="batchSelectUsers" resultType="User">  
  3.         select *  
  4.         from mhc_user where id in  
  5.         <foreach collection="list" index="index" item="item" open="(" separator="," close=")">  
  6.         #{item}  
  7.         </foreach>  
  8.     </select>  

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

[html]  view plain  copy
  1. <typeAliases>  
  2.         <!-- 注册实体Bean -->  
  3.         <typeAlias type="com.mahaochen.mybatis.domain.User" alias="User"/>  
  4. </typeAliases>  

3. 创建批量操作的工具类BatchDataUtils,编写批量查询方法。

[java]  view plain  copy
  1. /** 
  2.     * 批量查询操作  
  3.     * @param ids 
  4.     * @return 
  5.     */  
  6.     public static List<User> batchSelectUsers(List ids){  
  7.         SqlSessionFactory ssf = MyBatisUtil.getSqlSessionFactory();  
  8.         SqlSession session = ssf.openSession();  
  9.         List<User> users = null;  
  10.         try {  
  11.             UserMapper userMapper = session.getMapper(UserMapper.class);  
  12.             users = userMapper.batchSelectUsers(ids);  
  13.         } catch (Exception e) {  
  14.             e.printStackTrace();  
  15.         } finally {  
  16.             MyBatisUtil.closeSession(session);  
  17.         }  
  18.         return users;  
  19.     }  
  20. }  

批量更细操作步骤

1. 在接口UserMapper中添加批量增加方法。

[java]  view plain  copy
  1. /** 
  2.      * 批量更新操作  
  3.      * @param ids 
  4.      */  
  5.     public void batchUpdateUsers(List users);  

2.在User.xml中添加批量更新操作的配置。

[html]  view plain  copy
  1. <!-- 批量更新操作 -->  
  2.     <!-- FOR MySQL mysql需要数据库连接配置&allowMultiQueries=true   
  3.         例如:jdbc:mysql://127.0.0.1:3306/mhc?allowMultiQueries=true -->  
  4.     <update id="batchUpdateUsers" parameterType="java.util.List">  
  5.         <foreach collection="list" item="item" index="index" open="" close="" separator=";">  
  6.         update mhc_user   
  7.         <set>  
  8.             userName = #{item.userName}, password = #{item.password}  
  9.         </set>  
  10.         where id = #{item.id}  
  11.         </foreach>  
  12.     </update>  
  13.       
  14.     <!-- 【扩展知识】 FOR Oracle  有以下三种方式-->  
  15.     <!-- 方式一 -->  
  16.     <update id="batchUpdateUsers01" parameterType="java.util.List">  
  17.         <foreach collection="list" item="item" index="index" open="begin" close=";end;" separator=";" >   
  18.             update mhc_user   
  19.             <set>         
  20.                 userName = #{item.userName}, password = #{item.password}  
  21.             </set>  
  22.             where id = #{item.id}  
  23.         </foreach>  
  24.     </update>  
  25.     <!-- 方式二 -->  
  26.     <update id="batchUpdateUsers02" parameterType="java.util.List">  
  27.         <foreach collection="list" item="item" index="index" open="begin" close="end;" separator="" >   
  28.             update mhc_user   
  29.             <set>         
  30.                 userName = #{item.userName}, password = #{item.password}  
  31.             </set>  
  32.             where id = #{item.id};  
  33.         </foreach>  
  34.     </update>  
  35.     <!-- 方式三 -->  
  36.     <update id="batchUpdateUsers03" parameterType="java.util.List">  
  37.         begin  
  38.         <foreach collection="list" item="item" index="index" separator="" >   
  39.             update mhc_user   
  40.             <set>         
  41.                 userName = #{item.userName}, password = #{item.password}  
  42.             </set>  
  43.             where id = #{item.id};  
  44.         </foreach>  
  45.         end;  
  46.     </update>  

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


3. 创建批量操作的工具类BatchDataUtils,编写批量更新方法。

[java]  view plain  copy
  1. /** 
  2.     * 批量更新操作  
  3.     * @param users 
  4.     */  
  5.    public static void batchUpdateUsers(List users){  
  6.     SqlSessionFactory ssf = MyBatisUtil.getSqlSessionFactory();  
  7.     SqlSession session = ssf.openSession();  
  8.       
  9.     try {  
  10.         UserMapper userMapper = session.getMapper(UserMapper.class);  
  11.         userMapper.batchUpdateUsers(users);  
  12.         session.commit();  
  13.     } catch (Exception e) {  
  14.         e.printStackTrace();  
  15.     } finally {  
  16.         MyBatisUtil.closeSession(session);  
  17.     }  
  18.    }  
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值