框架整合測試程式開發
(1).在mysql資料庫中建立t_user表,sql語句如下
CREATE TABLE `t_user` ( `id` bigint(20) NOT NULL AUTO_INCREMENT, `user_name` varchar(255) DEFAULT NULL, `password` varchar(255) DEFAULT NULL, PRIMARY KEY (`id`) ) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=utf8;
(2).在main資料夾下的java原始檔夾下建立com.hafiz.www包,並在該包下依次建立:
controller包(存放控制器)、
exception包(存放自定義異常及全域性異常處理器)、
mapper包(存放mybatis的mapper介面)、
po包(存放資料庫表的實體類)、
service包(存放業務層介面),並在service包下建立
impl包(存放業務層實現)。
(3).在po包下面建立UserEntity.java類
package com.hafiz.www.po; /** * Desc:使用者表實體類 * Created by hafiz.zhang on 2016/8/27. */ public class UserEntity { private Long id; // 編號 private String userName; // 使用者名稱 private String password; // 密碼 public Long getId() { return id; } public void setId(Long id) { this.id = id; } public String getUserName() { return userName; } public void setUserName(String userName) { this.userName = userName; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } }
(4).在mapper包下建立UserEntityMapper.java類
package com.hafiz.www.mapper; import com.hafiz.www.po.UserEntity; import java.util.List; /** * Desc:使用者表實體mapper介面類 * Created by hafiz.zhang on 2016/8/27. */ public interface UserEntityMapper { /** * 查詢所有的使用者資訊 * * @return */ List<UserEntity> getAllUsers(); }
(5).在resources檔案下的mapper檔案下建立UserEntityMapper.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.hafiz.www.mapper.UserEntityMapper" > <resultMap id="BaseResultMap" type="com.hafiz.www.po.UserEntity" > <id column="id" property="id" jdbcType="INTEGER" /> <result column="user_name" property="userName" jdbcType="VARCHAR" /> <result column="password" property="password" jdbcType="VARCHAR" /> </resultMap> <sql id="Base_Column_List" > id, user_name, password </sql> <select id="getAllUsers" resultMap="BaseResultMap"> SELECT <include refid="Base_Column_List"/> FROM t_user </select> </mapper>
(6).在service包下建立UserService.java類
package com.hafiz.www.service; import com.hafiz.www.po.UserEntity; import java.util.List; /** * Desc:使用者表相關的service介面 * Created by hafiz.zhang on 2016/8/27. */ public interface UserService { /** * 獲取所有的使用者資訊 * * @return */ List<UserEntity> getAllUsers(); }
(7).在service包下的impl包建立UserServiceImpl.java類
package com.hafiz.www.service.impl; import com.hafiz.www.mapper.UserEntityMapper; import com.hafiz.www.po.UserEntity; import com.hafiz.www.service.UserService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import java.util.List; /** * Desc:使用者表相關的servie介面實現類 * * Created by hafiz.zhang on 2016/8/27. */ @Service public class UserServiceImpl implements UserService { @Autowired private UserEntityMapper mapper; @Override public List<UserEntity> getAllUsers() { return mapper.getAllUsers(); } }
(8).在controller包下建立UserController.java類
package com.hafiz.www.controller; import com.hafiz.www.po.UserEntity; import com.hafiz.www.service.UserService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.ResponseBody; import java.util.List; /** * Desc:使用者資訊控制器 * Created by hafiz.zhang on 2016/8/27. */ @Controller @RequestMapping("/user") public class UserController { @Autowired private UserService userService; @RequestMapping(value = "/all", method = RequestMethod.GET) @ResponseBody public List<UserEntity> getAllUsers(){ List<UserEntity> list = userService.getAllUsers(); return list; } }
(9).在exception包下建立全域性異常處理器CustomExceptionResolver.java類(該類必須實現HandlerExceptionResolver介面)
package com.hafiz.www.exception; import org.springframework.web.servlet.HandlerExceptionResolver; import org.springframework.web.servlet.ModelAndView; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; /** * Desc:全域性異常處理器 * Created by hafiz.zhang on 2016/8/27. */ public class CustomExceptionResolver implements HandlerExceptionResolver { @Override public ModelAndView resolveException(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) { //handler就是處理器介面卡要執行的處理器(只有method方法) //1.解析出異常型別 CustomException exception = null; //如果該異常型別是系統自定義的異常,直接取出異常資訊,在錯誤頁面展示 if(ex instanceof CustomException){ exception = (CustomException)ex; } else{ //如果該異常型別不是系統自定義的異常,構造一個自定義的異常型別(資訊為“未知錯誤”) exception = new CustomException("未知錯誤,請於管理員聯絡"); } ModelAndView modelAndView = new ModelAndView(); //將錯誤資訊傳到頁面 modelAndView.addObject("message", exception.getMessage()); //指定錯誤頁面 modelAndView.setViewName("error"); return modelAndView; } }
(10)在exception包下創CustomException.java建自定義異常類
package com.hafiz.www.exception; /** * Desc:自定義異常類 * Created by hafiz.zhang on 2016/8/27. */ public class CustomException extends Exception{ private String message; public CustomException(String message) { super(message); this.message = message; } public String getMessage() { return message; } public void setMessage(String message) { this.message = message; } }
(11).在webapp下的WEB-INF資料夾下建立jsp資料夾,並在該資料夾下建立error.jsp用來顯示捕獲的異常資訊
<%@ page contentType="text/html;charset=UTF-8" language="java" %> <html> <head> <title>錯誤頁面</title> </head> <body> ${message} </body> </html>
到此為止,我們就完成了測試框架整合結果的程式。