springboot整合mybatis plus
时间: 2023-06-05 11:48:05 浏览: 127
springboot整合mybatis
Spring Boot整合Mybatis Plus可以通过以下步骤实现:
1. 引入Mybatis Plus和Mybatis的依赖
在pom.xml文件中添加以下依赖:
```
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.4.2</version>
</dependency>
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.2.</version>
</dependency>
```
2. 配置数据源
在application.properties文件中配置数据源信息,例如:
```
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=utf-8&useSSL=false&serverTimezone=Asia/Shanghai
spring.datasource.username=root
spring.datasource.password=root
```
3. 配置Mybatis Plus
在application.properties文件中添加以下配置:
```
# Mybatis Plus配置
mybatis-plus.mapper-locations=classpath*:mapper/**/*.xml
mybatis-plus.type-aliases-package=com.example.demo.entity
```
4. 编写实体类和Mapper接口
在com.example.demo.entity包下编写实体类,例如:
```
@Data
public class User {
private Long id;
private String name;
private Integer age;
private String email;
}
```
在com.example.demo.mapper包下编写Mapper接口,例如:
```
public interface UserMapper extends BaseMapper<User> {
}
```
5. 编写Mapper XML文件
在resources/mapper目录下编写Mapper XML文件,例如:
```
<?xml version="1." encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.//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="age" property="age"/>
<result column="email" property="email"/>
</resultMap>
<select id="selectById" resultMap="BaseResultMap">
select * from user where id=#{id}
</select>
<insert id="insert" parameterType="com.example.demo.entity.User">
insert into user(name, age, email) values(#{name}, #{age}, #{email})
</insert>
<update id="updateById" parameterType="com.example.demo.entity.User">
update user set name=#{name}, age=#{age}, email=#{email} where id=#{id}
</update>
<delete id="deleteById" parameterType="java.lang.Long">
delete from user where id=#{id}
</delete>
</mapper>
```
6. 编写Service和Controller
在com.example.demo.service包下编写Service接口和实现类,例如:
```
public interface UserService extends IService<User> {
}
```
```
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService {
}
```
在com.example.demo.controller包下编写Controller类,例如:
```
@RestController
@RequestMapping("/user")
public class UserController {
@Autowired
private UserService userService;
@GetMapping("/{id}")
public User getById(@PathVariable Long id) {
return userService.getById(id);
}
@PostMapping
public boolean save(@RequestBody User user) {
return userService.save(user);
}
@PutMapping
public boolean updateById(@RequestBody User user) {
return userService.updateById(user);
}
@DeleteMapping("/{id}")
public boolean removeById(@PathVariable Long id) {
return userService.removeById(id);
}
}
```
7. 启动应用程序
运行Spring Boot应用程序,访问http://localhost:808/user/{id}可以查询用户信息,访问http://localhost:808/user可以新增用户信息,访问http://localhost:808/user可以更新用户信息,访问http://localhost:808/user/{id}可以删除用户信息。
阅读全文