SpringBoot結(jié)合Mybatis實(shí)現(xiàn)創(chuàng)建數(shù)據(jù)庫表的方法
前言
系統(tǒng)環(huán)境:
- JAVA JDK 版本:1.8
- MySQL 版本:8.0.27
- MyBatis 版本:3.5.9
- SpingBoot版本: 2.6.2
為什么要通過應(yīng)用實(shí)現(xiàn)創(chuàng)建表的功能
最近接了項(xiàng)目時(shí),由于客戶需要分庫分表,而且每次手動(dòng)創(chuàng)建很多表,可能是自己閑麻煩,于是乎就找了一些通過應(yīng)用自動(dòng)創(chuàng)建表的解決方案,其中本人比較熟悉使用 MyBatis,所以通過博文的形式給大家講解一下,如何在 SpringBoot 環(huán)境中,使用 Mybatis 動(dòng)態(tài)的創(chuàng)建數(shù)據(jù)庫中的表的功能。
準(zhǔn)備創(chuàng)建表的 SQL 語句
創(chuàng)建表 SQL 語句,內(nèi)容如下:
CREATE TABLE IF NOT EXISTS `user`
(
`id` int(0) NOT NULL AUTO_INCREMENT COMMENT '主鍵',
`group_id` int(0) NULL DEFAULT NULL COMMENT '組號(hào)',
`username` varchar(20) NULL DEFAULT NULL COMMENT '用戶名',
`password` varchar(20) NULL DEFAULT NULL COMMENT '密碼',
PRIMARY KEY (`id`)
) ENGINE = InnoDB
AUTO_INCREMENT = 9
CHARACTER SET = utf8mb4 COMMENT ='用于測(cè)試的用戶表';
實(shí)現(xiàn)通過 MyBatis 創(chuàng)建數(shù)據(jù)庫表示例
目的就是解決通過 MyBatis 執(zhí)行創(chuàng)建表的語句,從而實(shí)現(xiàn)創(chuàng)建數(shù)據(jù)庫中的表的功能,實(shí)現(xiàn)代碼如下:
在 Maven 中引入相關(guān)依賴
在 Maven的 pom.xml文件中,引入 SpringBoot、MySql、MyBytis 等依賴,內(nèi)容如下:
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" ? ? ? ? ?xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> ? ? <modelVersion>4.0.0</modelVersion> ? ? <parent> ? ? ? ? <groupId>org.springframework.boot</groupId> ? ? ? ? <artifactId>spring-boot-starter-parent</artifactId> ? ? ? ? <version>2.6.2</version> ? ? </parent> ? ? <groupId>club.mydlq</groupId> ? ? <artifactId>springboot-mybatis-create-table-example</artifactId> ? ? <version>1.0.0</version> ? ? <name>springboot-mybatis-example</name> ? ? <description>springboot mybatis create table example project</description> ? ? <properties> ? ? ? ? <java.version>1.8</java.version> ? ? </properties> ? ? <dependencies> ? ? ? ? <!-- Web --> ? ? ? ? <dependency> ? ? ? ? ? ? <groupId>org.springframework.boot</groupId> ? ? ? ? ? ? <artifactId>spring-boot-starter-web</artifactId> ? ? ? ? </dependency> ? ? ? ? <!-- Lombok --> ? ? ? ? <dependency> ? ? ? ? ? ? <groupId>org.projectlombok</groupId> ? ? ? ? ? ? <artifactId>lombok</artifactId> ? ? ? ? ? ? <optional>true</optional> ? ? ? ? </dependency> ? ? ? ? <!-- Mysql --> ? ? ? ? <dependency> ? ? ? ? ? ? <groupId>mysql</groupId> ? ? ? ? ? ? <artifactId>mysql-connector-java</artifactId> ? ? ? ? </dependency> ? ? ? ? <!-- MyBatis --> ? ? ? ? <dependency> ? ? ? ? ? ? <groupId>org.mybatis.spring.boot</groupId> ? ? ? ? ? ? <artifactId>mybatis-spring-boot-starter</artifactId> ? ? ? ? ? ? <version>2.2.1</version> ? ? ? ? </dependency> ? ? </dependencies> ? ? <build> ? ? ? ? <plugins> ? ? ? ? ? ? <plugin> ? ? ? ? ? ? ? ? <groupId>org.springframework.boot</groupId> ? ? ? ? ? ? ? ? <artifactId>spring-boot-maven-plugin</artifactId> ? ? ? ? ? ? </plugin> ? ? ? ? </plugins> ? ? </build> </project>
在 SpringBoot 配置文件中添加數(shù)據(jù)庫配置
在 SpringBoot 的 application.yml 文件中,添加數(shù)據(jù)庫連接的參數(shù),配置內(nèi)容如下:
spring: ? application: ? ? name: springboot-mybatis-create-table-example ? # 數(shù)據(jù)庫配置 ? datasource: ? ? type: com.zaxxer.hikari.HikariDataSource ? ? driverClassName: com.mysql.cj.jdbc.Driver ? ? url: jdbc:mysql://127.0.0.1:3306/test?serverTimezone=Asia/Shanghai&useUnicode=true&useSSL=false&allowPublicKeyRetrieval=true ? ? hikari: ? ? ? pool-name: DatebookHikariCP ? ? ? minimum-idle: 5 ? ? ? maximum-pool-size: 15 ? ? ? max-lifetime: 1800000 ? ? ? connection-timeout: 30000 ? ? ? username: root ? ? ? password: 123456 # 指定 mapper 的 xml 文件位置 mybatis: ? mapper-locations: classpath:mappers/*.xml
創(chuàng)建測(cè)試的 Mapper 接口類
創(chuàng)建一個(gè)用戶建表的 MyBatis 的 Mapper 接口,代碼如下:
import org.apache.ibatis.annotations.Mapper;
@Mapper
public interface TableMapper {
? ? /**
? ? ?* 創(chuàng)建數(shù)據(jù)庫表
? ? ?*
? ? ?* @param tableName 表名稱
? ? ?*/
? ? void createTable(String tableName);
}創(chuàng)建與 Mapper 關(guān)聯(lián)的 XML 文件
創(chuàng)建一個(gè)用于和 Mapper 接口關(guān)聯(lián)的 xml 文件 TableMapper.xml,在里面添加用于創(chuàng)建表的 SQL 語句,內(nèi)容如下:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="club.mydlq.mappers.TableMapper">
? ? <!--創(chuàng)建表的 SQL 語句-->
? ? <update id="createTable" parameterType="java.lang.String">
? ? ? ? CREATE TABLE IF NOT EXISTS `${tableName}`
? ? ? ? (
? ? ? ? ? ? `id` ? ? ? int(0) ? ? ?NOT NULL AUTO_INCREMENT COMMENT '主鍵',
? ? ? ? ? ? `group_id` int(0) ? ? ?NULL DEFAULT NULL COMMENT '組號(hào)',
? ? ? ? ? ? `username` varchar(20) NULL DEFAULT NULL COMMENT '用戶名',
? ? ? ? ? ? `password` varchar(20) NULL DEFAULT NULL COMMENT '密碼',
? ? ? ? ? ? PRIMARY KEY (`id`)
? ? ? ? ) ENGINE = InnoDB
? ? ? ? ? AUTO_INCREMENT = 9
? ? ? ? ? CHARACTER SET = utf8mb4 COMMENT ='用于測(cè)試的用戶表';
? ? </update>
</mapper>創(chuàng)建用于測(cè)試的 Controller 類
創(chuàng)建一個(gè)用于測(cè)試的 Controller 類,里面提供一個(gè)創(chuàng)建表的接口,代碼如下:
import club.mydlq.mappers.TableMapper;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
@RestController
public class TestController {
? ? @Resource
? ? private TableMapper tableMapper;
? ? /**
? ? ?* 創(chuàng)建數(shù)據(jù)庫表
? ? ?*
? ? ?* @param tableName 表名稱
? ? ?* @return 是否創(chuàng)建成功
? ? ?*/
? ? @PostMapping("/createTable")
? ? public ResponseEntity<String> createTableTest(@RequestParam String tableName) {
? ? ? ? try {
? ? ? ? ? ? // 創(chuàng)建數(shù)據(jù)庫表
? ? ? ? ? ? tableMapper.createTable(tableName);
? ? ? ? } catch (Exception e) {
? ? ? ? ? ? return ResponseEntity.status(500).body("創(chuàng)建數(shù)據(jù)庫表失敗");
? ? ? ? }
? ? ? ? return ResponseEntity.ok("創(chuàng)建數(shù)據(jù)庫表成功");
? ? }
}創(chuàng)建 SpringBoot 啟動(dòng)類
創(chuàng)建一個(gè)用于啟動(dòng) SpringBoot 的啟動(dòng)類,代碼如下:
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class Application {
? ? public static void main(String[] args) {
? ? ? ? SpringApplication.run(Application.class, args);
? ? }
}調(diào)用創(chuàng)建表的接口進(jìn)行測(cè)試
執(zhí)行 curl 命令,使用 POST 方法調(diào)用之前示例項(xiàng)目 Controller 類中提供的創(chuàng)建表接口 /createTable,在數(shù)據(jù)庫 test 中創(chuàng)建一個(gè) user 表:
$ curl -X POST http://localhost:8080/createTable?tableName=user
執(zhí)行完接口后,再進(jìn)入數(shù)據(jù)庫,輸入下面命令觀察庫中是否創(chuàng)建包成功:
mysql> use test; Database changed mysql> show tables; +----------------------------------------+ | Tables_in_test ? ? ? ? ? ? ? ? ? ? ? ? | +----------------------------------------+ | user ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? | +----------------------------------------+ 1 rows in set (0.00 sec)
可以看到 test 庫中已經(jīng)成功創(chuàng)建了 user 表。
到此這篇關(guān)于SpringBoot結(jié)合Mybatis實(shí)現(xiàn)創(chuàng)建數(shù)據(jù)庫表的方法的文章就介紹到這了,更多相關(guān)SpringBoot Mybatis創(chuàng)建數(shù)據(jù)庫表內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
詳解SpringBoot+Mybatis實(shí)現(xiàn)動(dòng)態(tài)數(shù)據(jù)源切換
這篇文章主要介紹了詳解SpringBoot+Mybatis實(shí)現(xiàn)動(dòng)態(tài)數(shù)據(jù)源切換,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2021-05-05
springboot多模塊項(xiàng)目mvn打包遇到存在依賴但卻無法發(fā)現(xiàn)符號(hào)問題
在SpringBoot多模塊項(xiàng)目中,如果遇到依賴存在但無法發(fā)現(xiàn)符號(hào)的問題,常見原因可能是pom.xml配置問題,例如,如果某個(gè)模塊僅作為依賴而不是啟動(dòng)工程,不應(yīng)在其pom中配置spring-boot-maven-plugin插件,因?yàn)檫@將影響jar包的生成方式2024-09-09
基于application和bootstrap的加載順序及區(qū)別說明
這篇文章主要介紹了application和bootstrap的加載順序及區(qū)別說明,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-07-07
Java反射與Fastjson的危險(xiǎn)反序列化詳解
在?Java?中,Computer.class是一個(gè)引用,它表示了?Computer?的字節(jié)碼對(duì)象(Class對(duì)象),這個(gè)對(duì)象被廣泛應(yīng)用于反射、序列化等操作中,那么為什么?parseObject?需要這個(gè)引用呢,帶著這個(gè)問題我們一起通過本文學(xué)習(xí)下吧2024-07-07
springboot整合JSR303校驗(yàn)功能實(shí)現(xiàn)代碼
這篇文章主要介紹了springboot整合JSR303校驗(yàn)功能實(shí)現(xiàn),JSR303校驗(yàn)方法有統(tǒng)一校驗(yàn)的需求,統(tǒng)一校驗(yàn)實(shí)現(xiàn)以及分組校驗(yàn),本文結(jié)合實(shí)例代碼給大家介紹的非常詳細(xì),需要的朋友可以參考下2023-01-01

