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

Keywords: SQL Session less xml

44. Receiving address - display list - controller layer

(a) Handling newly created exceptions

nothing

(b) Requests to be processed for design

Request path / addresses/
Request parameter: HttpSession session
 Request method: GET
 Response result: jsonresult < list < address > >

(c) Process request

// http://localhost:8080/addresses
@GetMapping("")
public JsonResult<List<Address>> getByUid(HttpSession session) {
    Integer uid = getUidFromSession(session);
    List<Address> data = addressService.getByUid(uid);
    return new JsonResult<>(OK, data);
}

45. Receiving address - display list - front page

46. Receiving address - set default - persistent layer

(a) Plan the SQL statements that need to be executed

If you need to set a delivery address as the default, the SQL statement to be executed is roughly as follows:

update t_address set is_default=1, modified_user=?, modified_time=? where aid=?

In addition to setting this receipt address as default, you should also set the original default receipt address as non default! This operation should be performed before "set default" above. The SQL statements to be executed are as follows:

update t_address set is_default=0 where uid=?

In addition, before performing the update, you should also check whether the set default receiving address exists and whether the data ownership is correct! The SQL statements to be executed are as follows:

select * from t_address where aid=?

(b) Interface and abstract method

Add in AddressMapper:

/**
 * Set the specified delivery address as the default
 * @param aid id of receiving address
 * @param modifiedUser Modified by
 * @param modifiedTime Modification time
 * @return Rows affected
 */
Integer updateDefaultByAid(
        @Param("aid") Integer aid, 
        @Param("modifiedUser") String modifiedUser, 
        @Param("modifiedTime") Date modifiedTime);

/**
 * Set all receiving addresses of a user as non default
 * @param uid User id
 * @return Rows affected
 */
Integer updateNonDefaultByUid(Integer uid);

/**
 * Query the receiving address details according to the receiving address id
 * @param aid Receiving address id
 * @return Matched receiving address details. If there is no matching data, null will be returned
 */
Address findByAid(Integer aid);

(c) Configuration mapping

Configure the map in AddressMapper.xml:

<!-- Set the specified delivery address as the default -->
<!-- Integer updateDefaultByAid(
        @Param("aid") Integer aid, 
        @Param("modifiedUser") String modifiedUser, 
        @Param("modifiedTime") Date modifiedTime) -->
<update id="updateDefaultByAid">
    UPDATE
        t_address
    SET
        is_default=1,
        modified_user=#{modifiedUser},
        modified_time=#{modifiedTime}
    WHERE
        aid=#{aid}
</update>

<!-- Set all receiving addresses of a user as non default -->
<!-- Integer updateNonDefaultByUid(Integer uid) -->
<update id="updateNonDefaultByUid">
    UPDATE
        t_address
    SET
        is_default=0
    WHERE
        uid=#{uid}
</update>

<!-- According to receiving address id Query receiving address details -->
<!-- Address findByAid(Integer aid) -->
<select id="findByAid"
    resultMap="AddressEntityMap">
    SELECT
        *
    FROM
        t_address
    WHERE
        aid=#{aid}
</select>

Test in AddressMapperTests:

@Test
public void updateDefaultByAid() {
    Integer aid = 28;
    String modifiedUser = "Default administrator";
    Date modifiedTime = new Date();
    Integer rows = mapper.updateDefaultByAid(aid, modifiedUser, modifiedTime);
    System.err.println("rows=" + rows);
}

@Test
public void updateNonDefaultByUid() {
    Integer uid = 18;
    Integer rows = mapper.updateNonDefaultByUid(uid);
    System.err.println("rows=" + rows);
}

@Test
public void findByAid() {
    Integer aid = 26;
    Address result = mapper.findByAid(aid);
    System.err.println(result);
}

47. Receiving address - set default - business layer

(a) Planning exceptions

Before setting the default, check whether the data exists. If not, throw AddressNotFoundException;

It is also necessary to determine whether the data ownership is correct. If not, an AccessDeniedException will be thrown;

If the operation of updating data will be performed finally, UpdateException may be thrown;

You need to create AddressNotFoundException and AccessDeniedException!

(b) Business interface and abstract method

Add abstract methods to the IAddressService interface:

void setDefault(Integer aid, Integer uid, String username);

(c) Implementing abstract methods

Implemented in AddressServiceImpl:

@Transactional
public void setDefault(Integer aid, Integer uid, String username) {
    // Query the receiving address data according to the parameter aid
    // Judge whether the query result is null
    // Yes: AddressNotFoundException

    // Judge whether the uid in the query result is inconsistent with the uid in the parameter
    // Yes: AccessDeniedException

    // Set all the receiving addresses of this user as non default and get the return value
    // Judge whether the return value is less than 1
    // Yes: UpdateException

    // Set the specified delivery address as the default and get the return value
    // Judge whether the return value is not 1
    // Yes: UpdateException
}

Specific code:

@Override
@Transactional
public void setDefault(Integer aid, Integer uid, String username) {
    // Query the receiving address data according to the parameter aid
    Address result = addressMapper.findByAid(aid);
    // Judge whether the query result is null
    if (result == null) {
        // Yes: AddressNotFoundException
        throw new AddressNotFoundException(
            "Failed to set default receiving address! The data you are trying to access does not exist!");
    }

    // Judge whether the uid in the query result is inconsistent with the uid in the parameter
    if (!result.getUid().equals(uid)) {
        // Yes: AccessDeniedException
        throw new AccessDeniedException(
            "Failed to set default receiving address! Illegal access has been denied!");
    }

    // Set all the receiving addresses of this user as non default and get the return value
    Integer rows = addressMapper.updateNonDefaultByUid(uid);
    // Judge whether the return value is less than 1
    if (rows < 1) {
        // Yes: UpdateException
        throw new UpdateException(
            "Failed to set default receiving address[1]!An unknown error occurred while updating the receiving address data. Please contact the system administrator!");
    }

    // Set the specified delivery address as the default and get the return value
    rows = addressMapper.updateDefaultByAid(aid, username, new Date());
    // Judge whether the return value is not 1
    if (rows != 1) {
        // Yes: UpdateException
        throw new UpdateException(
            "Failed to set default receiving address[2]!An unknown error occurred while updating the receiving address data. Please contact the system administrator!");
    }
}

Then, test in AddressServiceImpl:

@Test
public void setDefault() {
    try {
        Integer aid = 25;
        Integer uid = 18;
        String username = "Receiving address administrator";
        service.setDefault(aid, uid, username);
        System.err.println("OK.");
    } catch (ServiceException e) {
        System.err.println(e.getClass().getName());
        System.err.println(e.getMessage());
    }
}

48. Receiving address - set default - controller layer

(a) Handling newly created exceptions

(b) Requests to be processed for design

Request path:
Request parameters:
Request method:
Response result: jsonresult <? >

(c) Process request

49. Receiving address - set default - front page

-----------------------------------

Appendix 1: about transaction

Transaction is a mechanism that can ensure the success or failure of multiple addition, deletion and modification operations in the same business in the database field!

Under the development mode based on spring JDBC, adding @ Transactional annotation to the business method means that "the business method will execute multiple addition, deletion and modification operations in the way of transaction", which can achieve the effect of "all execution succeeded" or "all execution failed"!

Account balance
 Cangsong 100
 Guobin 1000

Guobin transfers 500 yuan to Cangsong

update xx set balance = balance + 500 where account = Cangsong
 update xx set balance = balance - 500 where account = Guobin
159 original articles published, 22 praised, 160000 visitors+
Private letter follow

Posted by tengkie on Thu, 16 Jan 2020 05:31:01 -0800