欢迎访问悦橙教程(wld5.com),关注java教程。悦橙教程  java问答|  每日更新
页面导航 : > > 文章正文

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

来源: javaer 分享于  点击 49164 次 点评:42

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


目录
  • 1、环境
  • 2、新建springboot工程
    • 2.1、pom依赖如下
    • 2.2、application配置
  • 3、集成mybatis-plus
    • 4、业务操作
      • 5、测试结果

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

        言归正传,作为技术从业者,还是要多深耕技术。有小伙伴问,在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自动建表内容请搜索3672js教程以前的文章或继续浏览下面的相关文章希望大家以后多多支持3672js教程! 

        您可能感兴趣的文章:
        • SpringBoot 整合MyBatis+MyBatis-Plus+MyBatisX插件使用
        • SpringBoot中使用MyBatis-Plus实现分页接口的详细教程
        • SpringBoot+Mybatis-plus+shardingsphere实现分库分表的方案
        • SpringBoot3.x版本与Mybatis-Plus不兼容问题
        • springboot3集成mybatis-plus报sqlSession异常的问题解决
        • SpringBoot整合Mybatis-Plus、Jwt实现登录token设置
        • springboot项目中mybatis-plus@Mapper注入失败问题
        相关栏目:

        用户点评