Merchants can sell their own products on the platform for consumers to buy.

Add More Merchant Type Users into Database

Run the following SQL inside the database:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
USE shop;

INSERT INTO
user(`created_at`, `updated_at`, `last_logged_on`, `first_name`, `last_name`, `phone`, `email`, `password`, `type`, `is_enabled`, `avatar`, `introduction`)
VALUES
(UNIX_TIMESTAMP(), UNIX_TIMESTAMP(), UNIX_TIMESTAMP(), 'Dong', 'Chen', '1234567890', '[email protected]', 'Useless Pass', 2, 1, "\\1557635057\\front.jpg", 'New user'),
(UNIX_TIMESTAMP(), UNIX_TIMESTAMP(), UNIX_TIMESTAMP(), 'Dong', 'Chen', '1234567890', '[email protected]', 'Useless Pass', 2, 1, "\\1557635057\\front.jpg", 'New user'),
(UNIX_TIMESTAMP(), UNIX_TIMESTAMP(), UNIX_TIMESTAMP(), 'Dong', 'Chen', '1234567890', '[email protected]', 'Useless Pass', 2, 1, "\\1557635057\\front.jpg", 'New user'),
(UNIX_TIMESTAMP(), UNIX_TIMESTAMP(), UNIX_TIMESTAMP(), 'Dong', 'Chen', '1234567890', '[email protected]', 'Useless Pass', 2, 1, "\\1557635057\\front.jpg", 'New user'),
(UNIX_TIMESTAMP(), UNIX_TIMESTAMP(), UNIX_TIMESTAMP(), 'Dong', 'Chen', '1234567890', '[email protected]', 'Useless Pass', 2, 1, "\\1557635057\\front.jpg", 'New user'),
(UNIX_TIMESTAMP(), UNIX_TIMESTAMP(), UNIX_TIMESTAMP(), 'Dong', 'Chen', '1234567890', '[email protected]', 'Useless Pass', 2, 1, "\\1557635057\\front.jpg", 'New user'),
(UNIX_TIMESTAMP(), UNIX_TIMESTAMP(), UNIX_TIMESTAMP(), 'Dong', 'Chen', '1234567890', '[email protected]', 'Useless Pass', 2, 1, "\\1557635057\\front.jpg", 'New user');
SELECT * FROM user;

Update merchant.xml

Copy and paste the following content into merchant.xml under the service/src/main/resources/mapper directory:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
        PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="co.dongchen.shop.mapper.MerchantMapper">
    <sql id="merchantFields">
        first_name, last_name, phone, email, type
    </sql>
    <select id="queryPagedMerchants" resultType="merchant">
        select * from user
        <where>
            is_enabled = 1 and type = 2
            <if test="merchant.id != null and merchant.id != 0">
                and id = #{merchant.id}
            </if>
            <if test="merchant.email != null and merchant.email != ''">
                and email = #{merchant.email}
            </if>
        </where>
        order by id desc
        <if test="pageHelperCondition.offSet != null and pageHelperCondition.limit != null">
            limit #{pageHelperCondition.offSet},#{pageHelperCondition.limit}
        </if>
        <if test="pageHelperCondition.offSet == null and pageHelperCondition.limit != null">
            limit #{pageHelperCondition.limit}
        </if>
    </select>
    <select id="queryPagedCount" resultType="long">
        select count(id)
        from user
        <where>
            is_enabled = 1 and type = 2
            <if test="merchant.id != null and merchant.id != 0">
                and id = #{merchant.id}
            </if>
            <if test="merchant.email != null and merchant.email != ''">
                and email = #{merchant.email}
            </if>
        </where>
    </select>
</mapper>

Update MerchantMapper

Copy and paste the following content into MerchantMapper.java under the service/src/main/java/co.dongchen.shop/mapper directory:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
package co.dongchen.shop.mapper;

import co.dongchen.shop.common.model.User;
import co.dongchen.shop.common.util.PageHelper;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;

import java.util.List;

@Mapper
public interface MerchantMapper {

    List<User> queryPagedMerchants(@Param("merchant") User merchant, @Param("pageHelperCondition") PageHelper pageHelperCondition);

    Long queryPagedCount(@Param("merchant") User merchant);
}

Update MerchantService

Copy and paste the following content into MerchantService.java under the service/src/main/java/co.dongchen.shop/service directory:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
package co.dongchen.shop.service;

import co.dongchen.shop.common.model.User;
import co.dongchen.shop.common.util.PageHelper;
import co.dongchen.shop.common.util.pager.PagingData;
import co.dongchen.shop.mapper.MerchantMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class MerchantService {

    @Autowired
    private MerchantMapper merchantMapper;

    @Value("${fileserver.img_path}")
    private String imgPath;

    public User getMerchantById(Long merchantId) {
        User merchant = new User();
        merchant.setId(merchantId);
        merchant.setType(2);
        List<User> merchants = merchantMapper.queryPagedMerchants(merchant, PageHelper.init(1, 1));
        getImg(merchants);
        if (! merchants.isEmpty()) {
            return merchants.get(0);
        }
        return null;
    }

    private void getImg(List<User> merchants) {
        merchants.forEach(m -> m.setAvatar(imgPath + m.getAvatar()));
    }

    public PagingData<User> getPagedMerchants(PageHelper pageHelper) {
        List<User> merchants = merchantMapper.queryPagedMerchants(new User(), pageHelper);
        getImg(merchants);
        Long count = merchantMapper.queryPagedCount(new User());
        return PagingData.initPaging(merchants, count, pageHelper.getPageSize(), pageHelper.getPageNumber());
    }
}

Create MerchantController

  • Right click controller/src/main/java/co.dongchen.shop/controller directory: New > Java Class
  • Fill in “MerchantController”
  • Click “OK” button
img

Copy and paste the following content into MerchantController.java:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
package co.dongchen.shop.controller;

import co.dongchen.shop.common.model.Product;
import co.dongchen.shop.common.model.User;
import co.dongchen.shop.common.util.PageHelper;
import co.dongchen.shop.common.util.pager.PagingData;
import co.dongchen.shop.service.MerchantService;
import co.dongchen.shop.service.ProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
public class MerchantController {

    @Autowired
    private MerchantService merchantService;

    @Autowired
    private ProductService productService;

    @RequestMapping("/merchant/pagination")
    public String pagination(PageHelper pageHelper, ModelMap modelMap) {
        PagingData<User> pageData = merchantService.getPagedMerchants(PageHelper.init(pageHelper.getPageSize(), pageHelper.getPageNumber()));
        modelMap.put("pageData", pageData);
        return "/user/merchant/pagination";
    }

    @RequestMapping("/merchant/info")
    public String merchantInfo(Long id, ModelMap modelMap) {
        User merchant = merchantService.getMerchantById(id);
        Product productCondition = new Product();
        productCondition.setUserId(id);
        // Show the first ten products
        PagingData<Product> ownedProducts = productService.getProducts(productCondition, new PageHelper(10, 1));
        if (ownedProducts != null) {
            modelMap.put("ownedProducts", ownedProducts.getList());
        }

        modelMap.put("merchant", merchant);
        return "/user/merchant/info";
    }
}

Create pagination.ftl

  • Right click controller/src/main/resources/templates/user directory: New > Directory
  • Fill in “merchant”
  • Click “OK” button
img
  • Right click controller/src/main/resources/templates/user/merchant directory: New > File
  • Fill in “pagination.ftl”
  • Click “OK” button
img

Copy and paste the following content into pagination.ftl:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
<div>
    <#foreach merchant in pageData.list>
        <div>
            <p><a href="/merchant/info?id=${merchant.id}">${merchant.firstName} ${merchant.lastName}</a></p>
        </div>
    </#foreach>
</div>
<div>
    <@common.paging pageData.pagination/>
</div>
<@common.js/>

Create info.ftl

  • Right click controller/src/main/resources/templates/user/merchant directory: New > File
  • Fill in “info.ftl”
  • Click “OK” button
img

Copy and paste the following content into info.ftl:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
<html lang="en-NZ">
<@common.header/>
<body>
${errorMsg!}
${successMsg!}

<h1>Merchant Info</h1>
<p>Merchant: ${merchant.firstName} ${merchant.firstName}</p>
<h1>Products Owned</h1>
<#foreach ownedProduct in ownedProducts>
    <div>
        <p>${ownedProduct.name}</p>
    </div>
</#foreach>

</body>
<@common.footer/>
</html>

Verify

Run the App

1
2
// Short Cut for "Run the Program"
Alt + Shift + F10
img

Choose the correspondent option and press enter.

View Merchant List Page in the Browser

1
http://localhost:8080/merchant/pagination
img

Click linkage number 2 to the second page:

img

Click the third Dong Chen linkage to the merchant info page:

img

Buy me a coffeeBuy me a coffee