MyBatis bulk additions and deletions to check operations

Source: Internet
Author: User

Before we introduced the MyBatis basic additions and deletions of the check operation, this article introduces the bulk of the deletion and modification of the operation. Front Address: http://blog.csdn.net/mahoking/article/details/43673741

Batch increment operation step

1. Add a batch addition method to the interface Usermapper.

	/**
     * Bulk Increase Operation
     * @param users
    /public void Batchinsertusers (list<user> users);

2. Add the configuration for the bulk increment operation in User.xml.

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

The value of ParameterType is java.util.List because the parameter in the method of batch increase is list.

3. Create Batch Operation tool class Batchdatautils, write batch increment method.

/**
     * Bulk Increase Operation
     * @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)}
		}
    

Bulk Delete operation steps

1. Add the Delete addition method in the interface Usermapper.

/**
     * Bulk deletion operation
     * @param ids
    /public void Batchdeleteusers (List IDs);

2. Add the configuration for the bulk increment operation in User.xml.

<!--Bulk 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 parameter in the method of bulk deletion is list, the value of ParameterType is java.util.List.

3. Write the batch deletion method in the Batchdatautils of the batch Operation tool class.

/**
     * Bulk 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)}
		}


Bulk Query Operation steps

1. Add the bulk query method to the interface Usermapper.

/**
    * Bulk Query Operation 
    * @param ids
    * @return
    /Public list<user> batchselectusers (List IDs);

2. Add the configuration of the bulk query operation in User.xml.

<!--Bulk Query operations-->
	<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 method of the bulk query is returned as LIST&LT;USER&GT, the Resulttype value is User, or com.mahaochen.mybatis.domain.User. See Configuration.xml in the details.

<typeAliases>
		<!--register entity Bean-->
		<typealias type= "Com.mahaochen.mybatis.domain.User" Alias = "User"/>
</typeAliases>

3. Create bulk Operation Tool class Batchdatautils, write batch Query method.

/**
    * Bulk Query Operation 
    * @param ids
    * @return
    /public static list<user> Batchselectusers (List IDs) { C5/>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 more detailed operation steps

1. Add a batch addition method to the interface Usermapper.

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

2. Add the configuration of the bulk update operation in User.xml.

<!--batch update operations--> <!--for MySQL MySQL requires a database connection configuration &allowmultiqueries=true For example: 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 I d = #{item.id} </foreach> </update> <!--"Extended knowledge" for Oracle has the following three ways--> <!--way one--> <up Date id= "batchUpdateUsers01" parametertype= "java.util.List" > <foreach collection= "List" item= "item" index= "I       
                Ndex "open=" Begin "close="; end; "Separator="; "> Update Mhc_user <set> 
        UserName = #{item.username}, password = #{item.password} </set> where id = #{item.id} </foreach> </update> <!--mode two--> <updatE id= "BatchUpdateUsers02" parametertype= "java.util.List" > <foreach collection= "List" item= "item" index= "Inde       
                X "open=" Begin "close=" END; "separator=" "> Update mhc_user <set>
        UserName = #{item.username}, password = #{item.password} </set> where id = #{item.id}; </foreach> </update> <!--mode three--> <update id= "BatchUpdateUsers03 parametertype=" ja 
            Va.util.List "> Begin <foreach collection=" List "item=" item "index=" index "separator=" "> Update Mhc_user <set> userName = #{item.username}, Password = #{item.passwo
        RD} </set> where id = #{item.id};
    </foreach> end;
 </update>

Because the parameter in the method of batch update is list, the value of ParameterType is java.util.List.


3. Create bulk Operation Tool class Batchdatautils, write 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)}
		}
    

"Reprint use, please indicate the source: http://blog.csdn.net/mahoking"

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.