Batch add and delete mybatis -- (2) Delete mybatis

Source: Internet
Author: User

Batch add and delete mybatis -- (2) Delete mybatis

1. First, you should understand that the value of the number of affected rows in the int type is returned when mybatis is added, deleted, or modified.

 

Mapper Interface

Package cn. xm. mapper; import java. util. list; import cn. xm. pojo. questions; /*** custom batch deletion and batch addition of question * @ author liqiang **/public interface QuestionsCustomMapper {/*** batch import question * @ param list the question set to be added * @ return * @ throws Exception */public int saveQuestionBatch () throws Exception ;}

 

Mapper. xml

<? Xml version = "1.0" encoding = "UTF-8"?> <! DOCTYPE mapperPUBLIC "-// mybatis.org//DTD Mapper 3.0 //" http://mybatis.org/dtd/mybatis-3-mapper.dtd "> <! -- Namespace is used to classify and manage SQL statements and understand SQL isolation. Note: Using mapper proxy for development, namespace has a special and important role --> <mapper namespace = "cn. xm. mapper. questionsCustomMapper "> <insert id =" saveQuestionBatch "> insert into 'exam '. 'question' VALUES ('7', '1', 'test question 7', NULL, "security knowledge ") </insert> </mapper>

 

 

Test code:

Package cn. xm. test. mybatis; import java. io. inputStream; import java.net. URL; import java. util. date; import java. util. list; import org. apache. ibatis. io. resources; import org. apache. ibatis. session. sqlSession; import org. apache. ibatis. session. sqlSessionFactory; import org. apache. ibatis. session. sqlSessionFactoryBuilder; import org. apache. tomcat. jdbc. pool. interceptor. slowQueryReportJmxMBean; import org. junit. before; import org. junit. test; import cn. xm. mapper. employeeInMapper; import cn. xm. mapper. questionsCustomMapper; import cn. xm. pojo. employeeIn; import cn. xm. pojo. employeeInExample; public class MybatisTest2 {private SqlSessionFactory sqlSessionFactory; @ Before public void setUp () throws Exception {// use the global configuration file as a stream String resource = "sqlMapConfig. xml "; String realPath = this. getClass (). getClassLoader (). getResource ("sqlMapConfig. xml "). getPath (); InputStream inputStream = Resources. getResourceAsStream (resource); // create a SqlSession factory sqlSessionFactory = new SqlSessionFactoryBuilder (). build (inputStream);} // added @ Test public void testAdd () throws Exception {SqlSession sqlSession = sqlSessionFactory. openSession (); QuestionsCustomMapper qsm = sqlSession. getMapper (QuestionsCustomMapper. class); int total = qsm. saveQuestionBatch (); System. out. println (total); sqlSession. commit (); sqlSession. close ();}}

 

Result:

 

2. Batch increase

SQL statement: insert into xxx values ("xx1", 'xxx1'), ("xx2", "xxx2"), ("xx3", "xxx3 ")

 

Mapper Interface

/*** Batch import question * @ param list the question set to be added * @ return affects the number of rows * @ throws Exception */public int saveQuestionBatch (List <Questions> list) throws Exception;

 

 

Xml configuration

<? Xml version = "1.0" encoding = "UTF-8"?> <! DOCTYPE mapperPUBLIC "-// mybatis.org//DTD Mapper 3.0 //" http://mybatis.org/dtd/mybatis-3-mapper.dtd "> <! -- Namespace is used to classify and manage SQL statements and understand SQL isolation. Note: Using mapper proxy for development, namespace has a special and important role --> <mapper namespace = "cn. xm. mapper. questionsCustomMapper "> <! -- Insert into 'exam '. 'question' VALUES ('7', '1', 'test question 7', NULL, "security knowledge ") --> <insert id = "saveQuestionBatch" parameterType = "java. util. list "> insert into 'exam '. 'questions' VALUES <foreach collection = "list" item = "question" separator = ","> (# {question. questionid}, # {question. questionbankid}, # {question. question}, # {question. questionwithtag}, # {question. answer}, # {question. analysis}, # {question. type },# {question. level },# {question. employeeid}, # {question. uploadtime },# {question. status },# {question. knowledgetype}) </foreach> </insert> </mapper>

 

 

Test code:

Package cn. xm. test. mybatis; import java. io. inputStream; import java. util. arrayList; import java. util. list; import org. apache. ibatis. io. resources; import org. apache. ibatis. session. sqlSession; import org. apache. ibatis. session. sqlSessionFactory; import org. apache. ibatis. session. sqlSessionFactoryBuilder; import org. junit. before; import org. junit. test; import cn. xm. bean. basebean. questions; import cn. xm. mapper. questionsCustomMapper; public class MybatisTest2 {private SqlSessionFactory sqlSessionFactory; @ Before public void setUp () throws Exception {// use the global configuration file as a stream String resource = "sqlMapConfig. xml "; String realPath = this. getClass (). getClassLoader (). getResource ("sqlMapConfig. xml "). getPath (); InputStream inputStream = Resources. getResourceAsStream (resource); // create a SqlSession factory sqlSessionFactory = new SqlSessionFactoryBuilder (). build (inputStream);} // added @ Test public void testBatchAdd () throws Exception {SqlSession sqlSession = sqlSessionFactory. openSession (); QuestionsCustomMapper qsm = sqlSession. getMapper (QuestionsCustomMapper. class); List <Questions> list = new ArrayList <> (); list. add (new Questions ("8", "1", "Test Question 8", "", null, "", ""); list. add (new Questions ("9", "1", "Test Question 8", "", ", null, "", ""); list. add (new Questions ("10", "1", "Test Question 8", "", ", null, "", ""); list. add (new Questions ("11", "1", "Test Question 8", "", "null, "", ""); int total = qsm. saveQuestionBatch (list); System. out. println (total); sqlSession. commit (); sqlSession. close ();}}

 

 

3. Batch Delete

 

SQL statement: DELETE FROM 'exam'. 'questions' WHERE 'questionid' IN ('10', '11', '8', '9 ')

 

Java interface:

/*** Batch Delete * @ param ids Set * @ return number of deleted items * @ throws Exception */public int deleteQuestionBatch (List <String> ids) throws Exception;

 

 

Mapper. xml

<! -- Batch Delete --> <! -- Delete from 'exam '. 'question' WHERE 'questionid' in ('10', '11', '8', '9') --> <delete id = "deleteQuestionBatch" parameterType = "java. util. list "> delete from 'exam '. 'questions' WHERE 'questionid' in <foreach collection = "list" item = "id" separator = "," open = "(" close = ") ">#{ id} </foreach> </delete>

 

 

Test code:

Package cn. xm. test. mybatis; import java. io. inputStream; import java. util. arrayList; import java. util. list; import org. apache. ibatis. io. resources; import org. apache. ibatis. session. sqlSession; import org. apache. ibatis. session. sqlSessionFactory; import org. apache. ibatis. session. sqlSessionFactoryBuilder; import org. junit. before; import org. junit. test; import cn. xm. bean. basebean. questions; import cn. xm. mapper. questionsCustomMapper; public class MybatisTest2 {private SqlSessionFactory sqlSessionFactory; @ Before public void setUp () throws Exception {// use the global configuration file as a stream String resource = "sqlMapConfig. xml "; String realPath = this. getClass (). getClassLoader (). getResource ("sqlMapConfig. xml "). getPath (); InputStream inputStream = Resources. getResourceAsStream (resource); // create a SqlSession factory sqlSessionFactory = new SqlSessionFactoryBuilder (). build (inputStream);} // Test batch deletion @ Test public void testBatchDelete () throws Exception {SqlSession sqlSession = sqlSessionFactory. openSession (); QuestionsCustomMapper qsm = sqlSession. getMapper (QuestionsCustomMapper. class); List <String> ids = new ArrayList <> (); ids. add ("8"); ids. add ("9"); ids. add ("10"); ids. add ("11"); int total = qsm. deleteQuestionBatch (ids); System. out. println (total); sqlSession. commit (); sqlSession. close ();}}

 

Related Article

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.