SpringBoot+Mybatis的自动建表

之前一段时间使用nutz的自动建表,感觉自动建表这个功能对项目的开发效率还是提高很多的,所以在接触到新的框架的时候会有一些学习的方向性,之前有实践过hibernate的自动建表,感觉还是比较简单的,然后今天也实践了一波mybatis的自动建表。

然后在网上去寻找一些学习资源,然后写一篇实践的流程,做一个记录。

参考:

https://www.jianshu.com/p/25db002b0367

 

mybatis的自动建表是通过一个A.CTable的开源框架来实现自动创建/更新表结构的方式。

在新建好一个项目后的pom.xml中的内容:



	4.0.0
	
		org.springframework.boot
		spring-boot-starter-parent
		2.1.1.RELEASE
		 
	
	com.xyh
	MoodStation
	0.0.1-SNAPSHOT
	MoodStation
	Demo project for Spring Boot
	
		1.8
	

	
	
	
		
			org.springframework.boot
			spring-boot-starter-thymeleaf
		
		
            net.sourceforge.nekohtml
            nekohtml
            1.9.22
         
		
			org.springframework.boot
			spring-boot-starter-web
		
		
			org.mybatis.spring.boot
			mybatis-spring-boot-starter
			1.3.2
		
		
		
			com.alibaba
			druid
			1.0.18
		
		
		
           com.gitee.sunchenbin.mybatis.actable
           mybatis-enhance-actable
           1.0.3
        
		
			mysql
			mysql-connector-java
			5.1.17
			runtime
		
		
			org.springframework.boot
			spring-boot-starter-test
			test
		
		
		
			org.apache.commons
			commons-lang3
			3.4
		
		
			net.sf.json-lib
			json-lib
			2.4
			jdk15
			
				
					commons-logging
					commons-logging
				
			
		
	

	
		
			
				org.springframework.boot
				spring-boot-maven-plugin
			
		
	


项目的目录截图:

SpringBoot+Mybatis的自动建表_第1张图片

在 application.properties的文件中加入一些所需要的配置信息

spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost:3306/mybatis?characterEncoding=UTF-8
spring.datasource.username=root
spring.datasource.password=123456

#create,系统启动后,会将所有表删除,然后根据路径包中的实体类进行重新建表(会破坏原有数据)
#none,不做任何处理
#系统启动是会自动判断哪些表需要新建,哪些字段需要修改(不会破坏原有数据)
mybatis.table.auto=update
#建表的对象包路径
mybatis.model.pack=com.xyh.entity
mybatis.database.type=mysql

在config包中的两个配置文件:

1.MybatisTableConfig.java

package com.xyh.config;

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;

import com.alibaba.druid.pool.DruidDataSource;
@Configuration
@ComponentScan(basePackages = {"com.gitee.sunchenbin.mybatis.actable.manager.*"})
public class MybatisTableConfig {
	    @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.xyh.entity.*");
	        return sqlSessionFactoryBean;
	    }

}

2.MyBatisMapperScannerConfig.java

package com.xyh.config;

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(MybatisTableConfig.class)
public class MyBatisMapperScannerConfig {
	 @Bean
	    public MapperScannerConfigurer mapperScannerConfigurer() throws Exception{
	        MapperScannerConfigurer mapperScannerConfigurer = new MapperScannerConfigurer();
	        mapperScannerConfigurer.setBasePackage("com.xyh.mapper.*;com.gitee.sunchenbin.mybatis.actable.dao.*");
	        mapperScannerConfigurer.setSqlSessionFactoryBeanName("sqlSessionFactory");
	        return mapperScannerConfigurer;
	    }
}

 

在entity包中放入一个实体类来运行测试 (以User.java为例):这里用到的注解不多,所以可能会写一个所有的注解列表,方便之后的使用

package com.xyh.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;

@Table(name="user")
public class User {
	@Column(name="id",type=MySqlTypeConstant.BIGINT,isKey=true,isAutoIncrement = true)
	private long id;
	@Column(name="userno",type=MySqlTypeConstant.VARCHAR,length=20)
	private String userNo;
	@Column(name="phone",type=MySqlTypeConstant.VARCHAR,length=20)
	private String phone;
	@Column(name="password",type=MySqlTypeConstant.VARCHAR,length=50)
	private String password;
	public long getId() {
		return id;
	}
	public void setId(long id) {
		this.id = id;
	}
	public String getUserNo() {
		return userNo;
	}
	public void setUserNo(String userNo) {
		this.userNo = userNo;
	}
	public String getPhone() {
		return phone;
	}
	public void setPhone(String phone) {
		this.phone = phone;
	}
	public String getPassword() {
		return password;
	}
	public void setPassword(String password) {
		this.password = password;
	}
	
}

以上就是mybatis 的自动建表,在最近一段时间准备实际运用这些技术去写一个小项目,练练手,顺便学习一下shiro和redis,然后准备学习一下运用bootstrap写前端。

你可能感兴趣的:(java框架实践)