Skip to content

Commit 443442f

Browse files
author
quding
committed
fix ssm
1 parent e1cfb12 commit 443442f

File tree

15 files changed

+133
-156
lines changed

15 files changed

+133
-156
lines changed

SSM-Demo/pom.xml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -186,6 +186,13 @@
186186
<artifactId>jcl-over-slf4j</artifactId>
187187
<version>${slf4j.jcl.version}</version>
188188
</dependency>
189+
<dependency>
190+
<groupId>javax.servlet</groupId>
191+
<artifactId>jstl</artifactId>
192+
<version>1.2</version>
193+
<scope>runtime</scope>
194+
</dependency>
195+
189196
</dependencies>
190197

191198
<build>
Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package cn.mrdear.controller;
22

3-
import org.springframework.web.bind.annotation.RequestMapping;
4-
import org.springframework.web.bind.annotation.ResponseBody;
3+
import org.springframework.web.bind.annotation.GetMapping;
54
import org.springframework.web.bind.annotation.RestController;
65

76
import java.util.List;
@@ -13,17 +12,16 @@
1312

1413
/**
1514
* @author Niu Li
16-
* @date 2016/9/22
15+
* @since 2016/9/22
1716
*/
1817
@RestController
1918
public class BookController {
19+
2020
@Resource
2121
private BookService bookService;
2222

23-
@RequestMapping(value = "/books")
24-
@ResponseBody
23+
@GetMapping("/books")
2524
public List<Book> books(){
26-
List<Book> books = bookService.findAll();
27-
return books;
25+
return bookService.findAll();
2826
}
2927
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package cn.mrdear.controller;
2+
3+
import org.springframework.stereotype.Controller;
4+
import org.springframework.web.bind.annotation.GetMapping;
5+
6+
/**
7+
* @author Niu Li
8+
* @since 2017/4/19
9+
*/
10+
@Controller
11+
public class ErrorController {
12+
13+
@GetMapping("/error")
14+
public String error(){
15+
return "error";
16+
}
17+
}

SSM-Demo/src/main/java/cn/mrdear/entity/Appointment.java

Lines changed: 0 additions & 31 deletions
This file was deleted.

SSM-Demo/src/main/java/cn/mrdear/entity/AppointmentKey.java

Lines changed: 0 additions & 57 deletions
This file was deleted.
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
package cn.mrdear.exception;
2+
3+
import com.alibaba.fastjson.JSONObject;
4+
5+
import org.apache.commons.lang3.StringUtils;
6+
import org.slf4j.Logger;
7+
import org.slf4j.LoggerFactory;
8+
import org.springframework.http.HttpStatus;
9+
import org.springframework.web.bind.annotation.ControllerAdvice;
10+
import org.springframework.web.bind.annotation.ExceptionHandler;
11+
import org.springframework.web.bind.annotation.ResponseBody;
12+
import org.springframework.web.bind.annotation.ResponseStatus;
13+
14+
import java.io.IOException;
15+
16+
import javax.servlet.ServletException;
17+
import javax.servlet.http.HttpServletRequest;
18+
import javax.servlet.http.HttpServletResponse;
19+
20+
/**
21+
* 全局异常配置
22+
* @author Niu Li
23+
* @since 2017/4/19
24+
*/
25+
@ControllerAdvice
26+
public class GlobalException {
27+
28+
private static Logger logger = LoggerFactory.getLogger(GlobalException.class);
29+
/**
30+
* 全局异常处理
31+
*
32+
* @param request 请求
33+
* @param response 返回
34+
* @param ex 异常
35+
*/
36+
@ExceptionHandler(value = Exception.class)
37+
@ResponseStatus(HttpStatus.OK)//错误码可以自己定义
38+
@ResponseBody
39+
public Object exceptionHandler(HttpServletRequest request, HttpServletResponse response, Exception ex) {
40+
logger.error("ExceptionHander catch error: ", ex);
41+
//判断是否为ajax请求
42+
String xRequested = request.getHeader("x-requested-with");
43+
if (StringUtils.equalsIgnoreCase(xRequested,"XMLHttpRequest")){
44+
return handlerAjax(ex);
45+
}
46+
47+
//重定向到错误页面,错误页面需要自己定义
48+
request.setAttribute("errorMessage",ex.getMessage());
49+
redirect("/error", HttpStatus.NOT_FOUND,request,response);
50+
return null;
51+
}
52+
53+
/**
54+
* 处理ajax异常
55+
* @param ex 异常
56+
* @return json异常信息
57+
*/
58+
private JSONObject handlerAjax(Exception ex){
59+
JSONObject resultVO = new JSONObject();
60+
resultVO.put("status",0);
61+
resultVO.put("msg",ex.getMessage());
62+
return resultVO;
63+
}
64+
65+
/**
66+
* 重定向到错误页面
67+
* @param url 链接
68+
*/
69+
private void redirect(String url,HttpStatus status,HttpServletRequest request,
70+
HttpServletResponse response){
71+
try {
72+
response.setStatus(status.value());
73+
request.getRequestDispatcher(url).forward(request,response);
74+
} catch (IOException | ServletException e) {
75+
logger.error("redirect fail,e:{}",e);
76+
}
77+
}
78+
}

SSM-Demo/src/main/java/cn/mrdear/mapper/AppointmentMapper.java

Lines changed: 0 additions & 8 deletions
This file was deleted.

SSM-Demo/src/main/java/cn/mrdear/service/BookServiceImpl.java renamed to SSM-Demo/src/main/java/cn/mrdear/service/impl/BookServiceImpl.java

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package cn.mrdear.service;
1+
package cn.mrdear.service.impl;
22

33
import org.springframework.stereotype.Service;
44

@@ -8,13 +8,15 @@
88

99
import cn.mrdear.entity.Book;
1010
import cn.mrdear.mapper.BookMapper;
11+
import cn.mrdear.service.BookService;
1112

1213
/**
1314
* @author Niu Li
14-
* @date 2017/1/23
15+
* @since 2017/1/23
1516
*/
1617
@Service
17-
public class BookServiceImpl implements BookService{
18+
public class BookServiceImpl implements BookService {
19+
1820
@Resource
1921
private BookMapper bookMapper;
2022

SSM-Demo/src/main/resources/db.properties

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
jdbc.driver=com.mysql.jdbc.Driver
2-
jdbc.url=jdbc:mysql://localhost:3306/ssm?useUnicode=true&characterEncoding=utf8&characterSetResults=utf8
2+
jdbc.url=jdbc:mysql://115.159.185.14:3306/ssm?useUnicode=true&characterEncoding=utf8&characterSetResults=utf8
33
jdbc.username=root
44
jdbc.password=7946521
55

SSM-Demo/src/main/resources/db.sql

Lines changed: 0 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -7,33 +7,11 @@ MySQL - 5.6.24 : Database - ssm
77

88
/*!40101 SET NAMES utf8 */;
99

10-
/*!40101 SET SQL_MODE=''*/;
11-
12-
/*!40014 SET @OLD_UNIQUE_CHECKS=@@UNIQUE_CHECKS, UNIQUE_CHECKS=0 */;
13-
/*!40014 SET @OLD_FOREIGN_KEY_CHECKS=@@FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS=0 */;
14-
/*!40101 SET @OLD_SQL_MODE=@@SQL_MODE, SQL_MODE='NO_AUTO_VALUE_ON_ZERO' */;
15-
/*!40111 SET @OLD_SQL_NOTES=@@SQL_NOTES, SQL_NOTES=0 */;
1610
CREATE DATABASE /*!32312 IF NOT EXISTS*/`ssm` /*!40100 DEFAULT CHARACTER SET utf8 */;
1711

1812
USE `ssm`;
1913

20-
/*Table structure for table `appointment` */
21-
22-
CREATE TABLE `appointment` (
23-
`book_id` bigint(20) NOT NULL COMMENT '图书ID',
24-
`student_id` bigint(20) NOT NULL COMMENT '学号',
25-
`appoint_time` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '预约时间',
26-
PRIMARY KEY (`book_id`,`student_id`),
27-
KEY `idx_appoint_time` (`appoint_time`)
28-
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='预约图书表';
29-
30-
/*Data for the table `appointment` */
31-
32-
LOCK TABLES `appointment` WRITE;
3314

34-
insert into `appointment`(`book_id`,`student_id`,`appoint_time`) values (1000,12345678910,'2017-01-22 10:36:31');
35-
36-
UNLOCK TABLES;
3715

3816
/*Table structure for table `book` */
3917

@@ -51,8 +29,3 @@ LOCK TABLES `book` WRITE;
5129
insert into `book`(`book_id`,`name`,`number`) values (1000,'Java程序设计',10),(1001,'数据结构',10),(1002,'设计模式',10),(1003,'编译原理',10);
5230

5331
UNLOCK TABLES;
54-
55-
/*!40101 SET SQL_MODE=@OLD_SQL_MODE */;
56-
/*!40014 SET FOREIGN_KEY_CHECKS=@OLD_FOREIGN_KEY_CHECKS */;
57-
/*!40014 SET UNIQUE_CHECKS=@OLD_UNIQUE_CHECKS */;
58-
/*!40111 SET SQL_NOTES=@OLD_SQL_NOTES */;

SSM-Demo/src/main/resources/mybatis/mapper/AppointmentMapper.xml

Lines changed: 0 additions & 18 deletions
This file was deleted.

SSM-Demo/src/main/resources/mybatis/mapper/BookMapper.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?xml version="1.0" encoding="UTF-8" ?>
22
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
3-
<mapper namespace="cn.mrdear.mapper.AppointmentMapper.BookMapper" >
3+
<mapper namespace="cn.mrdear.mapper.AppointMentMapper.BookMapper" >
44
<resultMap id="BaseResultMap" type="cn.mrdear.entity.Book" >
55
<!--
66
WARNING - @mbg.generated

SSM-Demo/src/main/resources/spring/applicationContext.xml

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
xmlns:tx="http://www.springframework.org/schema/tx"
66
xmlns:aop="http://www.springframework.org/schema/aop"
77
xmlns:task="http://www.springframework.org/schema/task"
8-
xmlns:p="http://www.springframework.org/schema/p"
98
xmlns:cache="http://www.springframework.org/schema/cache"
109
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task.xsd http://www.springframework.org/schema/cache http://www.springframework.org/schema/cache/spring-cache.xsd">
1110

@@ -103,7 +102,9 @@
103102
rollback-for="java.lang.Exception" />
104103
<tx:method name="save*" propagation="REQUIRED" read-only="false"
105104
rollback-for="java.lang.Exception" />
106-
<tx:method name="select*" propagation="SUPPORTS" read-only="true"/>
105+
<!--查询使用只读事务-->
106+
<tx:method name="find*" propagation="SUPPORTS" read-only="true"/>
107+
<tx:method name="query*" propagation="SUPPORTS" read-only="true"/>
107108
</tx:attributes>
108109
</tx:advice>
109110

SSM-Demo/src/main/resources/spring/dispatcher-servlet.xml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,6 @@
4848

4949
<!--静态资源映射-->
5050
<mvc:resources mapping="/static/**" location="/static/" />
51-
<mvc:resources mapping="/uploadimg/**" location="/uploadimg/" />
5251

5352
<!--拦截器配置-->
5453
<!--<mvc:interceptors>-->
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<%--
2+
Created by IntelliJ IDEA.
3+
User: niuli
4+
Date: 2017/4/19
5+
Time: 09:45
6+
To change this template use File | Settings | File Templates.
7+
--%>
8+
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
9+
<html>
10+
<head>
11+
<title>Title</title>
12+
</head>
13+
<body>
14+
<h1>Error 页面</h1>
15+
</body>
16+
</html>

0 commit comments

Comments
 (0)