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>
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 }
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); }
Create the Spring Boot main class
@SpringBootApplication public class Application { public static void main(String[] args) { SpringApplication.run(Application.class, args); } }
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()); } }
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!