- mybatispuls的出现使我们在开发中对于xml文件的书写时越来越少,提高了开发效率,但同时对一些不知道或者没有使用过mybatisPuls的程序猿来说,是不知道如何使用的,博主怀着一个试探的心理,去学习使用了mybatisPuls,发现结果是比较好的,但是对于新手来说,建议还是继续使用xml进行编写SQL,只有基础牢固了,才能更好的使用mybatisPuls,从而提高开发效率。
- 在使用mybatisPuls的过程中,博主发现一些基础的问题,就是对于刚刚学习使用mybatisPuls的人来说,一些单表查询,一些简单的查询,是可以使用上mybatisPuls的,但是对于较为复杂的却不如去xml中写SQL来的快些,因此,博主个人建议,简单SQL可以直接使用mybatisPuls进行查询,对于多表联查等复杂查询,应使用xml进行查询,这样的好处不仅仅是对不熟悉mybatisPuls的人来说提升了效率,也为后期维护时,成本降低了些许。
- 以上是博主个人的认为,下面介绍一些mybatisPuls的简单用法,其主要用法可归为三类,1是key键查询,2是条件查询,3是多表查询。
- mybatisPuls配置使用:
mybatisPuls使用配置(pom文件),这只是mybatisPuls的jar包
<!--mybatisPuls-->
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.4.0</version>
</dependency>
在使用mybatisPuls的同时,也可以使用代码生成,在本次案例中没有使用,也就不进行说明配置方法,在mybatisPuls官方文档中存在代码生成器的使用规则,有兴趣的可以查看使用
附上mybatispuls的官方文档:https://baomidou.com/guide/annotation.html#tablename
5. key键查询使用方法
配置完成之后,我们需要在mapper中进行继承BaseMapper类,其类中的泛型为改mapper所在表实体类型,一些简单的查询方法,在BaseMapper就已经自动实现,无需手动添加,例如key键查询,也在BaseMapper中默认实现了的
6. 复杂的单表查询,在BaseMapper中也是有方法进行使用的,文中就添加几个例子,其他详细可看BaseMapper官方文档使用规范
QueryWrapper<User> userQueryWrapper = new QueryWrapper<>();
userQueryWrapper.like("sname",user.getSname()).gt("sage",user.getSage());
这里的QueryWrapper是对象查询的封装类,可以通过其封装类进行对指定条件进行赋值,也就可使用固定条件查询
userQueryWrapper.like("sname",user.getSname()).gt("sage",user.getSage());
此句为对sname字段进行模糊查询,并查询sage字段大于输入指定数字的
7. 这就是简单使用方法, 对于分页,等多表联查在官方文档中都有介绍,可以查阅使用.
8. 附上案例代码,以供使用:
ServiceImpl
package com.hui.mybaits.service;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.hui.mybaits.mapper.GetUserMapper;
import com.hui.mybaits.module.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.GetMapping;
import java.util.List;
/**
* @description:
* @author: liuhui
* @createDate: 2020/9/13
* @version: 1.0
*/
@Service
public class UserServiceImpl {
@Autowired
private GetUserMapper getUserMapper;
/*简单的数据查询,使用主键id进行获取数据*/
public User getUser(String id){
return getUserMapper.selectById(id);
}
/*复杂的条件查询可以通过@Select 注解在mapper方法上写SQL,多表查询的话建议使用xml进行书写,
* 这样的话更加方便维护,一些单表的条件查询可以通过下列方法进行书写*/
public User longUser(User user){
QueryWrapper<User> userQueryWrapper = new QueryWrapper<>();
userQueryWrapper.like("sname",user.getSname()).gt("sage",user.getSage());
return getUserMapper.selectOne(userQueryWrapper);
}
/*这是演示的一个简单的使用@Select注解查询的一个实例,同时也可以使用@Inster等方法做一些SQL*/
public List<User> getUserList(){
return getUserMapper.getUserList();
}
}
Mapper
package com.hui.mybaits.mapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.hui.mybaits.module.User;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;
import java.util.List;
@Mapper
public interface GetUserMapper extends BaseMapper<User> {
@Select("select * from usser")
List<User>getUserList();
}
Controller
package com.hui.mybaits.controller;
import com.hui.mybaits.module.User;
import com.hui.mybaits.service.UserServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;
/**
* @description:
* @author: liuhui
* @createDate: 2020/9/13
* @version: 1.0
*/
@RestController
public class GetUserController {
@Autowired
private UserServiceImpl userService;
@PostMapping("/getUser")
public User getUser(@RequestParam("id")String id){
User user = userService.getUser(id);
return user;
}
@PostMapping("/longUser")
public User longUser(@RequestBody User user){
return userService.longUser(user);
}
@PostMapping("/getUserList")
public List<User> getUserList(){
return userService.getUserList();
}
}
module
package com.hui.mybaits.module;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
import lombok.Data;
/**
* @description:
* @author: liuhui
* @createDate: 2020/9/13
* @version: 1.0
*/
@Data
@TableName(value = "usser") /*设置表名,其他更多的实体注解可以参考mybatisPuls的官方文档进行查看*/
public class User {
private String sname;
private String sage;
private String ssex;
private String cid;
/*设置主键方法*/
@TableId(type = IdType.AUTO)
private String id;
private String brith;
}