springboot整合mybatis-plus

1、jar包依赖

<dependencys> 
    <dependency>
        <!-- mybatis核心依赖-->
        <groupId>com.baomidou</groupId>
        <artifactId>mybatis-plus-boot-starter</artifactId>
        <version>3.5.2</version>
    </dependency>       
    <dependency>
       <!-- 自动生成器 -->
       <groupId>com.baomidou</groupId>
       <artifactId>mybatis-plus-generator</artifactId>
       <version>3.5.2</version>
    </dependency>
    <dependency>
       <!-- 自动生成器使用的映射模版引擎 -->
       <groupId>org.freemarker</groupId>
       <artifactId>freemarker</artifactId>
       <version>2.3.31</version>
    </dependency>
</dependencys>

<build>
        <finalName>bidding</finalName>
        <resources>
            <resource>
                <!--扫描Java目录下的xml-->
                <directory>src/main/java</directory>
                <includes>
                    <include>**/*.xml</include>
                </includes>
            </resource>
            <resource>
                <!--扫描resources目录下的xml-->
                <directory>src/main/resources</directory>
            </resource>
        </resources>
</build>

2、application.yml

spring:
  main:
    allow-bean-definition-overriding: true
  application:
    name: bidding
  mvc:
    pathmatch:
      matching-strategy: ANT_PATH_MATCHER
  cache:
    type: redis   
  datasource:
    url: jdbc:mysql://localhost:3306/local-base?useUnicode=true&characterEncoding=utf8&allowMultiQueries=true&serverTimezone=GMT%2b8&useSSL=false&verifyServerCertificate=false
    username: profiles
    password: dduvWTDJiVvGNIUp!
    driver-class-name: com.mysql.cj.jdbc.Driver
    type: com.alibaba.druid.pool.DruidDataSource
    druid:
      initialSize: 5 #初始化连接池大小
      minIdle: 5 #minIdle设置最小连接池数量
      maxActive: 200 #maxActive设置最大连接池数量
      maxWait: 60000 #获取连接时最大等待时间,单位为毫秒,-1表示无限制
      timeBetweenEvictionRunsMillis: 180000 #Destroy线程检测连接的间隔时间单位是毫秒
      validationQuery: SELECT 1 FROM DUAL  #检测连接是否有效。
      testWhileIdle: true #建议配置为true,不影响性能,并且保证安全性。
      testOnBorrow: false #申请连接时执行validationQuery检测连接是否有效
      testOnReturn: false #归还连接时执行validationQuery检测连接是否有效
mybatis-plus:
  #扫描置指定位置的xml文件
  mapper-locations: classpath*:com/axzo/bidding/service/infrastructure/mapper/**/*.xml
  #实体扫描,多个package用逗号或者分号分隔
  type-aliases-package: com.axzo.bindding.service.infrastructure.persistence
  typeEnumsPackage: com.axzo.bidding.service.domain.shared.enums
  global-config:
    banner: false
    db-config:
      #全局逻辑删除的实体字段名,配置后可以忽略实体字段的@TableLogic 注解
      logic-delete-field: isDelete
      #逻辑删除值
      logic-delete-value: '1'
      #逻辑未删除值
      logic-not-delete-value: '0'
      #主键类型  0:"数据库ID自增", 1:"用户输入ID",2:"全局唯一ID (数字类型唯一ID)", 3:"全局唯一ID UUID";
      id-type: ASSIGN_ID
      #驼峰下划线转换
      table_underline: true
      #数据库大写下划线转换
      capital-mode: false
  configuration:
    map-underscore-to-camel-case: true
    cache-enabled: false
    call-setters-on-nulls: true
    log-impl: org.apache.ibatis.logging.stdout.StdOutImpl

3、代码自动生成器

mport com.baomidou.mybatisplus.generator.FastAutoGenerator;
import com.baomidou.mybatisplus.generator.config.OutputFile;
import com.baomidou.mybatisplus.generator.engine.FreemarkerTemplateEngine;

import java.util.Collections;

/**
 * 项目名称:bidding
 * 类 名 称:CodeGenerator
 * 类 描 述:TODO
 * 创建时间:2022/9/23 19:10
 * 创 建 人:panyong
 */
public class CodeGenerator {
    

    public static void main(String[] args) {

        FastAutoGenerator.create("jdbc:mysql://localhost:3306/localhost-database?useUnicode=true&characterEncoding=utf8&allowMultiQueries=true&serverTimezone=GMT%2b8&useSSL=false&verifyServerCertificate=false",
                        "name", "password")
                .globalConfig(builder -> {
                    builder.author("baomidou") // 设置作者
                            .enableSwagger() // 开启 swagger 模式
                            .fileOverride() // 覆盖已生成文件
                            .outputDir("/Users/user/files"); // 指定输出目录
                })
                .packageConfig(builder -> {
                    builder.parent("com.service.infrastructure.persistence") // 设置父包名
                            .moduleName("test") // 设置父包模块名
                            .pathInfo(Collections.singletonMap(OutputFile.xml, "/Users/user/files")); // 设置mapperXml生成路径
                })
                .strategyConfig(builder -> {
                    builder.addInclude("bi_process_flow_node") // 设置需要生成的表名
                            .addTablePrefix("bi_"); // 设置过滤表前缀
                })
                .templateEngine(new FreemarkerTemplateEngine()) // 使用Freemarker引擎模板,默认的是Velocity引擎模板
                .execute();
    }

}

4、批量更新

public interface ProcessFlowNodeMapper extends BaseMapper<ProcessFlowNodePO> {

    void batchModifyByParam(@Param("list")List<ProcessFlowNodePO> nodes);

}

public interface ProcessFlowRepository {

   void batchModifyByParam(List<ProcessFlowNodePO> nodes);
}


@Component
@Slf4j
public class ProcessFlowRepositoryImpl implements ProcessFlowRepository {

    @Autowired
    ProcessFlowNodeMapper processFlowNodeMapper;

    @Override
    public void batchModifyByParam(List<ProcessFlowNodePO> nodes) {
        processFlowNodeMapper.batchModifyByParam(nodes);
    }

}

5、mapper.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.axzo.bidding.service.infrastructure.mapper.ProcessFlowNodeMapper">

    <update id="batchModifyByParam" parameterType="java.util.List">
        update bi_process_flow_node
        <trim prefix="set" suffixOverrides=",">
            <trim prefix="status =case" suffix="end,">
                <foreach collection="list" item="item" index="index">
                    when id=#{item.id} then #{item.status}
                </foreach>
            </trim>
            <trim prefix="last_update_time =case" suffix="end,">
                <foreach collection="list" item="item" index="index">
                    when id=#{item.id} then #{item.lastUpdateTime}
                </foreach>
            </trim>
        </trim>
        where id in
        <foreach collection="list" index="index" item="item" separator="," open="(" close=")">
            #{item.id}
        </foreach>
    </update>
    
</mapper>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值