依赖:

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!--mybatis-plus-->
<dependency>
    <groupId>com.baomidou</groupId>
    <artifactId>mybatis-plus-boot-starter</artifactId>
    <version>3.4.0</version>
</dependency>
<dependency>
    <groupId>mysql</groupId>
    <artifactId>mysql-connector-java</artifactId>
    <version>8.0.20</version>
</dependency>
<!--mybatis-plus自动建表-->
<dependency>
    <groupId>com.gitee.sunchenbin.mybatis.actable</groupId>
    <artifactId>mybatis-enhance-actable</artifactId>
    <version>1.5.0.RELEASE</version>
</dependency>
<dependency>
    <groupId>org.projectlombok</groupId>
    <artifactId>lombok</artifactId>
    <version>1.18.12</version>
</dependency>
<dependency>
    <groupId>com.zaxxer</groupId>
    <artifactId>HikariCP</artifactId>
</dependency>

application配置 

spring:
  application:
    name: mybaties-plus-service
#数据库配置
  datasource:
    driver-class-name: com.mysql.cj.jdbc.Driver
    type: com.zaxxer.hikari.HikariDataSource
    url: jdbc:mysql://127.0.0.1:3306/zhongdemo?allowPublicKeyRetrieval=true&userSSL=true&useUnicode=true&characterEncoding=UTF8&serverTimezone=Asia/Shanghai
    username: zhong
    password: zhong123456
    hikari:
      minimum-idle: 10
      maximum-pool-size: 20
      idle-timeout: 30000
      max-lifetime: 1800000
      connection-timeout: 30000
      connection-test-query: SELECT 1
      validation-timeout: 3000
#自动建表设置
mybatis:
  table:
    auto: update
  model:
    #扫描用于创建表的对象的包名
    pack: com.zhong.spring.mybatiesplusdemo.entity
  database:
    type: mysql

#mybatis
mybatis-plus:
  mapper-locations: classpath*:com/gitee/sunchenbin/mybatis/actable/mapping/*/*.xml   #固定的

 启动类配置

import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

/**
 * @author ZSC
 * @date 2022/8/30 - 9:10
 */
@MapperScan("com.gitee.sunchenbin.mybatis.actable.dao.*")  //固定的
//"com.gitee.sunchenbin.mybatis.actable.manager.*  必须要有
@SpringBootApplication(scanBasePackages = {"com.zhong.spring.mybatiesplusdemo.*", "com.gitee.sunchenbin.mybatis.actable.manager.*"})
public class MybatiesPlusDemoApplication {

    public static void main(String[] args){
        SpringApplication.run(MybatiesPlusDemoApplication.class, args);
    }
}

实体映射类配置

@Data
@TableName(value = "zhong_test")
public class TestEntity {

    @Column(name = "id", type = MySqlTypeConstant.BIGINT, isNull = false, isKey = true, comment = "id")
    private Long id;


    @TableField(value = "name")
    @Column(name = "name", type = MySqlTypeConstant.VARCHAR, length = 64, comment = "名称")
    private String name;


    /**
     * IsNativeDefValue:开启默认值原生模式
     * 原生模式介绍:默认是false表示非原生,此时value只支持字符串形式,会将value值以字符串的形式设置到字段的默认值,例如value="aa" 即sql为 DEFAULT "aa"
     * 如果设置isNative=true,此时如果value="CURRENT_TIMESTAMP",即sql为 DEFAULT CURRENT_TIMESTAMP
     */
    @TableField(value = "create_time")
    @DefaultValue(value = "CURRENT_TIMESTAMP")
    @IsNativeDefValue
    @Column(name = "create_time", type = MySqlTypeConstant.TIMESTAMP, isNull = false, comment = "创建时间")
    private String creatTime;

}

结果:

 官网:SpringBoot配置方式 · 语雀

Logo

华为开发者空间,是为全球开发者打造的专属开发空间,汇聚了华为优质开发资源及工具,致力于让每一位开发者拥有一台云主机,基于华为根生态开发、创新。

更多推荐