MyBatis batch addition, deletion, modification, and query operations, and mybatis addition and Deletion

Source: Internet
Author: User

MyBatis batch addition, deletion, modification, and query operations, and mybatis addition and Deletion

In the previous article, we introduced the basic addition, deletion, and query operations for MyBatis. This article introduces batch addition, deletion, modification, and query operations. Address: http://blog.csdn.net/mahoking/article/details/43673741

Add operation steps in batches

1. Add the batch add method to the UserMapper interface.

/*** Add operations in batches * @ param users */public void batchInsertUsers (List <User> users );

2. Add batch add operation configurations in User. xml.

<! -- Batch add operations --> <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>

Because the parameters in the added method are List, the value of parameterType is java. util. List.

3. Create BatchDataUtils, a tool class for batch operations, and write a batch add method.

/*** Add operations in batches * @ 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 );}}

Batch Delete procedure

1. Add the delete add method to the UserMapper interface.

/*** Batch delete operation ** @ param ids */public void batchDeleteUsers (List ids );

2. Add batch add operation configurations in User. xml.

<! -- Batch delete operation --> <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>

Because the parameters in the batch Delete method are List, the parameterType value is java. util. List.

3. Compile the batch deletion method in BatchDataUtils.

/*** Batch delete operation ** @ 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 );}}

Batch query procedure

1. Add the batch query method to the interface UserMapper.

/*** Batch query operation ** @ param ids * @ return */public List <User> batchSelectUsers (List ids );

2. Add the batch query configuration in User. xml.

<! -- Batch query operation --> <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>

Because the return value of the batch query method is List <User>, the resultType value is User, that is, com. mahaochen. mybatis. domain. User. For details, see configuration. xml.

<TypeAliases> <! -- Register the Entity Bean --> <typeAlias type = "com. mahaochen. mybatis. domain. User" alias = "User"/> </typeAliases>

3. Create BatchDataUtils, a tool for batch operations, and compile a batch query method.

/*** Batch query operation ** @ 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 ;}}

Batch operations

1. Add the batch add method to the UserMapper interface.

/*** Batch update operation ** @ param ids */public void batchUpdateUsers (List users );

2. Add the configuration for batch update in User. xml.

<! -- Batch update operation --> <! -- FOR MySQL mysql requires database connection configuration & allowMultiQueries = true FOR example: jdbc: mysql: // 127.0.0.1: 3306/mhcs? 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> <! -- [Extended knowledge] There are three methods FOR Oracle --> <! -- Method 1 --> <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> <! -- Method 2 --> <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> <! -- Method 3 --> <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>

Because the parameters in the batch update method are List, the parameterType value is java. util. List.


3. Create BatchDataUtils, a tool for batch operations, and compile the batch update method.

/*** Batch update operation ** @ 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 );}}

[For more information, see http://blog.csdn.net/mahoking]

Copyright Disclaimer: This article is an original article by the blogger and cannot be reproduced without the permission of the blogger.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.