框架整合测试程序开发

(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框架集成搭建教程三(框架整合测试程序开发)的更多相关文章

  1. Spring+SpringMvc+Mybatis框架集成搭建教程

    一.背景 最近有很多同学由于没有过SSM(Spring+SpringMvc+Mybatis , 以下简称SSM)框架的搭建的经历,所以在自己搭建SSM框架集成的时候,出现了这样或者那样的问题,很是苦恼 ...

  2. Spring+SpringMvc+Mybatis框架集成搭建教程二(依赖配置及框架整合)

    依赖导入以及框架整合 (1).打开项目的pom.xml文件,声明依赖 <project xmlns="http://maven.apache.org/POM/4.0.0" x ...

  3. Spring+SpringMVC+Mybatis环境的搭建(使用Intellij IDEA)

    前言:本文主要介绍利用IDEA如何搭建SSM环境,并使用mybatis的逆向生成功能,根据数据表生成对应mapper接口和sql映射文件.具体步骤如下. 开发环境: IDEA 14.1.7 maven ...

  4. Spring+SpringMVC+Mybatis+Shiro环境搭建之IDEA下搭建Maven项目

    运行IntelliJ IDEA 2016.3.2(64)编译器新建项目   在弹出的窗体中选择maven,然后勾选要建的maven模板--这里选webApp 然后填入相应的maven项目组信息(Gro ...

  5. 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 二 ...

  6. Spring+SpringMvc+Mybatis框架集成搭建教程四(项目部署及测试)

    在IDEA中将项目部署到本地Tomcat下进行运行并验证整合结果 (1).点击如下图所示的下拉按钮,弹出Edit Configurations...后点击该项. (2).跳出如下界面后,点击红框内的& ...

  7. Spring+SpringMvc+Mybatis框架集成搭建教程五(项目源码发布到GitHub)

    一.背景 我们做完了上面的四步操作以后,来把我们写好的项目提交到自己的GitHub仓库进行版本管理,具体步骤如下. 二.提交步骤 1.首先你要保证你已经有GitHub的账号和密码(没有可以去githu ...

  8. Spring+SpringMVC+Mybatis(SSM)框架集成搭建

    Spring+SpringMVC+Mybatis框架集成搭建教程 一.背景 最近有很多同学由于没有过SSM(Spring+SpringMvc+Mybatis , 以下简称SSM)框架的搭建的经历,所以 ...

  9. SSM框架——Spring+SpringMVC+Mybatis的搭建教程

    一:概述 SSM框架在项目开发中经常使用到,相比于SSH框架,它在仅几年的开发中运用的更加广泛. Spring作为一个轻量级的框架,有很多的拓展功能,最主要的我们一般项目使用的就是IOC和AOP. S ...

随机推荐

  1. JavaScript零基础学习系列一

    JavaScript Js分成三大块: ECMAScript:语言本身,是标准,js是它的一个具体实现 BOM:浏览器对象模型 DOM:文档对象模型 ECMAScript有三种具体实现: JavaSc ...

  2. 数据结构图文解析之:二叉堆详解及C++模板实现

    0. 数据结构图文解析系列 数据结构系列文章 数据结构图文解析之:数组.单链表.双链表介绍及C++模板实现 数据结构图文解析之:栈的简介及C++模板实现 数据结构图文解析之:队列详解与C++模板实现 ...

  3. ED2k Resource

    http://www.lwkk.com/ http://www.ed2000.com/

  4. 【Alpha版本】冲刺随笔汇总

    [Alpha版本]冲刺-Day1 [Alpha版本]冲刺-Day2 [Alpha版本]冲刺-Day3 [Alpha版本]冲刺-Day4 [Alpha版本]冲刺-Day5 [Alpha版本]冲刺-Day ...

  5. Promise deeply lookup

    Motivation Consider the following synchronous JavaScript function to read a file and parse it as JSO ...

  6. linux下centos的网络连接

  7. CSS-学习笔记二

    1. table标签中的标题 caption { caption-side: top; } 2.table标签中边框重合 table { width: 300px; height: 200px; bo ...

  8. Python学习笔记——函数

    1.标准类型内建函数 <1>type() —— 返回对象的类型 <2>cmp() —— 比较两个对象,返回两个对象的ASCII码的差 <3>str().repr() ...

  9. Android开发环境的搭建

    在学习android时,环境的搭建是学习android的第一步,为了记住第一步特写了这篇文章. 第一步,安装jdk,因为eclipse的运行需要jdk才可以,所以jdk的安装时第一步.安装过程和普通的 ...

  10. jquery + header

    官网上搜索headers 基本用法(直接用下楼上的代码了) $.ajax({ //请求类型,这里为POST type: 'POST', //你要请求的api的URL url: url , //是否使用 ...