springboot 整合 mybatis
时间: 2023-06-01 21:02:17 浏览: 146
Spring Boot整合MyBatis步骤如下:
1. 添加依赖
在pom.xml文件中添加以下依赖:
```xml
<!-- Spring Boot MyBatis Starter -->
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.2.0</version>
</dependency>
<!-- MySQL Connector/J -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.23</version>
</dependency>
```
2. 配置数据源
在application.properties文件中配置数据源:
```properties
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost:3306/test?useSSL=false&serverTimezone=UTC&allowPublicKeyRetrieval=true
spring.datasource.username=root
spring.datasource.password=root
```
3. 配置MyBatis
在application.properties文件中配置MyBatis:
```properties
mybatis.mapper-locations=classpath:mapper/*.xml
mybatis.type-aliases-package=com.example.demo.entity
```
4. 编写Mapper
编写Mapper接口和Mapper映射文件。例如,创建UserMapper接口和UserMapper.xml文件。
UserMapper.java:
```java
package com.example.demo.mapper;
import com.example.demo.entity.User;
import org.apache.ibatis.annotations.Mapper;
import java.util.List;
@Mapper
public interface UserMapper {
List<User> findAll();
User findById(Long id);
void save(User user);
void update(User user);
void delete(Long id);
}
```
UserMapper.xml:
```xml
<?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="UserResultMap" type="com.example.demo.entity.User">
<id property="id" column="id"/>
<result property="name" column="name"/>
<result property="age" column="age"/>
</resultMap>
<select id="findAll" resultMap="UserResultMap">
SELECT * FROM user
</select>
<select id="findById" resultMap="UserResultMap">
SELECT * FROM user WHERE id=#{id}
</select>
<insert id="save">
INSERT INTO user(name, age) VALUES(#{name}, #{age})
</insert>
<update id="update">
UPDATE user SET name=#{name}, age=#{age} WHERE id=#{id}
</update>
<delete id="delete">
DELETE FROM user WHERE id=#{id}
</delete>
</mapper>
```
5. 编写Service和Controller
编写UserService和UserController类,并注入UserMapper。
UserService.java:
```java
package com.example.demo.service;
import com.example.demo.entity.User;
import com.example.demo.mapper.UserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.util.List;
@Service
@Transactional
public class UserService {
@Autowired
private UserMapper userMapper;
public List<User> findAll() {
return userMapper.findAll();
}
public User findById(Long id) {
return userMapper.findById(id);
}
public void save(User user) {
userMapper.save(user);
}
public void update(User user) {
userMapper.update(user);
}
public void delete(Long id) {
userMapper.delete(id);
}
}
```
UserController.java:
```java
package com.example.demo.controller;
import com.example.demo.entity.User;
import com.example.demo.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.List;
@RestController
@RequestMapping("/users")
public class UserController {
@Autowired
private UserService userService;
@GetMapping("")
public List<User> findAll() {
return userService.findAll();
}
@GetMapping("/{id}")
public User findById(@PathVariable Long id) {
return userService.findById(id);
}
@PostMapping("")
public void save(@RequestBody User user) {
userService.save(user);
}
@PutMapping("")
public void update(@RequestBody User user) {
userService.update(user);
}
@DeleteMapping("/{id}")
public void delete(@PathVariable Long id) {
userService.delete(id);
}
}
```
6. 运行测试
启动应用程序并访问http://localhost:8080/users,如果一切正常,则应该能够看到所有用户的列表。
以上是Spring Boot整合MyBatis的步骤,希望能帮助到你。
阅读全文