springboot整合mybatisplus jar
时间: 2023-12-20 18:31:58 浏览: 100
springboot-mybatisplus-demo.zip
```java
// SpringBoot整合MybatisPlus Jar
// 1. 首先在pom.xml文件中添加MybatisPlus的依赖
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.4.3</version>
</dependency>
// 2. 在application.properties或application.yml中配置数据源信息
spring.datasource.url=jdbc:mysql://localhost:3306/db_example
spring.datasource.username=springuser
spring.datasource.password=ThePassword
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
// 3. 创建实体类和Mapper接口,使用MybatisPlus的注解进行配置
import com.baomidou.mybatisplus.annotation.TableName;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import org.apache.ibatis.annotations.Mapper;
@Mapper
public interface UserMapper extends BaseMapper<User> {
}
@TableName("user")
public class User {
private Long id;
private String name;
// 省略getter和setter
}
// 4. 编写Service层和Controller层,调用Mapper接口进行数据库操作
@Service
public class UserService {
@Autowired
private UserMapper userMapper;
public User getUserById(Long id) {
return userMapper.selectById(id);
}
}
@RestController
public class UserController {
@Autowired
private UserService userService;
@GetMapping("/user/{id}")
public User getUser(@PathVariable Long id) {
return userService.getUserById(id);
}
}
```
阅读全文