springboot整合MyBatis實(shí)現(xiàn)動態(tài)創(chuàng)建表
點(diǎn)擊上方藍(lán)色字體,選擇“標(biāo)星公眾號”
優(yōu)質(zhì)文章,第一時間送達(dá)
66套java從入門到精通實(shí)戰(zhàn)課程分享
1:業(yè)務(wù)場景 單表數(shù)據(jù)量太大,需要用到分表的操作時,例如保存日志數(shù)據(jù)
代碼展示如下:?
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?動態(tài)創(chuàng)建表格的api")
????????????????.version("1.0")
????????????????.build();
????}
}
其中需要注意的點(diǎn)主要有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?'請求參數(shù)',
???`time`?bigint(20)?NOT?NULL?COMMENT?'執(zhí)行時長(毫秒)',
???`ip`?varchar(64)?DEFAULT?NULL?COMMENT?'IP地址',
???PRIMARY?KEY?(`id`)
?)?ENGINE=InnoDB?AUTO_INCREMENT=2897?DEFAULT?CHARSET=utf8?COMMENT='用戶操作日志';
??
粉絲福利:實(shí)戰(zhàn)springboot+CAS單點(diǎn)登錄系統(tǒng)視頻教程免費(fèi)領(lǐng)取
???
?長按上方微信二維碼?2 秒 即可獲取資料
感謝點(diǎn)贊支持下哈?
評論
圖片
表情
