Home> Java> javaTutorial> body text

How to integrate springboot and mybatis

WBOY
Release: 2023-05-16 15:52:06
forward
713 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

 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  
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
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!