zoukankan      html  css  js  c++  java
  • MyBatis批量增删改查操作

           前文我们介绍了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);
    		}
        }
    

    【转载使用,请注明出处:http://blog.csdn.net/mahoking



  • 相关阅读:
    Django之ModelForm组件
    Hibernate的继承映射
    hibernate的检索策略
    Hibernate的多对多映射关系
    Hibernate中的一对一映射关系
    Hibernate中双向的一对多关系
    Hibernate中的映射关系(一对多)
    Hibernate的映射组成关系
    Hibernate的大对象映射
    hibernate的日期映射
  • 原文地址:https://www.cnblogs.com/wgwyanfs/p/7161326.html
Copyright © 2011-2022 走看看