SSM Project - An On-line Shopping Website (3-2)

Part 3 - 1. Backend Services - Category Management

1. Function Analysis

We mainly enable 5 functions in Category Management Service.

- `Create` function: create a new category
- `Read` function: get the category list
- `Update` function: edit and update category
- `Delete` function: delete category
- `Page` function: 

2. Entity Design

At first, we design the POJO for Category according to the Category table in the DB.
The Category class has two private fields, one is categoryId and another is categoryName, and has corresponding getters and setters.

3. DAO Design

DAO, i.e. data access object, is an object that provides an abstract interface to some type of database or other persistence mechanism. By mapping application calls to the persistence layer, the DAO provides some specific data operations without exposing details of the database.

3.1 Configuration

In this project, we use MyBatis, which is a Java persistence framework and use XML mappers to interact with database. In the configuration file, i.e. spring-dao.xml, we create the mapper scanner bean and let spring control its life cycle. That is

<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
    <!-- sqlSessionFactory -->
    <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory" />
    <!-- specify DAO interfaces -->
    <property name="basePackage" value="com.example.tmall.dao" />
</bean>

3.2 Design CategoryDao

Create an interface CategoryDao in java folder, write CRUD methods.

1) Read method

public interface Category {
    List<Category> queryCategory(Page page);
}

1) Create method

public interface Category {
    List<Category> list(Page page);
}

3.3 Write according SQL statements

Write according SQL statments in an XML file, i.e. CategoryMapper in resource folder.

At first we specify the namespace of these SQL statments, i.e. the folder of DAO.

<mapper namespace="com.how2java.tmall.mapper.CategoryMapper">

1) Read SQL statment

Tips:

  • <if> is Dynamic SQL in MyBatis. This statement would provide an optional limit in our select of categories. If you passed in no start and count, then all categories would be returned. But if you do pass in the start and count values, it will only select categories from start to end. This functino is used for paging, i.e. We only want to display a fixed number of contents on each page. The datailed description of the util function page can be found .

3.4 Write CategoryService (both interface and implementation)

1-1) Read Service interface

public interface CategoryService {
    List<Category> getCategoryList();
}

1-2) Read Service implementation

@Service
public class CategoryServiceImpl implements CategoryService {

    @Autowired
    private CategoryDao categoryDao;

    @Override
    public List<Category> getCategoryList() {
        return categoryDao.queryCategory();
    }
}

Tips: we inject a categoryDao here using Annotation @Autowired. Essentially we are utilizing the Inversion of Control(IOC) property of Spring.

3.5 Write CategoryController

1) Read Controller

@Controller
@RequestMapping("")
public class CategoryController {
    @Autowired
    CategoryService categoryService;

    @RequestMapping("admin_category_list")
    public String list (Model model) {
        List<Category> categoryList = categoryService.list();
        model.addAttribute("categoryList", categoryList);
        return "admin/listCategory";
    }
}

Tips:

  • @Controller: delcear the current class is a controller
  • @Autowired : inject a CategoryService in the controller
  • @RequestMapping: specify the visiting url of this method
  • return “admin/listCategory” : let server redirect to the JSP viewer in admin/listCategory directory.

   Reprint policy


《SSM Project - An On-line Shopping Website (3-2)》 by Tong Shi is licensed under a Creative Commons Attribution 4.0 International License
  TOC