1. pom 依赖
<!--mybatis 自动建表-->
<dependency>
<groupId>com.gitee.sunchenbin.mybatis.actable</groupId>
<artifactId>mybatis-enhance-actable</artifactId>
<version>${mybatis-enhance-actable}</version>
</dependency>
<dependency>
<groupId>commons-lang</groupId>
<artifactId>commons-lang</artifactId>
<version>${commons-lang.version}</version>
</dependency>
2. resources 下新建 application.properties
#自动建表配置
mybatis.table.auto=update
mybatis.model.pack=com.artical.boot.domain
mybatis.database.type=mysql
3. 新建 MybatisTableConfig.java
package com.artical.boot.web.config.mybatis;
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;
/**
* @Author: DaiKuangYin
* @Date: 2019/11/7 10:49
*/
@Configuration
@ComponentScan(basePackages = {"com.gitee.sunchenbin.mybatis.actable.manager.*"})
public class MybatisTableConfig {
@Value("${spring.datasource.druid.driver-class-name}")
private String driver;
@Value("${spring.datasource.druid.url}")
private String url;
@Value("${spring.datasource.druid.username}")
private String username;
@Value("${spring.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.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.artical.boot.domain.*");
return sqlSessionFactoryBean;
}
}
其中 com.artical.boot.domain.* 为个人的entry包
4. 新建 MyBatisMapperScannerConfig.java
package com.artical.boot.web.config.mybatis;
import org.mybatis.spring.mapper.MapperScannerConfigurer;
import org.springframework.boot.autoconfigure.AutoConfigureAfter;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
/**
* @Author: DaiKuangYin
* @Date: 2019/11/7 10:50
*/
@Configuration
@AutoConfigureAfter(MybatisTableConfig.class)
public class MyBatisMapperScannerConfig {
@Bean
public MapperScannerConfigurer mapperScannerConfigurer() throws Exception{
MapperScannerConfigurer mapperScannerConfigurer = new MapperScannerConfigurer();
mapperScannerConfigurer.setBasePackage("com.artical.boot.mapper.*;com.gitee.sunchenbin.mybatis.actable.dao.*");
mapperScannerConfigurer.setSqlSessionFactoryBeanName("sqlSessionFactory");
return mapperScannerConfigurer;
}
}
其中 com.artical.boot.mapper.* 为个人的mapper包
5. 增加 @Table @Column
package com.artical.boot.domain;
import com.gitee.sunchenbin.mybatis.actable.annotation.Column;
import com.gitee.sunchenbin.mybatis.actable.annotation.Table;
import com.gitee.sunchenbin.mybatis.actable.constants.MySqlTypeConstant;
/**
* @Author: DaiKuangYin
* @Date: 2019/11/7 10:30
*/
@Table(name = "sys_user")
public class SysUser extends BaseBean{
@Column(name = "user_id",type = MySqlTypeConstant.INT,length = 20,isKey = true,isAutoIncrement = true)
private Integer userId;
@Column(name = "user_account",type = MySqlTypeConstant.VARCHAR,length = 20)
private String userAccount;//用户账号
@Column(name = "user_password",type = MySqlTypeConstant.VARCHAR,length = 20)
private String userPassword;//用户密码
}
版权声明:本文为weixin_43139183原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。