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 ...
随机推荐
- NB實體連線到公司的網路,無法上網解決方案,需設 proxy。
未使用 VPN Cisco Anyconnect 已連線到公司的網路: google-chrome-stable --proxy-server="proxy.XXXcomm.com:3128 ...
- 5.Android消息推送机制简单例子
1.首先布局文件xml代码: <?xml version="1.0" encoding="utf-8"?> <RelativeLayout x ...
- 线程间通信 GET POST
线程间通信有三种方法:NSThread GCD NSOperation 进程:操作系统里面每一个app就是一个进程. 一个进程里面可以包含多个线程,并且我们每一个app里面有且仅有一 ...
- js 时间相关函数
实例: <!doctype html> <html> <head> <meta charset="utf-8"> <title ...
- 直接拿来用,最火的.NET开源项目
综合类 微软企业库 微软官方出品,是为了协助开发商解决企业级应用开发过程中所面临的一系列共性的问题, 如安全(Security).日志(Logging).数据访问(Data Access).配置管理( ...
- [Nhibernate]一级缓存
目录 写在前面 文档与系列文章 一级缓存 一个例子 一级缓存管理 总结 写在前面 上篇文章介绍了nhibernate中对象的三种状态,通过对象的三种状态,很容易想到缓存. 什麽是缓存? 有时候,某些数 ...
- pyquery的问题
在使用pyquery时发现一些问题, 1.爬取的html中如果有较多的错误时,不能很好的补全. 2.如果要获取某个class中的内容时,如果内容太多不能取完整!只能取一部分. 这个在现在的最新版本中还 ...
- .NET导入Excel到SQL数据库
在我们开发各类应用型系统,经常会遇到导入导出Excel,为什么会用到他呢?企业或者单位在从无信息化到信息化的一个转变过程.在没有信息化的企业或单位之前,一般都采用Excel来记录相应的数据,做统计计算 ...
- C# 的界面控件属性修改线程安全问题
今天在实验delegate与thread 在初步的实验结束后,因为原来的delegate只有一个函数会被调用,感觉没有达到delegate的极致,所以又重新自己定义了一个delegate,在另一个线程 ...
- 混合开发 webview 中file 控件 点击后无反应解决方法
最近在做个项目 ,需要 使用 file 控件上传 图片到服务器 ,在手机浏览器中 可以正常选择照片,但是放到 android 应用中的webview中,file 控件点击后就没有反应. 百度了一番后, ...