store (mall project) Springboot+springmvc+ajax+mybatis

37. Provincial and urban area - acquisition list - controller layer

For requests to be processed:

Request path / districts/
Request parameter: String parent
 Request method: GET
 Response results: jsonresult < list < District > >
Block: No, you can access without login

Because there is no need to intercept, first add / district / * * to the white list in interceptor configuration class interceptor configurer class!

First, create the DistrictController class in the package cn.tedu.store.controller, inherit from BaseController, add @ RestController and @ requestmapping ("restrictions") annotations before the class declaration, and declare @ Autowired private IDistrictService districtService in the class; business interface:

/**
 * Controller class to process provincial / city data related requests
 */
@RestController
@RequestMapping("districts")
public class DistrictController extends BaseController {

    @Autowired
    private IDistrictService districtService;

}

Then, add a method to process the request in the class:

// http://localhost:8080/districts/?parent=86
@GetMapping({"", "/"})
public JsonResult<List<District>> getByParent(String parent) {
    // Call business object to get data
    // Return OK and data
}

38. Provincial and urban area - get list - front page

39. Provincial and urban area - get name according to administrative code - persistent layer

The SQL statements to be executed are as follows:

select name from t_dict_district where code=?

Add abstract methods in the DistrictMapper interface:

/**
 * Inquire the name according to the administrative code of the province / city / district
 * @param code Administrative code of province / city / district
 * @return The name of the matching province / city. If there is no matching data, null will be returned
 */
String findNameByCode(String code);

To configure a map in DistrictMapper.xml:

<!-- According to province/city/Administrative code name of district -->
<!-- String findNameByCode(String code) -->
<select id="findNameByCode"
    resultType="java.lang.String">
    SELECT
        name
    FROM
        t_dict_district
    WHERE
        code=#{code}
</select>

Test in DistrictMapperTests:

@Test
public void findNameByCode() {
    String code = "110000";
    String name = mapper.findNameByCode(code);
    System.err.println("name=" + name);
}

40. Provincial and urban area - get name according to administrative code - business layer

Add in the IDistrictService interface:

/**
 * Inquire the name according to the administrative code of the province / city / district
 * @param code Administrative code of province / city / district
 * @return The name of the matching province / city. If there is no matching data, null will be returned
 */
String getNameByCode(String code);

Implement the above methods in the DistrictServiceImpl class:

@Override
public String getNameByCode(String code) {
    return districtMapper.findNameByCode(code);
}

Test in DistrictServiceTests:

@Test
public void getNameByCode() {
    String code = "110000";
    String name = service.getNameByCode(code);
    System.err.println("name=" + name);
}

41. Receiving address - add - Business - Supplement

In the business of adding the receiving address, you need to complete "obtain the corresponding name according to the known administrative code"!

First, add a business object in AddressServiceImpl to process the data of provincial and urban areas:

@Autowired
private IDistrictService districtService;

Before inserting the receiving address data, supplement the data:

String provinceCode = address.getProvinceCode();
String provinceName = districtService.getNameByCode(provinceCode);
address.setProvinceName(provinceName);
String cityCode = address.getCityCode();
String cityName = districtService.getNameByCode(cityCode);
address.setCityName(cityName);
String areaCode = address.getAreaCode();
String areaName = districtService.getNameByCode(areaCode);
address.setAreaName(areaName);

42. Receiving address - display list - persistence layer

Display the receiving address list of a user. The SQL statements to be executed are as follows:

select * from t_address where uid=? order by is_default desc, modified_time desc

Job: refer to the function of "get all provinces in China" and develop it to the controller layer. Through http://localhost:8080/addresses, you can get the receiving address list data of the currently logged in user!

157 original articles published, 22 praised, 160000 visitors+
Private letter follow

Tags: SQL xml Java

Posted on Wed, 15 Jan 2020 05:01:07 -0800 by PRodgers4284