溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

使用mybatis實現(xiàn)自動建表的方法

發(fā)布時間:2020-11-03 14:59:18 來源:億速云 閱讀:1502 作者:Leah 欄目:開發(fā)技術

今天就跟大家聊聊有關使用mybatis實現(xiàn)自動建表的方法,可能很多人都不太了解,為了讓大家更加了解,小編給大家總結了以下內容,希望大家根據這篇文章可以有所收獲。

1.添加ACTable依賴

   <!--mybatis-plus自動建表-->
    <dependency>
      <groupId>com.gitee.sunchenbin.mybatis.actable</groupId>
      <artifactId>mybatis-enhance-actable</artifactId>
      <version>1.1.1.RELEASE</version>
    </dependency>

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

   <!--mybatis-plus自動建表-->
    <dependency>
      <groupId>com.gitee.sunchenbin.mybatis.actable</groupId>
      <artifactId>mybatis-enhance-actable</artifactId>
      <version>1.1.1.RELEASE</version>
    </dependency>

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;
}

運行日志

使用mybatis實現(xiàn)自動建表的方法

使用mybatis實現(xiàn)自動建表的方法

看完上述內容,你們對使用mybatis實現(xiàn)自動建表的方法有進一步的了解嗎?如果還想了解更多知識或者相關內容,請關注億速云行業(yè)資訊頻道,感謝大家的支持。

向AI問一下細節(jié)

免責聲明:本站發(fā)布的內容(圖片、視頻和文字)以原創(chuàng)、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。

AI