Get the App
SLTechnology News&Howtos  ›  Development  › 

The method of integrating springboot and mybatis

Shulou Source: shulou.com Published: 2022-06-01 00:46:06 09月30日 Update

This article mainly introduces the relevant knowledge of "the method of integration of springboot and mybatis". The editor shows you the operation process through the actual case. The operation method is simple, fast and practical. I hope this article "the method of integration of springboot and mybatis" can help you solve the problem.

Integrate MyBatis

Create a new Spring Boot project, or operate on a Chapter1 basis

Introducing dependency into pom.xml

Spring-boot-starter fundamentals and spring-boot-starter-test are used here for unit testing to verify data access.

Introduce the necessary dependency mysql-connector-java to connect to mysql

Introduce the core dependency mybatis-spring-boot-starter of integrated MyBatis

Spring-boot-starter-jdbc dependency is not introduced here because it is already included in mybatis-spring-boot-starter

Org.springframework.boot spring-boot-starter-parent 1.3.2.RELEASE org.springframework.boot spring-boot-starter org.springframework.boot spring-boot-starter-test test org.mybatis.spring.boot mybatis-spring-boot-starter 1.1.1 mysql mysql-connector-java 5.1.21

Configure the connection configuration of mysql in application.properties as previously described using jdbc and spring-data to connect to the database

Spring.datasource.url=jdbc:mysql://localhost:3306/test

Spring.datasource.username=root

Spring.datasource.password=123456

Spring.datasource.driver-class-name=com.mysql.jdbc.Driver

Like other Spring Boot projects, the basic configuration is simple and concise. Let's take a look at how to easily access the database using MyBatis on this basis.

Use MyBatis

Create a User table in Mysql that contains id (BIGINT), name (INT), and age (VARCHAR) fields. At the same time, create the mapping object User

Public class User {private Long id; private String name; private Integer age; / / omit getter and setter}

Create the operation UserMapper of User mapping, and implement insert and query operations for subsequent unit test verification

Mapperpublic interface UserMapper {@ Select ("SELECT * FROM USER WHERE NAME = # {name}") User findByName (@ Param ("name") String name); @ Insert ("INSERT INTO USER (NAME, AGE) VALUES (# {name}, # {age})") int insert (@ Param ("name") String name, @ Param ("age") Integer age);}

Create the Spring Boot main class

@ SpringBootApplicationpublic class Application {public static void main (String [] args) {SpringApplication.run (Application.class, args);}}

Create a unit test

Test logic: insert a record of name=AAA,age=20, then query according to name=AAA, and determine whether age is 20

Roll back the data at the end of the test to ensure that the data environment for each run of the test unit is independent

@ RunWith (SpringJUnit4ClassRunner.class) @ SpringApplicationConfiguration (classes = Application.class) public class ApplicationTests {@ Autowired private UserMapper userMapper; @ Test @ Rollback public void findByName () throws Exception {userMapper.insert ("AAA", 20); User u = userMapper.findByName ("AAA"); Assert.assertEquals (20, u.getAge (). IntValue ());}} this is the end of the introduction to "the method of springboot and mybatis Integration". Thank you for reading. If you want to know more about the industry, you can follow the industry information channel. The editor will update different knowledge points for you every day.

Tags: Testing integration data method unit foundation knowledge configuration database industry query verification different practical concise necessary content at the same time basic configuration field Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Huawei Linux Xiaomi vpn macOS