mybatis-plus依赖
<dependency>
<groupId>com.baomidougroupId>
<artifactId>mybatis-plus-boot-starterartifactId>
<version>3.1.1version>
dependency>
主要是在于创建Mapper类上
mybatis-plus自增加不修改,也就是说自需要继承BaseMapper>这个类,就能够调用一下增强后的单表操作的方法
@Mapper
public interface employeeMapper extends BaseMapper<employee> {
......
}
然后在SpringBoot的主函数上添加@MapperScan(“com.XXX.mapper”)
@MapperScan("com.XXX.mapper")
@SpringBootApplication
public class SpringbootCacheApplication {
public static void main(String[] args) {
SpringApplication.run(SpringbootCacheApplication.class, args);
}
}
/**
* selectList()
* 无条件查询所有
* */
List<employee> employees = employeeMapper.selectList(null);
for(employee employee: employees){
System.out.println(employee.toString());
}
/**
* insert()
* 通过实体对象插入一条数据,
* 插入后数据库中设置自增后的id会回填到getID()方法中
* **/
employee employee = new employee();
employee.setLastName("IE");
employee.setEmail("[email protected]");
employee.setGender("男");
employee.setDid(2);
int insert = this.employeeMapper.insert(employee);//影响表的行数
System.out.println("成功插入:"+insert+"条");
System.out.println(employee.getId());
/**
* selectById()
* 通过id查询某条数据
* **/
employee employee = employeeMapper.selectById(1);
System.out.println(employee.toString());
/**
* updateById()
* 通过id修改某条信息
* **/
employee employee = new employee();
employee.setId(2);
employee.setLastName("CJA");
int i = employeeMapper.updateById(employee);
System.out.println("成功插入" + i + "条");
/**
* update(employee, wrapper)
* 通过查询条件修改数据
* 使用QueryWrapper对象中eq方法进行数据条件设置进行更新信息
* **/
employee employee = new employee();
employee.setLastName("CJA");
employee.setEmail("[email protected]");
QueryWrapper<employee> wrapper = new QueryWrapper<>();
wrapper.eq("did",2);
int i = employeeMapper.update(employee, wrapper);
System.out.println("成功修改" + i + "条");
/**
* 通过查询条件修改数据
* 使用UpdateWrapper对象中set方法进行修改值,eq方法进行条件设置
* **/
UpdateWrapper<employee> wrapper = new UpdateWrapper<>();
wrapper.set("last_Name","lisi").set("gender","女").eq("did",1);
int i = employeeMapper.update(null, wrapper);
System.out.println("成功修改" + i + "条");
/**
* deleteById()
* 通过id为条件,删除数据
* **/
int i = employeeMapper.deleteById(5);
System.out.println("成功删除" + i + "条");
/**
* deleteByMap()
* 通过map对象作为条件,删除数据
* **/
Map<String,Object> map = new HashMap<>();
map.put("last_Name","JJJ");
map.put("email","[email protected]");
int i = employeeMapper.deleteByMap(map);
System.out.println("成功删除" + i + "条");
/**
* delete()
* 用法一、通过QueryWrapper对象添加删除条件,删除数据
* 用法二、通过给QuerWrapper()传设置好删除条件的对象删除数据
* **/
//用法一
QueryWrapper<employee> wrapper = new QueryWrapper<>();
wrapper.eq("last_Name","lisi").eq("gender","女");
int i = employeeMapper.delete(wrapper);
System.out.println("成功删除" + i + "条");
//用法二
employee employee = new employee();
employee.setLastName("1");
employee.setEmail("1");
employee.setGender("1");
QueryWrapper<employee> wrapper = new QueryWrapper<>(employee);
int i = employeeMapper.delete(wrapper);
System.out.println("成功删除" + i + "条");
/**deleteBatchIds(Arrays.asList())
* 通过数组的名字批量删除
* **/
int i = employeeMapper.deleteBatchIds(Arrays.asList(8, 9, 10));
System.out.println("成功删除" + i + "条");
/**selecBatchIds(Arrays.asList())
* 通过数组的名字批量查询
* **/
List<employee> i = employeeMapper.selectBatchIds(Arrays.asList(2,3,4));
for (employee employee : i) {
System.out.println( employee.toString() );
}
/**selectOne()
* 通过条件查询,只能查询一条唯一的数据
* **/
QueryWrapper<employee> wrapper = new QueryWrapper<>();
wrapper.eq("last_Name","王五");
employee employee = this.employeeMapper.selectOne(wrapper);
System.out.println(employee.toString());
/**selectCount()
* 通过条件查询,相同条件有多少条
* **/
QueryWrapper<employee> wrapper = new QueryWrapper<>();
wrapper.gt("did",1);
Integer employee = this.employeeMapper.selectCount(wrapper);
System.out.println("同样条件的查询到"+employee+"条");
/**selectList()
* 通过条件查询,返回多条数据
* like()模糊查询
* gt()大于/ge()大于
* lt()小于
* **/
QueryWrapper<employee> wrapper = new QueryWrapper<>();
wrapper.like("email","@qq.com")
.gt("did",1)
.lt("did",3);
List<employee> employees = this.employeeMapper.selectList(wrapper);
for (employee employees1: employees){
System.out.println(employees1.toString());
}
还有一个比较特殊的是分页查询,需要创建一个Configuration组件类
@Configuration
public class MybatisPlusConfig {
@Bean
public PaginationInterceptor paginationInterceptor(){
return new PaginationInterceptor();
}
}
/**
* selectPage()
* 分页查询
* **/
Page<employee> page =new Page<>(2,2);
QueryWrapper<employee> wrapper = new QueryWrapper<>();
// wrapper.like("email","@qq.com")
//.gt("did",2)
//.lt("did",3);
IPage<employee> IPage = this.employeeMapper.selectPage(page, wrapper);
System.out.println("总页数:"+IPage.getPages());
System.out.println("总条数:"+IPage.getTotal());
System.out.println("当前页数:"+IPage.getCurrent());
List<employee> records = IPage.getRecords();
for (employee employee:records){
System.out.println(employee.toString());
}
/**
* allEq()
* 通过map传参进行select查询筛选
* **/
Map<String,Object> map = new HashMap<>();
map.put("last_Name","俊安");
map.put("did","2");
map.put("email","");
QueryWrapper<employee> wrapper = new QueryWrapper<>();
//用法一、
wrapper.allEq(map);//查询所有条件
//用法二、
wrapper.allEq(map,false);//false代表着不把map中内容为空的字段作为条件查询
//用法三、
wrapper.allEq((k,v) ->(k.equals("last_Name")) ,map);//过滤掉map中,字段不为last_Name的条件,只剩equals中的字段作为查询条件
List<employee> list = this.employeeMapper.selectList(wrapper);
for (employee employee : list) {
System.out.println(employee.toString());
}
基本比较操作
符号 | 含义 |
---|---|
eq | = |
ne | != |
gt | > |
ge | >= |
lt | < |
lr | =< |
between | 之间 |
notBetween | 不在之间 |
in | 范围内 |
notIn | 不在范围内 |
模糊查询
符合 | 含义 |
---|---|
like | like"%值%" |
notLike | Not like “%值%” |
likeLeft | like “%值” |
likeRight | like “值%” |
排序查询
符合 | 含义 |
---|---|
orderBy | 正序 |
orderByAsc | 正序 |
orderByDesc | 倒序 |