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



  • 相关阅读:
    希望走过的路成为未来的基石
    第三次个人作业--用例图设计
    第二次结对作业
    第一次结对作业
    第二次个人编程作业
    第一次个人编程作业(更新至2020.02.07)
    Springboot vue 前后分离 跨域 Activiti6 工作流 集成代码生成器 shiro权限
    springcloud 项目源码 微服务 分布式 Activiti6 工作流 vue.js html 跨域 前后分离
    spring cloud springboot 框架源码 activiti工作流 前后分离 集成代码生成器
    java代码生成器 快速开发平台 二次开发 外包项目利器 springmvc SSM后台框架源码
  • 原文地址:https://www.cnblogs.com/wgwyanfs/p/7161326.html
Copyright © 2011-2022 走看看