java自动建表方案_mybatis自动建表的实现方法

1.添加ACTable依赖

com.gitee.sunchenbin.mybatis.actable

mybatis-enhance-actable

1.1.1.RELEASE

2.配置(在此需要注意配置的路径部分需要改成自己项目路径)

com.gitee.sunchenbin.mybatis.actable

mybatis-enhance-actable

1.1.1.RELEASE

3.添加配置类

package com.jpxx.clsh.autoconfig;

import com.alibaba.druid.pool.DruidDataSource;

import org.mybatis.spring.SqlSessionFactoryBean;

import org.springframework.beans.factory.annotation.Value;

import org.springframework.beans.factory.config.PropertiesFactoryBean;

import org.springframework.context.annotation.Bean;

import org.springframework.context.annotation.ComponentScan;

import org.springframework.context.annotation.Configuration;

import org.springframework.core.io.support.PathMatchingResourcePatternResolver;

import org.springframework.jdbc.datasource.DataSourceTransactionManager;

/**

* @ClassName TestConfig

* @Description

* @Author Administrator

* @Date 2020/11/2 0002 11:30

* @Version 1.0

*/

@Configuration

@ComponentScan(basePackages = {"com.gitee.sunchenbin.mybatis.actable.manager.*"})

public class DataSourceConfig{

//此处的路径按照yml或properties文件路径

@Value("${jpxx.datasource.druid.driverClassName}")

private String driver;

@Value("${jpxx.datasource.druid.url}")

private String url;

@Value("${jpxx.datasource.druid.username}")

private String username;

@Value("${jpxx.datasource.druid.password}")

private String password;

@Bean

public PropertiesFactoryBean configProperties() throws Exception{

PropertiesFactoryBean propertiesFactoryBean = new PropertiesFactoryBean();

PathMatchingResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();

propertiesFactoryBean.setLocations(resolver.getResources("classpath*:application.yml"));

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.jpxx.clsh.entity.*");

return sqlSessionFactoryBean;

}

}

package com.jpxx.clsh.autoconfig;

/**

* @ClassName MyBatisMapperScannerConfig

* @Description

* @Author Administrator

* @Date 2020/11/2 0002 10:15

* @Version 1.0

*/

import org.mybatis.spring.mapper.MapperScannerConfigurer;

import org.springframework.boot.autoconfigure.AutoConfigureAfter;

import org.springframework.context.annotation.Bean;

import org.springframework.context.annotation.Configuration;

@Configuration

@AutoConfigureAfter(DataSourceConfig.class)

public class MyBatisMapperScannerConfig {

@Bean

public MapperScannerConfigurer mapperScannerConfigurer() throws Exception{

MapperScannerConfigurer mapperScannerConfigurer = new MapperScannerConfigurer();

mapperScannerConfigurer.setBasePackage("com.jpxx.clsh.dao.*;com.gitee.sunchenbin.mybatis.actable.dao.*");

mapperScannerConfigurer.setSqlSessionFactoryBeanName("sqlSessionFactory");

return mapperScannerConfigurer;

}

}

4.实体类

package com.jpxx.clsh.entity;

import com.gitee.sunchenbin.mybatis.actable.annotation.Column;

import com.gitee.sunchenbin.mybatis.actable.annotation.Table;

import com.gitee.sunchenbin.mybatis.actable.constants.MySqlTypeConstant;

import lombok.Data;

/**

* @ClassName Test

* @Description

* @Author Administrator

* @Date 2020/10/30 0030 16:48

* @Version 1.0

*/

@Data

@Table(name = "aaaaaaaaaaaaaa")

public class Test {

@Column(name = "role_id", type = MySqlTypeConstant.INT, isNull = false,isKey = true, isAutoIncrement = true, comment = "自增id")

private Long id;

@Column(name = "name", type = MySqlTypeConstant.VARCHAR, isNull = false, length = 20, comment = "角色名字")

private String name;

@Column(name = "name_zh", type = MySqlTypeConstant.VARCHAR, isNull = true, length = 20, comment = "角色的中文名字")

private String name_zh;

}

运行日志

2bacad923175248610233f4724706058.png

0306b9360169a38fccf42837bc11900e.png

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值