Spring+SpringMvc+Mybatis框架集成搭建教程三(框架整合测试程序开发)
框架整合测试程序开发
(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>
到此为止,我们就完成了测试框架整合结果的程序。
感谢您花时间阅读此篇文章,如果您觉得这篇文章你学到了东西也是为了犒劳下博主的码字不易不妨打赏一下吧,让博主能喝上一杯咖啡,在此谢过了!
如果您觉得阅读本文对您有帮助,请点一下左下角“推荐”按钮,您的将是我最大的写作动力!另外您也可以选择【关注我】,可以很方便找到我!
本文版权归作者和博客园共有,来源网址:http://www.cnblogs.com/hafiz 欢迎各位转载,但是未经作者本人同意,转载文章之后必须在文章页面明显位置给出作者和原文连接,否则保留追究法律责任的权利!