最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

SpringBoot結(jié)合Mybatis實(shí)現(xiàn)創(chuàng)建數(shù)據(jù)庫(kù)表的方法

 更新時(shí)間:2022年01月18日 15:27:20   作者:蘇州程序大白  
本文主要介紹了SpringBoot結(jié)合Mybatis實(shí)現(xiàn)創(chuàng)建數(shù)據(jù)庫(kù)表的方法,文中通過(guò)示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下

前言

系統(tǒng)環(huán)境:

  • JAVA JDK 版本:1.8
  • MySQL 版本:8.0.27
  • MyBatis 版本:3.5.9
  • SpingBoot版本: 2.6.2

為什么要通過(guò)應(yīng)用實(shí)現(xiàn)創(chuàng)建表的功能

最近接了項(xiàng)目時(shí),由于客戶需要分庫(kù)分表,而且每次手動(dòng)創(chuàng)建很多表,可能是自己閑麻煩,于是乎就找了一些通過(guò)應(yīng)用自動(dòng)創(chuàng)建表的解決方案,其中本人比較熟悉使用 MyBatis,所以通過(guò)博文的形式給大家講解一下,如何在 SpringBoot 環(huán)境中,使用 Mybatis 動(dòng)態(tài)的創(chuàng)建數(shù)據(jù)庫(kù)中的表的功能。

準(zhǔn)備創(chuàng)建表的 SQL 語(yǔ)句

創(chuàng)建表 SQL 語(yǔ)句,內(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)通過(guò) MyBatis 創(chuàng)建數(shù)據(jù)庫(kù)表示例

目的就是解決通過(guò) MyBatis 執(zhí)行創(chuàng)建表的語(yǔ)句,從而實(shí)現(xiàn)創(chuàng)建數(shù)據(jù)庫(kù)中的表的功能,實(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ù)庫(kù)配置

在 SpringBoot 的 application.yml 文件中,添加數(shù)據(jù)庫(kù)連接的參數(shù),配置內(nèi)容如下:

spring:
? application:
? ? name: springboot-mybatis-create-table-example
? # 數(shù)據(jù)庫(kù)配置
? 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ù)庫(kù)表
? ? ?*
? ? ?* @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 語(yǔ)句,內(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 語(yǔ)句-->
? ? <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ù)庫(kù)表
? ? ?*
? ? ?* @param tableName 表名稱
? ? ?* @return 是否創(chuàng)建成功
? ? ?*/
? ? @PostMapping("/createTable")
? ? public ResponseEntity<String> createTableTest(@RequestParam String tableName) {
? ? ? ? try {
? ? ? ? ? ? // 創(chuàng)建數(shù)據(jù)庫(kù)表
? ? ? ? ? ? tableMapper.createTable(tableName);
? ? ? ? } catch (Exception e) {
? ? ? ? ? ? return ResponseEntity.status(500).body("創(chuàng)建數(shù)據(jù)庫(kù)表失敗");
? ? ? ? }
? ? ? ? return ResponseEntity.ok("創(chuàng)建數(shù)據(jù)庫(kù)表成功");
? ? }

}

創(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ù)庫(kù) test 中創(chuàng)建一個(gè) user 表:

$ curl -X POST http://localhost:8080/createTable?tableName=user

執(zhí)行完接口后,再進(jìn)入數(shù)據(jù)庫(kù),輸入下面命令觀察庫(kù)中是否創(chuàng)建包成功:

mysql> use test;
Database changed

mysql> show tables;
+----------------------------------------+
| Tables_in_test ? ? ? ? ? ? ? ? ? ? ? ? |
+----------------------------------------+
| user ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? |
+----------------------------------------+
1 rows in set (0.00 sec)

可以看到 test 庫(kù)中已經(jīng)成功創(chuàng)建了 user 表。

到此這篇關(guān)于SpringBoot結(jié)合Mybatis實(shí)現(xiàn)創(chuàng)建數(shù)據(jù)庫(kù)表的方法的文章就介紹到這了,更多相關(guān)SpringBoot Mybatis創(chuàng)建數(shù)據(jù)庫(kù)表內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • Java中的TreeSet集合詳解

    Java中的TreeSet集合詳解

    這篇文章主要介紹了Java中的TreeSet集合詳解,TreeSet 是一個(gè) 有序集合,它擴(kuò)展了 AbstractSet 類并實(shí)現(xiàn)了 NavigableSet 接口,作為自平衡二叉搜索樹(shù),二叉樹(shù)的每個(gè)節(jié)點(diǎn)包括一個(gè)額外的位,用于識(shí)別紅色或黑色的節(jié)點(diǎn)的顏色,需要的朋友可以參考下
    2023-09-09
  • 詳解SpringBoot+Mybatis實(shí)現(xiàn)動(dòng)態(tài)數(shù)據(jù)源切換

    詳解SpringBoot+Mybatis實(shí)現(xiàn)動(dòng)態(tài)數(shù)據(jù)源切換

    這篇文章主要介紹了詳解SpringBoot+Mybatis實(shí)現(xiàn)動(dòng)態(tài)數(shù)據(jù)源切換,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2021-05-05
  • Java的MyBatis框架中XML映射緩存的使用教程

    Java的MyBatis框架中XML映射緩存的使用教程

    MyBatis程序在做好XML映射后能夠有緩存的功能,這樣映射過(guò)SQL語(yǔ)句的配置以后就可以拿過(guò)來(lái)直接用了,這里我們來(lái)一起總結(jié)一下Java的MyBatis框架中XML映射緩存的使用教程
    2016-06-06
  • springboot多模塊項(xiàng)目mvn打包遇到存在依賴但卻無(wú)法發(fā)現(xiàn)符號(hào)問(wèn)題

    springboot多模塊項(xiàng)目mvn打包遇到存在依賴但卻無(wú)法發(fā)現(xiàn)符號(hào)問(wèn)題

    在SpringBoot多模塊項(xiàng)目中,如果遇到依賴存在但無(wú)法發(fā)現(xiàn)符號(hào)的問(wèn)題,常見(jiàn)原因可能是pom.xml配置問(wèn)題,例如,如果某個(gè)模塊僅作為依賴而不是啟動(dòng)工程,不應(yīng)在其pom中配置spring-boot-maven-plugin插件,因?yàn)檫@將影響jar包的生成方式
    2024-09-09
  • 單點(diǎn)登錄的三種方式和JWT的介紹與使用

    單點(diǎn)登錄的三種方式和JWT的介紹與使用

    這篇文章主要說(shuō)明了單點(diǎn)登錄的三種方式和JWT的介紹與使用,加深自己的印象以及幫助的諸位小伙伴兒們,需要的朋友可以參考下
    2023-03-03
  • Java之JNDI注入的實(shí)現(xiàn)

    Java之JNDI注入的實(shí)現(xiàn)

    JNDI是Java EE的重要部分,本文主要介紹了Java之JNDI注入的實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2021-11-11
  • 基于application和bootstrap的加載順序及區(qū)別說(shuō)明

    基于application和bootstrap的加載順序及區(qū)別說(shuō)明

    這篇文章主要介紹了application和bootstrap的加載順序及區(qū)別說(shuō)明,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2023-07-07
  • Java反射與Fastjson的危險(xiǎn)反序列化詳解

    Java反射與Fastjson的危險(xiǎn)反序列化詳解

    在?Java?中,Computer.class是一個(gè)引用,它表示了?Computer?的字節(jié)碼對(duì)象(Class對(duì)象),這個(gè)對(duì)象被廣泛應(yīng)用于反射、序列化等操作中,那么為什么?parseObject?需要這個(gè)引用呢,帶著這個(gè)問(wèn)題我們一起通過(guò)本文學(xué)習(xí)下吧
    2024-07-07
  • springboot整合JSR303校驗(yàn)功能實(shí)現(xiàn)代碼

    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
  • java實(shí)現(xiàn)圖片用Excel畫(huà)出來(lái)

    java實(shí)現(xiàn)圖片用Excel畫(huà)出來(lái)

    這篇文章主要為大家詳細(xì)介紹了java實(shí)現(xiàn)圖片用Excel畫(huà)出來(lái),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2020-03-03

最新評(píng)論

柯坪县| 盐池县| 洱源县| 萝北县| 宜黄县| 宣汉县| 稻城县| 措美县| 廉江市| 临沂市| 宜章县| 长治市| 福贡县| 兰州市| 德化县| 双江| 娄烦县| 赫章县| 宁德市| 沽源县| 衡东县| 水富县| 阳原县| 登封市| 法库县| 泸溪县| 洛川县| 辽宁省| 乃东县| 桂阳县| 乌拉特后旗| 绥滨县| 灵寿县| 互助| 清水河县| 大冶市| 呼玛县| 郓城县| 景洪市| 余干县| 凤山市|