欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

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ù)表的方法,文中通過示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下

前言

系統(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í),由于客戶需要分庫(kù)分表,而且每次手動(dòng)創(chuàng)建很多表,可能是自己閑麻煩,于是乎就找了一些通過應(yīng)用自動(dòng)創(chuàng)建表的解決方案,其中本人比較熟悉使用 MyBatis,所以通過博文的形式給大家講解一下,如何在 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)通過 MyBatis 創(chuàng)建數(shù)據(jù)庫(kù)表示例

目的就是解決通過 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)文章

  • 淺談Spring Boot 異常處理篇

    淺談Spring Boot 異常處理篇

    本篇文章主要介紹了淺談Spring Boot 異常處理篇,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧
    2017-08-08
  • SpringBoot+ShardingSphereJDBC實(shí)現(xiàn)讀寫分離詳情

    SpringBoot+ShardingSphereJDBC實(shí)現(xiàn)讀寫分離詳情

    這篇文章主要介紹了SpringBoot+ShardingSphereJDBC實(shí)現(xiàn)讀寫分離詳情,通過用??MySQL??進(jìn)行一主一從的主從復(fù)制展開全文內(nèi)容,需要的朋友可以參考一下
    2022-08-08
  • springboot集成springsecurity 使用OAUTH2做權(quán)限管理的教程

    springboot集成springsecurity 使用OAUTH2做權(quán)限管理的教程

    這篇文章主要介紹了springboot集成springsecurity 使用OAUTH2做權(quán)限管理的教程,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧
    2020-12-12
  • SpringBoot項(xiàng)目實(shí)戰(zhàn)之?dāng)?shù)據(jù)交互篇

    SpringBoot項(xiàng)目實(shí)戰(zhàn)之?dāng)?shù)據(jù)交互篇

    這篇文章主要給大家介紹了關(guān)于SpringBoot項(xiàng)目實(shí)戰(zhàn)之?dāng)?shù)據(jù)交互篇的相關(guān)資料,文中通過實(shí)例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2022-03-03
  • Java數(shù)字簽名算法DSA實(shí)例詳解

    Java數(shù)字簽名算法DSA實(shí)例詳解

    這篇文章主要介紹了Java數(shù)字簽名算法DSA,結(jié)合實(shí)例形式分析了Java數(shù)字簽名算法DSA具體定義與使用技巧,需要的朋友可以參考下
    2018-05-05
  • 使用springboot整合mybatis-plus實(shí)現(xiàn)數(shù)據(jù)庫(kù)的增刪查改示例

    使用springboot整合mybatis-plus實(shí)現(xiàn)數(shù)據(jù)庫(kù)的增刪查改示例

    這篇文章主要介紹了使用springboot整合mybatis-plus實(shí)現(xiàn)數(shù)據(jù)庫(kù)的增刪查改示例,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2021-04-04
  • java中最易犯錯(cuò)的特殊字符示例詳解

    java中最易犯錯(cuò)的特殊字符示例詳解

    這篇文章主要給大家介紹了關(guān)于java中最易犯錯(cuò)的特殊字符的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家學(xué)習(xí)或者使用java具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來一起學(xué)習(xí)學(xué)習(xí)吧
    2019-09-09
  • SpringSecurity自定義資源攔截規(guī)則及登錄界面跳轉(zhuǎn)問題

    SpringSecurity自定義資源攔截規(guī)則及登錄界面跳轉(zhuǎn)問題

    這篇文章主要介紹了SpringSecurity自定義資源攔截規(guī)則及登錄界面跳轉(zhuǎn)問題,我們想要自定義認(rèn)證邏輯,就需要?jiǎng)?chuàng)建一些原來不存在的bean,這個(gè)時(shí)候就可以使@ConditionalOnMissingBean注解,本文給大家介紹的非常詳細(xì),需要的朋友參考下吧
    2023-12-12
  • java通過ssh連接服務(wù)器執(zhí)行shell命令詳解及實(shí)例

    java通過ssh連接服務(wù)器執(zhí)行shell命令詳解及實(shí)例

    這篇文章主要介紹了java通過ssh連接服務(wù)器執(zhí)行shell命令詳解及實(shí)例方法的相關(guān)資料
    2017-02-02
  • java使用es查詢的示例代碼

    java使用es查詢的示例代碼

    本篇文章主要介紹了java使用es查詢的示例代碼,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧
    2018-01-01

最新評(píng)論