实际开发中我们操作数据库持久化,总是需要写重复的mapper,service,xml浪费了我们大量的时间,在这里推荐大家使用SqlSessionTemplate

1.application.xml配置

server.port=8081
#server.servlet.context-path=/recordLog#数据源必填项
spring.datasource.url=jdbc:mysql://localhost:3306/test?generateSimpleParameterMetadata=true&allowMultiQueries=true
spring.datasource.username=root
spring.datasource.password=123456
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
#验证连接的有效性
spring.datasource.secondary.test-while-idle=true
#获取连接时候验证,会影响性能
spring.datasource.secondary.test-on-borrow=false
#在连接归还到连接池时是否测试该连接
spring.datasource.secondary.test-on-return=false
spring.datasource.secondary.validation-query=SELECT 1 FROM DUAL
#空闲连接回收的时间间隔,与test-while-idle一起使用,设置5分钟
spring.datasource.secondary.time-between-eviction-runs-millis=300000
#连接池空闲连接的有效时间 ,设置30分钟
spring.datasource.secondary.min-evictable-idle-time-millis=1800000
spring.datasource.secondary.initial-size=5
#指定连接池中最大的活跃连接数.
spring.datasource.secondary.max-active=50
#指定连接池等待连接返回的最大等待时间,毫秒单位.
spring.datasource.secondary.max-wait=60000
#指定必须保持连接的最小值
spring.datasource.secondary.min-idle=5#Mybatis配置
#mybatis.config-location=classpath:config/sqlMapConfig.xml
mybatis.type-aliases-package=com.example.recordlog.bean
#mybatis.mapper-locations=classpath:mybatis/mapper/*.xml
#引用mybatis-plus-boot-starter写法如下
mybatis-plus.mapper-locations=classpath:mybatis/mapper/*.xml

2.BaseDAO

import java.util.List;/*** @author liuminglin* */
public interface BaseDao {/*** 单个查询** @Param namespace :xml的映射路径* @Param sqlId     :xml中方法的名字* @Param params    :参数* @Return*/<T, E> E select(String namespace, String sqlId, T params);<T, E> List<E> selectList(String namespace, String sqlId, T params);/*** @Desc* @Param 单个修改* @Return*/<T> int update(String namespace, String sqlId, T params);/*** @Desc* @Param 批量修改* @Return*/<T> List<Long> updateList(String namespace, String sqlId, List<T> list);/*** @Desc* @Param 单个插入* @Return*/<T> long insert(String namespace, String sqlId, T params);/*** @Desc* @Param 批量差入* @Return*/<T> List<Long> insertList(String namespace, String sqlId, List<T> list);/*** @Desc* @Param 单个删除* @Return*/<T> int delete(String namespace, String sqlId, T params);/*** @Desc* @Param 批量删除* @Return*/<T> List<Long> deleteList(String namespace, String sqlId, List<T> list);/*** @Desc* @Param 所有的批量都可以用这个方法,它识别的是xml的sql,与方法无关;bathcount指的是多少条提交一次事物* 注意:数据库连接池请求超时 HikariPool-1 - Connection is not available, request timed out after 30009ms* 解决方案:* 1.增加数据库连接池* 2.多个线程通过 SynchronousQueue.pool() 获取连接* @Return*/<T> void batchALL(String namespace, String sqlId, List<T> params, Integer bathcount);/*** @Desc* @Param 所有的批量都可以用这个方法,*/<T> void batchExecutor(String namespace, String sqlId, List<T> params);}

3.BaseDAOImpl


import com.example.recordlog.service.BaseDao;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.ibatis.mapping.BoundSql;
import org.apache.ibatis.mapping.MappedStatement;
import org.apache.ibatis.mapping.ParameterMapping;
import org.apache.ibatis.mapping.SqlCommandType;
import org.apache.ibatis.session.ExecutorType;
import org.apache.ibatis.session.SqlSession;
import org.mybatis.spring.SqlSessionTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;import java.lang.reflect.Method;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;@Slf4j
@Component
public class BaseDaoImpl implements BaseDao {@Autowiredprivate SqlSessionTemplate sqlSessionTemplate;@Overridepublic <T, E> E select(String namespace, String sqlId, T params) {if (params == null) {return sqlSessionTemplate.selectOne(namespace + "." + sqlId);} else {return sqlSessionTemplate.selectOne(namespace + "." + sqlId, params);}}@Overridepublic <T, E> List<E> selectList(String namespace, String sqlId, T params) {if (params == null) {return sqlSessionTemplate.selectList(namespace + "." + sqlId);} else {return sqlSessionTemplate.selectList(namespace + "." + sqlId, params);}}@Transactional(rollbackFor = Exception.class)@Overridepublic <T> int update(String namespace, String sqlId, T params) {if (params == null) {return sqlSessionTemplate.update(namespace + "." + sqlId);} else {return sqlSessionTemplate.update(namespace + "." + sqlId, params);}}@Transactional(rollbackFor = Exception.class)@Overridepublic <T> List<Long> updateList(String namespace, String sqlId, List<T> list) {Long startTime = System.currentTimeMillis();try {if (CollectionUtils.isEmpty(list)) {return null;}MappedStatement ms = sqlSessionTemplate.getConfiguration().getMappedStatement(namespace + "." + sqlId);SqlCommandType sqlCommandType = ms.getSqlCommandType();BoundSql boundSql = ms.getSqlSource().getBoundSql(list.get(0));String sql = boundSql.getSql();List<ParameterMapping> list2 = boundSql.getParameterMappings();//此处错误// Connection connection = sqlSessionTemplate.getConnection();Connection connection = sqlSessionTemplate.getSqlSessionFactory().openSession().getConnection();PreparedStatement statement = null;if (sqlCommandType == SqlCommandType.INSERT) {statement = connection.prepareStatement(sql, Statement.RETURN_GENERATED_KEYS);} else {statement = connection.prepareStatement(sql);}for (T item : list) {if (item == null) {continue;}if (item instanceof Map) {Map<String, Object> map = (Map<String, Object>) item;for (int index = 0; index < list2.size(); index++) {ParameterMapping pm = list2.get(index);Object value = map.get(pm.getProperty());statement.setObject(index + 1, value);}} else if (item instanceof Long || item instanceof String || item instanceof Integer) {statement.setObject(1, item);} else {for (int index = 0; index < list2.size(); index++) {ParameterMapping pm = list2.get(index);// String methodName = StringUtil.hump("get_" + pm.getProperty(), "_");String methodName = getMethodName(pm.getProperty());Method method = item.getClass().getMethod(methodName);Object value = method.invoke(item);statement.setObject(index + 1, value);}}statement.addBatch();}List<Long> resultList = new ArrayList<Long>();int[] resultArray = statement.executeBatch();if (sqlCommandType != SqlCommandType.INSERT) {for (int intval : resultArray) {resultList.add(Long.valueOf(intval + ""));}} else {ResultSet resultSet = statement.getGeneratedKeys();while (resultSet.next()) {//此处错误,由于rs.next()遍历查询结果时,下标是从“1”开始,而这里是从“0”开始,导致出错//resultList.add(resultSet.getLong(0));resultList.add(resultSet.getLong(1));}}Long endTime = System.currentTimeMillis();log.info("sql耗时:{}", endTime - startTime + "毫秒");return resultList;} catch (Exception e) {throw new RuntimeException(e.getMessage());}}@Transactional(rollbackFor = Exception.class)@Overridepublic <T> long insert(String namespace, String sqlId, T params) {return update(namespace, sqlId, params);}@Transactional(rollbackFor = Exception.class)@Overridepublic <T> List<Long> insertList(String nameSpace, String id, List<T> list) {if (list == null || list.size() == 0) {return new ArrayList<>();}return updateList(nameSpace, id, list);}@Transactional(rollbackFor = Exception.class)@Overridepublic <T> int delete(String namespace, String sqlId, T params) {return update(namespace, sqlId, params);}@Transactional(rollbackFor = Exception.class)@Overridepublic <T> List<Long> deleteList(String namespace, String sqlId, List<T> list) {return updateList(namespace, sqlId, list);}@Transactional(rollbackFor = Exception.class)@Overridepublic <T> void batchALL(String namespace, String sqlId, List<T> list, Integer bathcount) {Long startTime = System.currentTimeMillis();List<T> data = new ArrayList<>();for (int i = 0; i < list.size(); i++) {data.add(list.get(i));if (data.size() == bathcount || i == list.size() - 1) {this.batchUtil(namespace, sqlId, data);data.clear();}}Long endTime = System.currentTimeMillis();log.info("batchALL方法sql耗时:{}", endTime - startTime + "毫秒");}@Transactional(rollbackFor = Exception.class)@Overridepublic <T> void batchExecutor(String namespace, String sqlId, List<T> params) {batchUpdate(namespace, sqlId, params);}private <T> void batchUtil(String namespace, String sqlId, List<T> list) {try {if (list == null || list.isEmpty()) {return;}MappedStatement ms = sqlSessionTemplate.getConfiguration().getMappedStatement(namespace + "." + sqlId);SqlCommandType sqlCommandType = ms.getSqlCommandType();BoundSql boundSql = ms.getSqlSource().getBoundSql(list.get(0));String sql = boundSql.getSql();List<ParameterMapping> list2 = boundSql.getParameterMappings();Connection connection = sqlSessionTemplate.getSqlSessionFactory().openSession().getConnection();PreparedStatement statement = null;if (sqlCommandType == SqlCommandType.INSERT) {statement = connection.prepareStatement(sql, Statement.RETURN_GENERATED_KEYS);} else {statement = connection.prepareStatement(sql);}sql = sql.replaceAll("\\n", "");sql = sql.replaceAll("\\t", "");sql = sql.replaceAll("[[ ]]{2,}", " ");log.info("==>  Preparing:" + sql);for (T item : list) {if (item == null) {continue;}StringBuffer values = new StringBuffer();if (item instanceof Map) {Map<String, Object> map = (Map<String, Object>) item;for (int index = 0; index < list2.size(); index++) {ParameterMapping pm = list2.get(index);Object value = map.get(pm.getProperty());values.append(value).append("(").append(value.getClass()).append("),");statement.setObject(index + 1, value);}} else if (item instanceof Long || item instanceof String || item instanceof Integer) {statement.setObject(1, item);values.append(item).append("(").append(StringUtils.substringAfterLast(item.getClass().toString(), ".")).append("),");} else {List<String> params = new ArrayList<>();for (int index = 0; index < list2.size(); index++) {ParameterMapping pm = list2.get(index);//String methodName = StringUtil.hump("get_" + pm.getProperty(), "_");String methodName = getMethodName(pm.getProperty());Method method = item.getClass().getMethod(methodName);Object value = method.invoke(item);params.add(value.toString());statement.setObject(index + 1, value);values.append(value).append("(").append(StringUtils.substringAfterLast(value.getClass().toString(), ".")).append("),");}}statement.addBatch();values.delete(values.length() - 1, values.length());log.info("==> Parameters:" + values);}List<Long> resultList = new ArrayList<>();int[] resultArray = statement.executeBatch();if (sqlCommandType != SqlCommandType.INSERT) {for (int intval : resultArray) {resultList.add(Long.valueOf(intval + ""));}} else {ResultSet resultSet = statement.getGeneratedKeys();while (resultSet.next()) {try {resultList.add(resultSet.getLong(1));} catch (Exception e) {log.error("错误:" + e.toString());}}}return;} catch (Exception e) {log.error("错误:" + e.toString());throw new RuntimeException(e.toString());}}void batchUpdate(String namespace, String sqlId, List parameterList) {Long startTime = System.currentTimeMillis();String statement = namespace + "." + sqlId;if (parameterList == null || parameterList.size() == 0) {return;}SqlSession sqlSession = null;try {sqlSession = sqlSessionTemplate.getSqlSessionFactory().openSession(ExecutorType.BATCH, false);for (Object obj : parameterList) {sqlSession.update(statement, obj);}sqlSession.commit();//清理缓存防止溢出sqlSession.clearCache();} catch (Exception e) {log.error("batch insert error");if (sqlSession != null) {sqlSession.rollback();}} finally {if (sqlSession != null) {sqlSession.close();Long endTime = System.currentTimeMillis();log.info("batchExecutor方法sql耗时:{}", endTime - startTime + "毫秒");}}}@SuppressWarnings("unchecked")protected <T> void printSql(String id, T params) {try {MappedStatement ms = sqlSessionTemplate.getConfiguration().getMappedStatement(id);BoundSql boundSql = ms.getSqlSource().getBoundSql(params);String sql = boundSql.getSql();sql = sql.replaceAll("\\n", "");sql = sql.replaceAll("\\t", "");sql = sql.replaceAll("[[ ]]{2,}", " ");List<ParameterMapping> list2 = boundSql.getParameterMappings();if (params == null) {} else if (params instanceof Map) {Map<String, Object> map = (Map<String, Object>) params;for (int index = 0; index < list2.size(); index++) {ParameterMapping pm = list2.get(index);Object value = map.get(pm.getProperty());sql = sql.replaceFirst("[?]", value + "");}} else if (params instanceof Long || params instanceof String || params instanceof Integer) {sql = sql.replaceFirst("[?]", params + "");} else {for (int index = 0; index < list2.size(); index++) {ParameterMapping pm = list2.get(index);//String methodName = StringUtil.hump("get_" + pm.getProperty(), "_");String methodName = getMethodName(pm.getProperty());Method method = params.getClass().getMethod(methodName);Object value = method.invoke(params);sql = sql.replaceFirst("[?]", value + "");}}log.info(sql);} catch (Exception e) {e.printStackTrace();}}private String getMethodName(String fieldName) {if (StringUtils.isBlank(fieldName)) {return null;}String firstLetter = fieldName.substring(0, 1).toUpperCase();String methodName = "get" + firstLetter + fieldName.substring(1);return methodName;}
}

说明:namespace指的是你的xml的映射路径,不喜欢的可以写成自己的xml所在路径,

sqlid指的是你xml中方法的名字,无论是单个操作还是批量操作,你的xml中的sql都是单个,这里的批量用的并不是mybatis的foreach操作而是通过传进来的集合批量提交事务到数据库‘'

4.NameSpaceEnum

/*** @author liuminglin* @date 2021/8/23 16:46* @description: TODO*/
public enum NameSpaceEnum {USER_INFO_MAPPER("com.example.recordlog.mapper.UserInfoMapper");public String nameSpace;NameSpaceEnum(String nameSpace) {this.nameSpace = nameSpace;}public String getNameSpace() {return nameSpace;}}

4.mapper接口

@Repository
public interface UserInfoMapper {int deleteByPrimaryKey(Long id);Long batchdeleteByPrimaryKey(List<?> list);int insert(UserInfo record);int insertSelective(UserInfo record);UserInfo selectByPrimaryKey(Long id);UserInfo selectByPrimaryKey();List<UserInfo> selectAll();int updateByPrimaryKeySelective(UserInfo record);int updateByPrimaryKey(UserInfo record);Integer batchUpdateInfo(@Param("userInfoList") List<UserInfo> userInfoList);Integer batchInsert(@Param("userInfoList") List<UserInfo> userInfoList);
}

5.sql.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">
<mapper namespace="com.example.recordlog.mapper.UserInfoMapper"><resultMap id="BaseResultMap" type="com.example.recordlog.bean.UserInfo"><id column="id" jdbcType="BIGINT" property="id"/><result column="user_Id" jdbcType="VARCHAR" property="userId"/><result column="user_name" jdbcType="VARCHAR" property="userName"/><result column="phone" jdbcType="VARCHAR" property="phone"/><result column="hometown" jdbcType="VARCHAR" property="hometown"/><result column="email" jdbcType="VARCHAR" property="email"/><result column="address" jdbcType="VARCHAR" property="address"/><result column="creat_time" jdbcType="TIMESTAMP" property="creatTime"/><result column="modify_date" jdbcType="TIMESTAMP" property="modifyDate"/></resultMap><sql id="Base_Column_List">id, user_Id, user_name, phone, hometown, email, address, creat_time, modify_date</sql><select id="selectByPrimaryKey" parameterType="java.lang.Long" resultMap="BaseResultMap">select<include refid="Base_Column_List"/>from user_infowhere id = #{id,jdbcType=BIGINT}</select><select id="selectAll" resultMap="BaseResultMap">select<include refid="Base_Column_List"/>from user_info</select><delete id="deleteByPrimaryKey" parameterType="java.lang.Long">deletefrom user_infowhere id = #{id,jdbcType=BIGINT}</delete><delete id="batchdeleteByPrimaryKey" parameterType="java.util.List">deletefrom user_infowhere id = #{id,jdbcType=BIGINT}</delete><insert id="insert" parameterType="com.example.recordlog.bean.UserInfo">insert into user_info (id, user_Id, user_name,phone, hometown, email,address, creat_time, modify_date)values (#{id,jdbcType=BIGINT}, #{userId,jdbcType=VARCHAR}, #{userName,jdbcType=VARCHAR},#{phone,jdbcType=VARCHAR}, #{hometown,jdbcType=VARCHAR}, #{email,jdbcType=VARCHAR},#{address,jdbcType=VARCHAR}, #{creatTime,jdbcType=TIMESTAMP}, #{modifyDate,jdbcType=TIMESTAMP})</insert><insert id="insertSelective" parameterType="com.example.recordlog.bean.UserInfo">insert into user_info<trim prefix="(" suffix=")" suffixOverrides=","><if test="id != null">id,</if><if test="userId != null">user_Id,</if><if test="userName != null">user_name,</if><if test="phone != null">phone,</if><if test="hometown != null">hometown,</if><if test="email != null">email,</if><if test="address != null">address,</if><if test="creatTime != null">creat_time,</if><if test="modifyDate != null">modify_date,</if></trim><trim prefix="values (" suffix=")" suffixOverrides=","><if test="id != null">#{id,jdbcType=BIGINT},</if><if test="userId != null">#{userId,jdbcType=VARCHAR},</if><if test="userName != null">#{userName,jdbcType=VARCHAR},</if><if test="phone != null">#{phone,jdbcType=VARCHAR},</if><if test="hometown != null">#{hometown,jdbcType=VARCHAR},</if><if test="email != null">#{email,jdbcType=VARCHAR},</if><if test="address != null">#{address,jdbcType=VARCHAR},</if><if test="creatTime != null">#{creatTime,jdbcType=TIMESTAMP},</if><if test="modifyDate != null">#{modifyDate,jdbcType=TIMESTAMP},</if></trim></insert><update id="updateByPrimaryKeySelective" parameterType="com.example.recordlog.bean.UserInfo">update user_info<set><if test="userId != null">user_Id = #{userId,jdbcType=Ingeger},</if><if test="userName != null">user_name = #{userName,jdbcType=VARCHAR},</if><if test="phone != null">phone = #{phone,jdbcType=VARCHAR},</if><if test="hometown != null">hometown = #{hometown,jdbcType=VARCHAR},</if><if test="email != null">email = #{email,jdbcType=VARCHAR},</if><if test="address != null">address = #{address,jdbcType=VARCHAR},</if><if test="creatTime != null">creat_time = #{creatTime,jdbcType=TIMESTAMP},</if><if test="modifyDate != null">modify_date = #{modifyDate,jdbcType=TIMESTAMP},</if></set>where id = #{id,jdbcType=BIGINT}</update><update id="updateByPrimaryKey" parameterType="com.example.recordlog.bean.UserInfo">update user_infoset user_Id     = #{userId,jdbcType=VARCHAR},user_name   = #{userName,jdbcType=VARCHAR},phone       = #{phone,jdbcType=VARCHAR},hometown    = #{hometown,jdbcType=VARCHAR},email       = #{email,jdbcType=VARCHAR},address     = #{address,jdbcType=VARCHAR},creat_time  = #{creatTime,jdbcType=TIMESTAMP},modify_date = #{modifyDate,jdbcType=TIMESTAMP}where id = #{id,jdbcType=BIGINT}</update>
<!-- mapper.xml使用foreach标签遍历   --><update id="batchUpdateInfo" parameterType="java.util.List"><foreach collection="userInfoList" item="item" index="index" open="" close="" separator=";">update user_info<set><if test="item.userId != null ">user_Id = #{item.userId,jdbcType=VARCHAR},</if><if test="item.userName != null">user_name = #{item.userName,jdbcType=VARCHAR},</if></set>where id = #{item.id,jdbcType=BIGINT}</foreach></update><insert id="batchInsert" parameterType="java.util.List">insert into user_info<trim prefix="(" suffix=")" suffixOverrides=","><if test="id != null">id,</if><if test="userId != null">user_Id,</if><if test="userName != null">user_name,</if><if test="phone != null">phone,</if><if test="hometown != null">hometown,</if><if test="email != null">email,</if><if test="address != null">address,</if><if test="creatTime != null">creat_time,</if><if test="modifyDate != null">modify_date,</if></trim><trim prefix="values (" suffix=")" suffixOverrides=","><if test="id != null">#{id,jdbcType=BIGINT},</if><if test="userId != null">#{userId,jdbcType=VARCHAR},</if><if test="userName != null">#{userName,jdbcType=VARCHAR},</if><if test="phone != null">#{phone,jdbcType=VARCHAR},</if><if test="hometown != null">#{hometown,jdbcType=VARCHAR},</if><if test="email != null">#{email,jdbcType=VARCHAR},</if><if test="address != null">#{address,jdbcType=VARCHAR},</if><if test="creatTime != null">#{creatTime,jdbcType=TIMESTAMP},</if><if test="modifyDate != null">#{modifyDate,jdbcType=TIMESTAMP},</if></trim></insert></mapper>

6.使用


import com.example.recordlog.bean.UserInfo;
import com.example.recordlog.constant.NameSpaceEnum;
import com.example.recordlog.service.BaseDao;
import com.example.recordlog.tools.ResponseUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;import java.util.ArrayList;
import java.util.Date;
import java.util.List;/*** @author* @Date 2021/11/24 10:15* @Desc BaseDao测试controler* @Param* @Return*/@RestController
@RequestMapping("/base")
public class BaseDaoController {@Autowiredprivate BaseDao baseDao;@RequestMapping(value = "/insertList", method = {RequestMethod.GET})public ResponseUtils batchInsertList() {List<UserInfo> userInfoList = new ArrayList<>();for (int i = 1; i <= 200000; i++) {UserInfo userInfo = new UserInfo();userInfo.setId(i);userInfo.setUserId(String.valueOf(i));userInfo.setUserName("张三" + String.valueOf(i));userInfo.setPhone(String.valueOf(i));userInfo.setHometown(String.valueOf(i));userInfo.setEmail(String.valueOf(i));userInfo.setAddress(String.valueOf(i));userInfo.setCreatTime(new Date());userInfo.setModifyDate(new Date());userInfoList.add(userInfo);}System.out.println(userInfoList);baseDao.insertList(NameSpaceEnum.USER_MAPPER, "batchInsert", userInfoList);ResponseUtils responseUtils = ResponseUtils.success(true);return responseUtils;}@RequestMapping(value = "/batchALL", method = {RequestMethod.GET})public ResponseUtils batchALL() {List<UserInfo> userInfoList = new ArrayList<>();for (int i = 1; i <= 200000; i++) {UserInfo userInfo = new UserInfo();userInfo.setId(i);userInfo.setUserId(String.valueOf(i));userInfo.setUserName("张三" + String.valueOf(i));userInfo.setPhone(String.valueOf(i));userInfo.setHometown(String.valueOf(i));userInfo.setEmail(String.valueOf(i));userInfo.setAddress(String.valueOf(i));userInfo.setCreatTime(new Date());userInfo.setModifyDate(new Date());userInfoList.add(userInfo);}System.out.println(userInfoList);baseDao.batchALL(NameSpaceEnum.USER_MAPPER, "batchInsert", userInfoList, 5000);//baseDao.batchALL(NameSpaceEnum.USER_MAPPER, "batchdeleteByPrimaryKey", userInfoList, 5000);//ResponseUtils responseUtils = ResponseUtils.success(true);return ResponseUtils.success(true);}@RequestMapping(value = "/batchRemoveInfo", method = {RequestMethod.GET})//@RequestMapping(value = "/batchRemoveInfo", produces = "application/json;charset=utf-8", method = {RequestMethod.GET})public ResponseUtils batchRemoveInfo() {List<UserInfo> userInfoList = new ArrayList<>();for (int i = 1; i <= 200000; i++) {UserInfo userInfo = new UserInfo();userInfo.setId(i);userInfoList.add(userInfo);}List deleteList = baseDao.deleteList(NameSpaceEnum.USER_MAPPER, "batchdeleteByPrimaryKey", userInfoList);ResponseUtils responseUtils = ResponseUtils.success(deleteList);return responseUtils;}@RequestMapping(value = "/batchExecutor", method = {RequestMethod.GET})public ResponseUtils batchExecutor() {List<UserInfo> userInfoList = new ArrayList<>();for (int i = 1; i <= 200000; i++) {UserInfo userInfo = new UserInfo();userInfo.setId(i);userInfo.setUserId(String.valueOf(i));userInfo.setUserName("张三" + String.valueOf(i));userInfo.setPhone(String.valueOf(i));userInfo.setHometown(String.valueOf(i));userInfo.setEmail(String.valueOf(i));userInfo.setAddress(String.valueOf(i));userInfo.setCreatTime(new Date());userInfo.setModifyDate(new Date());userInfoList.add(userInfo);}System.out.println(userInfoList);baseDao.batchExecutor(NameSpaceEnum.USER_MAPPER, "batchInsert", userInfoList);return ResponseUtils.success(true);}}

本次实现参考了https://www.jb51.net/article/218677.htm

springBoot整合SqlSessionTemplate使用相关推荐

  1. SpringBoot整合RabbitMQ-消息可靠性投递

    本系列是学习SpringBoot整合RabbitMQ的练手,包含服务安装,RabbitMQ整合SpringBoot2.x,消息可靠性投递实现等三篇博客. 学习路径:https://www.imooc. ...

  2. 实践丨SpringBoot整合Mybatis-Plus项目存在Mapper时报错

    本文分享自华为云社区<SpringBoot整合MybatisPlus项目存在Mapper时运行报错的问题分析与修复>,作者:攻城狮Chova . 异常信息 在SpringBoot运行测试M ...

  3. 在springboot整合mybatis遇到的数据库连接不上问题解决

    我用的: jdk1.8 mysql5.1版本 运行出现下列错误 org.mybatis.spring.MyBatisSystemException: nested exception is org.a ...

  4. SpringBoot整合activiti5-业务表单

    系列文章目录(springboot整合activiti5) 在实际的开发当中,除了简单的业务逻辑之外,还有更为复杂的业务,例如常见的主从表单,总之采用Activiti的内置表单和外置表单方式无法满足所 ...

  5. SpringBoot整合:Druid、MyBatis、MyBatis-Plus、多数据源、knife4j、日志、Redis,Redis的Java操作工具类、封装发送电子邮件等等

    SpringBoot笔记 一.SpringBoot 介绍 1.1.SpringBoot简介 SpringBoot 是一个快速开发的框架, 封装了Maven常用依赖.能够快速的整合第三方框架:简化XML ...

  6. SpringBoot整合Mybatis遇到的问题,已解决

    ① 启动报错Failed to configure a DataSource: 'url' attribute is not specified and - 根据报错日志分析是在springboot项 ...

  7. 披荆斩棘之springboot整合atomikos

    披荆斩棘之springboot整合atomikos 1.导入jar包 <dependency><groupId>org.springframework.boot</gro ...

  8. 2022-12-08 SSM项目转springboot整合jsp

    目录 1.添加springboot相关pom依赖 2.Springboot整合jsp 2.1.使用打jar包方式执行 2.2.打war包执行 3.多数据源xml文件配置提取 3.1.数据源bean提取 ...

  9. springboot整合atomikos实践—单体项目多数据源整合

    springboot整合atomikos-单体项目多数据源整合 最近管理后台增加了其他数据库的一些操作,如果只是简单的切换数据源的话使用动态数据源就可以实现,但动态数据源切换容易,加上事务处理就非常麻 ...

最新文章

  1. 使用git更新github上的开源项目
  2. 基础排序算法详解与优化
  3. c的开始,求最大数。
  4. 从view 得到图片
  5. 优雅且高效的使用Chrome Developer Tools
  6. 每个Java开发人员都应该知道的10个基本工具
  7. two sum python_Python | Leetcode 之 Two Sum
  8. 微擎乐慧云智慧农场源码V1.0.1
  9. coreldraw水涟漪怎么做_都说女人是水做的,温柔如水,你怎么一点也不温柔呢?...
  10. linux正则表达式_Linux 中几个正则表达式的用法
  11. Lync 小技巧-39-批量-设置-AD-分机-手机-启用-Lync-设置-Lync-分机
  12. mysql workbench uml_Ubuntu 16.04下UML建模PowerDesigner的替代ERMaster和MySQL Workbench
  13. 中国智能手机行业的江湖事
  14. Unity开发(2)建片草地
  15. RxJava的Single、Completable以及Maybe
  16. 2019年9月19日好货十元内精选包邮
  17. LINUX关闭防火墙、开放特定端口等常用操作
  18. 实验:使用SSMS创建并管理数据库及其基本表(代码版)
  19. 怎样利用计算机电源,如何使用智能手机的电源给笔记本电脑供电
  20. 阿姆斯特朗数 matlab,c中阿姆斯特朗数

热门文章

  1. c语言txt文件写入数学,文本文件输入文件.txt中存有一个学生的学号,性别,年龄,数学,语文,英语三门课的成绩....
  2. Kali [Sqlmap]
  3. 计算机组成原理实验-logisim实现自动售糖机
  4. Matlab 点云高斯曲率计算
  5. vue中如何加入横线_vue
  6. Cesium中加载地形影像切片,以及3dtiles和shp及kml方法
  7. Kaggle手写数字识别(Digit Recognizer)记录
  8. 计算机excel 的分栏在哪,excel分栏在哪里
  9. 苹果手机点击输入框时页面自动放大
  10. python基础(25):StringIO和BytesIO 序列化