<dependency>
<groupId>tk.mybatisgroupId>
<artifactId>mapper-spring-boot-starterartifactId>
<version>2.0.4version>
dependency>
Example example = createExample(searchMap);
selectByExample(example);
/**
* 构建查询对象
*
* @param searchMap
* @return
*/
private Example createExample(Map<String, Object> searchMap) {
Example example = new Example(Sku.class);
Example.Criteria criteria = example.createCriteria();
if (searchMap != null) {
// 商品id
if (searchMap.get("id") != null && !"".equals(searchMap.get("id"))) {
criteria.andEqualTo("id", searchMap.get("id"));
}
// 商品条码
if (searchMap.get("sn") != null && !"".equals(searchMap.get("sn"))) {
criteria.andEqualTo("sn", searchMap.get("sn"));
}
// SKU名称
if (searchMap.get("name") != null && !"".equals(searchMap.get("name"))) {
criteria.andLike("name", "%" + searchMap.get("name") + "%");
}
// 商品图片
if (searchMap.get("image") != null && !"".equals(searchMap.get("image"))) {
criteria.andLike("image", "%" + searchMap.get("image") + "%");
}
// 商品图片列表
if (searchMap.get("images") != null && !"".equals(searchMap.get("images"))) {
criteria.andLike("images", "%" + searchMap.get("images") + "%");
}
// SPUID
if (searchMap.get("spuId") != null && !"".equals(searchMap.get("spuId"))) {
criteria.andEqualTo("spuId", searchMap.get("spuId"));
}
// 类目名称
if (searchMap.get("categoryName") != null && !"".equals(searchMap.get("categoryName"))) {
criteria.andLike("categoryName", "%" + searchMap.get("categoryName") + "%");
}
// 品牌名称
if (searchMap.get("brandName") != null && !"".equals(searchMap.get("brandName"))) {
criteria.andLike("brandName", "%" + searchMap.get("brandName") + "%");
}
// 规格
if (searchMap.get("spec") != null && !"".equals(searchMap.get("spec"))) {
criteria.andLike("spec", "%" + searchMap.get("spec") + "%");
}
// 商品状态 1-正常,2-下架,3-删除
if (searchMap.get("status") != null && !"".equals(searchMap.get("status"))) {
criteria.andEqualTo("status", searchMap.get("status"));
}
// 价格(分)
if (searchMap.get("price") != null) {
criteria.andEqualTo("price", searchMap.get("price"));
}
// 库存数量
if (searchMap.get("num") != null) {
criteria.andEqualTo("num", searchMap.get("num"));
}
// 库存预警数量
if (searchMap.get("alertNum") != null) {
criteria.andEqualTo("alertNum", searchMap.get("alertNum"));
}
// 重量(克)
if (searchMap.get("weight") != null) {
criteria.andEqualTo("weight", searchMap.get("weight"));
}
// 类目ID
if (searchMap.get("categoryId") != null) {
criteria.andEqualTo("categoryId", searchMap.get("categoryId"));
}
// 销量
if (searchMap.get("saleNum") != null) {
criteria.andEqualTo("saleNum", searchMap.get("saleNum"));
}
// 评论数
if (searchMap.get("commentNum") != null) {
criteria.andEqualTo("commentNum", searchMap.get("commentNum"));
}
}
return example;
}
insertSelective–有选择性的保存数据
比如User里面有三个字段:id name age password
User u=new user();
u.setName("bill");
mapper.insertSelective(u);
insertSelective执行对应的sql语句的时候,只插入对应的name字段
sql语句如下:
insert into tb_user (name) value ("bill")
insert则是每个字段都要添加一遍
insert into tb_user (id,name,age,password) value(null,"bill",null,null);
在yml文件内配置pageHelper
#分页插件
pagehelper:
helper-dialect: mysql # mysql数据库
reasonable: true # 负数页码显示第一页 超出最大页码显示最后一页
support-methods-arguments: true
params: count=countSql
1、导入坐标
<dependency>
<groupId>com.github.pagehelpergroupId>
<artifactId>pagehelper-spring-boot-starterartifactId>
<version>1.2.3version>
dependency>
2、Controller层代码
@GetMapping(value = "/search/{page}/{size}")
@ApiOperation("分页")
public Result findPage(@PathVariable("page") int page, @PathVariable("size") int size){
Page<Brand> pageList = brandService.findPage(page, size);
PageResult pageResult = new PageResult(pageList.getTotal(), pageList.getResult());
return new Result(true,StatusCode.OK,"查询成功",pageResult);
}
3、服务实现类代码
/**
* 分页查询
*
* @param page
* @param size
* @return
*/
@Override
public Page<Brand> findPage(int page, int size) {
PageHelper.startPage(page, size);
return (Page<Brand>) brandMapper.selectAll();
}