mapper接口java
package cn.xm.mapper; import java.util.List; import cn.xm.pojo.Questions; /** * 自定義的批量刪除與批量增長試題 * @author liqiang * */ public interface QuestionsCustomMapper { /** * 批量導入試題 * @param list 須要倒入的試題集合 * @return * @throws Exception */ public int saveQuestionBatch()throws Exception; }
mapper.xmlmysql
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd"> <!-- namespace命名空間,做用就是對sql進行分類化管理,理解sql隔離 注意:使用mapper代理方法開發,namespace有特殊重要的做用 --> <mapper namespace="cn.xm.mapper.QuestionsCustomMapper"> <insert id="saveQuestionBatch"> INSERT INTO `exam`.`questions` VALUES ('7', '1', '測試題目7', NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, "安全知識") </insert> </mapper>
測試代碼:spring
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 { // 將全局配置文件做爲一個流 String resource = "sqlMapConfig.xml"; String realPath = this.getClass().getClassLoader().getResource("sqlMapConfig.xml").getPath(); InputStream inputStream = Resources.getResourceAsStream(resource); // 創建一個SqlSession工廠 sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream); } // 測試增長 @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(); } }
結果:sql
sql語句: insert into xxx values ("xx1",'xxx1'),("xx2","xxx2"),("xx3","xxx3")apache
mapper接口tomcat
/** * 批量導入試題 * @param list 須要倒入的試題集合 * @return 影響的行數 * @throws Exception */ public int saveQuestionBatch(List<Questions> list)throws Exception;
xml配置安全
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd"> <!-- namespace命名空間,做用就是對sql進行分類化管理,理解sql隔離 注意:使用mapper代理方法開發,namespace有特殊重要的做用 --> <mapper namespace="cn.xm.mapper.QuestionsCustomMapper"> <!-- INSERT INTO `exam`.`questions` VALUES ('7', '1', '測試題目7', NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, "安全知識") --> <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>
測試代碼:session
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 { // 將全局配置文件做爲一個流 String resource = "sqlMapConfig.xml"; String realPath = this.getClass().getClassLoader().getResource("sqlMapConfig.xml").getPath(); InputStream inputStream = Resources.getResourceAsStream(resource); // 創建一個SqlSession工廠 sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream); } // 測試批量增長 @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", "測試題目8", "", "", "", "", "", "",null, "", "")); list.add(new Questions("9", "1", "測試題目8", "", "", "", "", "", "",null, "", "")); list.add(new Questions("10", "1", "測試題目8", "", "", "", "", "", "",null, "", "")); list.add(new Questions("11", "1", "測試題目8", "", "", "", "", "", "",null, "", "")); int total = qsm.saveQuestionBatch(list); System.out.println(total); sqlSession.commit(); sqlSession.close(); } }
總結:傳入單個List上面SQL中collection名字應該是list,輸入類型是List
mybatis
sql語句: DELETE FROM `exam`.`questions` WHERE `questionId` IN ('10','11','8','9')app
java接口:
/** * 批量刪除 * @param ids id集合 * @return 刪除條數 * @throws Exception */ public int deleteQuestionBatch(List<String> ids)throws Exception;
mapper.xml
<!-- 批量刪除 --> <!-- DELETE FROM `exam`.`questions` 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>
測試代碼:
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 { // 將全局配置文件做爲一個流 String resource = "sqlMapConfig.xml"; String realPath = this.getClass().getClassLoader().getResource("sqlMapConfig.xml").getPath(); InputStream inputStream = Resources.getResourceAsStream(resource); // 創建一個SqlSession工廠 sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream); } // 測試批量刪除 @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(); } }
須要在db連接url後面帶一個參數 &allowMultiQueries=true
spring.datasource.url = jdbc:mysql://localhost:3306/fc?useUnicode=true&characterEncoding=utf-8&allowMultiQueries=true
以下:
接口方法:
void updateUsers(List<User> users);
xml:
<update id="updateUsers" parameterType="list"> <foreach collection="list" separator=";" item="item"> update user <trim prefix="set" suffixOverrides=","> <if test="item.phone != null and item.phone != ''"> phone = #{item.phone}, </if> <if test="item.roles != null and item.roles != ''"> roles = #{item.roles}, </if> </trim> <where> <if test="item.id != null and item.id != ''"> id = #{item.id} </if> </where> </foreach>
生成的SQL以下:
2019-09-12 11:23:32.397 DEBUG 16468 --- [nio-8088-exec-1] c.qs.mapper.user.UserMapper.updateUsers : ==> Preparing: update user set phone = ?, roles = ? WHERE id = ? ; update user set phone = ?, roles = ? WHERE id = ? ; update user set phone = ?, roles = ? WHERE id = ? ; update user set phone = ?, roles = ? WHERE id = ? ; update user set phone = ?, roles = ? WHERE id = ? 2019-09-12 11:23:32.397 DEBUG 16468 --- [nio-8088-exec-1] c.qs.mapper.user.UserMapper.updateUsers : ==> Parameters: 123123(String), 3(String), 1(Integer), 123123(String), 8(String), 2(Integer), 123123(String), 6(String), 3(Integer), 123123(String), 7(String), 4(Integer), 123123(String), 7(String), 5(Integer) 2019-09-12 11:23:32.613 DEBUG 16468 --- [nio-8088-exec-1] c.qs.mapper.user.UserMapper.updateUsers : <== Updates: 1
還有第二種case when 語句的批量更新,這種就不通用了。