最近自己刚学完ssm,今天就框架之间的关系进行一个整合,这也是目前市面上发展的趋势,ssm将会慢慢取代ssh,占领主导地位
图片说明:
在客户端发送请求到来时,服务器会通过控制器controller来调用service层的方法,service层中会通过dao层的数据关系映射去数据库中查找相关信息,然后将数据进行封装成SysLog对象,返回给control层;在control层中进一步对对象信息进行封装,然后通过spring转换成Json格式数据返回给客户端。客户端接受到请求之后会进行解析,将数据呈献给客户,一次请求就完成。
需要添加的依赖
配置文件:spring-config.xml
xmlns="http://www.springframework.org/schema/beans"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:util="http://www.springframework.org/schema/util"
xmlns:jpa="http://www.springframework.org/schema/data/jpa"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.3.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-4.3.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-4.3.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-4.3.xsd
http://www.springframework.org/schema/util
http://www.springframework.org/schema/util/spring-util-4.3.xsd
http://www.springframework.org/schema/data/jpa
http://www.springframework.org/schema/data/jpa/spring-jpa-1.3.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.3.xsd" >
destroy-method="close" init-method="init" lazy-init="true">
classpath:mapper/*.xml
value="com.jt.**.dao"/>
web.xml文件中对DispatcherServlet的配置
CGB-JT-SYS-V1.01
index.html
frontController
org.springframework.web.servlet.DispatcherServlet
contextConfigLocation
classpath:spring-config.xml
1
frontController
*.do
日志信息的封装
package com.jt.sys.entity;
import com.fasterxml.jackson.annotation.JsonFormat;
import java.io.Serializable;
import java.util.Date;public class SysLog implements Serializable {
private static final long serialVersionUID = 1L;
private Integer id;
//用户名
private String username;
//用户操作
private String operation;
//请求方法
private String method;
//请求参数
private String params;
//执行时长(毫秒)
private Long time;
//IP地址
private String ip;
//创建时间
@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
private Date createdTime;/**
* 设置:
*/
public void setId(Integer id) {
this.id = id;
}
/**
* 获取:
*/
public Integer getId() {
return id;
}
/**
* 设置:用户名
*/
public void setUsername(String username) {
this.username = username;
}
/**
* 获取:用户名
*/
public String getUsername() {
return username;
}
/**
* 设置:用户操作
*/
public void setOperation(String operation) {
this.operation = operation;
}
/**
* 获取:用户操作
*/
public String getOperation() {
return operation;
}
/**
* 设置:请求方法
*/
public void setMethod(String method) {
this.method = method;
}
/**
* 获取:请求方法
*/
public String getMethod() {
return method;
}
/**
* 设置:请求参数
*/
public void setParams(String params) {
this.params = params;
}
/**
* 获取:请求参数
*/
public String getParams() {
return params;
}
/**
* 设置:IP地址
*/
public void setIp(String ip) {
this.ip = ip;
}
/**
* 获取:IP地址
*/
public String getIp() {
return ip;
}
/**
* 设置:创建时间
*/
public void setCreateDate(Date createdTime) {
this.createdTime = createdTime;
}
/**
* 获取:创建时间
*/
public Date getCreatedTime() {
return createdTime;
}public Long getTime() {
return time;
}public void setTime(Long time) {
this.time = time;
}
}
controller类的实现
package com.jt.sys.controller;
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.ResponseBody;import com.jt.common.vo.JsonResult;
import com.jt.common.vo.PageObject;
import com.jt.sys.entity.SysLog;
import com.jt.sys.service.SysLogService;@RequestMapping("/log/")
@Controller
public class SysLogController {
@Autowired
private SysLogService sysLogService;@RequestMapping("doFindPageObjects")
@ResponseBody
public JsonResult doFindPageObjects(String name, Integer pageCurrent) {
PageObjectpageObject = sysLogService.findPageObjects(name, pageCurrent);
return new JsonResult(pageObject);
}
}
两个公共类的实现
JsonResult--用来封装返回给客户端的信息
package com.jt.common.vo;
import java.io.Serializable;
public class JsonResult implements Serializable {
private static final long serialVersionUID = -856924038217431339L;//SysResult/Result/R
/**状态码*/
private int state=1;//1表示SUCCESS,0表示ERROR
/**状态信息*/
private String message="ok";
/**正确数据*/
private Object data;
public JsonResult() {}
public JsonResult(String message){
this.message=message;
}
/**一般查询时调用,封装查询结果*/
public JsonResult(Object data) {
this.data=data;
}
/**出现异常时时调用*/
public JsonResult(Throwable t){
this.state=0;
this.message=t.getMessage();
}
public int getState() {
return state;
}
public void setState(int state) {
this.state = state;
}
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;
}
}
PageObject封装分页查询的相关信息
package com.jt.common.vo;
import java.io.Serializable;
import java.util.List;public class PageObject
implements Serializable{
private static final long serialVersionUID = 1L;
/**当前页的页码值*/
private int pageCurrent=1;
/**每一页显示的记录条数*/
private int pageSize=3;
/**数据库中记录性行数*(通过查询获得)*/
private int rowCount=0;
/**总页数(通过计算获得)*/
private int pageCount=0;
/**当前页记录*/
private Listrecords; public int getPageCurrent() {
return pageCurrent;
}public void setPageCurrent(int pageCurrent) {
this.pageCurrent = pageCurrent;
}public int getPageSize() {
return pageSize;
}public void setPageSize(int pageSize) {
this.pageSize = pageSize;
}public int getRowCount() {
return rowCount;
}public void setRowCount(int rowCount) {
this.rowCount = rowCount;
}public int getPageCount() {
return pageCount;
}public void setPageCount(int pageCount) {
this.pageCount = pageCount;
}public List
getRecords() {
return records;
}public void setRecords(List
records) {
this.records = records;
}
}
GlobalExceptionHandler----定义全局异常的处理类,避免用户直接看到后台的不友好界面
package com.jt.common.web;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;import com.jt.common.vo.JsonResult;
@ControllerAdvice
public class GlobalExceptionHandler {
// JDK中的自带的日志API
@ExceptionHandler(RuntimeException.class)
@ResponseBody
public JsonResult doHandleRuntimeException(RuntimeException e) {
e.printStackTrace();// 也可以写日志
return new JsonResult(e);// 封装异常信息
}}
Dao层接口的实现
package com.jt.sys.dao;
import java.util.List;
import org.apache.ibatis.annotations.Param;
import com.jt.sys.entity.SysLog;
/**
* 这个类用来实现日志查询的接口
* @author RYH
*
*/
public interface SysLogDao {
/**
* 基于给定的条件分页查询日志信息
* @param username
* @param startIndex
* @param pageSize
* @return
*/
public ListfindPageObjects(
@Param("username")String username,
@Param("startIndex")Integer startIndex,
@Param("pageSize")Integer pageSize);
/**
* 根据用名查询其总记录数
* @param name
* @return
*/
public int getRowCount(@Param("username")String username);
}
Servce接口的
package com.jt.sys.service;
import org.springframework.stereotype.Service;
import com.jt.common.vo.PageObject;
import com.jt.sys.entity.SysLog;public interface SysLogService {
/**
* 通过此方法实现分页查询操作
* @param name
* @param PageCurrent
* @return
*/
public PageObjectfindPageObjects(
String name,
Integer pageCurrent);
}
ServiceException---自定义的异常类,便于快速定位代码中的异常信息
package com.jt.sys.service.exception;
/***
* 为什么使用自定义异常?
* 更加精确定位具体异常信息
*/public class ServiceException extends RuntimeException {
private static final long serialVersionUID = -1169100027771948958L;
public ServiceException() {
super();
// TODO Auto-generated constructor stub
}public ServiceException(String message, Throwable cause, boolean enableSuppression, boolean writableStackTrace) {
super(message, cause, enableSuppression, writableStackTrace);
// TODO Auto-generated constructor stub
}public ServiceException(String message, Throwable cause) {
super(message, cause);
// TODO Auto-generated constructor stub
}public ServiceException(String message) {
super(message);
// TODO Auto-generated constructor stub
}public ServiceException(Throwable cause) {
super(cause);
// TODO Auto-generated constructor stub
}
}
SysLogServiceImpl---service接口的实现类
package com.jt.sys.service.impl;
import java.rmi.ServerException;
import java.util.List;import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;import com.jt.common.vo.PageObject;
import com.jt.sys.dao.SysLogDao;
import com.jt.sys.entity.SysLog;
import com.jt.sys.service.SysLogService;
import com.jt.sys.service.exception.ServiceException;@Service("SysLogService")
public class SysLogServiceImpl implements SysLogService {@Autowired
private SysLogDao sysLogDao;
@Override
public PageObjectfindPageObjects(String name, Integer pageCurrent) {
//验证参数合法性
if(pageCurrent==null||pageCurrent<1){
throw new IllegalArgumentException("当前页码不正确");
}
//基于条件查询总记录数
int rowCount=sysLogDao.getRowCount(name);
if(rowCount==0){
throw new ServiceException("系统没有找到相应的记录");
}
//基于条件查询当前页记录
int pageSize=2;
int startIndex=(pageCurrent-1)*pageSize;
Listrecords=sysLogDao.findPageObjects(name, startIndex, pageSize);
//对分页信息以及当前页记录进行封装
PageObject< SysLog> pageObject=new PageObject<>();
pageObject.setPageCurrent(pageCurrent);
pageObject.setPageSize(pageSize);
pageObject.setRowCount(rowCount);
pageObject.setRecords(records);
pageObject.setPageCount((rowCount-1)*pageSize+1);
return pageObject;
}}
SysLogMapper.xml-----------------mybatis中用来书写sql语句的与dao层对应的接口映射文件
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
username like concat('%',#{username},'%')
还需要给定一个properties文件用来给定druid中所需的数据库的配置信息
jdbcDriver=com.mysql.jdbc.Driver
jdbcUrl=jdbc:mysql:///jtsys
jdbcUser=root
jdbcPassword=123456
项目发布,访问即可看到书记库中的相关信息,结果是以Json格式字符串显示在页面。至此,后端的功能已经实现,剩下的具有前端进行Json的解析就可以