前言
系统环境:
- JAVA JDK 版本:1.8
- MySQL 版本:8.0.27
- MyBatis 版本:3.5.9
- SpingBoot版本: 2.6.2
为什么要通过应用实现创建表的功能
最近接了项目时,由于客户需要分库分表,而且每次手动创建很多表,可能是自己闲麻烦,于是乎就找了一些通过应用自动创建表的解决方案,其中本人比较熟悉使用 MyBatis,所以通过博文的形式给大家讲解一下,如何在 SpringBoot 环境中,使用 Mybatis 动态的创建数据库中的表的功能。
准备创建表的 SQL 语句
创建表 SQL 语句,内容如下:
- CREATE TABLE IF NOT EXISTS `user`
- (
- `id` int(0) NOT NULL AUTO_INCREMENT COMMENT '主键',
- `group_id` int(0) NULL DEFAULT NULL COMMENT '组号',
- `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 ='用于测试的用户表';
实现通过 MyBatis 创建数据库表示例
目的就是解决通过 MyBatis 执行创建表的语句,从而实现创建数据库中的表的功能,实现代码如下:
在 Maven 中引入相关依赖
在 Maven的 pom.xml文件中,引入 SpringBoot、MySql、MyBytis 等依赖,内容如下:
- <?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 配置文件中添加数据库配置
在 SpringBoot 的 application.yml 文件中,添加数据库连接的参数,配置内容如下:
- spring:
- ? application:
- ? ? name: springboot-mybatis-create-table-example
- ? # 数据库配置
- ? 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
创建测试的 Mapper 接口类
创建一个用户建表的 MyBatis 的 Mapper 接口,代码如下:
- import org.apache.ibatis.annotations.Mapper;
-
- @Mapper
- public interface TableMapper {
-
- ? ? /**
- ? ? ?* 创建数据库表
- ? ? ?*
- ? ? ?* @param tableName 表名称
- ? ? ?*/
- ? ? void createTable(String tableName);
-
- }
创建与 Mapper 关联的 XML 文件
创建一个用于和 Mapper 接口关联的 xml 文件 TableMapper.xml,在里面添加用于创建表的 SQL 语句,内容如下:
- <?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">
-
- ? ? <!--创建表的 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 '组号',
- ? ? ? ? ? ? `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 ='用于测试的用户表';
- ? ? </update>
-
- </mapper>
创建用于测试的 Controller 类
创建一个用于测试的 Controller 类,里面提供一个创建表的接口,代码如下:
- 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;
-
- ? ? /**
- ? ? ?* 创建数据库表
- ? ? ?*
- ? ? ?* @param tableName 表名称
- ? ? ?* @return 是否创建成功
- ? ? ?*/
- ? ? @PostMapping("/createTable")
- ? ? public ResponseEntity<String> createTableTest(@RequestParam String tableName) {
- ? ? ? ? try {
- ? ? ? ? ? ? // 创建数据库表
- ? ? ? ? ? ? tableMapper.createTable(tableName);
- ? ? ? ? } catch (Exception e) {
- ? ? ? ? ? ? return ResponseEntity.status(500).body("创建数据库表失败");
- ? ? ? ? }
- ? ? ? ? return ResponseEntity.ok("创建数据库表成功");
- ? ? }
-
- }
创建 SpringBoot 启动类
创建一个用于启动 SpringBoot 的启动类,代码如下:
- 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);
- ? ? }
-
- }
调用创建表的接口进行测试
执行 curl 命令,使用 POST 方法调用之前示例项目 Controller 类中提供的创建表接口 /createTable,在数据库 test 中创建一个 user 表:
- $ curl -X POST http://localhost:8080/createTable?tableName=user
执行完接口后,再进入数据库,输入下面命令观察库中是否创建包成功:
- mysql> use test;
- Database changed
-
- mysql> show tables;
- +----------------------------------------+
- | Tables_in_test ? ? ? ? ? ? ? ? ? ? ? ? |
- +----------------------------------------+
- | user ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? |
- +----------------------------------------+
- 1 rows in set (0.00 sec)
可以看到 test 库中已经成功创建了 user 表。
到此这篇关于SpringBoot结合Mybatis实现创建数据库表的方法的文章就介绍到这了,更多相关SpringBoot Mybatis创建数据库表内容请搜索w3xue以前的文章或继续浏览下面的相关文章希望大家以后多多支持w3xue!