Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What is the method of creating database tables by combining SpringBoot with Mybatis?

2025-04-03 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article analyzes "what is the method of creating database tables by combining SpringBoot with Mybatis". The content is detailed and easy to understand, "what is the method of SpringBoot combined with Mybatis to create database tables?" interested friends can follow the editor's train of thought to read it slowly and deeply. I hope it will be helpful to everyone after reading. Let's follow the editor to learn more about "what is the method of creating database tables by combining SpringBoot with Mybatis".

Preface

System environment:

JAVA JDK version: 1.8

MySQL version: 8.0.27

MyBatis version: 3.5.9

SpingBoot version: 2.6.2

Why to realize the function of creating tables through applications?

Recently received the project, as customers need sub-database sub-table, and each time to create a lot of tables manually, may be their own trouble, so I found some automatically create tables through the application of the solution, in which I am more familiar with the use of MyBatis, so through the form of blog post to explain to you, how to use Mybatis to dynamically create tables in the database in the SpringBoot environment.

Prepare the SQL statement to create the table

Create a table SQL statement as follows:

CREATE TABLE IF NOT EXISTS `user` (`id` int (0) NOT NULL AUTO_INCREMENT COMMENT 'primary key', `group_ id` int (0) NULL DEFAULT NULL COMMENT 'group number', `username` varchar (20) NULL DEFAULT NULL COMMENT 'username', `password` varchar (20) NULL DEFAULT NULL COMMENT 'password', PRIMARY KEY (`id`) ENGINE = InnoDB AUTO_INCREMENT = 9 CHARACTER SET = utf8mb4 COMMENT = 'user table for testing'; create a database example through MyBatis

The purpose is to implement the function of creating tables in the database by executing the statement of creating tables through MyBatis. The implementation code is as follows:

Introducing related dependencies into Maven

Introduce SpringBoot, MySql, MyBytis and other dependencies into the pom.xml file of Maven, as follows:

4.0.0 org.springframework.boot spring-boot-starter-parent 2.6.2 club.mydlq springboot-mybatis-create-table-example 1.0.0 springboot-mybatis-example springboot mybatis create table example project 1.8 org.springframework.boot spring-boot-starter-web Org.projectlombok lombok true mysql mysql-connector-java org.mybatis.spring.boot mybatis-spring-boot-starter 2.2.1 Org.springframework.boot spring-boot-maven-plugin adds database configuration to the SpringBoot configuration file

In the application.yml file of SpringBoot, add the database connection parameters as follows:

Spring: application: name: springboot-mybatis-create-table-example # Database configuration 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: 12345 specify the xml file location of mapper mybatis: mapper-locations: classpath:mappers/*.xml to create the Mapper interface class for the test

Create a Mapper interface for MyBatis where the user creates the table, with the following code:

Import org.apache.ibatis.annotations.Mapper;@Mapperpublic interface TableMapper {/ * create database table * * @ param tableName table name * / void createTable (String tableName);} create the XML file associated with Mapper

Create a xml file TableMapper.xml associated with the Mapper API, and add the SQL statement to create the table, as shown below:

CREATE TABLE IF NOT EXISTS `$ {tableName} `(`id` int (0) NOT NULL AUTO_INCREMENT COMMENT 'primary key', `password` int (0) NULL DEFAULT NULL COMMENT 'group number', `username` varchar (20) NULL DEFAULT NULL COMMENT 'username', `password` varchar (20) NULL DEFAULT NULL COMMENT 'password' PRIMARY KEY (`id`) ENGINE = InnoDB AUTO_INCREMENT = 9 CHARACTER SET = utf8mb4 COMMENT = 'user table for testing' Create a Controller class for testing

Create a Controller class for testing, which provides an interface for creating tables. The code is as follows:

Import club.mydlq.mappers.TableMapper;import org.springframework.http.ResponseEntity;import org.springframework.web.bind.annotation.*;import javax.annotation.Resource;@RestControllerpublic class TestController {@ Resource private TableMapper tableMapper / * create database table * * @ param tableName table name * @ return whether created successfully * / @ PostMapping ("/ createTable") public ResponseEntity createTableTest (@ RequestParam String tableName) {try {/ / create database table tableMapper.createTable (tableName) } catch (Exception e) {return ResponseEntity.status (500) .body ("failed to create database table");} return ResponseEntity.ok ("create database table succeeded");}} create SpringBoot startup class

Create a startup class to start SpringBoot with the following code:

Import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.SpringBootApplication;@SpringBootApplicationpublic class Application {public static void main (String [] args) {SpringApplication.run (Application.class, args);} calls the API that creates the table for testing

Execute the curl command, use the POST method to call the create table interface / createTable provided in the previous sample project Controller class, and create a user table in the database test:

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

After executing the interface, enter the database and enter the following command to see whether the package is created successfully in the library:

Mysql > use test;Database changedmysql > show tables +-- + | Tables_in_test | +-- + | user | + -- + 1 rows in set (0.00 sec)

You can see that the user table has been successfully created in the test library.

What is springboot? springboot is a new programming specification, which is designed to simplify the initial construction and development process of new Spring applications. SpringBoot is also a framework that serves the framework, and the scope of services is to simplify configuration files.

On SpringBoot combined with Mybatis implementation to create database tables what is the method to share here, I hope that the above content can make you improve. If you want to learn more knowledge, please pay more attention to the editor's updates. Thank you for following the website!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report