Configuration Practice Guide for Spring Boot and MyBatis
Introduction:
Spring Boot is a rapid development framework used to simplify the startup and deployment process of Spring applications . And MyBatis is a popular persistence framework that can easily interact with various relational databases. This article will introduce how to configure and use MyBatis in a Spring Boot project and provide specific code examples.
1. Project configuration
1.Introduce dependencies
In the pom.xml file, add the following dependencies:
<dependencies> <!-- Spring Boot Web --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <!-- MyBatis --> <dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> </dependency> <!-- 数据库驱动(例如,MySQL)--> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> </dependency> </dependencies>
2.Configure database connection
Inapplication.properties
file, configure database connection information. For example, if you use a MySQL database, you can add the following configuration:
spring.datasource.url=jdbc:mysql://localhost:3306/mydatabase spring.datasource.username=root spring.datasource.password=123456 spring.datasource.driver-class-name=com.mysql.jdbc.Driver
2. Create the entity class
1. Create the entity class
in the com.example.demo.entity
package , create an entity class named User
:
public class User { private int id; private String name; private String email; // 省略 getters 和 setters }
2. Create Mapper interface
In the com.example.demo.mapper
package, create An interface named UserMapper
:
public interface UserMapper { List<User> getAllUsers(); User getUserById(int id); void addUser(User user); void updateUser(User user); void deleteUser(int id); }
3. Create Mapper XML file
Create UserMapper
corresponding Mapper XML file UserMapper.xml
, and configure the corresponding SQL operations:
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd"> <mapper namespace="com.example.demo.mapper.UserMapper"> <resultMap id="BaseResultMap" type="com.example.demo.entity.User"> <id column="id" property="id"/> <result column="name" property="name"/> <result column="email" property="email"/> </resultMap> <select id="getAllUsers" resultMap="BaseResultMap"> SELECT * FROM user </select> <select id="getUserById" resultMap="BaseResultMap"> SELECT * FROM user WHERE id=#{id} </select> <insert id="addUser"> INSERT INTO user(name, email) VALUES (#{name}, #{email}) </insert> <update id="updateUser"> UPDATE user SET name=#{name}, email=#{email} WHERE id=#{id} </update> <delete id="deleteUser"> DELETE FROM user WHERE id=#{id} </delete> </mapper>
4. Configure MyBatis
1. Create a configuration class
In the com.example.demo.config
package, create a Configuration class named MyBatisConfig
:
@Configuration @MapperScan("com.example.demo.mapper") public class MyBatisConfig { }
2. Complete the configuration
In the application.properties
file, add the following configuration:
# MyBatis mybatis.mapper-locations=classpath*:com/example/demo/mapper/*.xml
So far, we have completed the configuration and preparation of the project. Next, we will learn how to use MyBatis in a Spring Boot project.
5. Using MyBatis
1. Write business logic
In the com.example.demo.service
package, create a service named UserService
Class:
@Service public class UserService { @Autowired private UserMapper userMapper; public List<User> getAllUsers() { return userMapper.getAllUsers(); } public User getUserById(int id) { return userMapper.getUserById(id); } public void addUser(User user) { userMapper.addUser(user); } public void updateUser(User user) { userMapper.updateUser(user); } public void deleteUser(int id) { userMapper.deleteUser(id); } }
2. Create a controller
In the com.example.demo.controller
package, create a controller class named UserController
:
@RestController @RequestMapping("/users") public class UserController { @Autowired private UserService userService; @GetMapping("") public List<User> getAllUsers() { return userService.getAllUsers(); } @GetMapping("/{id}") public User getUserById(@PathVariable int id) { return userService.getUserById(id); } @PostMapping("") public void addUser(@RequestBody User user) { userService.addUser(user); } @PutMapping("/{id}") public void updateUser(@PathVariable int id, @RequestBody User user) { user.setId(id); userService.updateUser(user); } @DeleteMapping("/{id}") public void deleteUser(@PathVariable int id) { userService.deleteUser(id); } }
3. Test API
Start the Spring Boot application, visit the following URL in the browser, test the API:
Summary:
This article introduces the configuration practice method of using MyBatis in the Spring Boot project and provides specific code examples. I hope this article can help readers quickly understand and use the combination of Spring Boot and MyBatis to develop Spring applications more efficiently.
The above is the detailed content of Spring Boot and MyBatis Configuration Tips Guide. For more information, please follow other related articles on the PHP Chinese website!