2021-7-31 commodity micro Service - brand addition, deletion, modification and query

4.1 demand analysis
Create a commodity micro service to realize the function of adding, deleting, modifying and querying the brand table. Specifically include
(1) Query all list data
(2) Query entity data by ID
(3) Increase
(4) Modification
(5) Delete
(6) Condition query
(7) Paging query
(8) Paging + conditional query
4.3 code implementation
The above Brand table corresponds to the Brand entity class

@Table(name="tb_brand")
public class Brand implements Serializable{
	@Id
	private Integer id;//Brand id
	private String name;//Brand name
	private String image;//Brand picture address
	private String letter;//Brand initials
	private Integer seq;//sort
	// getter and setter  ..... (omitted)
}

@Both Table and @ Id are JPA annotations, @ Table is used to configure the mapping relationship between Table and entity class, and @ Id is used to identify the primary key attribute.
4.3.1 brand list
(1)Dao create
Create com.com under the Changgou service goods micro service changgou. goods. dao. Brandmapper interface, code as follows:

public interface BrandMapper extends Mapper<Brand> {
}

Inheriting Mapper interface, the common methods of adding, deleting, modifying and querying are automatically realized.
(2) Business layer
Create com changgou. goods. service. Brandservice interface, code as follows:

public interface BrandService {
    /***
     * Query all brands
     * @return
     */
    List<Brand> findAll();
}

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

@Service
public class BrandServiceImpl {
    @Autowired
    private BrandMapper brandMapper;
    /**
     * All data
     * @return
     */
    public List<Brand> findAll(){
        return brandMapper.selectAll();
    }
}

(3) Control layer
Control layer com changgou. Create the controller package under the goods package, and create classes under the package

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

    @Autowired
    private BrandService brandService;

    /***
     * Query all data
     * @return
     */
    @GetMapping
    public Result<Brand> findAll(){
        List<Brand> brandList = brandService.findAll();
        return new Result<Brand>(true, StatusCode.OK,"query was successful",brandList) ;
    }
}
Test: http://localhost:18081/brand

4.3.2 query brand by ID
(1) Business layer
Modify com changgou. goods. service. Brandservice interface, add the method of querying brand data according to ID, and the code is as follows:

/**
 * Query by ID
 * @param id
 * @return
 */
Brand findById(Integer id);

Modify com changgou. goods. service. impl. Brandserviceimpl adds a new method with the following code:

/**
 * Query by ID
 * @param id
 * @return
 */
@Override
public Brand findById(Integer id){
    return  brandMapper.selectByPrimaryKey(id);
}

(2) Control layer
BrandController new method

/***
 * Query brand data according to ID
 * @param id
 * @return
 */
@GetMapping("/{id}")
public Result<Brand> findById(@PathVariable Integer id){
    //Query by ID
    Brand brand = brandService.findById(id);
    return new Result<Brand>(true,StatusCode.OK,"query was successful",brand);
}
Test: http://localhost:18081/brand/14026

4.3.3 new brands
(1) Business layer
Modify com changgou. goods. service. Brandservice, new method

/***
 * New brand
 * @param brand
 */
void add(Brand brand);

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

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

(2) Control layer
BrandController new method

/***
 * New brand data
 * @param brand
 * @return
 */
@PostMapping
public Result add(@RequestBody Brand brand){
    brandService.add(brand);
    return new Result(true,StatusCode.OK,"Added successfully");
}
Test: http://localhost:18081/brand

4.3.4 brand modification
(1) Business layer
Need to change com changgou. goods. service. Brandservice, add and modify the brand method, and the code is as follows:

/***
 * Modify brand data
 * @param brand
 */
void update(Brand brand);

Modify com changgou. goods. service. impl. Brandserviceimpl, add and modify the brand method, and the code is as follows:

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

(2) Control layer
BrandController new method

/***
 * Modify brand data
 * @param brand
 * @param id
 * @return
 */
@PutMapping(value="/{id}")
public Result update(@RequestBody Brand brand,@PathVariable Integer id){
    //Set ID
    brand.setId(id);
    //Modify data
    brandService.update(brand);
    return new Result(true,StatusCode.OK,"Modified successfully");
}
Test: http://localhost:18081/brand/325415

4.3.5 delete brand
(1) Business layer

Modify com changgou. goods. service. Brandservice, the method of adding and deleting brands, the code is as follows:

/***
 * Delete brand
 * @param id
 */
void delete(Integer id);

Modify com changgou. goods. service. impl. Brandserviceimpl, the method of adding and deleting a brand, and the code is as follows:

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

(2) Control layer

BrandController new method

/***
 * Delete brand data according to ID
 * @param id
 * @return
 */
@DeleteMapping(value = "/{id}" )
public Result delete(@PathVariable Integer id){
    brandService.delete(id);
    return new Result(true,StatusCode.OK,"Delete succeeded");
}
Test: http://localhost:18081/brand/325415

Query criteria list
(1) Business layer

Modify com changgou. goods. service. Brandservice, add the method of searching brand according to criteria, and the code is as follows:

/***
 * Multi condition search brand method
 * @param brand
 * @return
 */
List<Brand> findList(Brand brand);

Modify com changgou. goods. service. impl. Brandserviceimpl, add the implementation of the brand search method according to multiple conditions, and the code is as follows:

/**
 * Condition query
 * @param brand
 * @return
 */
@Override
public List<Brand> findList(Brand brand){
    //Build query criteria
    Example example = createExample(brand);
    //Query data according to the constructed conditions
    return brandMapper.selectByExample(example);
}


/**
 * Building query objects
 * @param brand
 * @return
 */
public Example createExample(Brand brand){
    Example example=new Example(Brand.class);
    Example.Criteria criteria = example.createCriteria();
    if(brand!=null){
        // Brand name
        if(!StringUtils.isEmpty(brand.getName())){
            criteria.andLike("name","%"+brand.getName()+"%");
        }
        // Brand picture address
        if(!StringUtils.isEmpty(brand.getImage())){
            criteria.andLike("image","%"+brand.getImage()+"%");
        }
        // Brand initials
        if(!StringUtils.isEmpty(brand.getLetter())){
            criteria.andLike("letter","%"+brand.getLetter()+"%");
        }
        // Brand id
        if(!StringUtils.isEmpty(brand.getLetter())){
            criteria.andEqualTo("id",brand.getId());
        }
        // sort
        if(!StringUtils.isEmpty(brand.getSeq())){
            criteria.andEqualTo("seq",brand.getSeq());
        }
    }
    return example;
}

(2) Control layer

BrandController new method

/***
 * Multi criteria search for brand data
 * @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,"query was successful",list);
}
Test: http://localhost:18081/brand/search

4.3.7 paging query of brand list
(1) Business layer

Modify com changgou. goods. service. Brandservice adds a paging method. The code is as follows:

/***
 * Paging query
 * @param page
 * @param size
 * @return
 */
PageInfo<Brand> findPage(int page, int size);

Modify com changgou. goods. service. impl. Brandserviceimpl adds a paging method implementation. The code is as follows:

/**
 * Paging query
 * @param page
 * @param size
 * @return
 */
@Override
public PageInfo<Brand> findPage(int page, int size){
    //Static paging
    PageHelper.startPage(page,size);
    //Paging query
    return new PageInfo<Brand>(brandMapper.selectAll());

(2) Control layer

BrandController new method

/***
 * Paging search implementation
 * @param page:Current page
 * @param size:How many are displayed per page
 * @return
 */
@GetMapping(value = "/search/{page}/{size}" )
public Result<PageInfo> findPage(@PathVariable  int page, @PathVariable  int size){
    //Paging query
    PageInfo<Brand> pageInfo = brandService.findPage(page, size);
    return new Result<PageInfo>(true,StatusCode.OK,"query was successful",pageInfo);
}
Test: http://localhost:18081/brand/search/1/3

4.3.8 brand list condition + paging query
(1) Business layer

Modify com changgou. goods. service. Brandservice, add multi condition paging query method, and the code is as follows:

/***
 * Multi condition paging query
 * @param brand
 * @param page
 * @param size
 * @return
 */
PageInfo<Brand> findPage(Brand brand, int page, int size);

Modify com changgou. goods. service. impl. Brandserviceimpl, add multi condition paging query method code as follows:

/**
 * Condition + paging query
 * @param brand query criteria
 * @param page Page number
 * @param size Page size
 * @return Paging results
 */
@Override
public PageInfo<Brand> findPage(Brand brand, int page, int size){
    //paging
    PageHelper.startPage(page,size);
    //Search criteria construction
    Example example = createExample(brand);
    //Perform search
    return new PageInfo<Brand>(brandMapper.selectByExample(example));
}

(2) Control layer

BrandController new method

/***
 * Paging search implementation
 * @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){
    //Perform search
    PageInfo<Brand> pageInfo = brandService.findPage(brand, page, size);
    return new Result(true,StatusCode.OK,"query was successful",pageInfo);
}
Test: http://localhost:18081/brand/search/1/3

Added by tomm098 on Sun, 02 Jan 2022 20:20:35 +0200