ACTable +Mybatis-plus+SpringBoot 实现实体类自动建表

1,464 阅读2分钟

本文已参与「新人创作礼」活动

ACTable Bean类自动建表

新公司使用的这套框架,觉得很有意思,比之前工作中面向数据库编程的编码模式方便许多,在这里分享给大家

首先第一步创建一个SpringBoot 的后端项目默认。创建完成后在pom 文件中引入AcTable 和MyBatis-plus的依赖

<!--    mybatis-->
        <dependency>
            <groupId>com.baomidou</groupId>
            <artifactId>mybatis-plus-boot-starter</artifactId>
            <version>3.5.1</version>
        </dependency>
        <!--AC TABLE -->
        <dependency>
            <groupId>com.gitee.sunchenbin.mybatis.actable</groupId>
            <artifactId>mybatis-enhance-actable</artifactId>
            <version>1.5.0.RELEASE</version>
            <exclusions>
                <exclusion>
<!---这里是ACtable 中某个依赖和mybatis 冲突了--->
                    <groupId>com.baomidou</groupId>
                    <artifactId>mybatis-plus-annotation</artifactId>
                </exclusion>
            </exclusions>
        </dependency>

第二步配置yml 连接数据库,数据库名称需要自己先创建好,这给大家展示我的yml mysql配置以及actable 的配置

spring:
  # ??druid???
  datasource:
    name: test
    url: jdbc:mysql://localhost:3306/nczjd?useUnicode=true&characterEncoding=utf8&zeroDateTimeBehavior=convertToNull&useSSL=false&serverTimezone=UTC
    username: root
    password: hw.nbabc
    type: com.alibaba.druid.pool.DruidDataSource
    driver-class-name: com.mysql.jdbc.Driver
actable:
  table:
    auto: update
  model:
    pack: com.hy.nczjd.actable
  database:
    type: mysql
mybatis-plus:
  mapper-locations: classpath*:com/gitee/sunchenbin/mybatis/actable/mapping/*/*.xml,classpath*:mapper/*/*.xml
  configuration:
    log-impl: org.apache.ibatis.logging.stdout.StdOutImpl

第三步写Bean类 像一些@IsAutoIncrement 注解是是否自增。大家搭建项目后可以自己看一下文档,看一下源码的注解

import com.gitee.sunchenbin.mybatis.actable.annotation.*;

import java.math.BigDecimal;
import java.util.Date;

/**
 * 全部采用actable自有的注解
 */
@Table(comment = "actable简单配置")
public class ACTableSimple {

    @IsKey
    @IsAutoIncrement
    private Long id;

    @Column
    @Index
    @IsNotNull
    private String name;

    @Column
    private Date createTime;

    @Column(defaultValue = "false")
    private Boolean isTrue;

    @Column
    private Integer age;

    @Column
    private BigDecimal price;

    @Column
    @Unique
    private String identitycard;

}

第四步在启动页面配置MapperScan ComponentScan注解扫描

主要是这俩个
@MapperScan({"这里配置你本地的项目mapper路径", "com.gitee.sunchenbin.mybatis.actable.dao.*"})
@ComponentScan(basePackages = {"和上面同理*","com.gitee.sunchenbin.mybatis.actable.manager.*"})
本地项目路径可以不配,不影响项目启动,和actable 建表,但是!!!如果不写的话后面项目的controller 接口层会404
我看了好久才发现是这个问题。

最后启动项目

图片描述 会执行一些sql 就自动帮你把表创建好了, 如果后续有表接口改动也只需去改bean 类就可以了。

相关资料 1Actable语雀官网 www.yuque.com/sunchenbin/… 就分享这么多。喜欢的给俺点赞投币!