springboot整合MyBatis实现动态创建表
点击上方蓝色字体,选择“标星公众号”
优质文章,第一时间送达
1:业务场景 单表数据量太大,需要用到分表的操作时,例如保存日志数据
代码展示如下:
pom依赖:
org.springframework.boot
spring-boot-starter
org.projectlombok
lombok
true
org.springframework.boot
spring-boot-starter-test
test
org.springframework.boot
spring-boot-starter-web
io.springfox
springfox-swagger2
2.9.2
io.springfox
springfox-swagger-ui
2.9.2
org.mybatis.spring.boot
mybatis-spring-boot-starter
2.0.0
mysql
mysql-connector-java
org.springframework.boot
spring-boot-maven-plugin
src/main/java
**/sqlmap/*.xml
false
src/main/resources
**/*.*
true
配置类:
@SpringBootConfiguration
@MapperScan("com.example.demo.dao") //扫描dao
public class MybatiesConfig {
@Autowired
private DataSource dataSource;
@Bean
public SqlSessionFactory sqlSessionFactory() throws Exception {
SqlSessionFactoryBean sqlSessionFactoryBean = new SqlSessionFactoryBean();
sqlSessionFactoryBean.setDataSource(dataSource);
sqlSessionFactoryBean.setTypeAliasesPackage("com.example.demo.model"); //扫描model
PathMatchingResourcePatternResolver resourcePatternResolver = new PathMatchingResourcePatternResolver();
sqlSessionFactoryBean.setMapperLocations(resourcePatternResolver.getResources("classpath*:**/sqlmap/*.xml")); //扫描xml
return sqlSessionFactoryBean.getObject();
}
}
//swagger 文档的配置类
@SpringBootConfiguration
@EnableSwagger2
public class Swagger {
@Bean
public Docket createRestApi(){
return new Docket(DocumentationType.SWAGGER_2).apiInfo(apiInfo())
.select()
.apis(RequestHandlerSelectors.any())
.paths(PathSelectors.any())
.build();
}
private ApiInfo apiInfo(){
return new ApiInfoBuilder()
.title("springboot api doc")
.description("springboot 动态创建表格的api")
.version("1.0")
.build();
}
}
其中需要注意的点主要有xml文件中代码展示:
?xml version="1.0" encoding="UTF-8"?>
"-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
"com.example.demo.mapper.UserLogMapper">
"BaseResultMap" type="com.example.demo.model.UserLog">
"id" jdbcType="BIGINT" property="id" />
"user_name" jdbcType="VARCHAR" property="userName" />
"operation" jdbcType="VARCHAR" property="operation" />
"method" jdbcType="VARCHAR" property="method" />
"params" jdbcType="VARCHAR" property="params" />
"time" jdbcType="BIGINT" property="time" />
"ip" jdbcType="VARCHAR" property="ip" />
"Base_Column_List">
id, user_name, operation, method, params, time, ip
粉丝福利:实战springboot+CAS单点登录系统视频教程免费领取
???
?长按上方微信二维码 2 秒 即可获取资料
感谢点赞支持下哈
评论