springboot 集成 mybatis puls
时间: 2023-08-15 07:14:14 浏览: 124
好的,我可以为您提供Spring Boot集成MyBatis Plus的具体步骤。
1. 添加MyBatis Plus的依赖
在项目的pom.xml文件中添加MyBatis Plus的依赖:
```xml
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.x.x</version>
</dependency>
```
2. 配置数据源和MyBatis Plus
在Spring Boot的配置文件application.yml中配置数据源和MyBatis Plus的相关配置,例如:
```yaml
spring:
datasource:
url: jdbc:mysql://localhost:3306/db_name?useUnicode=true&characterEncoding=utf-8&useSSL=false
username: root
password: root
driver-class-name: com.mysql.cj.jdbc.Driver
# MyBatis Plus 配置
mybatis-plus:
configuration:
map-underscore-to-camel-case: true # 下划线转驼峰命名
mapper-locations: classpath:mapper/*.xml # mapper文件路径
```
3. 编写Mapper接口
编写Mapper接口,可以继承MyBatis Plus提供的BaseMapper接口,例如:
```java
public interface UserMapper extends BaseMapper<User> {
}
```
4. 编写Mapper.xml文件
在resources目录下创建mapper目录,然后编写Mapper.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">
<select id="selectById" parameterType="long" resultType="com.example.demo.entity.User">
select * from user where id = #{id}
</select>
</mapper>
```
5. 使用Mapper接口
在Service中使用Mapper接口,例如:
```java
@Service
public class UserServiceImpl implements UserService {
@Autowired
private UserMapper userMapper;
@Override
public User selectById(Long id) {
return userMapper.selectById(id);
}
}
```
这样,您就可以使用MyBatis Plus来进行数据库操作了。希望能帮到您!
阅读全文