开发环境:Win10+MyEclipse2017+ JDK1.8+ Tomcat8.5+mysql5.7+Maven+Navicat
一.创建maven项目
- 新建项目,新建web项目
建好之后的项目长这个样子
二.SSM整合
1.Maven引入需要的JAR包
修改pom.xml文件,直接把下面的内容粘到pom.xml里面替换原有的properties和dependencies:
<properties>
<!-- 文件拷贝时的编码 -->
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<!-- 编译时的编码 -->
<maven.compiler.encoding>UTF-8</maven.compiler.encoding>
<!-- spring版本号 -->
<spring.version>4.3.0.RELEASE</spring.version>
<spring-data-redis.version>1.7.1.RELEASE</spring-data-redis.version>
<!--mysql的版本号 -->
<mysql.version>5.1.39</mysql.version>
<!-- mybatis版本号 -->
<mybatis.version>3.4.0</mybatis.version>
<mybatis-spring.version>1.3.0</mybatis-spring.version>
<!-- log4j日志文件管理包版本 -->
<log4j.version>2.1</log4j.version>
</properties>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<!-- 表示开发的时候引入,发布的时候不会加载此包 -->
<scope>test</scope>
</dependency>
<!-- 数据库链接jar包 -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>${mysql.version}</version>
</dependency>
<!-- mybatis核心包 -->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>${mybatis.version}</version>
</dependency>
<!-- mybatis/spring包 -->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
<version>${mybatis-spring.version}</version>
</dependency>
<!-- 导入dbcp的jar包,用来在applicationContext.xml中配置数据库 -->
<dependency>
<groupId>commons-dbcp</groupId>
<artifactId>commons-dbcp</artifactId>
<version>1.2.2</version>
</dependency>
<!-- spring核心包 -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-oxm</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-tx</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aop</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context-support</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jms</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-redis</artifactId>
<version>${spring-data-redis.version}</version>
</dependency>
<!-- 导入java ee -->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>4.0.0-b01</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>javax.mail</groupId>
<artifactId>mail</artifactId>
<version>1.5.0-b01</version>
</dependency>
<!-- JSTL标签类 -->
<dependency>
<groupId>jstl</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
</dependency>
<!-- 日志文件管理包 -->
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>1.7.21</version>
</dependency>
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.17</version>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-core</artifactId>
<version>${log4j.version}</version>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-api</artifactId>
<version>${log4j.version}</version>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-slf4j-impl</artifactId>
<version>${log4j.version}</version>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-jcl</artifactId>
<version>${log4j.version}</version>
</dependency>
<dependency>
<groupId>com.lmax</groupId>
<artifactId>disruptor</artifactId>
<version>3.0.1</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-core</artifactId>
<version>2.5.1</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.5.1</version>
</dependency>
<dependency>
<groupId>commons-fileupload</groupId>
<artifactId>commons-fileupload</artifactId>
<version>1.3.1</version>
</dependency>
<dependency>
<groupId>net.sf.json-lib</groupId>
<artifactId>json-lib</artifactId>
<version>2.4</version>
<classifier>jdk15</classifier>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.0</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
</plugins>
</build>
2.在src/main/resources目录下添加3个配置文件
- jdbc.properties(JDBC属性文件)
driver=com.mysql.jdbc.Driver
url=jdbc:mysql://127.0.0.1:3306/test2?useUnicode=true&characterEncoding=utf8&zeroDateTimeBehavior=convertToNull&allowMultiQueries=true&serverTimezone=PRC&useSSL=false
username=root
password=zh123456
#定义初始连接数
initialSize=0
#定义最大连接数
maxActive=20
#定义最大空闲
maxIdle=20
#定义最小空闲
minIdle=1
#定义最长等待时间
maxWait=60000
- spring-mybatis.xml(主要进行自动扫描,自动注入,配置数据库)
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.1.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd">
<!-- 自动扫描 -->
<context:component-scan base-package="cn.com">
<context:exclude-filter type="annotation"
expression="org.springframework.stereotype.Controller" />
</context:component-scan>
<!-- 加载配置文件 -->
<!-- 引入配置文件 -->
<bean id="propertyConfigurer"
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="location" value="classpath:jdbc.properties" />
</bean>
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
destroy-method="close">
<property name="driverClassName" value="${driver}" />
<property name="url" value="${url}" />
<property name="username" value="${username}" />
<property name="password" value="${password}" />
<!-- 初始化连接大小 -->
<property name="initialSize" value="${initialSize}"></property>
<!-- 连接池最大数量 -->
<property name="maxActive" value="${maxActive}" ></property>
<!-- 连接池最大空闲 -->
<property name="maxIdle" value="${maxIdle}" ></property>
<!-- 连接池最小空闲 -->
<property name="minIdle" value="${minIdle}" ></property>
<!-- 获取连接最大等待时间 -->
<property name="maxWait" value="${maxWait}" ></property>
</bean>
<!-- spring和mybatis完美整合,不需要mybatis的配置映射文件 -->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<!-- 自动扫描mapping.xml文件 -->
<property name="mapperLocations" value="classpath:sql/*.xml"></property>
</bean>
<!-- DAO接口所在的包名,spring会自动查找其下的类,!注意这句 value="sqlSessionFactory",不是ref而是value -->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="cn.com.dao" />
<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory" />
</bean>
</beans>
- log4j2.xml
<?xml version="1.0" encoding="UTF-8"?>
<configuration status="off">
<properties>
<property name="LOG_HOME">${sys:catalina.home}/webAppLogs</property>
<!-- 日志备份目录 -->
<property name="BACKUP_HOME">backup</property>
<property name="SERVER_NAME">dataSync</property>
<property name="EVERY_FILE_SIZE">5M</property><!-- 日志切割的最小单位 -->
<property name="OUTPUT_LOG_LEVEL">DEBUG</property><!-- 日志输出级别 -->
<property name="log.user.active.folder">c:\\crt</property>
<property name="log.user.active.file">active</property>
<property name="log.user.active.path">${log.user.active.folder}/${log.user.active.file}/${log.user.active.file}.log
</property>
</properties>
<appenders>
<Console name="Console" target="SYSTEM_OUT">
<ThresholdFilter level="trace" onMatch="ACCEPT"
onMismatch="DENY" />
<PatternLayout pattern="%msg%n" />
</Console>
<RollingFile name="RollingFile" fileName="${LOG_HOME}/dev_${SERVER_NAME}.log"
filePattern="${LOG_HOME}/dev_${BACKUP_HOME}/dev_${SERVER_NAME}.%d{yyyy-MM-dd-HH}.log">
<PatternLayout pattern="%msg%n" />
<Policies>
<SizeBasedTriggeringPolicy size="${EVERY_FILE_SIZE}" />
<TimeBasedTriggeringPolicy interval="1"
modulate="true" />
</Policies>
<Filters>
<ThresholdFilter level="error" onMatch="DENY"
onMismatch="NEUTRAL" />
<ThresholdFilter level="trace" onMatch="ACCEPT"
onMismatch="DENY" />
</Filters>
</RollingFile>
<!-- 只显示error级别的信息 -->
<RollingFile name="RollingFileError"
fileName="${LOG_HOME}/dev_${SERVER_NAME}_error.log"
filePattern="${LOG_HOME}/dev_${BACKUP_HOME}/dev_${SERVER_NAME}_error.%d{yyyy-MM-dd-HH}.log">
<PatternLayout pattern="%msg%xEx%n" />
<Policies>
<SizeBasedTriggeringPolicy size="${EVERY_FILE_SIZE}" />
<TimeBasedTriggeringPolicy interval="1"
modulate="true" />
</Policies>
<Filters>
<ThresholdFilter level="error" onMatch="ACCEPT"
onMismatch="DENY" />
</Filters>
</RollingFile>
<!-- 产品数据分析日志 -->
<RollingRandomAccessFile name="ProductLog"
fileName="${LOG_HOME}/${SERVER_NAME}.log"
filePattern="${LOG_HOME}/${BACKUP_HOME}/${SERVER_NAME}.%d{yyyy-MM-dd-HH}.log">
<PatternLayout pattern="%msg%xEx%n" />
<Policies>
<SizeBasedTriggeringPolicy size="${EVERY_FILE_SIZE}" />
<TimeBasedTriggeringPolicy interval="1"
modulate="true" />
</Policies>
</RollingRandomAccessFile>
<!-- 用户行为日志 -->
<RollingRandomAccessFile name="UserActiveLog"
fileName="${log.user.active.path}"
filePattern="${log.user.active.folder}/${BACKUP_HOME}/${log.user.active.file}.%d{yyyy-MM-dd-HH}.log">
<PatternLayout pattern="%msg%xEx%n" />
<Policies>
<SizeBasedTriggeringPolicy size="${EVERY_FILE_SIZE}" />
<TimeBasedTriggeringPolicy interval="1"
modulate="true" />
</Policies>
</RollingRandomAccessFile>
</appenders>
<loggers>
<logger name="org.slf4j" level="INFO" />
<logger name="org.springframework" level="INFO" />
<logger name="org.apache.zookeeper" level="WARN" />
<AsyncLogger name="cn.rebuy" level="${OUTPUT_LOG_LEVEL}"
additivity="false" includeLocation="true">
<appender-ref ref="Console" />
<appender-ref ref="ProductLog" />
</AsyncLogger>
<asyncRoot level="info" includeLocation="true">
<appender-ref ref="Console" />
<appender-ref ref="RollingFile" />
<appender-ref ref="RollingFileError" />
</asyncRoot>
</loggers>
</configuration>
- spring-mvc.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.2.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.2.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-4.2.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-4.2.xsd">
<!-- 自动扫描该包,使SpringMVC认为包下用了@controller注解的类是控制器 -->
<context:component-scan base-package="cn.com.controller" >
<context:exclude-filter type="annotation" expression="org.springframework.stereotype.Service"/>
</context:component-scan>
<mvc:annotation-driven >
<mvc:message-converters>
<bean class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter">
<property name="supportedMediaTypes">
<list>
<value>text/html;charset=UTF-8</value>
<value>application/json;charset=UTF-8</value>
<value>application/x-www-form-urlencoded;charset=UTF-8</value>
</list>
</property>
</bean>
</mvc:message-converters>
</mvc:annotation-driven>
</beans>
(3)在src/main/webapp/WEB-INF目录下修改web.xml配置文件:
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
version="3.1">
<display-name>longTest</display-name>
<!-- 配置加载Spring文件的监听器-->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring-mybatis.xml</param-value>
</context-param>
<!-- 编码过滤器 -->
<filter>
<filter-name>encoding</filter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>UTF-8</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>encoding</filter-name>
<url-pattern>*.action</url-pattern>
</filter-mapping>
<!-- 防止Spring内存溢出监听器 -->
<listener>
<listener-class>org.springframework.web.util.IntrospectorCleanupListener</listener-class>
</listener>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!-- 配置Spring MVC前端核心控制器 -->
<servlet>
<servlet-name>SpringMVC</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring-mvc.xml</param-value>
</init-param>
<!-- 配置服务器启动后立即加载Spring MVC配置文件 -->
<load-on-startup>1</load-on-startup>
<async-supported>true</async-supported>
</servlet>
<servlet-mapping>
<servlet-name>SpringMVC</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>
三.整合完成写个小demo测试
1.新建表
CREATE TABLE `test_user` (
`ID` int(32) NOT NULL AUTO_INCREMENT,
`USER_NAME` varchar(255) DEFAULT '',
`PASSWORD` varchar(255) DEFAULT '',
`ADDRESS` varchar(255) CHARACTER SET utf8 DEFAULT NULL,
`PHONE` varchar(255) DEFAULT NULL,
PRIMARY KEY (`ID`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
2.根据表创建实体类、接口、sql配置文件(这里可利用MyBatis Generator自动创建)
整体项目结构:
- entity实体类
package cn.com.entity;
import java.io.Serializable;
public class TestUser implements Serializable{
/**
*
*/
private static final long serialVersionUID = 1L;
private Integer id;
private String userName;
private String password;
private String address;
private String phone;
public Integer getId() {
return id;
}
public void setId(Integer 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;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
public String getPhone() {
return phone;
}
public void setPhone(String phone) {
this.phone = phone;
}
}
- sql配置文件
<?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="cn.com.dao.TestUserMapper" >
<resultMap id="BaseResultMap" type="cn.com.entity.TestUser" >
<id column="ID" property="id" jdbcType="INTEGER" />
<result column="USER_NAME" property="userName" jdbcType="VARCHAR" />
<result column="PASSWORD" property="password" jdbcType="VARCHAR" />
<result column="ADDRESS" property="address" jdbcType="VARCHAR" />
<result column="PHONE" property="phone" jdbcType="VARCHAR" />
</resultMap>
<sql id="Base_Column_List" >
ID, USER_NAME, PASSWORD, ADDRESS, PHONE
</sql>
<select id="selectByPrimaryKey" resultMap="BaseResultMap" parameterType="java.lang.Integer" >
select
<include refid="Base_Column_List" />
from test_user
where id = #{id,jdbcType=INTEGER}
</select>
<select id="selectByUserName" resultMap="BaseResultMap" parameterType="java.lang.String" >
select
<include refid="Base_Column_List" />
from test_user
where USER_NAME = #{userName,jdbcType=VARCHAR}
</select>
</mapper>
- dao接口
package cn.com.dao;
import cn.com.entity.TestUser;
public interface TestUserMapper {
TestUser selectByUserName(String userName);
}
- service服务层
package cn.com.service;
import java.util.Map;
/**
* 用户管理服务类
* @author zhangheng
*@time 2018年11月7日 上午11:57:29
*/
public interface UserService {
/**
* 查询用户
* @param userName 用户名
* @return
*/
Map<String, Object> loadUser(String userName,String password);
}
package cn.com.service.imp;
import java.util.HashMap;
import java.util.Map;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import cn.com.dao.TestUserMapper;
import cn.com.entity.Constant;
import cn.com.entity.TestUser;
import cn.com.service.UserService;
@Service
public class UserServiceImp implements UserService{
@Autowired
private TestUserMapper userDao;
@Override
public Map<String, Object> loadUser(String userName, String password) {
Map<String,Object> data=new HashMap<String,Object>();
//查询用户是否存在
TestUser user = userDao.selectByUserName(userName);
//如果user为空帐号不存在
if(user==null){
data.put("code", Constant.LOGIN_NAME_ERROR);
return data;
} else {
// 取出输入密码比较
if (user.getPassword().equals(password)) {
data.put("code", Constant.LOGIN_OK);
data.put("userName",user.getUserName());
data.put("phone", user.getPhone());
data.put("address", user.getAddress());
return data;
} else {
data.put("code", Constant.LOGIN_PASSWORD_ERROR);
return data;
}
}
}
}
- controller层
package cn.com.controller;
import java.util.Enumeration;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.lang.RandomStringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import cn.com.entity.Constant;
import cn.com.entity.Result;
import cn.com.service.UserService;
import cn.com.util.TimeUtil;
import net.sf.json.JSONObject;
@RestController
public class UserController {
@Autowired
private UserService userService;
private static final Logger Logger = LoggerFactory.getLogger(UserController.class);
/** 登陆接口 */
@RequestMapping(value="/login",method=RequestMethod.POST,consumes="application/x-www-form-urlencoded",produces="application/json;charset=UTF-8")
public Result login(HttpServletRequest request, HttpServletResponse response){
response.setContentType("utf-8");
response.setCharacterEncoding("utf-8");
String userName="";
String password="";
Enumeration<String> params = request.getParameterNames();
while (params.hasMoreElements()){
String paramName = (String)params.nextElement();
if ("userName".equals(paramName)) {
userName = request.getParameter(paramName);
}else if("password".equals(paramName)){
password = request.getParameter(paramName);
}
}
String requestId = RandomStringUtils.randomAlphanumeric(32);
String currentTime = TimeUtil.getCurrentTime();
JSONObject json = new JSONObject();
json.put("currentTime", currentTime);
json.put("userName", userName);
json.put("password", password);
json.put("requestId", requestId);
Logger.info("登陆请求入参"+json);
try{
//调用userService登录方法验证
Map<String, Object> data = userService.loadUser(userName, password);
Result result=new Result();
int res=(Integer)data.get("code");
//成功返回的data数据集合
//判断登录结果
if(res==Constant.LOGIN_OK){
result.setCode(Constant.LOGIN_OK);
result.setMessage(Constant.LOGIN_OK_MESSAGE);
result.setData(data);
}
else if(res==Constant.LOGIN_NAME_ERROR){
result.setCode(Constant.LOGIN_NAME_ERROR);
result.setMessage(Constant.LOGIN_NAME_ERROR_MESSAGE);
result.setData("");
}else if(res==Constant.LOGIN_PASSWORD_ERROR){
result.setCode(Constant.LOGIN_PASSWORD_ERROR);
result.setMessage(Constant.LOGIN_PASSWORD_ERROR_MESSAGE);
result.setData("");
}
JSONObject rjson = new JSONObject();
String returnTime = TimeUtil.getCurrentTime();
rjson.put("requestId",requestId);
rjson.put("returnTime", returnTime);
rjson.put("result", result);
Logger.info("登陆请求出参"+rjson);
return result;
}catch(Exception e){
Result result=new Result();
result.setCode(Constant.ERROR);
result.setMessage(Constant.ERROR_MESSAGE);
result.setData("");
return result;
}
}
}
- 这个测试还用到了两个自定义封装类 Result、Constant
package cn.com.entity;
public class Constant {
public static final int LOGIN_OK=2000;
public static final int LOGIN_ERROR=4000;
public static final String LOGIN_OK_MESSAGE="登陆成功";
public static final int LOGIN_NAME_ERROR=2001;
public static final String LOGIN_NAME_ERROR_MESSAGE="账号无效";
public static final int LOGIN_PASSWORD_ERROR=2002;
public static final String LOGIN_PASSWORD_ERROR_MESSAGE="密码错误";
public static final int ERROR=5000;
public static final String ERROR_MESSAGE="联系管理员";
}
package cn.com.entity;
import java.io.Serializable;
public class Result implements Serializable{
/**
*
*/
private static final long serialVersionUID = 1L;
private int code;
private String message;
private Object data;
public int getCode() {
return code;
}
public void setCode(int code) {
this.code = code;
}
public String getMessage() {
return message;
}
public void setMessage(String message) {
this.message = message;
}
public Object getData() {
return data;
}
public void setData(Object data) {
this.data = data;
}
public static long getSerialversionuid() {
return serialVersionUID;
}
}
四、登录测试
部署到tomcat服务器启动项目测试(测试有很多种方法,自己写的测试方法,还有一些工具如Postman等,最简单是将方法写成get请求,直接在浏览器请求)