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>
到此为止,我们就完成了测试框架整合结果的程序。
Spring+SpringMvc+Mybatis框架集成搭建教程三(框架整合测试程序开发)的更多相关文章
- Spring+SpringMvc+Mybatis框架集成搭建教程
一.背景 最近有很多同学由于没有过SSM(Spring+SpringMvc+Mybatis , 以下简称SSM)框架的搭建的经历,所以在自己搭建SSM框架集成的时候,出现了这样或者那样的问题,很是苦恼 ...
- Spring+SpringMvc+Mybatis框架集成搭建教程二(依赖配置及框架整合)
依赖导入以及框架整合 (1).打开项目的pom.xml文件,声明依赖 <project xmlns="http://maven.apache.org/POM/4.0.0" x ...
- Spring+SpringMVC+Mybatis环境的搭建(使用Intellij IDEA)
前言:本文主要介绍利用IDEA如何搭建SSM环境,并使用mybatis的逆向生成功能,根据数据表生成对应mapper接口和sql映射文件.具体步骤如下. 开发环境: IDEA 14.1.7 maven ...
- Spring+SpringMVC+Mybatis+Shiro环境搭建之IDEA下搭建Maven项目
运行IntelliJ IDEA 2016.3.2(64)编译器新建项目 在弹出的窗体中选择maven,然后勾选要建的maven模板--这里选webApp 然后填入相应的maven项目组信息(Gro ...
- Spring+SpringMvc+Mybatis框架集成搭建教程一(项目创建)
一.框架搭建环境 Spring 4.2.6.RELEASE SpringMvc 4.2.6.RELEASE Mybatis 3.2.8 Maven 3.3.9 Jdk 1.7 Idea 15.04 二 ...
- Spring+SpringMvc+Mybatis框架集成搭建教程四(项目部署及测试)
在IDEA中将项目部署到本地Tomcat下进行运行并验证整合结果 (1).点击如下图所示的下拉按钮,弹出Edit Configurations...后点击该项. (2).跳出如下界面后,点击红框内的& ...
- Spring+SpringMvc+Mybatis框架集成搭建教程五(项目源码发布到GitHub)
一.背景 我们做完了上面的四步操作以后,来把我们写好的项目提交到自己的GitHub仓库进行版本管理,具体步骤如下. 二.提交步骤 1.首先你要保证你已经有GitHub的账号和密码(没有可以去githu ...
- Spring+SpringMVC+Mybatis(SSM)框架集成搭建
Spring+SpringMVC+Mybatis框架集成搭建教程 一.背景 最近有很多同学由于没有过SSM(Spring+SpringMvc+Mybatis , 以下简称SSM)框架的搭建的经历,所以 ...
- SSM框架——Spring+SpringMVC+Mybatis的搭建教程
一:概述 SSM框架在项目开发中经常使用到,相比于SSH框架,它在仅几年的开发中运用的更加广泛. Spring作为一个轻量级的框架,有很多的拓展功能,最主要的我们一般项目使用的就是IOC和AOP. S ...
随机推荐
- 备忘:aliyun maven mirror
<mirror> <id>alimaven</id> <name>aliyun maven</name> &l ...
- 修改httpd默认端口号
Tomcat: vim /etc/httpd/conf/httpd.conf//别忘了service httpd restart Nginx: vim /etc/nginx/nginx.conf//完 ...
- mysql按日期检索数据
说明: 按日期归类, 查询2016年5月1号到5月31号每天用户注册数量 直接上源码: select DATE_FORMAT( creationTime, "%Y-%m-%d" ) ...
- JavaScript数组的方法
push() :将参数加载到数组的最后,返回数组的长度 pop() :删除数组的最后一个元素,返回删除的值 shift() :删除数组的第一个元素,返回删除的值 unshift ...
- Git的使用
1.从Git服务器上获取项目 2.提交我的修改项目 3.发布新版本 4.修复bug
- py-faster-rcnn +cudnn V5
转载自http://blog.csdn.net/u010733679/article/details/52221404,经过实际操作,采用了第二种手动替换代码文件.修改个别函数名的方式,成功编译. - ...
- 前后台获取上下文context
1.web server端获取上下文:Context ctx = WafContext.getInstance().getContext();上下文中包含当前登录组织.当前登录用户.语种.数据库.客户 ...
- div
<div id="container"> <div id="left">左边</div> <div id=" ...
- Tarjan三把刀
搞过OI的对tarjan这个人大概都不陌生.这个人发明了很多神奇的算法,在OI届广被采用. 他最广泛采用的三个算法都是和$dfn$,$low$相关的. 有向图求强连通分量 其实说直白点,就是缩点.用得 ...
- Java+jquery实现裁剪图片上传到服务器
大体分两步: 1.利用jquery裁剪图片,把裁剪到的几个点传入后端 2.利用前端传入的几个点,来裁剪图片 首先,用到一个jquery的插件 imgAreaSelect 实例及插件下载地址:http: ...