三、业务模块分层架构

一、创建数据库(环境Mysql)

Database Name(略)

Character(UTF-8 Unicode)

 

二、创建MVC结构

创建包:java/com.item.module

module下创建子包:Controller、Model、Service

 

三、Model层

Customer.java

public class Customer { private long ID; private String Name; private String Contact; private String Telephone; private String Email; private String Remark; //Getter and Setter
}

创建对应的数据库表,创建Demo数据。

 

四、控制层

用例:

列表界面:GET:/customer

查询动作:POST:/customer_search

详情界面:GET:/customer_show?id={id}

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

新建界面:GET:/customer_create

新建动作:POST:/customer_create

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

编辑界面:GET:/customer_edit?id={id}

编辑动作:PUT:/customer_edit?id={id}

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

删除动作:DELETE:/customer_delete?id={id}

创建Servlet:

CustomerServlet、CustomerShowServlet

CustomerCreateServlet

CustomerEditServlet

CustomerDeleteServlet

@WebServlet("/customer") public class CustomerServlet extends HttpServlet { protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { } protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { } }

 

五、服务层

public class CustomerService { /** * 获取客户列表 */
    public List<Customer> GetCustomerList() { //TODO
        return null; } /** * 获取客户 */
    public Customer GetCustomer(long id){ //TODO
        return null; } /** * 创建客户 */
    public boolean CreateCustomer(Map<String,Object> fieldMap){ //TODO
        return false; } /** * 更新客户 */
    public boolean UpdateCustomer(long id, Map<String,Object> fieldMap){ //TODO
        return false; } /** * 删除客户 */
    public boolean DeleteCustomer(long id){ //TODO
        return false; } }

 

六、单元测试

public class CustomerServiceTest { private final CustomerService customerService; public CustomerServiceTest(){ customerService = new CustomerService(); } @Before public void init(){ //TODO 初始化数据库
 } @Test public void GetCustomerListTest() throws Exception{ List<Customer> customerList = customerService.GetCustomerList(); Assert.assertEquals(2, customerList.size()); } @Test public void GetCustomerTest() throws Exception { long id = 1; Customer customer = customerService.GetCustomer(id); Assert.assertNotNull(customer); } @Test public void CreateCustomerTest() throws Exception { Map<String, Object> fieldMap = new HashMap<String, Object>(); fieldMap.put("name", "customer100"); fieldMap.put("contact", "John"); fieldMap.put("telephone", "13512345678"); boolean result = customerService.CreateCustomer(fieldMap); Assert.assertTrue(result); } @Test public void UpdateCustomerTest() throws Exception { long id = 1; Map<String, Object> fieldMap = new HashMap<String, Object>(); fieldMap.put("contact", "Eric"); boolean result = customerService.UpdateCustomer(id, fieldMap); Assert.assertTrue(result); } @Test public void DeleteCustomerTest() throws Exception { long id = 1; boolean result = customerService.DeleteCustomer(id); Assert.assertTrue(result); } }

 

七、视图层

WEB-INF\view:

customer.jsp

customer_create.jsp

customer_edit.jsp

customer_show.jsp

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>客户管理 - 创建客户</title>
</head>
<body>

<h1>创建客户界面</h1>

<%-- TODO --%>

</body>
</html>

 

你可能感兴趣的:(三、业务模块分层架构)