springboot-mybatisplus
1.导入mybatisplus依赖
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.5.1</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.2.11</version>
</dependency>
2.application.yml文件配置
spring:
datasource:
type: com.alibaba.druid.pool.DruidDataSource
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://localhost:3306/4031
username: root
password: 123456
mybatis:
configuration:
map-underscore-to-camel-case: true
server:
port: 80
3.设置dao层接口和代理对象
@Mapper
public interface UserDao extends BaseMapper<User> {
}
4.CRUD标准开发
4.1实体类
public class User {
@TableId
private Long userId;
private String userName;
private Double userBalance;
}
4.2业务层代码
@Service
public class UserServiceImpl implements UserService {
@Autowired
private UserDao userDao;
@Override
public Result queryUser() {
List<User> users = userDao.selectList(null);
int code=users==null?Code.QUERY_ERR:Code.QUERY_OK;
return new Result(code,null,users);
}
@Override
public Result getUserById(Integer userId) {
User user = userDao.selectById(userId);
int code= user==null?Code.QUERY_ERR:Code.QUERY_OK;
return new Result(code,null,user);
}
@Override
public Result saveUser(User user) {
int insert = userDao.insert(user);
int code= insert>0?Code.SAVE_OK:Code.SAVE_ERR;
return new Result(code,null,insert);
}
@Override
public Result updateUser(User user) {
int update = userDao.updateById(user);
int code= update>0?Code.UPDATE_OK:Code.UPDATE_ERR;
return new Result(code,null,update);
}
@Override
public Result deleteUser(Integer userId) {
int i = userDao.deleteById(userId);
int code= i>0?Code.DELETE_OK:Code.DELETE_ERR;
return new Result(code,null,i);
}
}
4.3控制层代码
@RestController
@RequestMapping("/users")
public class UserController {
@Autowired
private UserService userService;
@GetMapping
public Result queryUser() {
return userService.queryUser();
}
@GetMapping("/{id}")
public Result getUserById(@PathVariable Integer id) {
return userService.getUserById(id);
}
@PostMapping
public Result saveUser(@RequestBody User user) {
return userService.saveUser(user);
}
@PutMapping
public Result updateUser(@RequestBody User user){
return userService.updateUser(user);
}
@DeleteMapping("/{id}")
public Result deleteUser(@PathVariable Integer id){
return userService.deleteUser(id);
}
}
5.特殊查询
5.1分页查询
5.1.1业务层代码
public Result pageSelect(Integer current,Integer size) {
IPage page=new Page<>(current,size);
IPage<User> page1 = userDao.selectPage(page,null);
int code=page1.getRecords()==null?Code.QUERY_ERR:Code.QUERY_OK;
return new Result(code,null,page1);
}
5.1.2配置分页拦截器
@Configuration
public class MpConfig {
@Bean
public MybatisPlusInterceptor mpInterceptor(){
MybatisPlusInterceptor interceptor=new MybatisPlusInterceptor();
interceptor.addInnerInterceptor(new PaginationInnerInterceptor());
return interceptor;
}
}
自带分页查询的原理是在基本查询时进行拦截并进行切面编程加入limit语句实现分页
5.1.3控制层代码
@GetMapping("/{current}/{size}")
public Result pageUser(@PathVariable("current") Integer current,
@PathVariable("size") Integer size){
return userService.pageSelect(current,size);
}
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-HYMfI7h7-1663767549762)(C:\Users\冯瑞涛\AppData\Roaming\Typora\typora-user-images\1663205836197.png)]
? 访问和返回方法
关闭多余日志在 application.yml文件中写入
spring:
main:
banner-mode: off
mybatis-plus:
global-config:
banner: false
5.2条件查询
创建Wrapper接口的实现类对象来给查询加条件(以下代码为业务层代码)
第一个实现类(QueryWrapper)写法?
@Override
public Result conditionSelect(User user) {
QueryWrapper<User> wrapper =new QueryWrapper<>();
wrapper.lt("userBalance",50);
wrapper.gt("userBalance",100);
List<User> users = userDao.selectList(wrapper);
int code=users==null?Code.QUERY_ERR:Code.QUERY_OK;
return new Result(code,null,users);
}
第二种写法(lambda写法)?
public Result conditionSelect(User user) {
QueryWrapper<User> wrapper =new QueryWrapper<>();
wrapper.lambda().lt(User::getUserBalance,50);
wrapper.lambda().gt(User::getUserBalance,100);
List<User> users = userDao.selectList(wrapper);
int code=users==null?Code.QUERY_ERR:Code.QUERY_OK;
return new Result(code,null,users);
}
第二个实现类(LambdaQueryWrapper)写法?
public Result conditionSelect(User user) {
LambdaQueryWrapper<User> wrapper=new LambdaQueryWrapper();
wrapper.lt(User::getUserBalance,100);
wrapper.gt(User::getUserBalance,100);
List<User> users = userDao.selectList(wrapper);
int code=users==null?Code.QUERY_ERR:Code.QUERY_OK;
return new Result(code,null,users);
}
and条件
//直接写入两相反条件即可
wrapper.lt(User::getUserBalance,100);
wrapper.gt(User::getUserBalance,100);
//连接写法
wrapper.lt(User::getUserBalance,100).gt(User::getUserBalance,100);
or条件
wrapper.lt(User::getUserBalance,100).or();
wrapper.gt(User::getUserBalance,100);
条件个数不定查询
if(user.balance!=null){
wrapper.lt(User::getUserBalance,100);
wrapper.gt(User::getUserBalance,100);
}
wrapper.lt(user.balance!=null,User::getUserBalance,100);
wrapper.gt(user.balance!=null,User::getUserBalance,100);
模糊查询语句
wrapper.like(User::getUserName,"张");
查询投影
wrapper.select(User::getUserId,User::getUserName);
特殊查询投影
QueryWrapper<User> wrapper =new QueryWrapper<>();
wrapper.select("count(*) as count");
List<Map<String, Object>> users = userDao.selectMaps(wrapper);
分组统计查询
QueryWrapper<User> wrapper =new QueryWrapper<>();
wrapper.select("count(*) as count");
wrapper.groupBy("roleId");
List<Map<String, Object>> users = userDao.selectMaps(wrapper);
查询条件统计(lambda实现类对象条件下)
1.等匹配
wrapper.eq(User::getUserName,"张三")
2.范围查询
wrapper.between语句(User::getUserBalance,50,100)
6.常用注解
@TableName(value = "db_user")
@TableField(value = "pwd")
@TableId
@TableField(select = false)
7.配置id生产策略和配置表名实体类名映射
7.1id生成策略
注解方式
@TableId(type = IdType.NONE)
application.yml文件配置
mybatis-plus:
global-config:
db-config:
id-type: none
7.2表名实体类名映射
注解方式
@TableName(value = "表名")
application.yml文件配置通用
mybatis-plus:
global-config:
db-config:
table-prefix: 表前缀名
7.3逻辑删除
先在实体类与数据库表中添加状态字段:
注解方式
public class User {
private Long userId;
private String userName;
private Double userBalance;
@TableLogic(value = "0",delval = "1")
private Integer state;
}
application.yml文件配置通用
mybatis-plus:
global-config:
db-config:
logic-delete-field: state
logic-not-delete-value: 0
logic-delete-value: 1
mybatisplus批量删除
public Result sumDelete(){
List<Integer> list=new ArrayList<>();
list.add(1);
list.add(2);
list.add(3);
int i = userDao.deleteBatchIds(list);
int code = i > 0 ? Code.DELETE_OK : Code.DELETE_ERR;
return new Result(code,null,i);
}
字段 logic-not-delete-value: 0 #未删除的值 logic-delete-value: 1 #已删除的状态值
mybatisplus批量删除
```java
public Result sumDelete(){
List<Integer> list=new ArrayList<>();
list.add(1);
list.add(2);
list.add(3);
//mybatisplus自带的批量删除方法(需要一个存有需删除数据id的集合作为参数)
int i = userDao.deleteBatchIds(list);
int code = i > 0 ? Code.DELETE_OK : Code.DELETE_ERR;
return new Result(code,null,i);
}
|