经验首页 前端设计 程序设计 Java相关 移动开发 数据库/运维 软件/图像 大数据/云计算 其他经验
当前位置:技术经验 » 数据库/运维 » MyBatis » 查看文章
SpringBoot结合Mybatis实现创建数据库表的方法
来源:jb51  时间:2022/1/18 17:32:13  对本文有异议

前言

系统环境:

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

为什么要通过应用实现创建表的功能

最近接了项目时,由于客户需要分库分表,而且每次手动创建很多表,可能是自己闲麻烦,于是乎就找了一些通过应用自动创建表的解决方案,其中本人比较熟悉使用 MyBatis,所以通过博文的形式给大家讲解一下,如何在 SpringBoot 环境中,使用 Mybatis 动态的创建数据库中的表的功能。

准备创建表的 SQL 语句

创建表 SQL 语句,内容如下:

  1. CREATE TABLE IF NOT EXISTS `user`
  2. (
  3. `id` int(0) NOT NULL AUTO_INCREMENT COMMENT '主键',
  4. `group_id` int(0) NULL DEFAULT NULL COMMENT '组号',
  5. `username` varchar(20) NULL DEFAULT NULL COMMENT '用户名',
  6. `password` varchar(20) NULL DEFAULT NULL COMMENT '密码',
  7. PRIMARY KEY (`id`)
  8. ) ENGINE = InnoDB
  9. AUTO_INCREMENT = 9
  10. CHARACTER SET = utf8mb4 COMMENT ='用于测试的用户表';

实现通过 MyBatis 创建数据库表示例

目的就是解决通过 MyBatis 执行创建表的语句,从而实现创建数据库中的表的功能,实现代码如下:

在 Maven 中引入相关依赖

在 Maven的 pom.xml文件中,引入 SpringBoot、MySql、MyBytis 等依赖,内容如下:

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  3. ? ? ? ? ?xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  4. ? ? <modelVersion>4.0.0</modelVersion>
  5.  
  6. ? ? <parent>
  7. ? ? ? ? <groupId>org.springframework.boot</groupId>
  8. ? ? ? ? <artifactId>spring-boot-starter-parent</artifactId>
  9. ? ? ? ? <version>2.6.2</version>
  10. ? ? </parent>
  11.  
  12. ? ? <groupId>club.mydlq</groupId>
  13. ? ? <artifactId>springboot-mybatis-create-table-example</artifactId>
  14. ? ? <version>1.0.0</version>
  15. ? ? <name>springboot-mybatis-example</name>
  16. ? ? <description>springboot mybatis create table example project</description>
  17.  
  18. ? ? <properties>
  19. ? ? ? ? <java.version>1.8</java.version>
  20. ? ? </properties>
  21.  
  22. ? ? <dependencies>
  23. ? ? ? ? <!-- Web -->
  24. ? ? ? ? <dependency>
  25. ? ? ? ? ? ? <groupId>org.springframework.boot</groupId>
  26. ? ? ? ? ? ? <artifactId>spring-boot-starter-web</artifactId>
  27. ? ? ? ? </dependency>
  28. ? ? ? ? <!-- Lombok -->
  29. ? ? ? ? <dependency>
  30. ? ? ? ? ? ? <groupId>org.projectlombok</groupId>
  31. ? ? ? ? ? ? <artifactId>lombok</artifactId>
  32. ? ? ? ? ? ? <optional>true</optional>
  33. ? ? ? ? </dependency>
  34. ? ? ? ? <!-- Mysql -->
  35. ? ? ? ? <dependency>
  36. ? ? ? ? ? ? <groupId>mysql</groupId>
  37. ? ? ? ? ? ? <artifactId>mysql-connector-java</artifactId>
  38. ? ? ? ? </dependency>
  39. ? ? ? ? <!-- MyBatis -->
  40. ? ? ? ? <dependency>
  41. ? ? ? ? ? ? <groupId>org.mybatis.spring.boot</groupId>
  42. ? ? ? ? ? ? <artifactId>mybatis-spring-boot-starter</artifactId>
  43. ? ? ? ? ? ? <version>2.2.1</version>
  44. ? ? ? ? </dependency>
  45. ? ? </dependencies>
  46.  
  47. ? ? <build>
  48. ? ? ? ? <plugins>
  49. ? ? ? ? ? ? <plugin>
  50. ? ? ? ? ? ? ? ? <groupId>org.springframework.boot</groupId>
  51. ? ? ? ? ? ? ? ? <artifactId>spring-boot-maven-plugin</artifactId>
  52. ? ? ? ? ? ? </plugin>
  53. ? ? ? ? </plugins>
  54. ? ? </build>
  55.  
  56. </project>

在 SpringBoot 配置文件中添加数据库配置

在 SpringBoot 的 application.yml 文件中,添加数据库连接的参数,配置内容如下:

  1. spring:
  2. ? application:
  3. ? ? name: springboot-mybatis-create-table-example
  4. ? # 数据库配置
  5. ? datasource:
  6. ? ? type: com.zaxxer.hikari.HikariDataSource
  7. ? ? driverClassName: com.mysql.cj.jdbc.Driver
  8. ? ? url: jdbc:mysql://127.0.0.1:3306/test?serverTimezone=Asia/Shanghai&useUnicode=true&useSSL=false&allowPublicKeyRetrieval=true
  9. ? ? hikari:
  10. ? ? ? pool-name: DatebookHikariCP
  11. ? ? ? minimum-idle: 5
  12. ? ? ? maximum-pool-size: 15
  13. ? ? ? max-lifetime: 1800000
  14. ? ? ? connection-timeout: 30000
  15. ? ? ? username: root
  16. ? ? ? password: 123456
  17.  
  18. # 指定 mapper 的 xml 文件位置
  19. mybatis:
  20. ? mapper-locations: classpath:mappers/*.xml

创建测试的 Mapper 接口类

创建一个用户建表的 MyBatis 的 Mapper 接口,代码如下:

  1. import org.apache.ibatis.annotations.Mapper;
  2.  
  3. @Mapper
  4. public interface TableMapper {
  5.  
  6. ? ? /**
  7. ? ? ?* 创建数据库表
  8. ? ? ?*
  9. ? ? ?* @param tableName 表名称
  10. ? ? ?*/
  11. ? ? void createTable(String tableName);
  12.  
  13. }

创建与 Mapper 关联的 XML 文件

创建一个用于和 Mapper 接口关联的 xml 文件 TableMapper.xml,在里面添加用于创建表的 SQL 语句,内容如下:

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
  3.  
  4. <mapper namespace="club.mydlq.mappers.TableMapper">
  5.  
  6. ? ? <!--创建表的 SQL 语句-->
  7. ? ? <update id="createTable" parameterType="java.lang.String">
  8. ? ? ? ? CREATE TABLE IF NOT EXISTS `${tableName}`
  9. ? ? ? ? (
  10. ? ? ? ? ? ? `id` ? ? ? int(0) ? ? ?NOT NULL AUTO_INCREMENT COMMENT '主键',
  11. ? ? ? ? ? ? `group_id` int(0) ? ? ?NULL DEFAULT NULL COMMENT '组号',
  12. ? ? ? ? ? ? `username` varchar(20) NULL DEFAULT NULL COMMENT '用户名',
  13. ? ? ? ? ? ? `password` varchar(20) NULL DEFAULT NULL COMMENT '密码',
  14. ? ? ? ? ? ? PRIMARY KEY (`id`)
  15. ? ? ? ? ) ENGINE = InnoDB
  16. ? ? ? ? ? AUTO_INCREMENT = 9
  17. ? ? ? ? ? CHARACTER SET = utf8mb4 COMMENT ='用于测试的用户表';
  18. ? ? </update>
  19.  
  20. </mapper>

创建用于测试的 Controller 类

创建一个用于测试的 Controller 类,里面提供一个创建表的接口,代码如下:

  1. import club.mydlq.mappers.TableMapper;
  2. import org.springframework.http.ResponseEntity;
  3. import org.springframework.web.bind.annotation.*;
  4. import javax.annotation.Resource;
  5.  
  6. @RestController
  7. public class TestController {
  8.  
  9. ? ? @Resource
  10. ? ? private TableMapper tableMapper;
  11.  
  12. ? ? /**
  13. ? ? ?* 创建数据库表
  14. ? ? ?*
  15. ? ? ?* @param tableName 表名称
  16. ? ? ?* @return 是否创建成功
  17. ? ? ?*/
  18. ? ? @PostMapping("/createTable")
  19. ? ? public ResponseEntity<String> createTableTest(@RequestParam String tableName) {
  20. ? ? ? ? try {
  21. ? ? ? ? ? ? // 创建数据库表
  22. ? ? ? ? ? ? tableMapper.createTable(tableName);
  23. ? ? ? ? } catch (Exception e) {
  24. ? ? ? ? ? ? return ResponseEntity.status(500).body("创建数据库表失败");
  25. ? ? ? ? }
  26. ? ? ? ? return ResponseEntity.ok("创建数据库表成功");
  27. ? ? }
  28.  
  29. }

创建 SpringBoot 启动类

创建一个用于启动 SpringBoot 的启动类,代码如下:

  1. import org.springframework.boot.SpringApplication;
  2. import org.springframework.boot.autoconfigure.SpringBootApplication;
  3.  
  4. @SpringBootApplication
  5. public class Application {
  6.  
  7. ? ? public static void main(String[] args) {
  8. ? ? ? ? SpringApplication.run(Application.class, args);
  9. ? ? }
  10.  
  11. }

调用创建表的接口进行测试

执行 curl 命令,使用 POST 方法调用之前示例项目 Controller 类中提供的创建表接口 /createTable,在数据库 test 中创建一个 user 表:

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

执行完接口后,再进入数据库,输入下面命令观察库中是否创建包成功:

  1. mysql> use test;
  2. Database changed
  3.  
  4. mysql> show tables;
  5. +----------------------------------------+
  6. | Tables_in_test ? ? ? ? ? ? ? ? ? ? ? ? |
  7. +----------------------------------------+
  8. | user ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? |
  9. +----------------------------------------+
  10. 1 rows in set (0.00 sec)

可以看到 test 库中已经成功创建了 user 表。

到此这篇关于SpringBoot结合Mybatis实现创建数据库表的方法的文章就介绍到这了,更多相关SpringBoot Mybatis创建数据库表内容请搜索w3xue以前的文章或继续浏览下面的相关文章希望大家以后多多支持w3xue!

 友情链接:直通硅谷  点职佳  北美留学生论坛

本站QQ群:前端 618073944 | Java 606181507 | Python 626812652 | C/C++ 612253063 | 微信 634508462 | 苹果 692586424 | C#/.net 182808419 | PHP 305140648 | 运维 608723728

W3xue 的所有内容仅供测试,对任何法律问题及风险不承担任何责任。通过使用本站内容随之而来的风险与本站无关。
关于我们  |  意见建议  |  捐助我们  |  报错有奖  |  广告合作、友情链接(目前9元/月)请联系QQ:27243702 沸活量
皖ICP备17017327号-2 皖公网安备34020702000426号