springboot整合MyBatis实现动态创建表
java1234
共 5557字,需浏览 12分钟
·
2020-11-22 14:48
点击上方蓝色字体,选择“标星公众号”
优质文章,第一时间送达
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
"deleteByPrimaryKey" parameterType="java.lang.Long">
delete from ${tableName}
where id = #{id,jdbcType=BIGINT}
"insert" parameterType="com.example.demo.model.UserLog">
insert into ${tableName} (id, user_name, operation,
method, params, time,
ip)
values (#{userLog.id,jdbcType=BIGINT}, #{userLog.userName,jdbcType=VARCHAR}, #{userLog.operation,jdbcType=VARCHAR},
#{userLog.method,jdbcType=VARCHAR}, #{userLog.params,jdbcType=VARCHAR}, #{userLog.time,jdbcType=BIGINT},
#{userLog.ip,jdbcType=VARCHAR})
"insertSelective" parameterType="com.example.demo.model.UserLog">
insert into ${tableName}
"(" suffix=")" suffixOverrides=",">
<if test="userLog.id != null">
id,
if>
<if test="userLog.userName != null">
user_name,
if>
<if test="userLog.operation != null">
operation,
if>
<if test="userLog.method != null">
method,
if>
<if test="userLog.params != null">
params,
if>
<if test="userLog.time != null">
time,
if>
<if test="userLog.ip != null">
ip,
if>
"values (" suffix=")" suffixOverrides=",">
<if test="userLog.id != null">
#{userLog.id,jdbcType=BIGINT},
if>
<if test="userLog.userName != null">
#{userLog.userName,jdbcType=VARCHAR},
if>
<if test="userLog.operation != null">
#{userLog.operation,jdbcType=VARCHAR},
if>
<if test="userLog.method != null">
#{userLog.method,jdbcType=VARCHAR},
if>
<if test="userLog.params != null">
#{userLog.params,jdbcType=VARCHAR},
if>
<if test="userLog.time != null">
#{userLog.time,jdbcType=BIGINT},
if>
<if test="userLog.ip != null">
#{userLog.ip,jdbcType=VARCHAR},
if>
"updateByPrimaryKeySelective" parameterType="com.example.demo.model.UserLog">
update ${tableName}
<set>
<if test="userLog.userName != null">
user_name = #{userLog.userName,jdbcType=VARCHAR},
if>
<if test="userLog.operation != null">
operation = #{userLog.operation,jdbcType=VARCHAR},
if>
<if test="userLog.method != null">
method = #{userLog.method,jdbcType=VARCHAR},
if>
<if test="userLog.params != null">
params = #{userLog.params,jdbcType=VARCHAR},
if>
<if test="userLog.time != null">
time = #{userLog.time,jdbcType=BIGINT},
if>
<if test="userLog.ip != null">
ip = #{userLog.ip,jdbcType=VARCHAR},
if>
set>
where id = #{userLog.id,jdbcType=BIGINT}
"updateByPrimaryKey" parameterType="com.example.demo.model.UserLog">
update ${tableName}
set user_name = #{userLog.userName,jdbcType=VARCHAR},
operation = #{userLog.operation,jdbcType=VARCHAR},
method = #{userLog.method,jdbcType=VARCHAR},
params = #{userLog.params,jdbcType=VARCHAR},
time = #{userLog.time,jdbcType=BIGINT},
ip = #{userLog.ip,jdbcType=VARCHAR}
where id = #{userLog.id,jdbcType=BIGINT}
"dropTable">
DROP TABLE IF EXISTS ${tableName}
"createTable" parameterType="String">
CREATE TABLE ${tableName} (
`id` bigint(20) NOT NULL AUTO_INCREMENT COMMENT '编号',
`user_name` varchar(50) DEFAULT NULL COMMENT '用户名',
`operation` varchar(50) DEFAULT NULL COMMENT '用户操作',
`method` varchar(200) DEFAULT NULL COMMENT '请求方法',
`params` varchar(5000) DEFAULT NULL COMMENT '请求参数',
`time` bigint(20) NOT NULL COMMENT '执行时长(毫秒)',
`ip` varchar(64) DEFAULT NULL COMMENT 'IP地址',
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=2897 DEFAULT CHARSET=utf8 COMMENT='用户操作日志';
粉丝福利:实战springboot+CAS单点登录系统视频教程免费领取
???
?长按上方微信二维码 2 秒 即可获取资料
感谢点赞支持下哈
评论