mybatisplus、baomidou动态数据库@DS、sqlSession
SqlSession批量提交
@GetMapping(value = "/test") public String test(String xmh){ List<XmhTest> list = new ArrayList<>(); commonManager.saveBatch(list); @Component public class CommonManager { @Autowired private SqlSessionFactory sqlSessionFactory; public void saveBatch(List<XmhTest> list) { // 使用SqlSession进行插入操作 SqlSession sqlSession = sqlSessionFactory.openSession(ExecutorType.BATCH);// try { XmhTestMapper mapper = sqlSession.getMapper(XmhTestMapper.class); // 假设User对象有一个insert方法,用于插入操作 for (XmhTest xmhTest : list) { int rowsAffected = mapper.insert(xmhTest); System.out.println(rowsAffected); } sqlSession.commit(); } finally { sqlSession.close(); } } } @SpringBootApplication @MapperScan("com.xmh.mapper") public class XmhApplication { public static void main(String[] args) { SpringApplication.run(XmhApplication.class, args); } } public interface XmhTestMapper extends BaseMapper<XmhTest> { int insertXmh(XmhTest dto); @Data @Accessors(chain = true) @TableName("xmh_test") public class XmhTest implements Serializable { private static final long serialVersionUID = 1L; @Schema(description = "主键") @TableId(value = "id", type = IdType.ASSIGN_ID) private Long id; private String title; private int age; @Service @Transactional(rollbackFor = Exception.class) public class XmhTestServiceImpl extends ServiceImpl<XmhTestMapper, XmhTest> implements IXmhTestService { @Override public int insertXmh(XmhTest dto) { return baseMapper.insertXmh(dto); } public interface IXmhTestService extends IService<XmhTest> { int insertXmh(XmhTest dto); <?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.xmh.mapper.XmhTestMapper"> <resultMap id="BaseResultMap" type="com.xmh.model.entity.XmhTest"> <id column="id" property="id"/> <result column="title" property="title"/> <result column="age" property="age"/> </resultMap> <sql id="Basic_Column_List"> id, title, age </sql> <insert id="insertXmh" parameterType ="com.xmh.model.entity.XmhTest"> insert into xmh_test(id, title, age) values(#{id},#{title}, #{age}); </insert> <!-- mybatis相关 --> <dependency> <groupId>com.baomidou</groupId> <artifactId>mybatis-plus-boot-starter</artifactId> <version>3.5.1</version> </dependency> <dependency> <groupId>com.github.pagehelper</groupId> <artifactId>pagehelper-spring-boot-starter</artifactId> <version>1.4.1</version> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>8.0.29</version> <scope>runtime</scope> </dependency> <dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-databind</artifactId> <version>2.13.3</version> </dependency>
LambdaQueryWrapper<FileResource> wrapper = new LambdaQueryWrapper<>(); wrapper.eq(FileResource::getDeleted, Boolean.FALSE); count1 = baseMapper.selectCount(wrapper); QueryWrapper<FileResource> wrappe = new QueryWrapper<>(); wrappe.select("count(distinct OWN_COMPANY_ID) as count"); wrappe.lambda().eq(FileResource::getDeleted, Boolean.FALSE); Map<String,Object> map = this.getMap(wrappe); count2 = (long)map.get("count");
yaml配置
spring: application: name: xmh-service datasource: driver-class-name: ${DS_DRIVER:com.mysql.cj.jdbc.Driver} url: jdbc:mysql://${MYSQL_URL:xx}/${MYSQL_DBNAME:xx}?useUnicode=true&characterEncoding=UTF8&useSSL=false&serverTimezone=Asia/Shanghai username: ${MYSQL_USER:xx} password: ${MYSQL_PASSWORD:xx} type: ${DS_TYPE:com.zaxxer.hikari.HikariDataSource} mybatis-plus: configuration: # 配置日志打印 log-impl: org.apache.ibatis.logging.stdout.StdOutImpl global-config: db-config: # 配置逻辑删除字段和值 logic-delete-field: status logic-delete-value: 1 logic-not-delete-value: 0 logging: level: org.apache.ibatis: DEBUG
全链路不能使用@Transactional public interface XXXBasicMapper { @DS("operating") List<XXXBasicVo> findBasicList(XXXBasicPageDto dto); @Service @DS("operating") public class XXXXXXBasicServiceImpl2 implements IXXXBasicService2 { spring: datasource: hikari: pool-name: hikariCP-biz auto-commit: false read-only: false maximum-pool-size: 10 max-lifetime: 1800000 connection-timeout: 30000 idle-timeout: 600000 #动态数据源配置 dynamic: #主数据源 primary: master datasource: #数据源a01 master: driver-class-name: com.mysql.cj.jdbc.Driver url: jdbc:mysql://${MYSQL_URL:xxx:3306}/${MYSQL_DBNAME:xxx}?useUnicode=true&characterEncoding=UTF8&useSSL=false&serverTimezone=Asia/Shanghai username: ${MYSQL_USER:root} password: ${MYSQL_PASSWORD:xxx} #运营中间库 operating: driver-class-name: com.mysql.cj.jdbc.Driver url: jdbc:mysql://${MYSQL_URL:xxx:3306}/${MYSQL_DBNAME:xxxxxx}?useUnicode=true&characterEncoding=UTF8&useSSL=false&serverTimezone=Asia/Shanghai username: ${MYSQL_USER:root} password: ${MYSQL_PASSWORD:xxx} <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> <version>2.6.8</version> </dependency> <dependency> <groupId>com.baomidou</groupId> <artifactId>mybatis-plus-boot-starter</artifactId> <version>3.5.1</version> </dependency> <dependency> <groupId>com.github.pagehelper</groupId> <artifactId>pagehelper-spring-boot-starter</artifactId> <version>1.4.1</version> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>8.0.29</version> <scope>runtime</scope> </dependency> <dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-databind</artifactId> <version>2.13.3</version> </dependency> <dependency> <groupId>com.baomidou</groupId> <artifactId>dynamic-datasource-spring-boot-starter</artifactId> <version>2.5.6</version> </dependency>
@MapperScan("com.xmh.mapper") public class XmhApplication { src\main\resources\mapper\XxxBasicMapper.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.xmh.mapper.XxxBasicMapper"> <!-- 通用查询映射结果 --> <resultMap id="BaseResultMap" type="com.xmh.model.entity.XxxBasic"> <id column="id" property="id"/> <result column="type" property="type"/> <result column="level" property="level"/> </resultMap> <sql id="Basic_Column_List"> id, type, level, name </sql> <resultMap id="BasicResultMap" type="com.xmh.model.vo.XxxBasicVo"> <id column="id" property="id"/> <result column="type" property="type"/> <result column="level" property="level"/> <result column="create_time" property="createTime"/> <result column="update_time" property="updateTime"/> </resultMap> <sql id="BasicNeed_Column_List"> ab . `code` , ab.`name`, ab.type, ab.`level`, ab.dept_name as offerDept, slw.title, slw.dept_name as proposeDept, slw.create_name, slw.create_time, slw.id </sql> <select id="findBasicList" resultMap="BasicResultMap" parameterType="com.xmh.model.dto.XxxBasicPageDto"> select <include refid="Basic_Column_List"/> from Xxx_basic <trim prefix="where" prefixOverrides="and|or"> node = 1 and status != 2 <if test="name!=null and name!=''"> and name like concat('%', #{name}, '%') </if> <if test="code != null and code != ''"> and code = #{code} </if> <if test="deptCode != null and deptCode != ''"> and dept_code = #{deptCode} </if> <if test="keyword != null and keyword != ''"> and (name like concat('%', #{keyword}, '%') or code like concat('%', #{keyword}, '%')) </if> </trim> <if test="orderBy == null or orderBy == ''"> order by sort desc, update_time desc </if> </select> </mapper>