springboot通过mybatis的插件实现⾃动创建或更新表
今天在⼀个博客看到的关于引⼊mybatis的插件actable实现⾃动创建或者更新表,然后我就照着他的博客做了,但还是踩了很多坑,因为很多东西他都没有说清楚。很⽆奈,下⾯贴上我⾃⼰的代码。这个插件⽬前只⽀持mysql。
⾸先需要引⼊三个依赖,第⼀个依赖是ACtable的依赖,其他两个是⽀持它的依赖(原博主只丢出了⼀个依赖,我在这踩了个坑,⼀直报错bean⽆法注⼊):
<!--增加A.Ctable开源框架,仅限于mysql数据库-->
<dependency>
<groupId>com.batis.actable</groupId>
<artifactId>mybatis-enhance-actable</artifactId>
<version>1.0.3</version>
</dependency>
<!-- mvnrepository/artifact/org.apachemons/commons-lang3 -->
<dependency>
<groupId>org.apachemons</groupId>
<artifactId>commons-lang3</artifactId>
<version>3.4</version>
</dependency>
<dependency>
<groupId>net.sf.json-lib</groupId>
<artifactId>json-lib</artifactId>
<version>2.4</version>
<classifier>jdk15</classifier>
<exclusions>
<exclusion>
<artifactId>commons-logging</artifactId>
<groupId>commons-logging</groupId>
</exclusion>
</exclusions>
</dependency>
在springboot的配置⽂件中加⼊ACtable的配置:
application.properties:
#当mybatis.table.auto=create时,系统启动后,会将所有的表删除掉,然后根据model中配置的结构重新建表,该操作会破坏原有数据。
#当mybatis.table.auto=update时,系统会⾃动判断哪些表是新建的,哪些字段要修改类型等,哪些字段要删除,哪些字段要新增,该操作不会破坏原有数据。#当mybatis.table.auto=none时,系统不做任何处理。
mybatis.table.auto=update
#del.pack这个配置是⽤来配置要扫描的⽤于创建表的对象的包名
#数据库为mysql
pe=mysql
然后就是在⽬录下创建两个config配置类,我的⽬录结构如下:
我们需要创建的类是,MybatisTableConfig类和MybatisMapperScannerConfig类,具体代码如下(原博主并没有贴出需要导⼊的类,我⼜在这踩了个坑)
MybatisTableConfig:
package fig;
import com.alibaba.druid.pool.DruidDataSource;
batis.spring.SqlSessionFactoryBean;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.fig.PropertiesFactoryBean;
import t.annotation.Bean;
import t.annotation.ComponentScan;
import t.annotation.Configuration;
import io.support.PathMatchingResourcePatternResolver;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
@Configuration
@ComponentScan(basePackages = {"com.batis.actable.manager.*"})
public class MybatisTableConfig {
@Value("${spring.datasource.driverClassName}")
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.Resources("classpath*:application.properties"));
return propertiesFactoryBean;
}
@Bean
@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.Resources("classpath*:com/gitee/sunchenbin/mybatis/actable/mapping/*/*.xml")); sqlSessionFactoryBean.setTypeAliasesPackage("ity.*");
return sqlSessionFactoryBean;
}
}
MybatisMapperScannerConfig:
package fig;
batis.spring.mapper.MapperScannerConfigurer;springboot结构
import org.springframework.boot.autoconfigure.AutoConfigureAfter;
import t.annotation.Bean;
import t.annotation.Configuration;
@Configuration
@AutoConfigureAfter(MybatisTableConfig.class)
public class MyBatisMapperScannerConfig {
@Bean
public MapperScannerConfigurer mapperScannerConfigurer() throws Exception{
MapperScannerConfigurer mapperScannerConfigurer = new MapperScannerConfigurer();
mapperScannerConfigurer.setBasePackage("com.xiangzhang.mapper.*;com.batis.actable.dao.*");
mapperScannerConfigurer.setSqlSessionFactoryBeanName("sqlSessionFactory");
return mapperScannerConfigurer;
}
}
ity是实体类的包名, com.xiangzhang.mapper是实现注解⽅式的SQL接⼝
最后就是实体类啦,我在这⾥使⽤了lombok的Data注解,省去了getter和setter:
User:
package ity;
import com.batis.actable.annotation.Column;
import com.batis.actable.annotation.Table;
import com.batis.actablemand.BaseModel;
import com.stants.MySqlTypeConstant;
import lombok.Data;
@Data
@Table(name = "hero") //表名
public class User extends BaseModel {
/
**
* 属性上的注解定义了创建表时的各个字段的属性
*/
@Column(name = "id",type = MySqlTypeConstant.INT,length = 11,isKey = true,isAutoIncrement = true) private int id;
@Column(name = "name",type = MySqlTypeConstant.VARCHAR,length = 111)
private String name;
@Column(name = "hp",type = MySqlTypeConstant.DOUBLE,length = 16,decimalLength = 2)
private double hp;
@Column(name = "damage",type = MySqlTypeConstant.DOUBLE,length = 16,decimalLength = 2)
private double damage;
}
欢迎访问我的新博客:
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论