1. 程式人生 > 其它 >SSM整合測試

SSM整合測試

技術標籤:SSM邊學邊用

完成了SSM框架整合環境的搭建工作,就已經完成了這三個框架大部分的整合工作。接下來,以查詢客戶資訊為例,來講解SSM框架的整合開發,其具體實現步驟如下。

建立資料表

在test資料庫(依據db.properties配置檔案的資料庫)中建立customer資料表,並匯入測試資料

CREATE TABLE `customer` (
 `id` int(10) NOT NULL AUTO_INCREMENT,
 `username` varchar(255) NOT NULL,
 `job` varchar(255) NOT NULL,
 `phone` varchar(255) NOT NULL,
 PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
-- ----------------------------
-- Records of customer
-- ----------------------------
INSERT INTO `customer` VALUES ('1', '小明', '醫生','13567344567');
INSERT INTO `customer` VALUES ('2', '小紅', '教師','18756576778');

在com.syg.po包中

建立持久化類Customer:

package com.syg.po;
​
public class Customer {
  private Integer id;
  private String username;
  private String job;
  private String phone;
​
  public Integer getId() {
    return id;
   }
​
  public void setId(Integer id) {
    this.id = id;
   }
​
  public String getUsername() {
    return username;
   }
​
  public void setUsername(String username) {
    this.username = username;
   }
​
  public String getJob() {
    return job;
   }
​
  public void setJob(String job) {
    this.job = job;
   }
​
  public String getPhone() {
    return phone;
   }
​
  public void setPhone(String phone) {
    this.phone = phone;
   }
}

在com.syg.dao包中

在包中建立介面CustomerDao:

package com.syg.dao;
​
import com.syg.po.Customer;
​
public interface CustomerDao {
  public Customer findCustomerById(Integer id);
}

並在resources/mapper下建立對應的對映檔案CustomerDao.xml

<?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="com.syg.dao.CustomerDao">
  <select id="findCustomerById" parameterType="Integer" resultType="Customer">
     select * from customer where id = #{id}
  </select>
</mapper>

在com.syg.service包中

建立介面CustomerService,並在CustomerService中定義通過id查詢客戶的方法:

package com.syg.service;
​
import com.syg.po.Customer;
​
public interface CustomerService {
  public Customer findCustomerById(Integer id);
}

在com.syg.service.impl包中

在包中建立CustomerService介面的實現類CustomerServiceImpl

package com.syg.service.impl;
​
import com.syg.dao.CustomerDao;
import com.syg.po.Customer;
import com.syg.service.CustomerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
​
@Service
@Transactional
public class CustomerServiceImpl implements CustomerService {
​
  @Autowired
  private CustomerDao customerDao;
  //查詢客戶
  public Customer findCustomerById(Integer id){
    return this.customerDao.findCustomerById(id);
   }
}

在com.syg.controller包中

建立用於處理頁面請求的控制器類CustomerController

package com.syg.controller;
​
import com.syg.po.Customer;
import com.syg.service.CustomerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
​
@Controller
public class CustomerController {
  @Autowired
  private CustomerService customerService;
​
  @RequestMapping("/findCustomerById")
  public String findCustomerById(Integer id, Model model){
    Customer customer = customerService.findCustomerById(id);
    model.addAttribute("customer",customer);
    return "customer";
   }
}

建立展示頁面

在WEB-INF目錄下,建立一個jsp資料夾,在該資料夾下建立一個用於展示客戶詳情的頁面檔案customer.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
  <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
  <title>客戶資訊</title>
</head>
<body>
<table border="1">
  <tr>
    <td>編號</td>
    <td>姓名</td>
    <td>職業</td>
    <td>手機號</td>
  </tr>
  <tr>
    <td>${customer.id}</td>
    <td>${customer.username}</td>
    <td>${customer.job}</td>
    <td>${customer.phone}</td>
  </tr>
</table>
</body>
</html>

點選執行,在瀏覽器中訪問地址http://localhost:1234/findCustomerById?id=1

總結

理清每個包下存放的檔案是什麼作用