springboot+mybatis自动创建表

springboot+mybatis自动创建表_第1张图片

2.添加依赖

本人pom.xml配置



    4.0.0
    
        org.springframework.boot
        spring-boot-starter-parent
        2.1.2.RELEASE
         
    
    com.user
    test
    0.0.1-SNAPSHOT
    test
    Demo project for Spring Boot
    
        1.8
    
    
        
            org.springframework.boot
            spring-boot-starter-web
        
        
            org.springframework.boot
            spring-boot-starter-test
            test
        
        
            org.mybatis.spring.boot
            mybatis-spring-boot-starter
            1.3.2
        
        
            mysql
            mysql-connector-java
            runtime
        

        
            com.gitee.sunchenbin.mybatis.actable
            mybatis-enhance-actable
            1.0.4
        
        
            org.apache.commons
            commons-lang3
            3.4
        

        
            com.alibaba
            druid
            1.0.18
        
        
            net.sf.json-lib
            json-lib
            2.4
            jdk15
            
                
                    commons-logging
                    commons-logging
                
            
        
    
    
        
            
                org.springframework.boot
                spring-boot-maven-plugin
            
        
    

2.创建application.yml文件

springboot+mybatis自动创建表_第2张图片

3.配置application.properties

springboot+mybatis自动创建表_第3张图片

 4创建实体类

@Table(name = "user")
public class Test extends BaseModel{
 
    private static final long serialVersionUID = 5199200306752426433L;
 
    @Column(name = "id",type = MySqlTypeConstant.INT,length = 11,isKey = true,isAutoIncrement = true)
    private Integer id;
 
    @Column(name = "name",type = MySqlTypeConstant.VARCHAR,length = 111)
    private String  name;
 
   
    //省略Setter、Getter
 
}

5.创建MyBatisMapperScannerConfigTestConfig的java类

原文:https://blog.csdn.net/aa1215018028/article/details/80759311 
版权声明:本文为博主原创文章,转载请附上博文链接!

@Configuration
@ComponentScan(basePackages = {"com.gitee.sunchenbin.mybatis.actable.manager.*"})
public class TestConfig {
 
    @Value("${spring.datasource.driver-class-name}")
    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.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.example.entity.*");
        return sqlSessionFactoryBean;
    }
 
}

 

 

@Configuration
@AutoConfigureAfter(TestConfig.class)
public class MyBatisMapperScannerConfig {
 
    @Bean
    public MapperScannerConfigurer mapperScannerConfigurer() throws Exception{
        MapperScannerConfigurer mapperScannerConfigurer = new MapperScannerConfigurer();
        mapperScannerConfigurer.setBasePackage("com.example.mapper.*;com.gitee.sunchenbin.mybatis.actable.dao.*");
        mapperScannerConfigurer.setSqlSessionFactoryBeanName("sqlSessionFactory");
        return mapperScannerConfigurer;
    }
 
}

在配置文件中的,com.example.entity.*需要换成自己项目中的实体层目录,com.example.mapper.*需要换成自己项目中的mapper目录

运行启动

 

你可能感兴趣的:(springboot+mybatis自动创建表)