表结构:
实体类:
private Long id;
private String name;
private String password;
private Integer age;
private String tel;
@Override
public String toString() {
return "User{" +
"id=" + id +
", name='" + name + '\'' +
", password='" + password + '\'' +
", age=" + age +
", tel='" + tel + '\'' +
'}';
}
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
public String getTel() {
return tel;
}
public void setTel(String tel) {
this.tel = tel;
}
}
@Mapper
public interface UserDao extends BaseMapper<User> {
}
查询所有
@SpringBootTest
class Mybatispluse01ApplicationTests {
@Autowired
private UserDao userDao;
@Test
void contextLoads() {
List<User> users = userDao.selectList(null);
System.out.println(users);
}
}
MybatisPlus官网:https://baomidou.com
@Autowired
private UserDao userDao;
//查全部
@Test
void testSelectAll() {
List<User> users = userDao.selectList(null);
System.out.println(users);
}
//插入
@Test
void testInsert(){
User user=new User();
user.setName("Jack");
user.setAge(18);
user.setPassword("jack");
user.setTel("183764849479");
userDao.insert(user);
}
//修改
@Test
void testUpdate(){
User user=new User();
user.setId(1L);
user.setName("Tom666");
userDao.updateById(user);
}
//根据id查询
@Test
void testSelectById(){
User user = userDao.selectById(1L);
System.out.println(user);
}
//根据id删除
@Test
void testDelete(){
userDao.deleteById(1551479559101206529L);
}
}
下载lomback的jar包
org.projectlombok
lombok
@Data注解(包含了set,get方法,无参构造方法,hashCode方法,equals方法)
@Data
public class User {
private Long id;
private String name;
private String password;
private Integer age;
private String tel;
}
@Test
void testByPage(){
IPage page=new Page(1,2 );//当前页码值,每页显示多少条数据
userDao.selectPage(page,null);
System.out.println("当前页码值:"+page.getCurrent());
System.out.println("每页显示数:"+page.getSize());
System.out.println("一共多少页:"+page.getPages());
System.out.println("一共多少条数据:"+page.getTotal());
System.out.println("数据:"+page.getRecords());
}
将所有的数据都查询出来了
完善:
增加一个拦截器
@Configuration
public class MpConfig {
@Bean
public MybatisPlusInterceptor mybatisPlusInterceptor(){
MybatisPlusInterceptor mybatisPlusInterceptor=new MybatisPlusInterceptor();
mybatisPlusInterceptor.addInnerInterceptor(new PaginationInnerInterceptor());
return mybatisPlusInterceptor;
}
}
数据就会显示对应的条数
@Test
void testSelectAll() {
//方式一:
QueryWrapper queryWrapper=new QueryWrapper();
//年龄小于18
queryWrapper.lt("age",18);
List<User> users = userDao.selectList(queryWrapper);
System.out.println(users);
}
//方式二:Lambda格式按条件查询
QueryWrapper<User> queryWrapper=new QueryWrapper();
queryWrapper.lambda().lt(User::getAge,18);
List<User> users = userDao.selectList(queryWrapper);
System.out.println(users);
//方式三:
LambdaQueryWrapper<User> lambdaQueryWrapper=new LambdaQueryWrapper<>();
lambdaQueryWrapper.lt(User::getAge,18);
List<User> users = userDao.selectList(lambdaQueryWrapper);
System.out.println(users);
}
//方式三:
LambdaQueryWrapper<User> lambdaQueryWrapper=new LambdaQueryWrapper<>();
lambdaQueryWrapper.lt(User::getAge,18);
//大于5岁
lambdaQueryWrapper.gt(User::getAge,5);
List<User> users = userDao.selectList(lambdaQueryWrapper);
System.out.println(users);
链式编程格式
LambdaQueryWrapper<User> lambdaQueryWrapper=new LambdaQueryWrapper<>();
//大于18或小于10岁
lambdaQueryWrapper.gt(User::getAge,18).or().lt(User::getAge,10);
List<User> users = userDao.selectList(lambdaQueryWrapper);
System.out.println(users);
}
创建一个类继承 User
添加一个属性age2
@Data
public class UserQuerry extends User {
private Integer age2;
}
UserQuerry userQuerry=new UserQuerry();
userQuerry.setAge(10);
userQuerry.setAge2(30);
LambdaQueryWrapper<User> lambdaQueryWrapper=new LambdaQueryWrapper<>();
lambdaQueryWrapper.lt(User::getAge,userQuerry.getAge());
lambdaQueryWrapper.gt(User::getAge,userQuerry.getAge2());
List<User> users = userDao.selectList(lambdaQueryWrapper);
System.out.println(users);
但是当userQuerry中的age或age2有一个为null的时候
UserQuerry userQuerry=new UserQuerry();
userQuerry.setAge(10);
LambdaQueryWrapper<User> lambdaQueryWrapper=new LambdaQueryWrapper<>();
lambdaQueryWrapper.lt(User::getAge,userQuerry.getAge());
lambdaQueryWrapper.gt(User::getAge,userQuerry.getAge2());
List<User> users = userDao.selectList(lambdaQueryWrapper);
System.out.println(users);
}
程序就会出错
解决:不推荐使用if语句
UserQuerry userQuerry=new UserQuerry();
userQuerry.setAge(10);
LambdaQueryWrapper<User> lambdaQueryWrapper=new LambdaQueryWrapper<>();
//当age为空,不添加这个给个条件
lambdaQueryWrapper.lt(null!=userQuerry.getAge(),User::getAge,userQuerry.getAge());
//同理
lambdaQueryWrapper.gt(null!=userQuerry.getAge2(),User::getAge,userQuerry.getAge2());
List<User> users = userDao.selectList(lambdaQueryWrapper);
System.out.println(users);
LambdaQueryWrapper<User> lambdaQueryWrapper=new LambdaQueryWrapper<>();
//只查询name
lambdaQueryWrapper.select(User::getName);
List<User> users = userDao.selectList(lambdaQueryWrapper);
System.out.println(users);
注意:使用的是 QueryWrapper
QueryWrapper queryWrapper=new QueryWrapper<>();
queryWrapper.select("count(*) as count");
List
QueryWrapper<User> queryWrapper=new QueryWrapper<>();
queryWrapper.select("count(*) as count","name");
//按照name分组
queryWrapper.groupBy("name");
List<Map<String, Object>> users=userDao.selectMaps(queryWrapper);
System.out.println(users);
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-yHQ44McC-1658892125326)(C:\Users\Administrator\AppData\Roaming\Typora\typora-user-images\image-20220725175630764.png)]
查询账号和密码符合的用户
LambdaQueryWrapper<User> lambdaQueryWrapper=new LambdaQueryWrapper<>();
lambdaQueryWrapper.eq(User::getName,"Tom666").eq(User::getPassword,"tom");
User user = userDao.selectOne(lambdaQueryWrapper);
System.out.println(user);
between(上下限不能互换)
//范围查询
LambdaQueryWrapper<User> lambdaQueryWrapper=new LambdaQueryWrapper<>();
lambdaQueryWrapper.between(User::getAge,10,30);
List<User> users = userDao.selectList(lambdaQueryWrapper);
System.out.println(users);
模糊查询
LambdaQueryWrapper<User> lambdaQueryWrapper=new LambdaQueryWrapper<>();
lambdaQueryWrapper.like(User::getName,"T");
//likeLeft相当于%J
//likeRight相当于J%
List<User> users = userDao.selectList(lambdaQueryWrapper);
System.out.println(users);
其他的查询条件可以查询api:条件构造器 | MyBatis-Plus (baomidou.com)
使用注解 @TableField(value = “pwd”)
使用注解 @TableField(exist = false)
@Data
@TableName("tbl_user")
public class User {
private Long id;
private String name;
@TableField(value = "pwd")
private String password;
private Integer age;
private String tel;
@TableField(exist = false)
private String online;
}
采用默认查询会将全部字段查询出来
使用注解
@TableField(value = “pwd”,select = false)
隐藏字段
@Data
@TableName("tbl_user")
public class User {
private Long id;
private String name;
@TableField(value = "pwd",select = false)
private String password;
private Integer age;
private String tel;
@TableField(exist = false)
private String online;
}
使用注解@TableName
@Data
@TableName("tbl_user")
public class User {
private Long id;
private String name;
@TableField(value = "pwd")
private String password;
private Integer age;
private String tel;
}
@TableId(type = IdType.AUTO)
private Long id;
@TableId(type = IdType.INPUT)
需要自己输入id
@Test
void testInsert(){
User user=new User();
user.setId(666L);
user.setName("Jack");
user.setAge(18);
user.setPassword("jack");
user.setTel("183764849479");
userDao.insert(user);
}
@TableId(type = IdType.ASSIGN_ID)
全局配置id生成的格式
mybatis-plus:
global-config:
db-config:
id-type: assign_id
@Test
void testDelete(){
List<Long>list=new ArrayList<>();
list.add(1551826211540389889L);
userDao.deleteBatchIds(list);
}
1.修改表结构增加字段表示是否被删除
2.修改实体类,增加标记
//逻辑删除字段,标记当前字段是否被删除
@TableLogic(value = "0",delval = "1")
private Integer deleted;
通用操作:
在配置文件中设置标记
@TableLogic
private Integer deleted;
mybatis-plus:
global-config:
db-config:
# id-type: assign_id
logic-delete-field: 0
logic-not-delete-val:1
3.执行删除操作
@Test
void testDelete(){
List<Long>list=new ArrayList<>();
list.add(5L);
userDao.deleteBatchIds(list);
}
新加一个字段version
实体类中定义该属性使用@Version注解
@Version
private Integer version;
开启拦截器
public MybatisPlusInterceptor mybatisPlusInterceptor(){
MybatisPlusInterceptor mybatisPlusInterceptor=new MybatisPlusInterceptor();
//分页拦截器
mybatisPlusInterceptor.addInnerInterceptor(new PaginationInnerInterceptor());
//添加乐观锁拦截器
mybatisPlusInterceptor.addInnerInterceptor(new OptimisticLockerInnerInterceptor());
return mybatisPlusInterceptor;
}
当执行update操作时,必须收集version
@Test
void testUpdate(){
User user=new User();
user.setVersion(1);
user.setId(1L);
user.setName("Tom666");
userDao.updateById(user);
}
也可以先查询
@Test
void testUpdate(){
//先查询,user里面就携带了version
User user = userDao.selectById(3L);
user.setId(1L);
user.setName("Tom666");
userDao.updateById(user);
}
每执行一次更新操作,version就会自动加1