mybatisplus如何整合springboot
时间: 2023-03-27 08:01:09 浏览: 105
mybatisplus可以通过在pom.xml文件中添加依赖来整合springboot,具体步骤如下:
1. 在pom.xml文件中添加mybatisplus和mysql的依赖:
```
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.4.2</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
```
2. 在application.properties文件中配置数据库连接信息:
```
spring.datasource.url=jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=utf8&serverTimezone=GMT%2B8
spring.datasource.username=root
spring.datasource.password=123456
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
```
3. 创建实体类和Mapper接口,并使用mybatisplus提供的注解来简化代码:
```
@Data
public class User {
@TableId(type = IdType.AUTO)
private Long id;
private String name;
private Integer age;
}
@Mapper
public interface UserMapper extends BaseMapper<User> {
}
```
4. 在Service层中调用Mapper接口的方法来操作数据库:
```
@Service
public class UserServiceImpl implements UserService {
@Autowired
private UserMapper userMapper;
@Override
public List<User> list() {
return userMapper.selectList(null);
}
}
```
以上就是整合mybatisplus和springboot的基本步骤。
阅读全文