springboot+mybatis-plus实现自动建表的示例

 更新时间:2024年06月24日 09:08:58   作者:ldcaws  
本文主要介绍了springboot+mybatis-plus实现自动建表的示例,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧

好长时间没输出了,最近工作上也是太多事,领导动不动就拍脑门,那叫一个酸爽~
工作能力的提现不但是技术或解决问题的能力上,还体现在要能立刻满足领导的各种需求,不管是哪方面的需求,这样才能够拍上马屁,步步高升。

言归正传,作为技术从业者,还是要多深耕技术。有小伙伴问,在springboot工程中,持久层采用的mybatis框架,如何能够自动建表,一个团队中各个小伙伴针对新增需求会添加或修改表,但各自调试时,数据库表更新又不及时,造成很大不便。下面记录一下springboot+mybatis-plus实现自动建表。

1、环境

  • springboot2.x
  • mybatis-plus3.5.0
  • mybatis-enhance-actable1.1.1.RELEASE
  • mysql5.7.x
  • idea开发工具

2、新建springboot工程

2.1、pom依赖如下

	<dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>com.baomidou</groupId>
            <artifactId>mybatis-plus-boot-starter</artifactId>
            <version>3.5.0</version>
        </dependency>
        <dependency>
            <groupId>com.gitee.sunchenbin.mybatis.actable</groupId>
            <artifactId>mybatis-enhance-actable</artifactId>
            <version>1.1.1.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
        </dependency>

        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

2.2、application配置

server:
  port: 9001

spring:
  #数据库配置
  datasource:
    url: jdbc:mysql://localhost:3306/test?serverTimezone=UTC&useUnicode=true&characterEncoding=utf-8&useSSL=false
    driver-class-name: com.mysql.cj.jdbc.Driver
    username: root
    password: root

#自动建表设置
mybatis:
  table:
    #create系统启动后,会将所有的表删除掉,然后根据model中配置的结构重新建表,该操作会破坏原有数据;
    #update系统会自动判断哪些表是新建的.哪些字段要修改类型等,哪些字段要删除,哪些字段要新增,该操作不会破坏原有数据;
    #add新增表/新增字段/新增索引新增唯一约束的功能,不做做修改和删除(只在版本1.0.9.RELEASE及以上支持);
    #none系统不做任何处理;
    auto: update
  model:
    #扫描用于创建表的对象的包名
    pack: com.*.*.model
  database:
    #数据库类型目前只支持mysql
    type: mysql

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

注意,mybatis-plus是固定的,采用的是mybatis-enhance-actable插件。

3、集成mybatis-plus

启动类配置如下

@SpringBootApplication
@MapperScan("com.*.*.mapper")
@ComponentScan("com.*.*.*")
@MapperScan({"com.gitee.sunchenbin.mybatis.actable.dao.*"})//固定的
@ComponentScan("com.gitee.sunchenbin.mybatis.actable.manager.*")//固定的
public class SpringbootMybatisPlus2Application {

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

}

注意,固定的是采用的是mybatis-enhance-actable插件。

实体类配置如下

@Data
@Table(name = "t_test")
@TableName(value = "t_test")
public class Test {

    @TableId(value = "id",type = IdType.AUTO)
    @IsKey
    @IsAutoIncrement
    @Column(name = "id",comment = "主键")
    private Long id;

    @TableField(value = "name")
    @Column(name = "",comment = "名称",isNull = false)
    private String name;

    @TableField(value = "create_time")
    @Column(name = "create_time",comment = "创建时间")
    private String creatTime;

    @Column(name = "update_time",comment = "修改时间")
    private String updateTime;

}

注解分别为mybatisplus提供的、mybatis-enhance-actable提供的,前者的注解是用来进行持久层操作的(增删改查),后者的注解是用来进行自动建表的。

4、业务操作

mapper类如下

@Mapper
public interface TestMapper extends BaseMapper<Test> {
}

service类如下

public interface TestService extends IService<Test> {}
@Service
public class TestServiceImpl extends ServiceImpl<TestMapper, Test> implements TestService {
}

controller类如下

@RestController
@RequiredArgsConstructor
public class TestController {

    private final TestServiceImpl testService;

    @GetMapping("/hello")
    public Object hello() {
        return "hello";
    }

    @GetMapping("/save")
    public Object save() {
        Test test = new Test();
        test.setName("caocao");
        testService.save(test);
        return test;
    }

    @GetMapping("/list")
    public Object list() {
        List<Test> list = testService.list();
        return list;
    }
}

5、测试结果

启动服务,结果类似如下

在这里插入图片描述

在这里插入图片描述

访问结果如下

在这里插入图片描述

在这里插入图片描述

在这里插入图片描述

到此这篇关于springboot+mybatis-plus实现自动建表的示例的文章就介绍到这了,更多相关springboot mybatisplus自动建表内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家! 

相关文章

  • springboot 监控管理模块搭建的方法

    springboot 监控管理模块搭建的方法

    本篇文章主要介绍了springboot 监控管理模块搭建的方法,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2018-03-03
  • MyBatis-Plus如何通过注解使用TypeHandler

    MyBatis-Plus如何通过注解使用TypeHandler

    这篇文章主要介绍了MyBatis-Plus如何通过注解使用TypeHandler,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2022-01-01
  • SpringBoot应用部署到外置Tomcat的实现

    SpringBoot应用部署到外置Tomcat的实现

    SpringBoot内置tomcat使用很方便,本文主要介绍了SpringBoot应用部署到外置Tomcat的实现,具有一定的参考价值,感兴趣的可以了解一下
    2024-07-07
  • Spring Cloud Gateway重试机制原理解析

    Spring Cloud Gateway重试机制原理解析

    这篇文章主要介绍了Spring Cloud Gateway重试机制原理解析,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
    2020-08-08
  • 从千千静听歌词服务器获取lrc歌词示例分享

    从千千静听歌词服务器获取lrc歌词示例分享

    这篇文章主要介绍了使用PHP从千千静听歌词服务器获取lrc歌词的方法,大家参考使用吧
    2014-01-01
  • 解决Springboot get请求是参数过长的情况

    解决Springboot get请求是参数过长的情况

    这篇文章主要介绍了解决Springboot get请求是参数过长的情况,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2020-09-09
  • springBoot集成shiro实现权限刷新

    springBoot集成shiro实现权限刷新

    在SpringBoot项目中集成Shiro进行权限管理,包括基础配置引入依赖、创建Shiro配置类以及用户认证与授权实现,具有一定的参考价值,感兴趣的可以了解一下
    2024-11-11
  • java ZipFile如何将多级目录压缩

    java ZipFile如何将多级目录压缩

    这篇文章主要介绍了java ZipFile如何将多级目录压缩问题,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教
    2024-08-08
  • Java面试题冲刺第一天--基础篇1

    Java面试题冲刺第一天--基础篇1

    这篇文章主要为大家分享了最有价值的三道java面试题,涵盖内容全面,包括数据结构和算法相关的题目、经典面试编程题等,感兴趣的小伙伴们可以参考一下
    2021-07-07
  • Java手写线程池的实现方法

    Java手写线程池的实现方法

    这篇文章主要为大家详细介绍了Java手写线程池的实现方法,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2018-03-03

最新评论