首页 > 代码库 > mybatis批量增加与删除——(二)
mybatis批量增加与删除——(二)
1.首先应该明白,mybatis增删改返回值是int型的影响行数的值
mapper接口
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.xml
<?xml version="1.0" encoding="UTF-8" ?><!DOCTYPE mapperPUBLIC "-//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>
测试代码:
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(); }}
结果:
2.批量增加
sql语句: insert into xxx values ("xx1",‘xxx1‘),("xx2","xxx2"),("xx3","xxx3")
mapper接口
/** * 批量导入试题 * @param list 需要倒入的试题集合 * @return 影响的行数 * @throws Exception */ public int saveQuestionBatch(List<Questions> list)throws Exception;
xml配置
<?xml version="1.0" encoding="UTF-8" ?><!DOCTYPE mapperPUBLIC "-//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>
测试代码:
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(); }}
3.批量删除
sql语句: DELETE FROM `exam`.`questions` WHERE `questionId` IN (‘10‘,‘11‘,‘8‘,‘9‘)
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(); }}
mybatis批量增加与删除——(二)
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。