栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

代码逆向生成表

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

代码逆向生成表

逆向工程自动生成表-ACTable
  • 引言
  • 1.引入组件
  • 2. 配置组件
    • 2.1 配置mybatisplus
    • 2.2 配置启动属性
    • 启动类型追加包扫描
  • 3.模型加表注解
  • 4.启动完成

引言

在一些小型项目或者新建项目中,可以通过代码注解配置,在项目启动时自动生成或更新表结构。现已经已有开源组件实现了这个目标。ACTable:官网地址。
好处:对于经常修改表,创建表的场景,可以不用双向更新表结构和原型;
缺点:代码入侵,需要像配置swagger文档注释一样编写注释(这是必然的,组件需要获取表字段信息)
下面介绍如何引入到SpringBoot+MybatisPlus项目中。

1.引入组件

maven配置:

       
        
            com.gitee.sunchenbin.mybatis.actable
            mybatis-enhance-actable
            1.5.0.RELEASE
            
                 
                    com.baomidou
                    mybatis-plus-annotation
                
            
        
2. 配置组件 2.1 配置mybatisplus

(ps.如果是mybatis则按照mybatis配置)
在mybatis-plus的mapper路径配置上追加actable的配置:classpath*:com/gitee/sunchenbin/mybatis/actable/mapping//.xml
示例:

#配置mapper xml文件的路径
mybatis-plus.mapper-locations=classpath:com/atguigu/educenter/mapper/xml*.xml
2.2 配置启动属性

示例:

actable.table.auto=update
#(ps:要扫描的model目录)
actable.model.pack=com.atguigu.educenter.entity
actable.database.type=mysql
启动类型追加包扫描

MapperScan:com.gitee.sunchenbin.mybatis.actable.dao.*
ComponentScan:com.gitee.sunchenbin
示例:

@SpringBootApplication
@ComponentScan(basePackages = {"com.atguigu","com.gitee.sunchenbin"})
@MapperScan({"com.atguigu.educenter.mapper","com.gitee.sunchenbin.mybatis.actable.dao.*"})
public class UcenterApplication {

    public static void main(String[] args) {
        SpringApplication.run(UcenterApplication.class, args);
    }
}
3.模型加表注解

示例:

@Data
@TableName(value = "ucenter_member")
public class User implements Serializable {
    private static final long serialVersionUID = 1L;
    @Column(isNull = false, length = 32, comment = "会员id")
    @TableId(value = "id", type = IdType.ID_WORKER_STR)
    private String id;
    @TableField
    @ColumnComment("微信openid")
    private String openid;
    
    @Column(length = 12, comment = "手机号")
    private String mobile;

    @Column(length = 32,comment = "密码")
    private String password;
}
4.启动完成


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/305805.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号