autotable 使用

前言: 关于依赖引入

由于我配置的是 阿里云 public maven 仓库, 导致一致无法引入依赖,后面把pom、maven的setting.xml 文件的镜像仓库删掉反而可以正常引入了
仓库示例

 <mirror>
	  <id>aliyunmaven</id>
	  <mirrorOf>central</mirrorOf>
	  <name>阿里云公共仓库</name>
	  <url>https://maven.aliyun.com/repository/central</url>
	</mirror>

一、正常引入

引入步骤

  1. 添加依赖
    auto-table-spring-boot-starter
    引入starter
  <dependency>
       <groupId>org.dromara.autotable</groupId>
        <artifactId>auto-table-spring-boot-starter</artifactId>
        <version>2.0.1.1</version>
    </dependency>



<!-- SpringBoot集成mybatis框架 -->
 <dependency>
     <groupId>org.mybatis.spring.boot</groupId>
     <artifactId>mybatis-spring-boot-starter</artifactId>
     <version>2.2.2</version>
 </dependency>



      <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
        </dependency>

yml 配置

spring:
  application:
    name: demo-auto
  datasource:
    driverClassName: com.mysql.cj.jdbc.Driver
    url: jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=utf8&zeroDateTimeBehavior=convertToNull&useSSL=true&serverTimezone=GMT%2B8&keepAlive=true&autoReconnect=true&autoReconnectForPools=true&connectTimeout=120000&socketTimeout=200000&allowMultiQueries=true
    username: root
    password: 123456

autotable依赖

  1. 添加注解 @EnableAutoTable 注解
@EnableAutoTable // 声明使用AutoTable框架
@SpringBootApplication
public class DemoAutoTableApplication {
    public static void main(String[] args) {
        SpringApplication.run(DemoAutoTableApplication.class, args);
    }
}
  1. 激活实体类 @AutoTable 注解
@Data
@AutoTable // 声明表信息(默认表名使用类名转下划线,即'test_table')
public class TestTable {

    private Integer id;

    private String username;

    private Integer age;

    private String phone;
}

二、项目带有 @PostConstruct 并查询数据库的引用

AutoTable启动顺序/时机

根据普通Java引入autotable 修改
在这里插入图片描述

  1. autotable启动
   private static void initDataBase() {
        SqlSessionFactory sessionFactory;
        String resource = "application-druid-pro.yml";
        try {
            // 使用SqlSessionFactoryBuilder加载配置文件
            sessionFactory = buildSqlSessionFactory(resource);
            // 1、设置当前数据源
            SqlSessionFactoryManager.setSqlSessionFactory(sessionFactory);
            // 2、【非必需】配置信息,AutoTableGlobalConfig 是 AutoTable 的全局配置,你所能自定义的配置,都在里面
            PropertyConfig propertyConfig = new PropertyConfig();
            String[] modelPackage = {"com.ruoyi.business.domain"};
            propertyConfig.setModelPackage(modelPackage);
            AutoTableGlobalConfig.setAutoTableProperties(propertyConfig);
            // 3、开始
            AutoTableBootstrap.start();
        } catch (Exception e) {
           log.error("初始化数据库失败===》 {}", e.getMessage());
        }
    }


    /**
     * 从yml 读取数据库配置
     * @param configFile
     * @return {@link SqlSessionFactory }
     * @throws Exception
     */
    public static SqlSessionFactory buildSqlSessionFactory(String configFile) throws Exception {
        InputStream inputStream = Resources.getResourceAsStream(configFile);
        YamlPropertiesFactoryBean yamlPropertiesFactoryBean = new YamlPropertiesFactoryBean();
        yamlPropertiesFactoryBean.setResources(new InputStreamResource(inputStream));
        Properties properties = yamlPropertiesFactoryBean.getObject();
        SqlSessionFactoryBean sessionFactory = new SqlSessionFactoryBean();
        sessionFactory.setConfigurationProperties(properties);
        DruidDataSource datasource = new DruidDataSource();
        datasource.setUrl(properties.get("spring.datasource.druid.master.url").toString());
        datasource.setUsername(properties.get("spring.datasource.druid.master.username").toString());
        datasource.setPassword(properties.get("spring.datasource.druid.master.password").toString());
        datasource.setDriverClassName(properties.get("spring.datasource.driverClassName").toString());
        sessionFactory.setDataSource(datasource);
        return sessionFactory.getObject();
    }
  1. 在启动类 引入 initDataBase() 方法 即可
 public static void main(String[] args)
    {
        initDataBase();
        SpringApplication.run(RuoYiApplication.class, args);
    }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值