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

springboot + mybatis plus + actable 自动建表

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

springboot + mybatis plus + actable 自动建表

1.添加引用,
mybatis-enhance-actable:1.0.1的版本存在问题,比如需要先存在表才能进行建表操作(我遇到的)

 2.添加config,以下两个配置类需要分开写
@Configuration
@ComponentScan(basePackages = {"com.gitee.sunchenbin.mybatis.actable.manager.*"})
public class ActableConfig {
    @Value("${spring.datasource.driver-class-name}")
    private String driver;

    @Value("${spring.datasource.url}")
    private String url;

    @Value("${spring.datasource.username}")
    private String username;

    @Value("${spring.datasource.password}")
    private String password;

    @Bean
    public PropertiesFactoryBean configProperties() throws Exception{
        PropertiesFactoryBean propertiesFactoryBean = new PropertiesFactoryBean();
        PathMatchingResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();
        propertiesFactoryBean.setLocations(resolver.getResources("classpath*:application.properties"));
        return propertiesFactoryBean;
    }

    @Bean
    public DruidDataSource dataSource() {
        DruidDataSource dataSource = new DruidDataSource();
        dataSource.setDriverClassName(driver);
        dataSource.setUrl(url);
        dataSource.setUsername(username);
        dataSource.setPassword(password);
        dataSource.setMaxActive(30);
        dataSource.setInitialSize(10);
        dataSource.setValidationQuery("SELECT 1");
        dataSource.setTestonBorrow(true);
        return dataSource;
    }

    @Bean
    public DataSourceTransactionManager dataSourceTransactionManager() {
        DataSourceTransactionManager dataSourceTransactionManager = new DataSourceTransactionManager();
        dataSourceTransactionManager.setDataSource(dataSource());
        return dataSourceTransactionManager;
    }

    @Bean
    public SqlSessionFactoryBean sqlSessionFactory() throws Exception{
        SqlSessionFactoryBean sqlSessionFactoryBean = new SqlSessionFactoryBean();
        sqlSessionFactoryBean.setDataSource(dataSource());
        PathMatchingResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();
        sqlSessionFactoryBean.setMapperLocations(resolver.getResources("classpath*:com/gitee/sunchenbin/mybatis/actable/mapping/*/*.xml"));
        sqlSessionFactoryBean.setTypeAliasesPackage("com.july.practice.*.entity.*");
        return sqlSessionFactoryBean;
    }
}
@Configuration
@AutoConfigureAfter(ActableConfig.class)
public class MyBatisMapperScannerConfig {
    @Bean
    public MapperScannerConfigurer mapperScannerConfigurer() throws Exception {
        MapperScannerConfigurer mapperScannerConfigurer = new MapperScannerConfigurer();
        mapperScannerConfigurer.setbasePackage("com.july.practice.*.mapper;com.gitee.sunchenbin.mybatis.actable.dao.*");
        mapperScannerConfigurer.setSqlSessionFactoryBeanName("sqlSessionFactory");
        return mapperScannerConfigurer;
    }

}

这两个路径是默认的,可以直接抄:
com.gitee.sunchenbin.mybatis.actable.manager.*
com.gitee.sunchenbin.mybatis.actable.dao.*
3.配置application.properties

mybatis.table.auto=update
mybatis.model.pack=com.july.practice.core.entity
mybatis.database.type=mysql
或者
actable.table.auto=update
actable.model.pack=com.july.practice.core.entity
actable.database.type=mysql
都可以

不过pack的值需要写明确包路径(版本1.0.1和1.3.0都需要写明,不然扫不到包)
像这样是不行的:actable.model.pack=com.july.practice.*.entity

还有就是我试过将参数配在yml文件中,但是不生效;有些文章写在yml中,坑了我好久 -_- !

 还需要在此处添加mapper-locations,我用的mybatis plus;mybatis也需要加的

4.实体类

@Table和@Column支持actable中的,也支持使用javax.persistence包下的@Table和@Column以及@Id

actable官网有说明,可以详细了解一下写法:A.CTable-frame: A.CTable是一个基于Spring和Mybatis的Maven项目,actable 支持springboot,增强了Mybatis的功能,通过配置model注解的方式来创建表,修改表结构,目前仅支持Mysql ,码云上有各个发布版本的记录:https://gitee.com/sunchenbin/mybatis-enhance/releaseshttps://gitee.com/luoye1216/A.CTable-frame

对于@Column,写了该注解就会添加,去掉这个注解就会把字段从数据库中删除(前提是之前存在在数据库中);

到此配置就结束了,以下是我的运行结果:

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

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

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