原文:https://blog.csdn.net/m0_51285952/article/details/116128800
<!--mybatis-plus自动建表功能-->
<dependency>
<groupId>com.gitee.sunchenbin.mybatis.actable</groupId>
<artifactId>mybatis-enhance-actable</artifactId>
<version>1.4.6.RELEASE</version>
</dependency>
<!--springboot启动mybates-plus-->
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.1.2</version>
</dependency>
<!--mybaties-plus-->
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus</artifactId>
<version>3.1.2</version>
</dependency>
<!--对mybaties-plus的加强-->
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-extension</artifactId>
<version>3.1.2</version>
</dependency>
<!--lomok-->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.2</version>
</dependency>
配置
server:
port: 8011
mybatis:
table:
auto: update
#create 系统启动后,会将所有的表删除掉,然后根据model中配置的结构重新建表,该操作会破坏原有数据。
#update 系统会自动判断哪些表是新建的,哪些字段要修改类型等,哪些字段要删除,哪些字段要新增,该操作不会破坏原有数据。
#none 系统不做任何处理。
#add 新增表/新增字段/新增索引/新增唯一约束的功能,不做做修改和删除 (只在版本1.0.9.RELEASE及以上支持)。
model:
pack: com.example.ac.entity #扫描用于创建表的对象的包名,多个包用“,”隔开
database:
type: mysql #数据库类型 目前只支持mysql
spring:
datasource:
url: jdbc:mysql://192.168.77.130:3306/jiangluohao?useSSL=false&&characterEncoding=UTF-8
driver-class-name: com.mysql.cj.jdbc.Driver
username: root
password: 123456
mybatis-plus:
mapper-locations: classpath*:com/gitee/sunchenbin/mybatis/actable/mapping/*/*.xml
#mapper-locations内容逗号前面是自己项目的xml文件所放位置,比如自己的xml文件在resources:mapper:userMapper.xml那么逗号前面的路径更改为classpath:mapper/*.xml逗号后面的直接复制
启动类
@SpringBootApplication
@MapperScan({"com.gitee.sunchenbin.mybatis.actable.dao.*","com.person.provider.mapper"}) //逗号前面直接复制不要更改,注意这里可以指定多个包,逗号间隔,逗号后面写自己的dao包路径
@ComponentScan({"com.gitee.sunchenbin.mybatis.actable.manager.*","com.person"}) //逗号前面直接复制不要更改,逗号后面写自己的controller层包路径
public class ProviderApp {
public static void main(String[] args){
SpringApplication.run(ProviderApp.class,args);
}
}
实体
package com.person.provider.entity;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableId;
import com.gitee.sunchenbin.mybatis.actable.annotation.Column;
import com.gitee.sunchenbin.mybatis.actable.annotation.IsAutoIncrement;
import com.gitee.sunchenbin.mybatis.actable.annotation.IsKey;
import lombok.Getter;
import lombok.Setter;
import java.util.Date;
@Getter //lombok注解,不用管
@Setter //lombok注解,不用管
public class SuperEntity {
/**
* 主键
*/
@TableId(type = IdType.AUTO) //mybatis-plus主键注解
@IsKey //actable主键注解
@IsAutoIncrement //自增
@Column //对应数据库字段,不配置name会直接采用属性名作为字段名
private Long id;
/**
* 创建时间
*/
@Column(name = "create_time",comment = "创建时间") // name指定数据库字段名,comment为备注
private Date createTime;
/**
* 最后修改时间
*/
@Column(name = "update_time",comment = "最后修改时间")
private Date updateTime;
}
package com.person.provider.entity;
import com.gitee.sunchenbin.mybatis.actable.annotation.Column;
import com.gitee.sunchenbin.mybatis.actable.annotation.Table;
import lombok.Data;
@Data //lombok注解,不用管
@Table(name = "t_user") //对应数据库表名,如果更改表名需要同步更改数据库表名,不然会重新创建表。
public class User extends SuperEntity {
@Column
private String username;
@Column
private String password;
}