首页 专题 文章 代码 归档

自用SpringBoot初始化通用配置

1. 配置文件

server:
  port: 4031

#mybatis
mybatis-plus:
  #  mapper-locations: classpath*:mapper/*.xml
  configuration:
    map-underscore-to-camel-case: true

spring:
  profiles:
    active: dev
  datasource:
  # 数据源其他配置
    initialSize: 5
    minIdle: 5
    maxActive: 20
    maxWait: 60000
    timeBetweenEvictionRunsMillis: 60000
    minEvictableIdleTimeMillis: 300000
    validationQuery: SELECT 1 FROM DUAL
    testWhileIdle: true
    testOnBorrow: false
    testOnReturn: false
    poolPreparedStatements: true


---

#开发环境

spring:
  profiles: dev
  datasource:
    driver-class-name: com.mysql.cj.jdbc.Driver
    username: root
    password: root
    #    url: jdbc:mysql://localhost:3306/vip_video?useSSL=false&serverTimeZone=UTC
    url: jdbc:p6spy:mysql://localhost:3306/vip_video?useSSL=false
    type: com.alibaba.druid.pool.DruidDataSource


mybatis-plus:
  configuration:
    log-impl: org.apache.ibatis.logging.stdout.StdOutImpl

---

#生产环境

spring:
  profiles: prod
  datasource:
    driver-class-name: com.mysql.cj.jdbc.Driver
    username: root
    password: root
    url: jdbc:mysql://localhost:3306/vip_video?useSSL=false&serverTimeZone=UTC
    #    url: jdbc:p6spy:mysql://localhost:3306/vip_video?useSSL=false
    type: com.alibaba.druid.pool.DruidDataSource

logging:
  file:
    name: logs/spring.log

上面配置了Druid的额外配置,所以需要手动创建个Bean

MyConfig

@Configuration
public class MyConfig {

    @ConfigurationProperties(prefix = "spring.datasource")
    @Bean
    public DruidDataSource druidDataSource() {
        return new DruidDataSource();
    }
}

kotlin版本

@Configuration
class MyConfig {
    @ConfigurationProperties(prefix = "spring.datasource")
    @Bean
    fun druidDataSource(): DruidDataSource {
        return DruidDataSource()
    }
}

2. mybatis-plus

MyMetaObjectHandler


@Slf4j
public class MyMetaObjectHandler implements MetaObjectHandler {
    @Override
    public void insertFill(MetaObject metaObject) {
        log.info("开始插入填充.....");
        if (metaObject.hasSetter("createdAt")) {
            log.info("createdAt");
            this.strictInsertFill(metaObject, "createdAt", Date.class, new Date());
        }
        if (metaObject.hasSetter("updatedAt")) {
            log.info("updatedAt");
            this.strictInsertFill(metaObject, "updatedAt", Date.class, new Date());
        }
    }

    @Override
    public void updateFill(MetaObject metaObject) {
        log.info("开始更新填充.....");
        if (metaObject.hasSetter("updatedAt")) {
            log.info("updatedAt");
            this.strictUpdateFill(metaObject, "updatedAt", Date.class, new Date());
        }
    }
}

MybatisPlusConfig

@Configuration
@MapperScan("com.misiai.work_blog.mapper")//扫描
public class MybatisPlusConfig {

    @Bean
    public MyMetaObjectHandler myMetaObjectHandler() {
        return new MyMetaObjectHandler();
    }
}

kotlin版本

@Configuration
@MapperScan("com.misiai.kotlin.mapper") //扫描
class MybatisPlusConfig {
    @Bean
    fun myMetaObjectHandler(): MyMetaObjectHandler {
        return MyMetaObjectHandler()
    }
}
class MyMetaObjectHandler : MetaObjectHandler {
    override fun insertFill(metaObject: MetaObject) {
//        log.info("开始插入填充.....")
        if (metaObject.hasSetter("createdAt")) {
//            log.info("createdAt")
            this.strictInsertFill(metaObject, "createdAt", Date::class.java, Date())
        }
        if (metaObject.hasSetter("updatedAt")) {
//            log.info("updatedAt")
            this.strictInsertFill(metaObject, "updatedAt", Date::class.java, Date())
        }
    }

    override fun updateFill(metaObject: MetaObject) {
//        log.info("开始更新填充.....")
        if (metaObject.hasSetter("updatedAt")) {
//            log.info("updatedAt")
            this.strictUpdateFill(metaObject, "updatedAt", Date::class.java, Date())
        }
    }
}

3. p6spy配置文件

spy.properties

#3.2.1以上使用
modulelist=com.baomidou.mybatisplus.extension.p6spy.MybatisPlusLogFactory,com.p6spy.engine.outage.P6OutageFactory
#3.2.1以下使用或者不配置
#modulelist=com.p6spy.engine.logging.P6LogFactory,com.p6spy.engine.outage.P6OutageFactory
# 自定义日志打印
logMessageFormat=com.baomidou.mybatisplus.extension.p6spy.P6SpyLogger
#日志输出到控制台
appender=com.baomidou.mybatisplus.extension.p6spy.StdoutLogger
# 使用日志系统记录 sql
#appender=com.p6spy.engine.spy.appender.Slf4JLogger
# 设置 p6spy driver 代理
deregisterdrivers=true
# 取消JDBC URL前缀
useprefix=true
# 配置记录 Log 例外,可去掉的结果集有error,info,batch,debug,statement,commit,rollback,result,resultset.
excludecategories=info,debug,result,commit,resultset
# 日期格式
dateformat=yyyy-MM-dd HH:mm:ss
# 实际驱动可多个
#driverlist=org.h2.Driver
# 是否开启慢SQL记录
outagedetection=true
# 慢SQL记录标准 2 秒
outagedetectioninterval=2

4. 一些依赖

<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-jdbc</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-devtools</artifactId>
        <scope>runtime</scope>
        <optional>true</optional>
    </dependency>
    <dependency>
        <groupId>mysql</groupId>
        <artifactId>mysql-connector-java</artifactId>
        <scope>runtime</scope>
    </dependency>
    <dependency>
        <groupId>org.projectlombok</groupId>
        <artifactId>lombok</artifactId>
        <optional>true</optional>
    </dependency>

    <!--数据库mybatis plus-->
    <dependency>
        <groupId>com.baomidou</groupId>
        <artifactId>mybatis-plus-boot-starter</artifactId>
        <version>3.3.1</version>
    </dependency>
    <!--数据源-->
    <dependency>
        <groupId>com.alibaba</groupId>
        <artifactId>druid</artifactId>
        <version>1.1.21</version>
    </dependency>
    <!--p6spy-->
    <dependency>
        <groupId>p6spy</groupId>
        <artifactId>p6spy</artifactId>
        <version>3.8.7</version>
    </dependency>

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
        <scope>test</scope>
        <exclusions>
            <exclusion>
                <groupId>org.junit.vintage</groupId>
                <artifactId>junit-vintage-engine</artifactId>
            </exclusion>
        </exclusions>
    </dependency>
</dependencies>
此文阅读完毕,您可以:分享
二维码图片 扫描关注我们哟