mybatis-plus在springmvc中的配置 java
时间: 2024-01-06 07:16:40 浏览: 96
好的,下面是mybatis-plus在springmvc中的配置:
1. 首先需要在pom.xml中添加mybatis-plus的依赖
```
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.4.3.1</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&serverTimezone=UTC
spring.datasource.username=root
spring.datasource.password=root
```
3. 创建实体类和Mapper接口
```
// 实体类
@Data
public class User {
private Long id;
private String name;
private Integer age;
}
// Mapper接口
public interface UserMapper extends BaseMapper<User> {
}
```
4. 在Spring配置文件中配置MapperScannerConfigurer
```
@Configuration
public class MybatisPlusConfig {
@Bean
public MapperScannerConfigurer mapperScannerConfigurer() {
MapperScannerConfigurer configurer = new MapperScannerConfigurer();
configurer.setBasePackage("com.example.mapper");
return configurer;
}
}
```
5. 最后在Controller中使用Mapper接口操作数据库
```
@RestController
public class UserController {
@Autowired
private UserMapper userMapper;
@GetMapping("/users")
public List<User> getUsers() {
return userMapper.selectList(null);
}
}
```
以上就是mybatis-plus在springmvc中的配置。
阅读全文