查询
如何快速使用MyBatisPlus查询语句
在完成MyBatisPlus项目创建完成后,调用对应的mapper类
@Autowired
private UserMapper mapper;
调用其中方法。如果是查询语句就使用select方法大致操作如数据库操作一样你也可以点击源码看看有哪些方法。
//这里我使用的selectList来测试
@Test
void Select(){
//可以传null也可以传Wrapper
mapper.selectList()
}
点击这个方法里面你就会看到如下图的方法和属性。我们需要传入一个wrapper的属性
点击去那个属性就会看到它是一个接口我们不能直接new出来我们就需要创建它的实现类。
创建好Warpper我们就可以在提示下完成你想要完成的操作这里我就使用比较这个方法来进行测试。如果你还是感觉比较难那么你就按着我的使用的测试进入接口看里面的注解差不多就明白了或者直接看api。官网上应该有的
@Test
void Select(){
//可以传null也可以传Wrapper
QueryWrapper Wrapper = new QueryWrapper();
Wrapper.eq( "username", "baozi");
mapper.selectList(Wrapper);
}
/**
* 不等于 <>
*
* @param condition 执行条件
* @param column 字段
* @param val 值
* @return children
*/
Children ne(boolean condition, R column, Object val);
/**
* ignore
*/
default Children gt(R column, Object val) {
return gt(true, column, val);
}
/**
* 大于 >
*
* @param condition 执行条件
* @param column 字段
* @param val 值
* @return children
*/
Children gt(boolean condition, R column, Object val);
在测试一个方法
@Test
void Select(){
//可以传null也可以传Wrapper
QueryWrapper Wrapper = new QueryWrapper();
// Wrapper.eq( "username", "bao");
//注意我这里是密码字段但是有数字的密码也可以比大小实战就不要玩了这里我就举个例子
Wrapper.gt("password", 1);
mapper.selectList(Wrapper);
}
}
分页查询
1.在使用方法前我们必须设置配置
package com.bdh.myatispulstest.config;
import com.baomidou.mybatisplus.extension.plugins.OptimisticLockerInterceptor;
import com.baomidou.mybatisplus.extension.plugins.PaginationInterceptor;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class MyBatisPlusConfig {
@Bean
//version乐观锁
public OptimisticLockerInterceptor optimisticLockerInterceptor(){
return new OptimisticLockerInterceptor();
}
@Bean
//分页配置
public PaginationInterceptor paginationInterceptor(){
return new PaginationInterceptor();
}
}
2.完成方法的属性进行测试
@Test
void Select(){
//可以传null也可以传Wrapper
QueryWrapper Wrapper = new QueryWrapper();
// Wrapper.eq( "username", "bao");
//注意我这里是密码字段但是有数字的密码也可以比大小实战就不要玩了这里我就举个例子
// Wrapper.gt("password", 1);
// mapper.selectList(Wrapper);
//分页查询
Page<TageName> page = new Page<>(1,2);
Page<TageName> tageNamePage = mapper.selectPage(page, null);
System.out.println(tageNamePage.getSize());
System.out.println(tageNamePage.getTotal());
tageNamePage.getRecords().forEach(System.out::println);
}
//分页操作第二个方法(封装到map集合)
Page< Map<String,Object> >page = new Page<>(1,2);
Page<Map<String, Object>> mapPage = mapper.selectMapsPage(page, null);
mapPage.getRecords().forEach(System.out::println);
注意selectOne只能查询一条语句
添加
具体的方法都可以按着查询的步骤查看
@Test
void Save(){
TageName tageName=new TageName();
tageName.setName("baozi");
tageName.setPassword("123");
mapper.insert(tageName);
System.out.println(tageName);
}
删除
同上
@Test
void Delete(){
mapper.deleteById(20);
}
修改
同上
@Test
void Update(){
TageName tageName = mapper.selectById(1);
QueryWrapper queryWrapper = new QueryWrapper();
queryWrapper.eq("username", "bao");
mapper.update(tageName,queryWrapper);
}
自定义SQL,多表关联
1.因为操作比较复杂,mybatis-puls不支持直接操作。所以我们需要自己创建你所想要的方法。如下就是我创建的例子:
SELECT e.*,n.title,n.content FROM edit e,news_content n where e.content_id=n.id and n.id=#{id}
这里你随便设置个多表就行了,不过一定需要在mysql下测试成功是否。
2.完成后创建对应的VO。(可能有人说你为什么不实现xxx表,绕后在调用其xxx完成xxx这种操作本身就是mybatis的操作而我们学习的plus所以这种方式并不适合我们,当然也可以完成我们这个操作就是了)
package com.bdh.myatispulstest.entity;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
@Data
@AllArgsConstructor
@NoArgsConstructor
//对应你的表
public class EditVO {
private String editer;
private String edit;
private String title;
private String content;
private int content_id;
private int id;
}
3.测试类
@Test
void Edit(){
mapper.editList(1).forEach(System.out::println);
}
ing edit;
private String title;
private String content;
private int content_id;
private int id;
}
3.测试类
```java
@Test
void Edit(){
mapper.editList(1).forEach(System.out::println);
}