当前位置: 首页 > news >正文

springBoot整合SqlSessionTemplate使用

实际开发中我们操作数据库持久化,总是需要写重复的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 {

    @Autowired
    private SqlSessionTemplate sqlSessionTemplate;


    @Override
    public <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);
        }
    }


    @Override
    public <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)
    @Override
    public <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)
    @Override
    public <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)
    @Override
    public <T> long insert(String namespace, String sqlId, T params) {
        return update(namespace, sqlId, params);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public <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)
    @Override
    public <T> int delete(String namespace, String sqlId, T params) {
        return update(namespace, sqlId, params);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public <T> List<Long> deleteList(String namespace, String sqlId, List<T> list) {
        return updateList(namespace, sqlId, list);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public <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)
    @Override
    public <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("

“, “”);
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 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 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("

“, “”);
sql = sql.replaceAll(”\t", “”);
sql = sql.replaceAll(“[[ ]]{2,}”, " ");
List 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_info
        where 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">
        delete
        from user_info
        where id = #{id,jdbcType=BIGINT}
    </delete>

    <delete id="batchdeleteByPrimaryKey" parameterType="java.util.List">
        delete
        from user_info
        where 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_info
        set 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 {

    @Autowired
    private 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

相关文章:

  • jieba—第三方中文分词函数库
  • Python桌面文件清理脚本
  • STM32开发板在RT-Thread中使用segger_rtt软件包
  • SpringBoot异常处理机制之自定义404、500错误提示页面
  • Debezium系列之:深入理解Debezium是如何处理bigint unsigned类型的字段
  • MySQL基础篇【第四篇】| 连接查询、子查询(嵌套)
  • 【Linux】如何实现虚拟机系统与本地系统的通信连接
  • MyBatis 操作数据库
  • Linux权限
  • 【深度学习】6-卷积过程中数据的结构变化
  • 牛客刷SQL
  • 如何高效的实现大型设备中卫星信号的传输和分配?
  • C语言描述数据结构 —— 二叉树(3)普通二叉树
  • Nginx rewrite
  • 【基于Arduino的垃圾分类装置开发教程一】
  • 网络传输文件的问题
  • 【108天】Java——《Head First Java》笔记(第1-4章)
  • 【翻译】babel对TC39装饰器草案的实现
  • 【剑指offer】让抽象问题具体化
  • 3.7、@ResponseBody 和 @RestController
  • C# 免费离线人脸识别 2.0 Demo
  • canvas实际项目操作,包含:线条,圆形,扇形,图片绘制,图片圆角遮罩,矩形,弧形文字...
  • Docker 1.12实践:Docker Service、Stack与分布式应用捆绑包
  • electron原来这么简单----打包你的react、VUE桌面应用程序
  • gulp 教程
  • HTTP中GET与POST的区别 99%的错误认识
  • Java反射-动态类加载和重新加载
  • JS创建对象模式及其对象原型链探究(一):Object模式
  • Linux Process Manage
  • Median of Two Sorted Arrays
  • Node项目之评分系统(二)- 数据库设计
  • Redis学习笔记 - pipline(流水线、管道)
  • RxJS: 简单入门
  • tab.js分享及浏览器兼容性问题汇总
  • 关于字符编码你应该知道的事情
  • 小程序 setData 学问多
  • 一个完整Java Web项目背后的密码
  • 用简单代码看卷积组块发展
  • ​Python 3 新特性:类型注解
  • (javascript)再说document.body.scrollTop的使用问题
  • (附源码)计算机毕业设计ssm电影分享网站
  • (学习日记)2024.04.04:UCOSIII第三十二节:计数信号量实验
  • (转) SpringBoot:使用spring-boot-devtools进行热部署以及不生效的问题解决
  • ***检测工具之RKHunter AIDE
  • **CI中自动类加载的用法总结
  • .netcore 如何获取系统中所有session_如何把百度推广中获取的线索(基木鱼,电话,百度商桥等)同步到企业微信或者企业CRM等企业营销系统中...
  • .Net高阶异常处理第二篇~~ dump进阶之MiniDumpWriter
  • .NET微信公众号开发-2.0创建自定义菜单
  • @ModelAttribute 注解
  • @我的前任是个极品 微博分析
  • [ACTF2020 新生赛]Upload 1
  • [Asp.net MVC]Asp.net MVC5系列——Razor语法
  • [BZOJ 4034][HAOI2015]T2 [树链剖分]
  • [Codeforces] combinatorics (R1600) Part.2
  • [C和指针].(美)Kenneth.A.Reek(ED2000.COM)pdf