Home > Java > javaTutorial > How to integrate springboot and mybatis

How to integrate springboot and mybatis

WBOY
Release: 2023-05-16 15:52:06
forward
769 people have browsed it

Integrate MyBatis

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

Introduce dependencies in pom.xml

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

  • Introducing the necessary dependency for connecting to mysql mysql-connector- java

  • Introduce the core dependency of integrating MyBatis mybatis-spring-boot-starter

  • The spring-boot-starter-jdbc dependency is not introduced here. This is because mybatis-spring-boot-starter already contains this dependency

<parent>
 <groupId>org.springframework.boot</groupId>
 <artifactId>spring-boot-starter-parent</artifactId>
 <version>1.3.2.RELEASE</version>
 <relativePath/> <!-- lookup parent from repository -->
</parent>
<dependencies>
 <dependency>
 <groupId>org.springframework.boot</groupId>
 <artifactId>spring-boot-starter</artifactId>
 </dependency>
 <dependency>
 <groupId>org.springframework.boot</groupId>
 <artifactId>spring-boot-starter-test</artifactId>
 <scope>test</scope>
 </dependency>
 <dependency>
 <groupId>org.mybatis.spring.boot</groupId>
 <artifactId>mybatis-spring-boot-starter</artifactId>
 <version>1.1.1</version>
 </dependency>
 <dependency>
 <groupId>mysql</groupId>
 <artifactId>mysql-connector-java</artifactId>
 <version>5.1.21</version>
 </dependency>
</dependencies>
Copy after login

is the same as the previously introduced use of jdbc and spring-data to connect to the database, in application.properties Configure the connection configuration of mysql

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 completed simply and concisely. Let’s see how to configure this It is easy and convenient to use MyBatis to access the database based on the basics.

Use MyBatis

Create a User table in Mysql, including 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;
  // 省略getter和setter
}
Copy after login

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

@Mapper
public 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);
}
Copy after login

Create the Spring Boot main class

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

Create unit test

Test logic: insert a record with name=AAA, age=20, then query based on name=AAA, and determine whether age is 20
Roll back the data after the test to ensure that the data environment of 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());
 }
}
Copy after login

The above is the detailed content of How to integrate springboot and mybatis. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:yisu.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template