1. Additions, deletions, and revisions of Mapper

Based on the changgou project

注意

insert和insertSelective区别
两者的区别在于如果选择insert 那么所有的字段都会添加一遍即使没有值
但是如果使用inserSelective就会只给有值的字段赋值(会对传进来的值做非空判断)
其他后面有Selective字眼的都是这样

Brand table corresponds to Brand entity class

@Table(name="tb_brand")
public class Brand implements Serializable{
    
    
	@Id
	private Integer id;//品牌id
	private String name;//品牌名称
	private String image;//品牌图片地址
	private String letter;//品牌的首字母
	private Integer seq;//排序
	
	// getter and setter  .....(省略)
}

@Table and @Id are both JPA annotations, @Table is used to configure the mapping relationship between the table and the entity class, and @Id is used to identify the primary key attribute.

1 Enquiry All-Brand List

brandMapper.selectAll();

Generally not used, general queries will use paging

(1) Created by Dao

Create the com.changgou.goods.dao.BrandMapper interface under the changgou-service-goods microservice, the code is as follows:

public interface BrandMapper extends Mapper<Brand> {
    
    
}

By inheriting the Mapper interface, the common methods of adding, deleting, modifying and checking are automatically implemented.

(2) Business layer

Create the com.changgou.goods.service.BrandService interface, the code is as follows:

public interface BrandService {
    
    

    /***
     * 查询所有品牌
     * @return
     */
    List<Brand> findAll();
}

Create a com.changgou.goods.service.impl.BrandServiceImpl implementation class, the code is as follows:

@Service
public class BrandServiceImpl {
    
    

    @Autowired
    private BrandMapper brandMapper;

    /**
     * 全部数据
     * @return
     */
    public List<Brand> findAll(){
    
    
        return brandMapper.selectAll();
    }
}

(3) Control layer

Create a controller package under the control layer com.changgou.goods package, and create a class under the package

@RestController
@RequestMapping("/brand")
public class BrandController {
    
    

    @Autowired
    private BrandService brandService;

    /***
     * 查询全部数据
     * @return
     */
    @GetMapping
    public Result<Brand> findAll(){
    
    
        List<Brand> brandList = brandService.findAll();
        return new Result<Brand>(true, StatusCode.OK,"查询成功",brandList) ;
    }
}

2 Query brand based on ID

2.1brandMapper.selectByPrimaryKey(id);

(1) Business layer

Modify the com.changgou.goods.service.BrandService interface and add a method to query brand data based on ID. The code is as follows:

/**
 * 根据ID查询
 * @param id
 * @return
 */
Brand findById(Integer id);

Modify the new method of com.changgou.goods.service.impl.BrandServiceImpl, the code is as follows:

/**
 * 根据ID查询
 * @param id
 * @return
 */
@Override
public Brand findById(Integer id){
    
    
    return  brandMapper.selectByPrimaryKey(id);
}

(2) Control layer

BrandController new method

/***
 * 根据ID查询品牌数据
 * @param id
 * @return
 */
@GetMapping("/{id}")
public Result<Brand> findById(@PathVariable Integer id){
    
    
    //根据ID查询
    Brand brand = brandService.findById(id);
    return new Result<Brand>(true,StatusCode.OK,"查询成功",brand);
}

2.2 categoryMapper.select(category);

(1)Service layer

Modify and com.changgou.goods.service.CategoryServiceadd to query all child nodes based on the parent class ID, the code is as follows:

/***
 * 根据分类的父ID查询子分类节点集合
 */
List<Category> findByParentId(Integer pid);

Modify and com.changgou.goods.service.impl.CategoryServiceImpladd the above implementation, the code is as follows:

/***
 * 根据分类的父节点ID查询所有子节点
 * @param pid
 * @return
 */
@Override
public List<Category> findByParentId(Integer pid) {
    
    
    //SELECT * FROM tb_category WHERE parent_id=?
    Category category = new Category();
    category.setParentId(pid);
    return categoryMapper.select(category);
}

(2) Controller layer

Modify and com.changgou.goods.controller.CategoryControlleradd to query all subclass collections based on the parent ID, the code is as follows:

/****
 * 根据节点ID查询所有子节点分类集合
 */
@GetMapping(value = "/list/{pid}")
public Result<List<Category>> findByParentId(@PathVariable(value = "pid")Integer pid){
    
    
    //调用Service实现查询
    List<Category> categories = categoryService.findByParentId(pid);
    return new Result<List<Category>>(true,StatusCode.OK,"查询成功!",categories);
}

3 New brand

brandMapper.insert(brand);

(1) Business layer

Modify com.changgou.goods.service.BrandService, add a new method

/***
 * 新增品牌
 * @param brand
 */
void add(Brand brand);

Modify com.changgou.goods.service.impl.BrandServiceImpl and add the brand method code as follows:

/**
 * 增加
 * @param brand
 */
@Override
public void add(Brand brand){
    
    
    brandMapper.insert(brand);
}

(2) Control layer

BrandController new method

/***
 * 新增品牌数据
 * @param brand
 * @return
 */
@PostMapping
public Result add(@RequestBody Brand brand){
    
    
    brandService.add(brand);
    return new Result(true,StatusCode.OK,"添加成功");
}

4 Modify the brand

brandMapper.updateByPrimaryKey(brand);

(1) Business layer

Need to change com.changgou.goods.service.BrandService, add the method to modify the brand, the code is as follows:

/***
 * 修改品牌数据
 * @param brand
 */
void update(Brand brand);

Modify com.changgou.goods.service.impl.BrandServiceImpl, add a method to modify the brand, the code is as follows:

/**
 * 修改
 * @param brand
 */
@Override
public void update(Brand brand){
    
    
    brandMapper.updateByPrimaryKey(brand);
}

(2) Control layer

BrandController new method

/***
 * 修改品牌数据
 * @param brand
 * @param id
 * @return
 */
@PutMapping(value="/{id}")
public Result update(@RequestBody Brand brand,@PathVariable Integer id){
    
    
    //设置ID
    brand.setId(id);
    //修改数据
    brandService.update(brand);
    return new Result(true,StatusCode.OK,"修改成功");
}

测试:http://localhost:18081/brand/325415

[External link image transfer failed. The source site may have an anti-leech link mechanism. It is recommended to save the image and upload it directly (img-qWeBA40C-1596863779007)(C:/Users/MT/Desktop/%E7%95%85%E8%B4 %AD/%E6%96%87%E6%A1%A3%E9%9B%86%E5%90%88/image/1560444209790.png)]

5 Remove brand

brandMapper.deleteByPrimaryKey(id);

(1) Business layer

Modify com.changgou.goods.service.BrandService, add and delete the brand method, the code is as follows:

/***
 * 删除品牌
 * @param id
 */
void delete(Integer id);

Modify com.changgou.goods.service.impl.BrandServiceImpl, add a method to delete the brand, the code is as follows:

/**
 * 删除
 * @param id
 */
@Override
public void delete(Integer id){
    
    
    brandMapper.deleteByPrimaryKey(id);
}

(2) Control layer

BrandController new method

/***
 * 根据ID删除品牌数据
 * @param id
 * @return
 */
@DeleteMapping(value = "/{id}" )
public Result delete(@PathVariable Integer id){
    
    
    brandService.delete(id);
    return new Result(true,StatusCode.OK,"删除成功");
}

测试:http://localhost:18081/brand/325415

[External link image transfer failed. The source site may have an anti-leech link mechanism. It is recommended to save the image and upload it directly (img-YWdzrWHk-1596863779011)(C:/Users/MT/Desktop/%E7%95%85%E8%B4 %AD/%E6%96%87%E6%A1%A3%E9%9B%86%E5%90%88/image/1560444351992.png)]

6 Brand list condition query

brandMapper.selectByExample(example);

(1) Business layer

Modify com.changgou.goods.service.BrandService, add a method to search for brands based on conditions, the code is as follows:

/***
 * 多条件搜索品牌方法
 * @param brand
 * @return
 */
List<Brand> findList(Brand brand);

Modify com.changgou.goods.service.impl.BrandServiceImpl and add the implementation of searching for brands based on multiple conditions. The code is as follows:

/**
 * 条件查询
 * @param brand
 * @return
 */
@Override
public List<Brand> findList(Brand brand){
    
    
    //构建查询条件
    Example example = createExample(brand);
    //根据构建的条件查询数据
    return brandMapper.selectByExample(example);
}


/**
 * 构建查询对象
 * @param brand
 * @return
 */
public Example createExample(Brand brand){
    
    
    Example example=new Example(Brand.class);
    Example.Criteria criteria = example.createCriteria();
    if(brand!=null){
    
    
        // 品牌名称
        if(!StringUtils.isEmpty(brand.getName())){
    
    
            criteria.andLike("name","%"+brand.getName()+"%");
        }
        // 品牌图片地址
        if(!StringUtils.isEmpty(brand.getImage())){
    
    
            criteria.andLike("image","%"+brand.getImage()+"%");
        }
        // 品牌的首字母
        if(!StringUtils.isEmpty(brand.getLetter())){
    
    
            criteria.andLike("letter","%"+brand.getLetter()+"%");
        }
        // 品牌id
        if(!StringUtils.isEmpty(brand.getLetter())){
    
    
            criteria.andEqualTo("id",brand.getId());
        }
        // 排序
        if(!StringUtils.isEmpty(brand.getSeq())){
    
    
            criteria.andEqualTo("seq",brand.getSeq());
        }
    }
    return example;
}

(2) Control layer

BrandController new method

/***
 * 多条件搜索品牌数据
 * @param brand
 * @return
 */
@PostMapping(value = "/search" )
public Result<List<Brand>> findList(@RequestBody(required = false) Brand brand){
    
    
    List<Brand> list = brandService.findList(brand);
    return new Result<List<Brand>>(true,StatusCode.OK,"查询成功",list);
}

测试:http://localhost:18081/brand/search

[External link image transfer failed. The source site may have an anti-leech link mechanism. It is recommended to save the image and upload it directly (img-zGjjBVrR-1596863779013)(C:/Users/MT/Desktop/%E7%95%85%E8%B4 %AD/%E6%96%87%E6%A1%A3%E9%9B%86%E5%90%88/image/1560445027032.png)]

7 Paging query of brand list

PageHelper.startPage(page,size);

new PageInfo<Brand>(brandMapper.selectAll())

(1) Business layer

Modify com.changgou.goods.service.BrandService to add a paging method, the code is as follows:

/***
 * 分页查询
 * @param page
 * @param size
 * @return
 */
PageInfo<Brand> findPage(int page, int size);

Modify com.changgou.goods.service.impl.BrandServiceImpl to add paging method implementation, the code is as follows:

/**
 * 分页查询
 * @param page
 * @param size
 * @return
 */
@Override
public PageInfo<Brand> findPage(int page, int size){
    
    
    //静态分页
    PageHelper.startPage(page,size);
    //分页查询
    return new PageInfo<Brand>(brandMapper.selectAll());
}

(2) Control layer

BrandController new method

/***
 * 分页搜索实现
 * @param page:当前页
 * @param size:每页显示多少条
 * @return
 */
@GetMapping(value = "/search/{page}/{size}" )
public Result<PageInfo> findPage(@PathVariable  int page, @PathVariable  int size){
    
    
    //分页查询
    PageInfo<Brand> pageInfo = brandService.findPage(page, size);
    return new Result<PageInfo>(true,StatusCode.OK,"查询成功",pageInfo);
}

8 Brand list conditions + pagination query

PageHelper.startPage(page,size);

Example example = createExample(brand);

PageInfo<Brand>(brandMapper.selectByExample(example));

(1) Business layer

Modify com.changgou.goods.service.BrandService, add a multi-condition paging query method, the code is as follows:

/***
 * 多条件分页查询
 * @param brand
 * @param page
 * @param size
 * @return
 */
PageInfo<Brand> findPage(Brand brand, int page, int size);

Modify com.changgou.goods.service.impl.BrandServiceImpl and add the multi-condition paging query method code as follows:

/**
 * 条件+分页查询
 * @param brand 查询条件
 * @param page 页码
 * @param size 页大小
 * @return 分页结果
 */
@Override
public PageInfo<Brand> findPage(Brand brand, int page, int size){
    
    
    //分页
    PageHelper.startPage(page,size);
    //搜索条件构建
    Example example = createExample(brand);
    //执行搜索
    return new PageInfo<Brand>(brandMapper.selectByExample(example));
}

(2) Control layer

BrandController new method

/***
 * 分页搜索实现
 * @param brand
 * @param page
 * @param size
 * @return
 */
@PostMapping(value = "/search/{page}/{size}" )
public Result<PageInfo> findPage(@RequestBody(required = false) Brand brand, @PathVariable  int page, @PathVariable  int size){
    
    
    //执行搜索
    PageInfo<Brand> pageInfo = brandService.findPage(brand, page, size);
    return new Result(true,StatusCode.OK,"查询成功",pageInfo);
}

ze);
//Search criteria construction
Example example = createExample(brand);
//Perform search
return new PageInfo(brandMapper.selectByExample(example));
}




(2)控制层

BrandController新增方法

```java
/***
 * 分页搜索实现
 * @param brand
 * @param page
 * @param size
 * @return
 */
@PostMapping(value = "/search/{page}/{size}" )
public Result<PageInfo> findPage(@RequestBody(required = false) Brand brand, @PathVariable  int page, @PathVariable  int size){
    //执行搜索
    PageInfo<Brand> pageInfo = brandService.findPage(brand, page, size);
    return new Result(true,StatusCode.OK,"查询成功",pageInfo);
}

Guess you like

Origin blog.csdn.net/qq_45850872/article/details/107878444