├── .gitignore ├── BookSystem_V0 ├── pom.xml └── src │ ├── main │ ├── java │ │ └── com │ │ │ └── hisen │ │ │ ├── dao │ │ │ └── BookDao.java │ │ │ ├── entity │ │ │ └── Book.java │ │ │ ├── service │ │ │ ├── BookService.java │ │ │ └── impl │ │ │ │ └── BookServiceImpl.java │ │ │ └── web │ │ │ └── BookController.java │ ├── resources │ │ ├── jdbc.properties │ │ ├── logback.xml │ │ ├── mapper │ │ │ └── BookMapper.xml │ │ ├── mybatis-config.xml │ │ └── spring │ │ │ ├── spring-dao.xml │ │ │ ├── spring-service.xml │ │ │ └── spring-web.xml │ └── webapp │ │ ├── WEB-INF │ │ ├── jsp │ │ │ ├── detail.jsp │ │ │ └── list.jsp │ │ └── web.xml │ │ └── index.jsp │ └── test │ └── com │ └── hisen │ └── test │ ├── BaseTest.java │ ├── BookDaoTest.java │ └── BookServiceImplTest.java ├── BookSystem_V1 ├── pom.xml └── src │ ├── main │ ├── java │ │ └── com │ │ │ └── hisen │ │ │ ├── dao │ │ │ ├── BookDao.java │ │ │ ├── RedisCache.java │ │ │ └── RedisCacheTransfer.java │ │ │ ├── entity │ │ │ └── Book.java │ │ │ ├── service │ │ │ ├── BookService.java │ │ │ └── impl │ │ │ │ └── BookServiceImpl.java │ │ │ └── web │ │ │ └── BookController.java │ ├── resources │ │ ├── jdbc.properties │ │ ├── logback.xml │ │ ├── mapper │ │ │ └── BookMapper.xml │ │ ├── mybatis-config.xml │ │ ├── redis.properties │ │ └── spring │ │ │ ├── spring-dao.xml │ │ │ ├── spring-service.xml │ │ │ └── spring-web.xml │ └── webapp │ │ ├── WEB-INF │ │ ├── jsp │ │ │ ├── detail.jsp │ │ │ └── list.jsp │ │ └── web.xml │ │ ├── add.jsp │ │ └── index.jsp │ └── test │ └── com │ └── hisen │ └── test │ ├── BaseTest.java │ ├── BookDaoTest.java │ ├── BookServiceImplTest.java │ └── MyBatisCacheSecondTest.java ├── BookSystem_V2 ├── README.md ├── pom.xml └── src │ ├── main │ ├── java │ │ └── com │ │ │ └── hisen │ │ │ ├── aop │ │ │ ├── GetMethodInfoHandler.java │ │ │ └── TimeHandler.java │ │ │ ├── dao │ │ │ ├── AppointmentMapper.java │ │ │ ├── BookDao.java │ │ │ ├── RedisCache.java │ │ │ ├── RedisCacheTransfer.java │ │ │ ├── UserMapper.java │ │ │ ├── form │ │ │ │ └── AppointmentForm.java │ │ │ └── sql │ │ │ │ └── booksystem.sql │ │ │ ├── entity │ │ │ ├── Appointment.java │ │ │ ├── AppointmentExample.java │ │ │ ├── Book.java │ │ │ ├── User.java │ │ │ ├── UserExample.java │ │ │ └── UserKey.java │ │ │ ├── service │ │ │ ├── AppointmentService.java │ │ │ ├── BookService.java │ │ │ └── impl │ │ │ │ ├── AppointmentServiceImpl.java │ │ │ │ └── BookServiceImpl.java │ │ │ └── web │ │ │ ├── AppointmengtController.java │ │ │ └── BookController.java │ ├── resources │ │ ├── generatorConfig.xml │ │ ├── jdbc.properties │ │ ├── logback.xml │ │ ├── mapper │ │ │ ├── AppointmentMapper.xml │ │ │ ├── BookMapper.xml │ │ │ └── UserMapper.xml │ │ ├── mybatis-config.xml │ │ ├── redis.properties │ │ └── spring │ │ │ ├── spring-aop.xml │ │ │ ├── spring-dao.xml │ │ │ ├── spring-service.xml │ │ │ └── spring-web.xml │ └── webapp │ │ ├── WEB-INF │ │ ├── jsp │ │ │ ├── detail.jsp │ │ │ └── list.jsp │ │ ├── templates │ │ │ ├── booklist.html │ │ │ ├── detail.html │ │ │ ├── footer.html │ │ │ ├── header.html │ │ │ ├── list.html │ │ │ ├── page.html │ │ │ └── readingList.html │ │ └── web.xml │ │ ├── add.html │ │ ├── index.html │ │ └── static │ │ └── js │ │ ├── jquery-3.1.1.min.js │ │ └── jquery.paginate.js │ └── test │ ├── AppointmentServiceImplTest.java │ ├── BaseTest.java │ └── UserDaoTest.java ├── BookSystem_V3 ├── README.md ├── pom.xml └── src │ ├── main │ ├── java │ │ └── com │ │ │ └── hisen │ │ │ ├── aop │ │ │ ├── GetMethodInfoHandler.java │ │ │ └── TimeHandler.java │ │ │ ├── bean │ │ │ ├── contains │ │ │ │ └── CommonEnum.java │ │ │ ├── dto │ │ │ │ └── UserInfoLoginDto.java │ │ │ ├── entity │ │ │ │ ├── Appointment.java │ │ │ │ ├── AppointmentExample.java │ │ │ │ ├── Book.java │ │ │ │ ├── JWTInfo.java │ │ │ │ ├── UserInfo.java │ │ │ │ └── UserInfoExample.java │ │ │ ├── request │ │ │ │ ├── AppointmentRequest.java │ │ │ │ ├── CommonRequest.java │ │ │ │ └── UserLoginRequest.java │ │ │ └── response │ │ │ │ ├── CommonResponse.java │ │ │ │ └── UserLoginResponse.java │ │ │ ├── common │ │ │ ├── CookieUtil.java │ │ │ ├── JWTUtil.java │ │ │ └── MD5Util.java │ │ │ ├── controller │ │ │ ├── AppointmengtController.java │ │ │ ├── BookController.java │ │ │ └── UserInfoController.java │ │ │ ├── dao │ │ │ ├── AppointmentMapper.java │ │ │ ├── BookDao.java │ │ │ ├── RedisCache.java │ │ │ ├── RedisCacheTransfer.java │ │ │ ├── UserInfoMapper.java │ │ │ └── sql │ │ │ │ └── booksystem.sql │ │ │ ├── filter │ │ │ ├── CorsFilter.java │ │ │ └── JWTCheckInterceptor.java │ │ │ └── service │ │ │ ├── AppointmentService.java │ │ │ ├── BookService.java │ │ │ ├── UserInfoService.java │ │ │ └── impl │ │ │ ├── AppointmentServiceImpl.java │ │ │ ├── BookServiceImpl.java │ │ │ └── UserInfoServiceImpl.java │ ├── resources │ │ ├── generatorConfig.xml │ │ ├── jdbc.properties │ │ ├── logback.xml │ │ ├── mapper │ │ │ ├── AppointmentMapper.xml │ │ │ ├── BookMapper.xml │ │ │ └── UserInfoMapper.xml │ │ ├── mybatis-config.xml │ │ ├── redis.properties │ │ └── spring │ │ │ ├── spring-aop.xml │ │ │ ├── spring-dao.xml │ │ │ ├── spring-service.xml │ │ │ └── spring-web.xml │ └── webapp │ │ ├── WEB-INF │ │ └── web.xml │ │ ├── front │ │ ├── html │ │ │ ├── bookList.html │ │ │ ├── login.html │ │ │ └── register.html │ │ └── js │ │ │ ├── bookList.js │ │ │ ├── demo.js │ │ │ ├── login.js │ │ │ └── register.js │ │ └── index.jsp │ └── test │ ├── AppointmentServiceImplTest.java │ └── BaseTest.java └── README.md /.gitignore: -------------------------------------------------------------------------------- 1 | .idea/ 2 | target/ 3 | 4 | 5 | *.iml 6 | *.bak 7 | *.exe 8 | *.doc 9 | *.docx 10 | *.xls 11 | *.xlsx 12 | *.iso 13 | *.log 14 | 15 | 16 | # Compiled class file 17 | *.class 18 | 19 | # Log file 20 | *.log 21 | log/ 22 | logs/ 23 | 24 | 25 | # BlueJ files 26 | *.ctxt 27 | 28 | # Mobile Tools for Java (J2ME) 29 | .mtj.tmp/ 30 | 31 | # Package Files # 32 | *.jar 33 | *.war 34 | *.nar 35 | *.ear 36 | *.zip 37 | *.tar.gz 38 | *.rar 39 | 40 | # virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml 41 | hs_err_pid* -------------------------------------------------------------------------------- /BookSystem_V0/pom.xml: -------------------------------------------------------------------------------- 1 | 3 | 4.0.0 4 | com.hisen 5 | BookSystem_V0 6 | war 7 | 1.0-SNAPSHOT 8 | BookSystem_V0 Maven Webapp 9 | http://maven.apache.org 10 | 11 | 12 | 13 | junit 14 | junit 15 | 4.11 16 | 17 | 18 | 19 | 20 | ch.qos.logback 21 | logback-classic 22 | 1.1.1 23 | 24 | 25 | 26 | mysql 27 | mysql-connector-java 28 | 5.1.37 29 | runtime 30 | 31 | 32 | c3p0 33 | c3p0 34 | 0.9.1.2 35 | 36 | 37 | 38 | org.mybatis 39 | mybatis 40 | 3.3.0 41 | 42 | 43 | org.mybatis 44 | mybatis-spring 45 | 1.2.3 46 | 47 | 48 | 49 | taglibs 50 | standard 51 | 1.1.2 52 | 53 | 54 | jstl 55 | jstl 56 | 1.2 57 | 58 | 59 | com.fasterxml.jackson.core 60 | jackson-databind 61 | 2.5.4 62 | 63 | 64 | javax.servlet 65 | javax.servlet-api 66 | 3.1.0 67 | 68 | 69 | 70 | 71 | org.springframework 72 | spring-core 73 | 4.1.7.RELEASE 74 | 75 | 76 | org.springframework 77 | spring-beans 78 | 4.1.7.RELEASE 79 | 80 | 81 | org.springframework 82 | spring-context 83 | 4.1.7.RELEASE 84 | 85 | 86 | 87 | org.springframework 88 | spring-jdbc 89 | 4.1.7.RELEASE 90 | 91 | 92 | org.springframework 93 | spring-tx 94 | 4.1.7.RELEASE 95 | 96 | 97 | 98 | org.springframework 99 | spring-web 100 | 4.1.7.RELEASE 101 | 102 | 103 | org.springframework 104 | spring-webmvc 105 | 4.1.7.RELEASE 106 | 107 | 108 | org.springframework 109 | spring-test 110 | 4.1.7.RELEASE 111 | 112 | 113 | 114 | BookSystem_V0 115 | 116 | 117 | org.apache.maven.plugins 118 | maven-compiler-plugin 119 | 120 | 1.8 121 | 1.8 122 | 123 | 124 | 125 | 126 | 127 | -------------------------------------------------------------------------------- /BookSystem_V0/src/main/java/com/hisen/dao/BookDao.java: -------------------------------------------------------------------------------- 1 | package com.hisen.dao; 2 | 3 | import com.hisen.entity.Book; 4 | import java.util.List; 5 | import org.apache.ibatis.annotations.Param; 6 | 7 | /** 8 | * Created by hisen on 17-4-24. 9 | */ 10 | public interface BookDao { 11 | Book queryById(long id); 12 | List queryAll(@Param("offset") int offset, @Param("limit") int limit); 13 | int addBook(Book book); 14 | int updateBook(Book book); 15 | int deleteBookById(long id); 16 | } 17 | -------------------------------------------------------------------------------- /BookSystem_V0/src/main/java/com/hisen/entity/Book.java: -------------------------------------------------------------------------------- 1 | package com.hisen.entity; 2 | 3 | /** 4 | * Created by hisen on 17-4-24. 5 | */ 6 | public class Book { 7 | private long bookId; 8 | private String name; 9 | private int number; 10 | private String detail; 11 | 12 | @Override 13 | public String toString() { 14 | return "Book{" + 15 | "bookId=" + bookId + 16 | ", name='" + name + '\'' + 17 | ", number=" + number + 18 | ", detail='" + detail + '\'' + 19 | '}'; 20 | } 21 | 22 | public long getBookId() { 23 | return bookId; 24 | } 25 | 26 | public void setBookId(long bookId) { 27 | this.bookId = bookId; 28 | } 29 | 30 | public String getName() { 31 | return name; 32 | } 33 | 34 | public void setName(String name) { 35 | this.name = name; 36 | } 37 | 38 | public int getNumber() { 39 | return number; 40 | } 41 | 42 | public void setNumber(int number) { 43 | this.number = number; 44 | } 45 | 46 | public String getDetail() { 47 | return detail; 48 | } 49 | 50 | public void setDetail(String detail) { 51 | this.detail = detail; 52 | } 53 | } 54 | -------------------------------------------------------------------------------- /BookSystem_V0/src/main/java/com/hisen/service/BookService.java: -------------------------------------------------------------------------------- 1 | package com.hisen.service; 2 | 3 | import com.hisen.entity.Book; 4 | import java.util.List; 5 | 6 | /** 7 | * Created by hisen on 17-4-24. 8 | */ 9 | public interface BookService { 10 | Book getById(long bookId); 11 | List getList(int start, int pageNum); 12 | int addBook(Book book); 13 | int updateBook(Book book); 14 | int deleteBookById(long id); 15 | } 16 | -------------------------------------------------------------------------------- /BookSystem_V0/src/main/java/com/hisen/service/impl/BookServiceImpl.java: -------------------------------------------------------------------------------- 1 | package com.hisen.service.impl; 2 | 3 | import com.hisen.dao.BookDao; 4 | import com.hisen.entity.Book; 5 | import com.hisen.service.BookService; 6 | import java.util.List; 7 | import org.springframework.beans.factory.annotation.Autowired; 8 | import org.springframework.stereotype.Service; 9 | 10 | /** 11 | * Created by hisen on 17-4-24. 12 | */ 13 | @Service 14 | public class BookServiceImpl implements BookService { 15 | @Autowired 16 | private BookDao bookDao; 17 | 18 | @Override 19 | public Book getById(long bookId) { 20 | return bookDao.queryById(bookId); 21 | } 22 | 23 | @Override 24 | public List getList(int start, int pageNum) { 25 | return bookDao.queryAll(start, pageNum); 26 | } 27 | 28 | @Override 29 | public int addBook(Book book) { 30 | return bookDao.addBook(book); 31 | } 32 | 33 | @Override 34 | public int updateBook(Book book) { 35 | return bookDao.updateBook(book); 36 | } 37 | 38 | @Override 39 | public int deleteBookById(long id) { 40 | return bookDao.deleteBookById(id); 41 | } 42 | } 43 | -------------------------------------------------------------------------------- /BookSystem_V0/src/main/java/com/hisen/web/BookController.java: -------------------------------------------------------------------------------- 1 | package com.hisen.web; 2 | 3 | import com.hisen.entity.Book; 4 | import com.hisen.service.BookService; 5 | 6 | import java.util.List; 7 | 8 | import org.slf4j.Logger; 9 | import org.slf4j.LoggerFactory; 10 | import org.springframework.beans.factory.annotation.Autowired; 11 | import org.springframework.stereotype.Controller; 12 | import org.springframework.ui.Model; 13 | import org.springframework.web.bind.annotation.PathVariable; 14 | import org.springframework.web.bind.annotation.RequestMapping; 15 | import org.springframework.web.bind.annotation.RequestMethod; 16 | import org.springframework.web.bind.annotation.ResponseBody; 17 | 18 | /** 19 | * Created by hisen on 17-4-24. 20 | */ 21 | @Controller 22 | @RequestMapping("/book") 23 | public class BookController { 24 | private Logger logger = LoggerFactory.getLogger(this.getClass()); 25 | 26 | @Autowired 27 | private BookService bookService; 28 | 29 | @RequestMapping(value = "/list", method = RequestMethod.GET) 30 | private String list(Model model) { 31 | List list = bookService.getList(0, 1000); 32 | model.addAttribute("list", list); 33 | // WEB-INF/jsp/"list".jsp 返回的list映射到此目录下的jsp文件 34 | return "list"; 35 | } 36 | 37 | @RequestMapping(value = "/detail/{bookId}", method = RequestMethod.GET) 38 | private String detail(@PathVariable("bookId") Long bookId, Model model) { 39 | Book book = bookService.getById(bookId); 40 | model.addAttribute("book", book); 41 | return "detail"; 42 | } 43 | 44 | @RequestMapping(value = "/add", method = RequestMethod.POST, produces = "text/plain;charset=UTF-8") 45 | @ResponseBody 46 | private String add(Book book) { 47 | logger.info("add req: " + book.toString()); 48 | Book hasBook = bookService.getById(book.getBookId()); 49 | int i = -2; 50 | if (hasBook == null) { 51 | i = bookService.addBook(book); 52 | } 53 | String res = i > 0 ? "success" : "error"; 54 | logger.info("add res: " + res); 55 | return res; 56 | } 57 | 58 | @RequestMapping(value = "/del/{bookId}", method = RequestMethod.GET) 59 | @ResponseBody 60 | private String deleteBookById(@PathVariable("bookId") Long id) { 61 | int i = bookService.deleteBookById(id); 62 | return i > 0 ? "success" : "error"; 63 | } 64 | } 65 | -------------------------------------------------------------------------------- /BookSystem_V0/src/main/resources/jdbc.properties: -------------------------------------------------------------------------------- 1 | jdbc.driver=com.mysql.jdbc.Driver 2 | jdbc.url=jdbc:mysql://127.0.0.1:3306/booksystem?useUnicode=true&characterEncoding=utf8 3 | jdbc.username=root 4 | jdbc.password=hisen -------------------------------------------------------------------------------- /BookSystem_V0/src/main/resources/logback.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | %d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n 7 | 8 | 9 | 10 | 11 | 12 | 13 | -------------------------------------------------------------------------------- /BookSystem_V0/src/main/resources/mapper/BookMapper.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 6 | 7 | 8 | 9 | SELECT 10 | book_id, 11 | name, 12 | number, 13 | detail 14 | FROM 15 | book 16 | WHERE 17 | book_id = #{bookId} 18 | 19 | 20 | 21 | SELECT 22 | book_id, 23 | name, 24 | number, 25 | detail 26 | FROM 27 | book 28 | ORDER BY 29 | book_id 30 | LIMIT #{offset}, #{limit} 31 | 32 | 33 | 34 | UPDATE book 35 | SET NAME = #{name}, 36 | NUMBER = #{number}, 37 | detail = #{detail} 38 | WHERE 39 | book_id = #{bookId} 40 | AND number > 0 41 | 42 | 43 | 44 | INSERT INTO book(`book_id`, `name`, `number`,`detail`) 45 | VALUES(#{bookId}, #{name}, #{number}, #{detail}) 46 | 47 | 48 | 49 | DELETE FROM book WHERE book_id = #{bookId} 50 | 51 | -------------------------------------------------------------------------------- /BookSystem_V0/src/main/resources/mybatis-config.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | -------------------------------------------------------------------------------- /BookSystem_V0/src/main/resources/spring/spring-dao.xml: -------------------------------------------------------------------------------- 1 | 2 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | 45 | 46 | 47 | 48 | 49 | 50 | -------------------------------------------------------------------------------- /BookSystem_V0/src/main/resources/spring/spring-service.xml: -------------------------------------------------------------------------------- 1 | 2 | 12 | 13 | 14 | 15 | 16 | 17 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | -------------------------------------------------------------------------------- /BookSystem_V0/src/main/resources/spring/spring-web.xml: -------------------------------------------------------------------------------- 1 | 2 | 12 | 13 | 14 | 18 | 19 | 20 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | -------------------------------------------------------------------------------- /BookSystem_V0/src/main/webapp/WEB-INF/jsp/detail.jsp: -------------------------------------------------------------------------------- 1 | <%-- 2 | Created by IntelliJ IDEA. 3 | User: Administrator 4 | Date: 2017/4/6 5 | Time: 20:21 6 | To change this template use File | Settings | File Templates. 7 | --%> 8 | <%@ page contentType="text/html;charset=UTF-8" language="java" %> 9 | <% String appPath = request.getContextPath(); %> 10 | 11 | 12 | 图书详情 13 | 14 | 15 | 16 | 17 | 18 | 19 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | HiSEN 图书管理系统 - by ssm基础框架 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 首页 39 | 图书具体信息 40 | 信息 41 | 42 | 43 | 44 | 45 | 46 | 47 | 48 | 49 | 图书详情 显示当前图书详细信息 50 | 51 | 52 | 53 | 54 | 55 | 56 | 57 | 58 | 59 | 图书编号 60 | 图书名字 61 | 图书数量 62 | 63 | 64 | 65 | 66 | ${requestScope.get('book').bookId} 67 | ${requestScope.get('book').name} 68 | ${requestScope.get('book').number} 69 | 70 | 71 | 72 | 73 | 74 | 75 | 76 | 77 | 《${requestScope.get('book').name}》 ${requestScope.get('book').detail} 78 | 79 | 80 | 81 | 82 | 83 | 84 | 85 | 86 | 87 | 88 | -------------------------------------------------------------------------------- /BookSystem_V0/src/main/webapp/WEB-INF/jsp/list.jsp: -------------------------------------------------------------------------------- 1 | <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> 2 | <%-- 3 | Created by IntelliJ IDEA. 4 | User: Administrator 5 | Date: 2017/4/6 6 | Time: 20:10 7 | note: 使用bootstrap输出后台返回的requestScope对象 8 | --%> 9 | <%@ page contentType="text/html;charset=UTF-8" language="java" %> 10 | <% String appPath = request.getContextPath(); %> 11 | 12 | 13 | 图书列表 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | HiSEN 图书管理系统 - by ssm基础框架 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 首页 33 | 图书具体信息 34 | 添加图书信息 35 | 信息 36 | 37 | 38 | 39 | 40 | 41 | 42 | 图书列表 显示当前图书库存信息 43 | 44 | 45 | 46 | 47 | 48 | 49 | 50 | 51 | 图书编号 52 | 图书名字 53 | 图书数量 54 | 操作 55 | 56 | 57 | 58 | 59 | 60 | ${book.bookId} 61 | ${book.name} 62 | ${book.number} 63 | 64 | 详情 | 65 | 删除 66 | 67 | 68 | 69 | 70 | 71 | 72 | 73 | 74 | 75 | 76 | 77 | 78 | 79 | -------------------------------------------------------------------------------- /BookSystem_V0/src/main/webapp/WEB-INF/web.xml: -------------------------------------------------------------------------------- 1 | 5 | 6 | 7 | seckill-dispatcher 8 | org.springframework.web.servlet.DispatcherServlet 9 | 13 | 14 | contextConfigLocation 15 | classpath:spring/spring-*.xml 16 | 17 | 18 | 19 | seckill-dispatcher 20 | 21 | / 22 | 23 | -------------------------------------------------------------------------------- /BookSystem_V0/src/main/webapp/index.jsp: -------------------------------------------------------------------------------- 1 | <%@ page contentType="text/html;charset=UTF-8" language="java" %> 2 | <% String appPath = request.getContextPath(); %> 3 | 4 | 5 | 6 | 7 | 8 | SSM_BookSystem --- V0 9 | 简单的查询、删除等基础功能 10 | 11 | 日期:2017-05-01 22:25:37 12 | 13 | 作者:hisenyuan 14 | 15 | 网站:hisen.me 16 | 17 | 图书系统:点击前往 18 | 19 | 20 | -------------------------------------------------------------------------------- /BookSystem_V0/src/test/com/hisen/test/BaseTest.java: -------------------------------------------------------------------------------- 1 | package com.hisen.test; 2 | 3 | /** 4 | * Created by hisenyuan on 2017/5/3 at 19:40. 5 | */ 6 | 7 | import org.junit.runner.RunWith; 8 | import org.springframework.test.context.ContextConfiguration; 9 | import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; 10 | 11 | /** 12 | * 配置spring和junit整合,junit启动时加载springIOC容器 spring-test,junit 13 | */ 14 | @RunWith(SpringJUnit4ClassRunner.class) 15 | // 告诉junit spring配置文件 16 | @ContextConfiguration({ "classpath:spring/spring-dao.xml", "classpath:spring/spring-service.xml" }) 17 | public class BaseTest { 18 | 19 | } 20 | -------------------------------------------------------------------------------- /BookSystem_V0/src/test/com/hisen/test/BookDaoTest.java: -------------------------------------------------------------------------------- 1 | package com.hisen.test; 2 | 3 | import com.hisen.dao.BookDao; 4 | import com.hisen.entity.Book; 5 | import java.util.List; 6 | import org.junit.Test; 7 | import org.slf4j.Logger; 8 | import org.slf4j.LoggerFactory; 9 | import org.springframework.beans.factory.annotation.Autowired; 10 | 11 | /** 12 | * Created by hisenyuan on 2017/5/3 at 19:41. 13 | */ 14 | public class BookDaoTest extends BaseTest { 15 | 16 | private Logger logger = LoggerFactory.getLogger(this.getClass()); 17 | 18 | @Autowired 19 | private BookDao bookDao; 20 | 21 | @Test 22 | public void addBook() { 23 | for (int i = 0; i < 10; i++) { 24 | Book book = new Book(); 25 | book.setDetail("描述" + i); 26 | book.setName("活着" + i); 27 | book.setNumber(i + 100); 28 | int num = bookDao.addBook(book); 29 | } 30 | } 31 | 32 | @Test 33 | public void queryById() { 34 | Book book = bookDao.queryById(101); 35 | System.out.println(book); 36 | } 37 | 38 | @Test 39 | public void queryAll() { 40 | List books = bookDao.queryAll(1, 1000); 41 | System.out.println(books); 42 | } 43 | 44 | @Test 45 | public void updateBook() { 46 | Book book = new Book(); 47 | book.setBookId(101); 48 | book.setDetail("描述---修改"); 49 | book.setName("活着---修改"); 50 | book.setNumber(100); 51 | int num = bookDao.updateBook(book); 52 | } 53 | 54 | @Test 55 | public void deleteBookById() { 56 | bookDao.deleteBookById(100); 57 | } 58 | } 59 | -------------------------------------------------------------------------------- /BookSystem_V0/src/test/com/hisen/test/BookServiceImplTest.java: -------------------------------------------------------------------------------- 1 | package com.hisen.test; 2 | 3 | import com.hisen.entity.Book; 4 | import com.hisen.service.BookService; 5 | import org.junit.Test; 6 | import org.springframework.beans.factory.annotation.Autowired; 7 | 8 | /** 9 | * Created by hisenyuan on 2017/5/3 at 20:16. 10 | */ 11 | public class BookServiceImplTest extends BookDaoTest { 12 | @Autowired 13 | private BookService bookService; 14 | 15 | @Test 16 | public void getById() { 17 | //在service里面改了一下名字,其实就是dao里面的queryById 18 | Book book = bookService.getById(101); 19 | System.out.println(book); 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /BookSystem_V1/src/main/java/com/hisen/dao/BookDao.java: -------------------------------------------------------------------------------- 1 | package com.hisen.dao; 2 | 3 | import com.hisen.entity.Book; 4 | import java.util.List; 5 | import org.apache.ibatis.annotations.Param; 6 | 7 | /** 8 | * Created by hisen on 17-4-24. 9 | */ 10 | public interface BookDao { 11 | Book queryById(long id); 12 | List queryAll(@Param("offset") int offset, @Param("limit") int limit); 13 | int addBook(Book book); 14 | int updateBook(Book book); 15 | int deleteBookById(long id); 16 | int countNum(); 17 | } 18 | -------------------------------------------------------------------------------- /BookSystem_V1/src/main/java/com/hisen/dao/RedisCache.java: -------------------------------------------------------------------------------- 1 | package com.hisen.dao; 2 | 3 | import java.util.concurrent.locks.ReadWriteLock; 4 | import java.util.concurrent.locks.ReentrantReadWriteLock; 5 | import org.apache.ibatis.cache.Cache; 6 | import org.slf4j.Logger; 7 | import org.slf4j.LoggerFactory; 8 | import org.springframework.data.redis.connection.jedis.JedisConnection; 9 | import org.springframework.data.redis.connection.jedis.JedisConnectionFactory; 10 | import org.springframework.data.redis.serializer.JdkSerializationRedisSerializer; 11 | import org.springframework.data.redis.serializer.RedisSerializer; 12 | import redis.clients.jedis.exceptions.JedisConnectionException; 13 | 14 | /** 15 | * 使用第三方内存数据库Redis作为二级缓存 16 | * 模仿Ehcache写的一个RedisCache缓存类 17 | * Created by hisenyuan on 2017/5/18 at 15:55. 18 | */ 19 | public class RedisCache implements Cache { 20 | 21 | private static final Logger logger = LoggerFactory.getLogger(RedisCache.class); 22 | private static JedisConnectionFactory jedisConnectionFactory; 23 | private final String id; 24 | /** 25 | * 写锁 26 | */ 27 | private final ReadWriteLock readWriteLock = new ReentrantReadWriteLock(); 28 | 29 | public RedisCache(final String id) { 30 | if (id == null) { 31 | throw new IllegalArgumentException("Cache instances require an ID"); 32 | } 33 | logger.info("MybatisRedisCache:id=" + id); 34 | this.id = id; 35 | } 36 | 37 | public String getId() { 38 | return this.id; 39 | } 40 | 41 | /** 42 | * 存放对象 43 | */ 44 | public void putObject(Object key, Object value) { 45 | JedisConnection connection = null; 46 | try { 47 | connection = jedisConnectionFactory.getConnection(); 48 | RedisSerializer serializer = new JdkSerializationRedisSerializer(); 49 | //设置超时时间,可以设置如下,此为三秒钟过期 50 | connection.setEx(serializer.serialize(key),30,serializer.serialize(value)); 51 | //connection.set(serializer.serialize(key), serializer.serialize(value)); 52 | } catch (JedisConnectionException e) { 53 | e.printStackTrace(); 54 | } finally { 55 | if (connection != null) { 56 | connection.close(); 57 | } 58 | } 59 | } 60 | 61 | /** 62 | * 获取对象 63 | */ 64 | public Object getObject(Object key) { 65 | Object result = null; 66 | JedisConnection connection = null; 67 | try { 68 | connection = jedisConnectionFactory.getConnection(); 69 | RedisSerializer serializer = new JdkSerializationRedisSerializer(); 70 | result = serializer.deserialize(connection.get(serializer.serialize(key))); 71 | } catch (JedisConnectionException e) { 72 | e.printStackTrace(); 73 | } finally { 74 | if (connection != null) { 75 | connection.close(); 76 | } 77 | } 78 | return result; 79 | } 80 | 81 | /** 82 | * 删除对象 83 | */ 84 | public Object removeObject(Object key) { 85 | JedisConnection connection = null; 86 | Object result = null; 87 | try { 88 | connection = jedisConnectionFactory.getConnection(); 89 | RedisSerializer serializer = new JdkSerializationRedisSerializer(); 90 | result = connection.expire(serializer.serialize(key), 0); 91 | } catch (JedisConnectionException e) { 92 | e.printStackTrace(); 93 | } finally { 94 | if (connection != null) { 95 | connection.close(); 96 | } 97 | } 98 | return result; 99 | } 100 | 101 | /** 102 | * 获得锁 103 | */ 104 | public ReadWriteLock getReadWriteLock() { 105 | return this.readWriteLock; 106 | } 107 | 108 | /** 109 | * 获取数据库大小 110 | */ 111 | public int getSize() { 112 | int result = 0; 113 | JedisConnection connection = null; 114 | try { 115 | connection = jedisConnectionFactory.getConnection(); 116 | result = Integer.valueOf(connection.dbSize().toString()); 117 | } catch (JedisConnectionException e) { 118 | e.printStackTrace(); 119 | } finally { 120 | if (connection != null) { 121 | connection.close(); 122 | } 123 | } 124 | return result; 125 | } 126 | 127 | /** 128 | * 配置连接工厂 129 | */ 130 | public static void setJedisConnectionFactory(JedisConnectionFactory jedisConnectionFactory) { 131 | RedisCache.jedisConnectionFactory = jedisConnectionFactory; 132 | } 133 | 134 | /** 135 | * 清空 136 | */ 137 | public void clear() { 138 | JedisConnection connection = null; 139 | try { 140 | connection = jedisConnectionFactory.getConnection(); 141 | connection.flushDb(); 142 | connection.flushAll(); 143 | } catch (JedisConnectionException e) { 144 | e.printStackTrace(); 145 | } finally { 146 | if (connection != null) { 147 | connection.close(); 148 | } 149 | } 150 | } 151 | } 152 | -------------------------------------------------------------------------------- /BookSystem_V1/src/main/java/com/hisen/dao/RedisCacheTransfer.java: -------------------------------------------------------------------------------- 1 | package com.hisen.dao; 2 | 3 | import org.springframework.beans.factory.annotation.Autowired; 4 | import org.springframework.data.redis.connection.jedis.JedisConnectionFactory; 5 | 6 | /** 7 | * 静态注入中间类 8 | * 完成RedisCache.jedisConnectionFactory的静态注入 9 | * Created by hisenyuan on 2017/5/18 at 16:05. 10 | */ 11 | public class RedisCacheTransfer { 12 | 13 | @Autowired 14 | public void setJedisConnectionFactory(JedisConnectionFactory jedisConnectionFactory) { 15 | RedisCache.setJedisConnectionFactory(jedisConnectionFactory); 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /BookSystem_V1/src/main/java/com/hisen/entity/Book.java: -------------------------------------------------------------------------------- 1 | package com.hisen.entity; 2 | 3 | import java.io.Serializable; 4 | 5 | /** 6 | * 图书实体类,需要实现Serializable接口以便存放在redis中 7 | * Created by hisen on 17-4-24. 8 | */ 9 | public class Book implements Serializable { 10 | private long bookId; 11 | private String name; 12 | private int number; 13 | private String detail; 14 | 15 | @Override 16 | public String toString() { 17 | return "Book{" + 18 | "bookId=" + bookId + 19 | ", name='" + name + '\'' + 20 | ", number=" + number + 21 | ", detail='" + detail + '\'' + 22 | '}'; 23 | } 24 | 25 | public long getBookId() { 26 | return bookId; 27 | } 28 | 29 | public void setBookId(long bookId) { 30 | this.bookId = bookId; 31 | } 32 | 33 | public String getName() { 34 | return name; 35 | } 36 | 37 | public void setName(String name) { 38 | this.name = name; 39 | } 40 | 41 | public int getNumber() { 42 | return number; 43 | } 44 | 45 | public void setNumber(int number) { 46 | this.number = number; 47 | } 48 | 49 | public String getDetail() { 50 | return detail; 51 | } 52 | 53 | public void setDetail(String detail) { 54 | this.detail = detail; 55 | } 56 | } 57 | -------------------------------------------------------------------------------- /BookSystem_V1/src/main/java/com/hisen/service/BookService.java: -------------------------------------------------------------------------------- 1 | package com.hisen.service; 2 | 3 | import com.hisen.entity.Book; 4 | import java.util.List; 5 | 6 | /** 7 | * Created by hisen on 17-4-24. 8 | */ 9 | public interface BookService { 10 | Book getById(long bookId); 11 | List getList(int start, int pageNum); 12 | int addBook(Book book); 13 | int updateBook(Book book); 14 | int deleteBookById(long id); 15 | int countNum(); 16 | } 17 | -------------------------------------------------------------------------------- /BookSystem_V1/src/main/java/com/hisen/service/impl/BookServiceImpl.java: -------------------------------------------------------------------------------- 1 | package com.hisen.service.impl; 2 | 3 | import com.hisen.dao.BookDao; 4 | import com.hisen.entity.Book; 5 | import com.hisen.service.BookService; 6 | import java.util.List; 7 | import org.springframework.beans.factory.annotation.Autowired; 8 | import org.springframework.stereotype.Service; 9 | 10 | /** 11 | * Created by hisen on 17-4-24. 12 | */ 13 | @Service 14 | public class BookServiceImpl implements BookService { 15 | 16 | @Autowired 17 | private BookDao bookDao; 18 | 19 | public Book getById(long bookId) { 20 | return bookDao.queryById(bookId); 21 | } 22 | 23 | public List getList(int start, int pageNum) { 24 | return bookDao.queryAll(start, pageNum); 25 | } 26 | 27 | public int addBook(Book book) { 28 | return bookDao.addBook(book); 29 | } 30 | 31 | public int updateBook(Book book) { 32 | return bookDao.updateBook(book); 33 | } 34 | 35 | public int deleteBookById(long id) { 36 | return bookDao.deleteBookById(id); 37 | } 38 | 39 | public int countNum() { 40 | return bookDao.countNum(); 41 | } 42 | } 43 | -------------------------------------------------------------------------------- /BookSystem_V1/src/main/java/com/hisen/web/BookController.java: -------------------------------------------------------------------------------- 1 | package com.hisen.web; 2 | 3 | import com.hisen.entity.Book; 4 | import com.hisen.service.BookService; 5 | import com.alibaba.fastjson.JSON; 6 | import java.util.List; 7 | import org.slf4j.Logger; 8 | import org.slf4j.LoggerFactory; 9 | import org.springframework.beans.factory.annotation.Autowired; 10 | import org.springframework.stereotype.Controller; 11 | import org.springframework.ui.Model; 12 | import org.springframework.web.bind.annotation.PathVariable; 13 | import org.springframework.web.bind.annotation.RequestMapping; 14 | import org.springframework.web.bind.annotation.RequestMethod; 15 | import org.springframework.web.bind.annotation.RequestParam; 16 | import org.springframework.web.bind.annotation.ResponseBody; 17 | 18 | /** 19 | * Created by hisen on 17-4-24. 20 | */ 21 | @Controller 22 | @RequestMapping("/book") 23 | public class BookController { 24 | private Logger logger = LoggerFactory.getLogger(this.getClass()); 25 | 26 | @Autowired 27 | private BookService bookService; 28 | 29 | @RequestMapping(value = "/list", method = RequestMethod.GET) 30 | private String list(Model model) { 31 | List list = bookService.getList(0, 10); 32 | model.addAttribute("list", list); 33 | return "list";// WEB-INF/jsp/"list".jsp 34 | } 35 | 36 | @RequestMapping(value = "/detail/{bookId}", method = RequestMethod.GET) 37 | private String detail(@PathVariable("bookId") Long bookId, Model model) { 38 | Book book = bookService.getById(bookId); 39 | model.addAttribute("book", book); 40 | return "detail"; 41 | } 42 | 43 | @RequestMapping(value = "/add", method = RequestMethod.POST, produces = "text/plain;charset=UTF-8") 44 | @ResponseBody 45 | private String add(Book book) { 46 | Book hasBook = bookService.getById(book.getBookId()); 47 | int i = -2; 48 | if (hasBook == null) { 49 | i = bookService.addBook(book); 50 | } 51 | return i > 0 ? "success" : "error"; 52 | } 53 | 54 | @RequestMapping(value = "/del/{bookId}", method = RequestMethod.GET) 55 | @ResponseBody 56 | private String deleteBookById(@PathVariable("bookId") Long id) { 57 | int i = bookService.deleteBookById(id); 58 | return i > 0 ? "success" : "error"; 59 | } 60 | 61 | /** 62 | * 查询总页数 63 | * @return 64 | */ 65 | @RequestMapping(value = "/countNum", method = RequestMethod.POST, produces = { 66 | "application/json; charset=utf-8"}) 67 | @ResponseBody 68 | private int countNum() { 69 | int num = bookService.countNum(); 70 | //计算页数,如果除以10有余数,得加上一页 71 | int countNum = num / 10 + ((num % 10) > 0 ? 1 : 0); 72 | return countNum; 73 | } 74 | 75 | /** 76 | * 分页查询方法 77 | * @param start 78 | * @return 79 | */ 80 | @RequestMapping(value = "/listpage", method = RequestMethod.POST) 81 | @ResponseBody 82 | private String listPage(@RequestParam("start") int start) { 83 | //默认一页10条 84 | List list = bookService.getList(start, 10); 85 | //阿里fastjson把数组转换为json 86 | String s = JSON.toJSONString(list); 87 | //System.out.println(s); 88 | return s; 89 | } 90 | } 91 | -------------------------------------------------------------------------------- /BookSystem_V1/src/main/resources/jdbc.properties: -------------------------------------------------------------------------------- 1 | jdbc.driverClassName=com.mysql.jdbc.Driver 2 | jdbc.url=jdbc:mysql://127.0.0.1:3306/booksystem?useUnicode=true&characterEncoding=utf8 3 | jdbc.username=root 4 | jdbc.password=hisen -------------------------------------------------------------------------------- /BookSystem_V1/src/main/resources/logback.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | %d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n 7 | 8 | 9 | 10 | 11 | 12 | 13 | -------------------------------------------------------------------------------- /BookSystem_V1/src/main/resources/mapper/BookMapper.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | SELECT book_id,name,number,detail 12 | FROM 13 | book 14 | WHERE 15 | book_id = #{bookId} 16 | 17 | 18 | 19 | SELECT 20 | book_id, 21 | name, 22 | number, 23 | detail 24 | FROM 25 | book 26 | ORDER BY 27 | book_id 28 | LIMIT #{offset}, #{limit} 29 | 30 | 31 | 32 | UPDATE book 33 | SET NAME = #{name}, 34 | NUMBER = #{number}, 35 | detail = #{detail} 36 | WHERE 37 | book_id = #{bookId} 38 | AND number > 0 39 | 40 | 41 | 42 | INSERT INTO book(`book_id`, `name`, `number`,`detail`) 43 | VALUES(#{bookId}, #{name}, #{number}, #{detail}) 44 | 45 | 46 | 47 | DELETE FROM book WHERE book_id = #{bookId} 48 | 49 | 50 | SELECT 51 | COUNT(*) 52 | FROM 53 | book 54 | 55 | -------------------------------------------------------------------------------- /BookSystem_V1/src/main/resources/mybatis-config.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | -------------------------------------------------------------------------------- /BookSystem_V1/src/main/resources/redis.properties: -------------------------------------------------------------------------------- 1 | # Redis settings 2 | redis.host=127.0.0.1 3 | redis.port=6379 4 | redis.pass= 5 | 6 | redis.maxIdle=300 7 | redis.maxActive=600 8 | redis.maxWait=1000 9 | redis.testOnBorrow=true -------------------------------------------------------------------------------- /BookSystem_V1/src/main/resources/spring/spring-dao.xml: -------------------------------------------------------------------------------- 1 | 2 | 7 | 8 | 9 | 11 | 12 | 13 | 14 | classpath:jdbc.properties 15 | 16 | classpath:redis.properties 17 | 18 | 19 | 20 | 21 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | 45 | 46 | 47 | 48 | 49 | 50 | 51 | 52 | 53 | 54 | 55 | 56 | 57 | 58 | 60 | 61 | 62 | 63 | 64 | 65 | 66 | 67 | 68 | 69 | 70 | 74 | 75 | 76 | 77 | 78 | 79 | 80 | 81 | 82 | 83 | 84 | 85 | 86 | 87 | 88 | 89 | 90 | 91 | 92 | 93 | 94 | 95 | 96 | 97 | 98 | -------------------------------------------------------------------------------- /BookSystem_V1/src/main/resources/spring/spring-service.xml: -------------------------------------------------------------------------------- 1 | 2 | 12 | 13 | 14 | 15 | 16 | 18 | 19 | 20 | 21 | 22 | 23 | -------------------------------------------------------------------------------- /BookSystem_V1/src/main/resources/spring/spring-web.xml: -------------------------------------------------------------------------------- 1 | 2 | 12 | 13 | 14 | 18 | 19 | 20 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | -------------------------------------------------------------------------------- /BookSystem_V1/src/main/webapp/WEB-INF/jsp/detail.jsp: -------------------------------------------------------------------------------- 1 | <%-- 2 | Created by IntelliJ IDEA. 3 | User: Administrator 4 | Date: 2017/4/6 5 | Time: 20:21 6 | To change this template use File | Settings | File Templates. 7 | --%> 8 | <%@ page contentType="text/html;charset=UTF-8" language="java" %> 9 | <% String appPath = request.getContextPath(); %> 10 | 11 | 12 | 图书详情 13 | 14 | 15 | 16 | 17 | 18 | 19 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | HiSEN 图书管理系统 - by ssm基础框架 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 首页 39 | 图书具体信息 40 | 信息 41 | 42 | 43 | 44 | 45 | 46 | 47 | 48 | 49 | 图书详情 显示当前图书详细信息 50 | 51 | 52 | 53 | 54 | 55 | 56 | 57 | 58 | 59 | 图书编号 60 | 图书名字 61 | 图书数量 62 | 63 | 64 | 65 | 66 | ${requestScope.get('book').bookId} 67 | ${requestScope.get('book').name} 68 | ${requestScope.get('book').number} 69 | 70 | 71 | 72 | 73 | 74 | 75 | 76 | 77 | 《${requestScope.get('book').name}》 ${requestScope.get('book').detail} 78 | 79 | 80 | 81 | 82 | 83 | 84 | 85 | 86 | 87 | 88 | -------------------------------------------------------------------------------- /BookSystem_V1/src/main/webapp/WEB-INF/web.xml: -------------------------------------------------------------------------------- 1 | 5 | 6 | 7 | seckill-dispatcher 8 | org.springframework.web.servlet.DispatcherServlet 9 | 13 | 14 | contextConfigLocation 15 | classpath:spring/spring-*.xml 16 | 17 | 18 | 19 | seckill-dispatcher 20 | 21 | / 22 | 23 | 24 | 25 | 26 | 27 | DruidStatView 28 | com.alibaba.druid.support.http.StatViewServlet 29 | 30 | 31 | DruidStatView 32 | /druid/* 33 | 34 | 35 | druidWebStatFilter 36 | com.alibaba.druid.support.http.WebStatFilter 37 | 38 | exclusions 39 | /public/*,*.js,*.css,/druid*,*.jsp,*.swf 40 | 41 | 42 | principalSessionName 43 | sessionInfo 44 | 45 | 46 | profileEnable 47 | true 48 | 49 | 50 | 51 | druidWebStatFilter 52 | /* 53 | 54 | -------------------------------------------------------------------------------- /BookSystem_V1/src/main/webapp/add.jsp: -------------------------------------------------------------------------------- 1 | <%-- 2 | Created by IntelliJ IDEA. 3 | User: Administrator 4 | Date: 2017/4/7 5 | Time: 18:52 6 | To change this template use File | Settings | File Templates. 7 | --%> 8 | <%@ page contentType="text/html;charset=UTF-8" language="java" %> 9 | <% String appPath = request.getContextPath(); %> 10 | 11 | 12 | 添加图书 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 首页 31 | 图书具体信息 32 | 添加图书信息 33 | 信息 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 添加图书 43 | 增加图书信息 44 | 45 | 46 | 47 | 48 | 49 | 50 | 51 | 52 | 53 | 图书ID 54 | 55 | 56 | 图书名字 57 | 58 | 59 | 图书描述 60 | 61 | 62 | 图书数量 63 | 64 | Submit 65 | 66 | 67 | 68 | 69 | 70 | 71 | 72 | × 73 | 74 | 提醒: 75 | 76 | 77 | 78 | 79 | 80 | 81 | 82 | 103 | 104 | 105 | -------------------------------------------------------------------------------- /BookSystem_V1/src/main/webapp/index.jsp: -------------------------------------------------------------------------------- 1 | <%@ page contentType="text/html;charset=UTF-8" language="java" %> 2 | <% String appPath = request.getContextPath(); %> 3 | 4 | 5 | 6 | 7 | 8 | SSM_BookSystem --- V1 9 | 目前包含简单的查询、分页、redis缓存. 10 | 11 | 更新:2017年5月18日 17:51:22 12 | 13 | 作者:hisenyuan 14 | 15 | 网站:hisen.me 16 | 17 | 图书系统:SSM_BookSystem V1(点击进入) 18 | 19 | 20 | -------------------------------------------------------------------------------- /BookSystem_V1/src/test/com/hisen/test/BaseTest.java: -------------------------------------------------------------------------------- 1 | package com.hisen.test; 2 | 3 | /** 4 | * Created by hisenyuan on 2017/5/3 at 19:40. 5 | */ 6 | 7 | import org.junit.runner.RunWith; 8 | import org.springframework.test.context.ContextConfiguration; 9 | import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; 10 | 11 | /** 12 | * 配置spring和junit整合,junit启动时加载springIOC容器 spring-test,junit 13 | */ 14 | @RunWith(SpringJUnit4ClassRunner.class) 15 | // 告诉junit spring配置文件 16 | @ContextConfiguration({ "classpath:spring/spring-dao.xml", "classpath:spring/spring-service.xml" }) 17 | public class BaseTest { 18 | 19 | } 20 | -------------------------------------------------------------------------------- /BookSystem_V1/src/test/com/hisen/test/BookDaoTest.java: -------------------------------------------------------------------------------- 1 | package com.hisen.test; 2 | 3 | import com.hisen.dao.BookDao; 4 | import com.hisen.entity.Book; 5 | import java.util.List; 6 | import org.junit.Test; 7 | import org.slf4j.Logger; 8 | import org.slf4j.LoggerFactory; 9 | import org.springframework.beans.factory.annotation.Autowired; 10 | 11 | /** 12 | * Created by hisenyuan on 2017/5/3 at 19:41. 13 | */ 14 | public class BookDaoTest extends BaseTest { 15 | 16 | private Logger logger = LoggerFactory.getLogger(this.getClass()); 17 | 18 | @Autowired 19 | private BookDao bookDao; 20 | 21 | @Test 22 | public void addBook() { 23 | for (int i = 0; i < 10; i++) { 24 | Book book = new Book(); 25 | book.setDetail("描述" + i); 26 | book.setName("活着" + i); 27 | book.setNumber(i + 100); 28 | int num = bookDao.addBook(book); 29 | } 30 | } 31 | 32 | @Test 33 | public void queryById() { 34 | Book book = bookDao.queryById(101); 35 | System.out.println(book); 36 | } 37 | 38 | @Test 39 | public void queryAll() { 40 | List books = bookDao.queryAll(1, 1000); 41 | System.out.println(books); 42 | } 43 | 44 | @Test 45 | public void updateBook() { 46 | Book book = new Book(); 47 | book.setBookId(101); 48 | book.setDetail("描述---修改"); 49 | book.setName("活着---修改"); 50 | book.setNumber(100); 51 | int num = bookDao.updateBook(book); 52 | } 53 | 54 | @Test 55 | public void deleteBookById() { 56 | bookDao.deleteBookById(100); 57 | } 58 | } 59 | -------------------------------------------------------------------------------- /BookSystem_V1/src/test/com/hisen/test/BookServiceImplTest.java: -------------------------------------------------------------------------------- 1 | package com.hisen.test; 2 | 3 | import com.hisen.entity.Book; 4 | import com.hisen.service.BookService; 5 | import org.junit.Test; 6 | import org.springframework.beans.factory.annotation.Autowired; 7 | 8 | /** 9 | * Created by hisenyuan on 2017/5/3 at 20:16. 10 | */ 11 | public class BookServiceImplTest extends BookDaoTest { 12 | @Autowired 13 | private BookService bookService; 14 | 15 | @Test 16 | public void getById() { 17 | //在service里面改了一下名字,其实就是dao里面的queryById 18 | Book book = bookService.getById(101); 19 | System.out.println(book); 20 | } 21 | @Test 22 | public void countNum(){ 23 | int i = bookService.countNum(); 24 | System.out.println(i); 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /BookSystem_V1/src/test/com/hisen/test/MyBatisCacheSecondTest.java: -------------------------------------------------------------------------------- 1 | package com.hisen.test; 2 | 3 | import com.hisen.entity.Book; 4 | import com.hisen.service.BookService; 5 | import java.util.List; 6 | import org.junit.Test; 7 | import org.springframework.beans.factory.annotation.Autowired; 8 | 9 | /** 10 | * redis缓存测试类 11 | * Created by hisenyuan on 2017/5/18 at 16:10. 12 | */ 13 | public class MyBatisCacheSecondTest extends BookDaoTest { 14 | 15 | @Autowired 16 | private BookService bookService; 17 | 18 | @Test 19 | public void testCache() { 20 | //查询两次,第二次就直接在redis取出 21 | List list = bookService.getList(0, 10); 22 | System.out.println(list); 23 | List list1 = bookService.getList(0, 10); 24 | System.out.println(list1); 25 | } 26 | /** 27 | * 提醒:在控制台搜索:Cache Hit Ratio 即可看到数据 28 | * 第一次查询 29 | * 17:00:38.946 [main] DEBUG com.hisen.dao.BookDao - Cache Hit Ratio [com.hisen.dao.BookDao]: 0.0 30 | * 第二次查询 31 | * 17:02:06.453 [main] DEBUG com.hisen.dao.BookDao - Cache Hit Ratio [com.hisen.dao.BookDao]: 1.0 32 | */ 33 | } 34 | -------------------------------------------------------------------------------- /BookSystem_V2/README.md: -------------------------------------------------------------------------------- 1 | ## 项目介绍 2 | 开发工具:intelliJ IDEA 3 | 4 | 项目相关:maven-3.39、jdk-8、github 5 | 6 | 相关框架:Spring-4.17、SpringMVC-4.17、Mybatis-3.30 7 | 8 | 数 据 库:mysql(存放数据)、redis(缓存) 9 | 10 | 模板引擎:thymeleaf 11 | 12 | 13 | ## 通过命令行导入数据库 14 | 1. 查看所有的数据库 15 | ``` 16 | mysql> show databases; 17 | +--------------------+ 18 | | Database | 19 | +--------------------+ 20 | | information_schema | 21 | | employees | 22 | | mysql | 23 | | performance_schema | 24 | | ssm | 25 | | sys | 26 | +--------------------+ 27 | 6 rows in set (0.00 sec) 28 | ``` 29 | 30 | 2. 如果不存在【booksystem】则创建 31 | ``` 32 | mysql> create database booksystem; 33 | Query OK, 1 row affected (0.00 sec) 34 | ``` 35 | 3. 选择数据库 36 | ``` 37 | mysql> use booksystem; 38 | Database changed 39 | ``` 40 | 41 | 4. 设置数据库编码 42 | ``` 43 | mysql> set names utf8; 44 | Query OK, 0 rows affected (0.00 sec) 45 | ``` 46 | 47 | 5. 导入数据库文件 48 | ``` 49 | mysql> source /home/hisen/dl/hisen/booksystem.sql 50 | ``` 51 | 52 | 6. 查看数据库拥有的表 53 | ``` 54 | mysql> show tables; 55 | +----------------------+ 56 | | Tables_in_booksystem | 57 | +----------------------+ 58 | | appointment | 59 | | book | 60 | | user | 61 | +----------------------+ 62 | 3 rows in set (0.00 sec) 63 | ``` 64 | 65 | 7. 完毕 66 | 67 | ## 开发过程中的感想 68 | 1. 目前aop没有具体的用处 69 | 2. AOP可以用的地方:参数校验,错误拦截,日志记录 70 | 3. 以后尽量少用jsp,不利于前后端分离(SOA:面向服务) 71 | 4. 为了安全尽量把bean分开。入参:form、出参:VO、DAO层:DTO 72 | 73 | 74 | ## 目录结构 75 | ``` 76 | hisen@hisen-pc:~/IdeaProjects/SSM_BookSystem/BookSystem_V2/src$ tree 77 | . 78 | ├── main 79 | │ ├── java 80 | │ │ └── com 81 | │ │ └── hisen 82 | │ │ ├── aop 83 | │ │ │ ├── GetMethodInfoHandler.java 84 | │ │ │ └── TimeHandler.java 85 | │ │ ├── dao 86 | │ │ │ ├── AppointmentMapper.java 87 | │ │ │ ├── BookDao.java 88 | │ │ │ ├── form 89 | │ │ │ │ └── AppointmentForm.java 90 | │ │ │ ├── RedisCache.java 91 | │ │ │ ├── RedisCacheTransfer.java 92 | │ │ │ ├── sql 93 | │ │ │ │ └── booksystem.sql 94 | │ │ │ └── UserMapper.java 95 | │ │ ├── entity 96 | │ │ │ ├── AppointmentExample.java 97 | │ │ │ ├── Appointment.java 98 | │ │ │ ├── Book.java 99 | │ │ │ ├── UserExample.java 100 | │ │ │ ├── User.java 101 | │ │ │ └── UserKey.java 102 | │ │ ├── service 103 | │ │ │ ├── AppointmentService.java 104 | │ │ │ ├── BookService.java 105 | │ │ │ └── impl 106 | │ │ │ ├── AppointmentServiceImpl.java 107 | │ │ │ └── BookServiceImpl.java 108 | │ │ └── web 109 | │ │ ├── AppointmengtController.java 110 | │ │ └── BookController.java 111 | │ ├── resources 112 | │ │ ├── generatorConfig.xml 113 | │ │ ├── jdbc.properties 114 | │ │ ├── logback.xml 115 | │ │ ├── mapper 116 | │ │ │ ├── AppointmentMapper.xml 117 | │ │ │ ├── BookMapper.xml 118 | │ │ │ └── UserMapper.xml 119 | │ │ ├── mybatis-config.xml 120 | │ │ ├── redis.properties 121 | │ │ └── spring 122 | │ │ ├── spring-aop.xml 123 | │ │ ├── spring-dao.xml 124 | │ │ ├── spring-service.xml 125 | │ │ └── spring-web.xml 126 | │ └── webapp 127 | │ ├── add.html 128 | │ ├── index.html 129 | │ ├── static 130 | │ │ └── js 131 | │ │ ├── jquery-3.1.1.min.js 132 | │ │ └── jquery.paginate.js 133 | │ └── WEB-INF 134 | │ ├── jsp 135 | │ │ ├── detail.jsp 136 | │ │ └── list.jsp 137 | │ ├── templates 138 | │ │ ├── booklist.html 139 | │ │ ├── detail.html 140 | │ │ ├── footer.html 141 | │ │ ├── header.html 142 | │ │ ├── list.html 143 | │ │ ├── page.html 144 | │ │ └── readingList.html 145 | │ └── web.xml 146 | └── test 147 | ├── AppointmentServiceImplTest.java 148 | ├── BaseTest.java 149 | └── UserDaoTest.java 150 | 151 | 22 directories, 50 files 152 | ``` -------------------------------------------------------------------------------- /BookSystem_V2/src/main/java/com/hisen/aop/GetMethodInfoHandler.java: -------------------------------------------------------------------------------- 1 | package com.hisen.aop; 2 | 3 | import java.util.Arrays; 4 | import org.aspectj.lang.JoinPoint; 5 | 6 | /** 7 | * Created by hisenyuan on 2017/7/26 at 16:43. 8 | */ 9 | public class GetMethodInfoHandler { 10 | public void getInfo(JoinPoint joinPoint) { 11 | System.out.println(); 12 | // Object[] getArgs:返回目标方法的参数 13 | // Signature getSignature:返回目标方法的签名 14 | // Object getTarget:返回被织入增强处理的目标对象 15 | // Object getThis:返回AOP框架为目标对象生成的代理对象 16 | System.out.println("执行过的函数相关信息 >>>>> 方法" + joinPoint.getSignature()); 17 | System.out.println("执行过的函数相关信息 >>>>> 参数" + Arrays.asList(joinPoint.getArgs())); 18 | System.out.println("执行过的函数相关信息 >>>>> 目标" + joinPoint.getTarget()); 19 | System.out.println("执行过的函数相关信息 >>>>> 代理" + joinPoint.getThis()); 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /BookSystem_V2/src/main/java/com/hisen/aop/TimeHandler.java: -------------------------------------------------------------------------------- 1 | package com.hisen.aop; 2 | 3 | import java.text.SimpleDateFormat; 4 | import java.util.Date; 5 | 6 | /** 7 | * Created by hisenyuan on 2017/7/26 at 15:53. 8 | */ 9 | public class TimeHandler { 10 | /** 11 | * 打印时间 12 | */ 13 | public void printTime() { 14 | //此处准备记录运行日志 15 | String time = new SimpleDateFormat("yyyy-MM-dd hh24:mm:ss").format(new Date()); 16 | System.out.println(); 17 | System.out.println("Spring AOP >>>>> 当前时间 = " + time); 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /BookSystem_V2/src/main/java/com/hisen/dao/AppointmentMapper.java: -------------------------------------------------------------------------------- 1 | package com.hisen.dao; 2 | 3 | import com.hisen.entity.Appointment; 4 | import com.hisen.entity.AppointmentExample; 5 | import java.util.List; 6 | import org.apache.ibatis.annotations.Param; 7 | 8 | public interface AppointmentMapper { 9 | /** 10 | * This method was generated by MyBatis Generator. 11 | * This method corresponds to the database table appointment 12 | * 13 | * @mbggenerated Thu Aug 03 16:47:27 CST 2017 14 | */ 15 | int countByExample(AppointmentExample example); 16 | 17 | /** 18 | * This method was generated by MyBatis Generator. 19 | * This method corresponds to the database table appointment 20 | * 21 | * @mbggenerated Thu Aug 03 16:47:27 CST 2017 22 | */ 23 | int deleteByExample(AppointmentExample example); 24 | 25 | /** 26 | * This method was generated by MyBatis Generator. 27 | * This method corresponds to the database table appointment 28 | * 29 | * @mbggenerated Thu Aug 03 16:47:27 CST 2017 30 | */ 31 | int deleteByPrimaryKey(Integer id); 32 | 33 | /** 34 | * This method was generated by MyBatis Generator. 35 | * This method corresponds to the database table appointment 36 | * 37 | * @mbggenerated Thu Aug 03 16:47:27 CST 2017 38 | */ 39 | int insert(Appointment record); 40 | 41 | /** 42 | * This method was generated by MyBatis Generator. 43 | * This method corresponds to the database table appointment 44 | * 45 | * @mbggenerated Thu Aug 03 16:47:27 CST 2017 46 | */ 47 | int insertSelective(Appointment record); 48 | 49 | /** 50 | * This method was generated by MyBatis Generator. 51 | * This method corresponds to the database table appointment 52 | * 53 | * @mbggenerated Thu Aug 03 16:47:27 CST 2017 54 | */ 55 | List selectByExample(AppointmentExample example); 56 | 57 | /** 58 | * This method was generated by MyBatis Generator. 59 | * This method corresponds to the database table appointment 60 | * 61 | * @mbggenerated Thu Aug 03 16:47:27 CST 2017 62 | */ 63 | Appointment selectByPrimaryKey(Integer id); 64 | 65 | /** 66 | * This method was generated by MyBatis Generator. 67 | * This method corresponds to the database table appointment 68 | * 69 | * @mbggenerated Thu Aug 03 16:47:27 CST 2017 70 | */ 71 | int updateByExampleSelective(@Param("record") Appointment record, @Param("example") AppointmentExample example); 72 | 73 | /** 74 | * This method was generated by MyBatis Generator. 75 | * This method corresponds to the database table appointment 76 | * 77 | * @mbggenerated Thu Aug 03 16:47:27 CST 2017 78 | */ 79 | int updateByExample(@Param("record") Appointment record, @Param("example") AppointmentExample example); 80 | 81 | /** 82 | * This method was generated by MyBatis Generator. 83 | * This method corresponds to the database table appointment 84 | * 85 | * @mbggenerated Thu Aug 03 16:47:27 CST 2017 86 | */ 87 | int updateByPrimaryKeySelective(Appointment record); 88 | 89 | /** 90 | * This method was generated by MyBatis Generator. 91 | * This method corresponds to the database table appointment 92 | * 93 | * @mbggenerated Thu Aug 03 16:47:27 CST 2017 94 | */ 95 | int updateByPrimaryKey(Appointment record); 96 | } -------------------------------------------------------------------------------- /BookSystem_V2/src/main/java/com/hisen/dao/BookDao.java: -------------------------------------------------------------------------------- 1 | package com.hisen.dao; 2 | 3 | import com.hisen.entity.Book; 4 | import java.util.List; 5 | import org.apache.ibatis.annotations.Param; 6 | 7 | /** 8 | * Created by hisen on 17-4-24. 9 | */ 10 | public interface BookDao { 11 | Book queryById(long id); 12 | List queryAll(@Param("offset") int offset, @Param("limit") int limit); 13 | int addBook(Book book); 14 | int updateBook(Book book); 15 | int deleteBookById(long id); 16 | int countNum(); 17 | List queryAllPlug(); 18 | } 19 | -------------------------------------------------------------------------------- /BookSystem_V2/src/main/java/com/hisen/dao/RedisCache.java: -------------------------------------------------------------------------------- 1 | package com.hisen.dao; 2 | 3 | import java.util.concurrent.locks.ReadWriteLock; 4 | import java.util.concurrent.locks.ReentrantReadWriteLock; 5 | import org.apache.ibatis.cache.Cache; 6 | import org.slf4j.Logger; 7 | import org.slf4j.LoggerFactory; 8 | import org.springframework.data.redis.connection.jedis.JedisConnection; 9 | import org.springframework.data.redis.connection.jedis.JedisConnectionFactory; 10 | import org.springframework.data.redis.serializer.JdkSerializationRedisSerializer; 11 | import org.springframework.data.redis.serializer.RedisSerializer; 12 | import redis.clients.jedis.exceptions.JedisConnectionException; 13 | 14 | /** 15 | * 使用第三方内存数据库Redis作为二级缓存 16 | * 模仿Ehcache写的一个RedisCache缓存类 17 | * Created by hisenyuan on 2017/5/18 at 15:55. 18 | */ 19 | public class RedisCache implements Cache { 20 | 21 | private static final Logger logger = LoggerFactory.getLogger(RedisCache.class); 22 | private static JedisConnectionFactory jedisConnectionFactory; 23 | private final String id; 24 | /** 25 | * 写锁 26 | */ 27 | private final ReadWriteLock readWriteLock = new ReentrantReadWriteLock(); 28 | 29 | public RedisCache(final String id) { 30 | if (id == null) { 31 | throw new IllegalArgumentException("Cache instances require an ID"); 32 | } 33 | logger.info("MybatisRedisCache:id=" + id); 34 | this.id = id; 35 | } 36 | 37 | public String getId() { 38 | return this.id; 39 | } 40 | 41 | /** 42 | * 存放对象 43 | */ 44 | public void putObject(Object key, Object value) { 45 | JedisConnection connection = null; 46 | try { 47 | connection = jedisConnectionFactory.getConnection(); 48 | RedisSerializer serializer = new JdkSerializationRedisSerializer(); 49 | //设置超时时间,可以设置如下,此为三秒钟过期 50 | connection.setEx(serializer.serialize(key),30,serializer.serialize(value)); 51 | //connection.set(serializer.serialize(key), serializer.serialize(value)); 52 | } catch (JedisConnectionException e) { 53 | e.printStackTrace(); 54 | } finally { 55 | if (connection != null) { 56 | connection.close(); 57 | } 58 | } 59 | } 60 | 61 | /** 62 | * 获取对象 63 | */ 64 | public Object getObject(Object key) { 65 | Object result = null; 66 | JedisConnection connection = null; 67 | try { 68 | connection = jedisConnectionFactory.getConnection(); 69 | RedisSerializer serializer = new JdkSerializationRedisSerializer(); 70 | result = serializer.deserialize(connection.get(serializer.serialize(key))); 71 | } catch (JedisConnectionException e) { 72 | e.printStackTrace(); 73 | } finally { 74 | if (connection != null) { 75 | connection.close(); 76 | } 77 | } 78 | return result; 79 | } 80 | 81 | /** 82 | * 删除对象 83 | */ 84 | public Object removeObject(Object key) { 85 | JedisConnection connection = null; 86 | Object result = null; 87 | try { 88 | connection = jedisConnectionFactory.getConnection(); 89 | RedisSerializer serializer = new JdkSerializationRedisSerializer(); 90 | result = connection.expire(serializer.serialize(key), 0); 91 | } catch (JedisConnectionException e) { 92 | e.printStackTrace(); 93 | } finally { 94 | if (connection != null) { 95 | connection.close(); 96 | } 97 | } 98 | return result; 99 | } 100 | 101 | /** 102 | * 获得锁 103 | */ 104 | public ReadWriteLock getReadWriteLock() { 105 | return this.readWriteLock; 106 | } 107 | 108 | /** 109 | * 获取数据库大小 110 | */ 111 | public int getSize() { 112 | int result = 0; 113 | JedisConnection connection = null; 114 | try { 115 | connection = jedisConnectionFactory.getConnection(); 116 | result = Integer.valueOf(connection.dbSize().toString()); 117 | } catch (JedisConnectionException e) { 118 | e.printStackTrace(); 119 | } finally { 120 | if (connection != null) { 121 | connection.close(); 122 | } 123 | } 124 | return result; 125 | } 126 | 127 | /** 128 | * 配置连接工厂 129 | */ 130 | public static void setJedisConnectionFactory(JedisConnectionFactory jedisConnectionFactory) { 131 | RedisCache.jedisConnectionFactory = jedisConnectionFactory; 132 | } 133 | 134 | /** 135 | * 清空 136 | */ 137 | public void clear() { 138 | JedisConnection connection = null; 139 | try { 140 | connection = jedisConnectionFactory.getConnection(); 141 | connection.flushDb(); 142 | connection.flushAll(); 143 | } catch (JedisConnectionException e) { 144 | e.printStackTrace(); 145 | } finally { 146 | if (connection != null) { 147 | connection.close(); 148 | } 149 | } 150 | } 151 | } 152 | -------------------------------------------------------------------------------- /BookSystem_V2/src/main/java/com/hisen/dao/RedisCacheTransfer.java: -------------------------------------------------------------------------------- 1 | package com.hisen.dao; 2 | 3 | import org.springframework.beans.factory.annotation.Autowired; 4 | import org.springframework.data.redis.connection.jedis.JedisConnectionFactory; 5 | 6 | /** 7 | * 静态注入中间类 8 | * 完成RedisCache.jedisConnectionFactory的静态注入 9 | * Created by hisenyuan on 2017/5/18 at 16:05. 10 | */ 11 | public class RedisCacheTransfer { 12 | 13 | @Autowired 14 | public void setJedisConnectionFactory(JedisConnectionFactory jedisConnectionFactory) { 15 | RedisCache.setJedisConnectionFactory(jedisConnectionFactory); 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /BookSystem_V2/src/main/java/com/hisen/dao/UserMapper.java: -------------------------------------------------------------------------------- 1 | package com.hisen.dao; 2 | 3 | import com.hisen.entity.User; 4 | import com.hisen.entity.UserExample; 5 | import com.hisen.entity.UserKey; 6 | import java.util.List; 7 | import org.apache.ibatis.annotations.Param; 8 | 9 | public interface UserMapper { 10 | /** 11 | * This method was generated by MyBatis Generator. 12 | * This method corresponds to the database table user 13 | * 14 | * @mbggenerated Thu Aug 03 16:47:27 CST 2017 15 | */ 16 | int countByExample(UserExample example); 17 | 18 | /** 19 | * This method was generated by MyBatis Generator. 20 | * This method corresponds to the database table user 21 | * 22 | * @mbggenerated Thu Aug 03 16:47:27 CST 2017 23 | */ 24 | int deleteByExample(UserExample example); 25 | 26 | /** 27 | * This method was generated by MyBatis Generator. 28 | * This method corresponds to the database table user 29 | * 30 | * @mbggenerated Thu Aug 03 16:47:27 CST 2017 31 | */ 32 | int deleteByPrimaryKey(UserKey key); 33 | 34 | /** 35 | * This method was generated by MyBatis Generator. 36 | * This method corresponds to the database table user 37 | * 38 | * @mbggenerated Thu Aug 03 16:47:27 CST 2017 39 | */ 40 | int insert(User record); 41 | 42 | /** 43 | * This method was generated by MyBatis Generator. 44 | * This method corresponds to the database table user 45 | * 46 | * @mbggenerated Thu Aug 03 16:47:27 CST 2017 47 | */ 48 | int insertSelective(User record); 49 | 50 | /** 51 | * This method was generated by MyBatis Generator. 52 | * This method corresponds to the database table user 53 | * 54 | * @mbggenerated Thu Aug 03 16:47:27 CST 2017 55 | */ 56 | List selectByExample(UserExample example); 57 | 58 | /** 59 | * This method was generated by MyBatis Generator. 60 | * This method corresponds to the database table user 61 | * 62 | * @mbggenerated Thu Aug 03 16:47:27 CST 2017 63 | */ 64 | User selectByPrimaryKey(UserKey key); 65 | 66 | /** 67 | * This method was generated by MyBatis Generator. 68 | * This method corresponds to the database table user 69 | * 70 | * @mbggenerated Thu Aug 03 16:47:27 CST 2017 71 | */ 72 | int updateByExampleSelective(@Param("record") User record, @Param("example") UserExample example); 73 | 74 | /** 75 | * This method was generated by MyBatis Generator. 76 | * This method corresponds to the database table user 77 | * 78 | * @mbggenerated Thu Aug 03 16:47:27 CST 2017 79 | */ 80 | int updateByExample(@Param("record") User record, @Param("example") UserExample example); 81 | 82 | /** 83 | * This method was generated by MyBatis Generator. 84 | * This method corresponds to the database table user 85 | * 86 | * @mbggenerated Thu Aug 03 16:47:27 CST 2017 87 | */ 88 | int updateByPrimaryKeySelective(User record); 89 | 90 | /** 91 | * This method was generated by MyBatis Generator. 92 | * This method corresponds to the database table user 93 | * 94 | * @mbggenerated Thu Aug 03 16:47:27 CST 2017 95 | */ 96 | int updateByPrimaryKey(User record); 97 | } -------------------------------------------------------------------------------- /BookSystem_V2/src/main/java/com/hisen/dao/form/AppointmentForm.java: -------------------------------------------------------------------------------- 1 | package com.hisen.dao.form; 2 | 3 | import com.hisen.entity.Appointment; 4 | 5 | /** 6 | * Created by hisenyuan on 2017/8/2 at 10:54. 7 | */ 8 | public class AppointmentForm extends Appointment { 9 | private String holdDay; 10 | 11 | public String getHoldDay() { 12 | return holdDay; 13 | } 14 | 15 | public void setHoldDay(String holdDay) { 16 | this.holdDay = holdDay; 17 | } 18 | 19 | @Override 20 | public String toString() { 21 | return "AppointmentForm{" + 22 | "holdDay='" + holdDay + '\'' + 23 | '}'; 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /BookSystem_V2/src/main/java/com/hisen/entity/Book.java: -------------------------------------------------------------------------------- 1 | package com.hisen.entity; 2 | 3 | import java.io.Serializable; 4 | 5 | /** 6 | * 图书实体类,需要实现Serializable接口以便存放在redis中 7 | * Created by hisen on 17-4-24. 8 | */ 9 | public class Book implements Serializable { 10 | private long bookId; 11 | private String name; 12 | private int number; 13 | private String detail; 14 | 15 | @Override 16 | public String toString() { 17 | return "Book{" + 18 | "bookId=" + bookId + 19 | ", name='" + name + '\'' + 20 | ", number=" + number + 21 | ", detail='" + detail + '\'' + 22 | '}'; 23 | } 24 | 25 | public long getBookId() { 26 | return bookId; 27 | } 28 | 29 | public void setBookId(long bookId) { 30 | this.bookId = bookId; 31 | } 32 | 33 | public String getName() { 34 | return name; 35 | } 36 | 37 | public void setName(String name) { 38 | this.name = name; 39 | } 40 | 41 | public int getNumber() { 42 | return number; 43 | } 44 | 45 | public void setNumber(int number) { 46 | this.number = number; 47 | } 48 | 49 | public String getDetail() { 50 | return detail; 51 | } 52 | 53 | public void setDetail(String detail) { 54 | this.detail = detail; 55 | } 56 | } 57 | -------------------------------------------------------------------------------- /BookSystem_V2/src/main/java/com/hisen/entity/User.java: -------------------------------------------------------------------------------- 1 | package com.hisen.entity; 2 | 3 | public class User extends UserKey { 4 | /** 5 | * This field was generated by MyBatis Generator. 6 | * This field corresponds to the database column user.user_name 7 | * 8 | * @mbggenerated Thu Aug 03 16:47:27 CST 2017 9 | */ 10 | private String userName; 11 | 12 | /** 13 | * This field was generated by MyBatis Generator. 14 | * This field corresponds to the database column user.user_phone 15 | * 16 | * @mbggenerated Thu Aug 03 16:47:27 CST 2017 17 | */ 18 | private String userPhone; 19 | 20 | /** 21 | * This field was generated by MyBatis Generator. 22 | * This field corresponds to the database column user.user_address 23 | * 24 | * @mbggenerated Thu Aug 03 16:47:27 CST 2017 25 | */ 26 | private String userAddress; 27 | 28 | /** 29 | * This method was generated by MyBatis Generator. 30 | * This method returns the value of the database column user.user_name 31 | * 32 | * @return the value of user.user_name 33 | * 34 | * @mbggenerated Thu Aug 03 16:47:27 CST 2017 35 | */ 36 | public String getUserName() { 37 | return userName; 38 | } 39 | 40 | /** 41 | * This method was generated by MyBatis Generator. 42 | * This method sets the value of the database column user.user_name 43 | * 44 | * @param userName the value for user.user_name 45 | * 46 | * @mbggenerated Thu Aug 03 16:47:27 CST 2017 47 | */ 48 | public void setUserName(String userName) { 49 | this.userName = userName == null ? null : userName.trim(); 50 | } 51 | 52 | /** 53 | * This method was generated by MyBatis Generator. 54 | * This method returns the value of the database column user.user_phone 55 | * 56 | * @return the value of user.user_phone 57 | * 58 | * @mbggenerated Thu Aug 03 16:47:27 CST 2017 59 | */ 60 | public String getUserPhone() { 61 | return userPhone; 62 | } 63 | 64 | /** 65 | * This method was generated by MyBatis Generator. 66 | * This method sets the value of the database column user.user_phone 67 | * 68 | * @param userPhone the value for user.user_phone 69 | * 70 | * @mbggenerated Thu Aug 03 16:47:27 CST 2017 71 | */ 72 | public void setUserPhone(String userPhone) { 73 | this.userPhone = userPhone == null ? null : userPhone.trim(); 74 | } 75 | 76 | /** 77 | * This method was generated by MyBatis Generator. 78 | * This method returns the value of the database column user.user_address 79 | * 80 | * @return the value of user.user_address 81 | * 82 | * @mbggenerated Thu Aug 03 16:47:27 CST 2017 83 | */ 84 | public String getUserAddress() { 85 | return userAddress; 86 | } 87 | 88 | /** 89 | * This method was generated by MyBatis Generator. 90 | * This method sets the value of the database column user.user_address 91 | * 92 | * @param userAddress the value for user.user_address 93 | * 94 | * @mbggenerated Thu Aug 03 16:47:27 CST 2017 95 | */ 96 | public void setUserAddress(String userAddress) { 97 | this.userAddress = userAddress == null ? null : userAddress.trim(); 98 | } 99 | } -------------------------------------------------------------------------------- /BookSystem_V2/src/main/java/com/hisen/entity/UserKey.java: -------------------------------------------------------------------------------- 1 | package com.hisen.entity; 2 | 3 | public class UserKey { 4 | /** 5 | * This field was generated by MyBatis Generator. 6 | * This field corresponds to the database column user.user_id 7 | * 8 | * @mbggenerated Thu Aug 03 16:47:27 CST 2017 9 | */ 10 | private Integer userId; 11 | 12 | /** 13 | * This field was generated by MyBatis Generator. 14 | * This field corresponds to the database column user.user_number 15 | * 16 | * @mbggenerated Thu Aug 03 16:47:27 CST 2017 17 | */ 18 | private String userNumber; 19 | 20 | /** 21 | * This method was generated by MyBatis Generator. 22 | * This method returns the value of the database column user.user_id 23 | * 24 | * @return the value of user.user_id 25 | * 26 | * @mbggenerated Thu Aug 03 16:47:27 CST 2017 27 | */ 28 | public Integer getUserId() { 29 | return userId; 30 | } 31 | 32 | /** 33 | * This method was generated by MyBatis Generator. 34 | * This method sets the value of the database column user.user_id 35 | * 36 | * @param userId the value for user.user_id 37 | * 38 | * @mbggenerated Thu Aug 03 16:47:27 CST 2017 39 | */ 40 | public void setUserId(Integer userId) { 41 | this.userId = userId; 42 | } 43 | 44 | /** 45 | * This method was generated by MyBatis Generator. 46 | * This method returns the value of the database column user.user_number 47 | * 48 | * @return the value of user.user_number 49 | * 50 | * @mbggenerated Thu Aug 03 16:47:27 CST 2017 51 | */ 52 | public String getUserNumber() { 53 | return userNumber; 54 | } 55 | 56 | /** 57 | * This method was generated by MyBatis Generator. 58 | * This method sets the value of the database column user.user_number 59 | * 60 | * @param userNumber the value for user.user_number 61 | * 62 | * @mbggenerated Thu Aug 03 16:47:27 CST 2017 63 | */ 64 | public void setUserNumber(String userNumber) { 65 | this.userNumber = userNumber == null ? null : userNumber.trim(); 66 | } 67 | } -------------------------------------------------------------------------------- /BookSystem_V2/src/main/java/com/hisen/service/AppointmentService.java: -------------------------------------------------------------------------------- 1 | package com.hisen.service; 2 | 3 | import com.hisen.dao.form.AppointmentForm; 4 | import com.hisen.entity.Appointment; 5 | 6 | /** 7 | * Created by hisenyuan on 2017/8/2 at 10:36. 8 | */ 9 | public interface AppointmentService { 10 | int appoint(AppointmentForm record); 11 | int returnBook(Appointment record); 12 | } 13 | -------------------------------------------------------------------------------- /BookSystem_V2/src/main/java/com/hisen/service/BookService.java: -------------------------------------------------------------------------------- 1 | package com.hisen.service; 2 | 3 | import com.hisen.entity.Book; 4 | import java.util.List; 5 | 6 | /** 7 | * Created by hisen on 17-4-24. 8 | */ 9 | public interface BookService { 10 | Book getById(long bookId); 11 | List getList(int start, int pageNum); 12 | int addBook(Book book); 13 | int updateBook(Book book); 14 | int deleteBookById(long id); 15 | int countNum(); 16 | List getListPlug(); 17 | } 18 | -------------------------------------------------------------------------------- /BookSystem_V2/src/main/java/com/hisen/service/impl/AppointmentServiceImpl.java: -------------------------------------------------------------------------------- 1 | package com.hisen.service.impl; 2 | 3 | import static com.google.common.base.Preconditions.checkNotNull; 4 | 5 | import com.hisen.dao.AppointmentMapper; 6 | import com.hisen.dao.BookDao; 7 | import com.hisen.dao.form.AppointmentForm; 8 | import com.hisen.entity.Appointment; 9 | import com.hisen.entity.AppointmentExample; 10 | import com.hisen.entity.Book; 11 | import com.hisen.service.AppointmentService; 12 | import java.util.List; 13 | import org.joda.time.DateTime; 14 | import org.slf4j.Logger; 15 | import org.slf4j.LoggerFactory; 16 | import org.springframework.beans.factory.annotation.Autowired; 17 | import org.springframework.stereotype.Service; 18 | import org.springframework.transaction.annotation.Transactional; 19 | 20 | /** 21 | * Created by hisenyuan on 2017/8/2 at 10:36. 22 | */ 23 | @Service 24 | public class AppointmentServiceImpl implements AppointmentService { 25 | 26 | private Logger logger = LoggerFactory.getLogger(this.getClass()); 27 | 28 | @Autowired 29 | private AppointmentMapper appointmentMapper; 30 | @Autowired 31 | private BookDao bookDao; 32 | 33 | /** 34 | * 预约图书 35 | */ 36 | //如果发生了异常,就进行回滚 37 | @Transactional(rollbackFor = {Exception.class}) 38 | public int appoint(AppointmentForm record) { 39 | //利用google guava判空 40 | checkNotNull(record.getUserNumber(), "用户号不能为空"); 41 | checkNotNull(record.getBookId(), "图书编号不能为空"); 42 | //利用joda-time生成时间,并且计算时间 43 | DateTime dt = new DateTime(); 44 | record.setAppointmentTime(dt.toDate()); 45 | //预计持有时间 46 | int holdDay = Integer.valueOf(record.getHoldDay()); 47 | //利用持有时间计算预计归还时间 48 | record.setExpectReturnTime(dt.plusDays(holdDay).toDate()); 49 | Book book = bookDao.queryById(record.getBookId()); 50 | int num = book.getNumber(); 51 | int insert = 0; 52 | if (num >= 1) { 53 | insert = appointmentMapper.insert(record); 54 | book.setNumber(num - 1); 55 | logger.info("借书入参 AppointmentServiceImpl >>>>> " + record.toString()); 56 | bookDao.updateBook(book); 57 | // checkNotNull(null, "出现异常,事物回滚。用来测试事物控制"); 58 | } 59 | return insert; 60 | } 61 | 62 | /** 63 | * 归还图书 64 | */ 65 | @Transactional(rollbackFor = {Exception.class}) 66 | public int returnBook(Appointment record) { 67 | //利用google guava判空 68 | checkNotNull(record.getUserNumber(), "用户号不能为空"); 69 | checkNotNull(record.getBookId(), "图书编号不能为空"); 70 | //利用joda-time生成时间,并且计算时间 71 | DateTime dt = new DateTime(); 72 | //查询是否存在已经借出的书籍 73 | AppointmentExample example = new AppointmentExample(); 74 | example.createCriteria() 75 | .andBookIdEqualTo(Integer.valueOf(record.getBookId())) 76 | .andUserNumberEqualTo(Integer.valueOf(record.getUserNumber())); 77 | List appointments = appointmentMapper.selectByExample(example); 78 | 79 | logger.info("appointments >>>>> " + appointments.toString()); 80 | logger.info("appointments size >>>>> " + appointments.size()); 81 | logger.info("appointments status >>>>> " + appointments.get(0).getStatus()); 82 | int i = 0; 83 | if (appointments.size() > 0) { 84 | Appointment appointment = appointments.get(0); 85 | if (0 == appointment.getStatus()) { 86 | appointment.setRealReturnTime(new DateTime().toDate()); 87 | appointment.setStatus(1); 88 | logger.info("还书入参 AppointmentServiceImpl >>>>> " + appointment.toString()); 89 | i = appointmentMapper.updateByPrimaryKey(appointment); 90 | logger.info("appointments i >>>>> " + i); 91 | } 92 | } else { 93 | logger.info("AppointmentServiceImpl 为进入更新状态 >>>>> " + i); 94 | } 95 | return i; 96 | } 97 | } 98 | -------------------------------------------------------------------------------- /BookSystem_V2/src/main/java/com/hisen/service/impl/BookServiceImpl.java: -------------------------------------------------------------------------------- 1 | package com.hisen.service.impl; 2 | 3 | import com.hisen.dao.BookDao; 4 | import com.hisen.entity.Book; 5 | import com.hisen.service.BookService; 6 | import java.util.List; 7 | import org.springframework.beans.factory.annotation.Autowired; 8 | import org.springframework.stereotype.Service; 9 | 10 | /** 11 | * Created by hisen on 17-4-24. 12 | */ 13 | @Service 14 | public class BookServiceImpl implements BookService { 15 | 16 | @Autowired 17 | private BookDao bookDao; 18 | 19 | public Book getById(long bookId) { 20 | return bookDao.queryById(bookId); 21 | } 22 | 23 | public List getList(int start, int pageNum) { 24 | return bookDao.queryAll(start, pageNum); 25 | } 26 | 27 | public int addBook(Book book) { 28 | return bookDao.addBook(book); 29 | } 30 | 31 | public int updateBook(Book book) { 32 | return bookDao.updateBook(book); 33 | } 34 | 35 | public int deleteBookById(long id) { 36 | return bookDao.deleteBookById(id); 37 | } 38 | 39 | public int countNum() { 40 | return bookDao.countNum(); 41 | } 42 | 43 | public List getListPlug() { 44 | return bookDao.queryAllPlug(); 45 | } 46 | } 47 | -------------------------------------------------------------------------------- /BookSystem_V2/src/main/java/com/hisen/web/AppointmengtController.java: -------------------------------------------------------------------------------- 1 | package com.hisen.web; 2 | 3 | import com.alibaba.fastjson.JSON; 4 | import com.hisen.dao.form.AppointmentForm; 5 | import com.hisen.entity.Appointment; 6 | import com.hisen.service.AppointmentService; 7 | import org.slf4j.Logger; 8 | import org.slf4j.LoggerFactory; 9 | import org.springframework.beans.factory.annotation.Autowired; 10 | import org.springframework.stereotype.Controller; 11 | import org.springframework.web.bind.annotation.PathVariable; 12 | import org.springframework.web.bind.annotation.RequestMapping; 13 | import org.springframework.web.bind.annotation.RequestMethod; 14 | import org.springframework.web.bind.annotation.ResponseBody; 15 | 16 | /** 17 | * Created by hisenyuan on 2017/8/2 at 10:27. 18 | */ 19 | @Controller 20 | @RequestMapping("/appointment") 21 | public class AppointmengtController { 22 | 23 | private Logger logger = LoggerFactory.getLogger(this.getClass()); 24 | @Autowired 25 | private AppointmentService appointmentService; 26 | 27 | /** 28 | * 借书的一个小功能 29 | * 本地测试成功,暂时不想写页面 30 | * localhost:8080/V2/appointment/appoint/103/20080808/20 31 | */ 32 | @RequestMapping(value = "/appoint/{bookId}/{userNumber}/{holdDay}", method = RequestMethod.GET, produces = "text/plain;charset=UTF-8") 33 | @ResponseBody 34 | public String appoint(@PathVariable("bookId") int bookId, 35 | @PathVariable("userNumber") int userNumber, @PathVariable("holdDay") String holdDay) { 36 | AppointmentForm form = new AppointmentForm(); 37 | form.setBookId(bookId); 38 | form.setUserNumber(userNumber); 39 | form.setHoldDay(holdDay); 40 | logger.info("借书入参 AppointmengtController >>>>> " + form.toString()); 41 | int appoint = appointmentService.appoint(form); 42 | String s = JSON.toJSONString(appoint > 0 ? "借书成功" : "借书失败"); 43 | return s; 44 | } 45 | 46 | @RequestMapping(value = "/return/{bookId}/{userNumber}", method = RequestMethod.GET, produces = "text/plain;charset=UTF-8") 47 | @ResponseBody 48 | public String returnBook(@PathVariable("bookId") int bookId, 49 | @PathVariable("userNumber") int userNumber) { 50 | Appointment form = new Appointment(); 51 | form.setBookId(bookId); 52 | form.setUserNumber(userNumber); 53 | logger.info("还书书入参 AppointmengtController >>>>> " + form.toString()); 54 | int i = appointmentService.returnBook(form); 55 | String s = JSON.toJSONString(i > 0 ? "还书成功" : "还书失败"); 56 | return s; 57 | } 58 | } 59 | -------------------------------------------------------------------------------- /BookSystem_V2/src/main/java/com/hisen/web/BookController.java: -------------------------------------------------------------------------------- 1 | package com.hisen.web; 2 | 3 | import com.alibaba.fastjson.JSON; 4 | import com.github.pagehelper.PageHelper; 5 | import com.hisen.entity.Book; 6 | import com.hisen.service.BookService; 7 | import java.util.List; 8 | import org.slf4j.Logger; 9 | import org.slf4j.LoggerFactory; 10 | import org.springframework.beans.factory.annotation.Autowired; 11 | import org.springframework.stereotype.Controller; 12 | import org.springframework.ui.Model; 13 | import org.springframework.web.bind.annotation.PathVariable; 14 | import org.springframework.web.bind.annotation.RequestMapping; 15 | import org.springframework.web.bind.annotation.RequestMethod; 16 | import org.springframework.web.bind.annotation.RequestParam; 17 | import org.springframework.web.bind.annotation.ResponseBody; 18 | 19 | /** 20 | * Created by hisen on 17-4-24. 21 | */ 22 | @Controller 23 | @RequestMapping("/book") 24 | public class BookController { 25 | private Logger logger = LoggerFactory.getLogger(this.getClass()); 26 | 27 | @Autowired 28 | private BookService bookService; 29 | 30 | @RequestMapping(value = "/list", method = RequestMethod.GET) 31 | private String list(Model model) { 32 | List list = bookService.getList(0, 10); 33 | model.addAttribute("books", list); 34 | return "list";// WEB-INF/jsp/"list".jsp 35 | } 36 | 37 | @RequestMapping(value = "/detail/{bookId}", method = RequestMethod.GET) 38 | private String detail(@PathVariable("bookId") Long bookId, Model model) { 39 | Book book = bookService.getById(bookId); 40 | model.addAttribute("book", book); 41 | return "detail"; 42 | } 43 | 44 | @RequestMapping(value = "/add", method = RequestMethod.POST, produces = "text/plain;charset=UTF-8") 45 | @ResponseBody 46 | private String add(Book book) { 47 | Book hasBook = bookService.getById(book.getBookId()); 48 | int i = -2; 49 | if (hasBook == null) { 50 | i = bookService.addBook(book); 51 | } 52 | return i > 0 ? "success" : "error"; 53 | } 54 | 55 | @RequestMapping(value = "/del/{bookId}", method = RequestMethod.GET) 56 | @ResponseBody 57 | private String deleteBookById(@PathVariable("bookId") Long id) { 58 | int i = bookService.deleteBookById(id); 59 | return i > 0 ? "success" : "error"; 60 | } 61 | 62 | /** 63 | * 查询总页数 64 | * @return 65 | */ 66 | @RequestMapping(value = "/countNum", method = RequestMethod.POST, produces = { 67 | "application/json; charset=utf-8"}) 68 | @ResponseBody 69 | private int countNum() { 70 | int num = bookService.countNum(); 71 | //计算页数,如果除以10有余数,得加上一页 72 | int countNum = num / 10 + ((num % 10) > 0 ? 1 : 0); 73 | return countNum; 74 | } 75 | 76 | /** 77 | * 分页查询方法 78 | * @param start 79 | * @return 80 | */ 81 | @RequestMapping(value = "/listpage", method = RequestMethod.POST) 82 | @ResponseBody 83 | private String listPage(@RequestParam("start") int start) { 84 | //默认一页10条 85 | List list = bookService.getList(start, 10); 86 | //阿里fastjson把数组转换为json 87 | String s = JSON.toJSONString(list); 88 | //System.out.println(s); 89 | return s; 90 | } 91 | 92 | @RequestMapping(value = "/listpageplug/{start}", method = RequestMethod.GET) 93 | // @ResponseBody 94 | private String listPagePlug(@PathVariable("start") String start, Model model) { 95 | PageHelper.startPage(Integer.valueOf(start), 20); 96 | //默认一页10条 97 | List readingList = bookService.getListPlug(); 98 | model.addAttribute("books", readingList); 99 | //System.out.println(s); 100 | return "readingList"; 101 | } 102 | } 103 | -------------------------------------------------------------------------------- /BookSystem_V2/src/main/resources/generatorConfig.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 8 | 9 | 10 | 11 | 12 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | -------------------------------------------------------------------------------- /BookSystem_V2/src/main/resources/jdbc.properties: -------------------------------------------------------------------------------- 1 | jdbc.driverClassName=com.mysql.jdbc.Driver 2 | jdbc.url=jdbc:mysql://127.0.0.1:3306/booksystem?useUnicode=true&characterEncoding=utf8 3 | jdbc.username=root 4 | jdbc.password=hisen -------------------------------------------------------------------------------- /BookSystem_V2/src/main/resources/logback.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | %d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n 7 | 8 | 9 | 10 | 11 | 12 | 13 | -------------------------------------------------------------------------------- /BookSystem_V2/src/main/resources/mapper/BookMapper.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | SELECT book_id,name,number,detail 12 | FROM 13 | book 14 | WHERE 15 | book_id = #{bookId} 16 | 17 | 18 | 19 | SELECT 20 | book_id, 21 | name, 22 | number, 23 | detail 24 | FROM 25 | book 26 | ORDER BY 27 | book_id 28 | LIMIT #{offset}, #{limit} 29 | 30 | 31 | 32 | SELECT 33 | book_id, 34 | name, 35 | number, 36 | detail 37 | FROM 38 | book 39 | ORDER BY 40 | book_id 41 | 42 | 43 | 44 | UPDATE book 45 | SET NAME = #{name}, 46 | NUMBER = #{number}, 47 | detail = #{detail} 48 | WHERE 49 | book_id = #{bookId} 50 | AND number > 0 51 | 52 | 53 | 54 | INSERT INTO book(`book_id`, `name`, `number`,`detail`) 55 | VALUES(#{bookId}, #{name}, #{number}, #{detail}) 56 | 57 | 58 | 59 | DELETE FROM book WHERE book_id = #{bookId} 60 | 61 | 62 | SELECT 63 | COUNT(*) 64 | FROM 65 | book 66 | 67 | -------------------------------------------------------------------------------- /BookSystem_V2/src/main/resources/mybatis-config.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | 45 | -------------------------------------------------------------------------------- /BookSystem_V2/src/main/resources/redis.properties: -------------------------------------------------------------------------------- 1 | # Redis settings 2 | redis.host=127.0.0.1 3 | redis.port=6379 4 | redis.pass= 5 | 6 | redis.maxIdle=300 7 | redis.maxActive=600 8 | redis.maxWait=1000 9 | redis.testOnBorrow=true -------------------------------------------------------------------------------- /BookSystem_V2/src/main/resources/spring/spring-aop.xml: -------------------------------------------------------------------------------- 1 | 2 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | -------------------------------------------------------------------------------- /BookSystem_V2/src/main/resources/spring/spring-service.xml: -------------------------------------------------------------------------------- 1 | 2 | 12 | 13 | 14 | 15 | 16 | 18 | 19 | 20 | 21 | 22 | 23 | -------------------------------------------------------------------------------- /BookSystem_V2/src/main/resources/spring/spring-web.xml: -------------------------------------------------------------------------------- 1 | 2 | 12 | 13 | 14 | 18 | 19 | 20 | 24 | 25 | 26 | 27 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | 45 | 46 | 47 | -------------------------------------------------------------------------------- /BookSystem_V2/src/main/webapp/WEB-INF/jsp/detail.jsp: -------------------------------------------------------------------------------- 1 | <%-- 2 | Created by IntelliJ IDEA. 3 | User: Administrator 4 | Date: 2017/4/6 5 | Time: 20:21 6 | To change this template use File | Settings | File Templates. 7 | --%> 8 | <%@ page contentType="text/html;charset=UTF-8" language="java" %> 9 | <% String appPath = request.getContextPath(); %> 10 | 11 | 12 | 图书详情 13 | 14 | 15 | 16 | 17 | 18 | 19 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | HiSEN 图书管理系统 - by ssm基础框架 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 首页 39 | 图书具体信息 40 | 信息 41 | 42 | 43 | 44 | 45 | 46 | 47 | 48 | 49 | 图书详情 显示当前图书详细信息 50 | 51 | 52 | 53 | 54 | 55 | 56 | 57 | 58 | 59 | 图书编号 60 | 图书名字 61 | 图书数量 62 | 63 | 64 | 65 | 66 | ${requestScope.get('book').bookId} 67 | ${requestScope.get('book').name} 68 | ${requestScope.get('book').number} 69 | 70 | 71 | 72 | 73 | 74 | 75 | 76 | 77 | 《${requestScope.get('book').name}》 ${requestScope.get('book').detail} 78 | 79 | 80 | 81 | 82 | 83 | 84 | 85 | 86 | 87 | 88 | -------------------------------------------------------------------------------- /BookSystem_V2/src/main/webapp/WEB-INF/templates/booklist.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | Title 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 图书编号 16 | 图书名字 17 | 图书数量 18 | 操作 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 详情 | 32 | 删除 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | -------------------------------------------------------------------------------- /BookSystem_V2/src/main/webapp/WEB-INF/templates/detail.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 图书详情 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 首页 26 | 图书具体信息 27 | 添加图书 28 | 信息 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 图书详情 38 | 显示当前图书详细信息 39 | 40 | 41 | 42 | 43 | 44 | 45 | 46 | 47 | 48 | 图书编号 49 | 图书名字 50 | 图书数量 51 | 52 | 53 | 54 | 55 | 56 | 57 | 58 | 59 | 60 | 61 | 62 | 63 | 64 | 65 | 66 | } 67 | 68 | 69 | 70 | 71 | 72 | 73 | 74 | 75 | 76 | -------------------------------------------------------------------------------- /BookSystem_V2/src/main/webapp/WEB-INF/templates/footer.html: -------------------------------------------------------------------------------- 1 | 2 | 4 | 5 | 6 | 7 | ... 8 | 9 | 12 | 13 | 14 | -------------------------------------------------------------------------------- /BookSystem_V2/src/main/webapp/WEB-INF/templates/header.html: -------------------------------------------------------------------------------- 1 | 2 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | HiSEN 11 | 12 | 13 | 14 | 15 | 16 | 17 | -------------------------------------------------------------------------------- /BookSystem_V2/src/main/webapp/WEB-INF/templates/list.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 图书列表 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 首页 18 | 图书具体信息 19 | 添加图书 20 | 信息 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 图书编号 30 | 图书名字 31 | 图书数量 32 | 操作 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | 45 | 详情 | 46 | 删除 47 | 48 | 49 | 50 | 51 | 52 | 53 | 54 | 55 | 56 | 57 | 58 | -------------------------------------------------------------------------------- /BookSystem_V2/src/main/webapp/WEB-INF/templates/page.html: -------------------------------------------------------------------------------- 1 | 2 | 4 | 5 | 6 | 47 | 48 | -------------------------------------------------------------------------------- /BookSystem_V2/src/main/webapp/WEB-INF/templates/readingList.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | Reading List 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 图书列表 19 | 显示当前图书库存信息 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 图书编号 30 | 图书名字 31 | 图书数量 32 | 操作 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | 详情 | 45 | 删除 46 | 47 | 48 | 49 | 50 | 51 | 52 | 53 | 54 | 55 | 56 | -------------------------------------------------------------------------------- /BookSystem_V2/src/main/webapp/WEB-INF/web.xml: -------------------------------------------------------------------------------- 1 | 5 | 6 | 7 | seckill-dispatcher 8 | org.springframework.web.servlet.DispatcherServlet 9 | 13 | 14 | contextConfigLocation 15 | classpath:spring/spring-*.xml 16 | 17 | 18 | 19 | seckill-dispatcher 20 | 21 | / 22 | 23 | 24 | 25 | 26 | 27 | DruidStatView 28 | com.alibaba.druid.support.http.StatViewServlet 29 | 30 | 31 | DruidStatView 32 | /druid/* 33 | 34 | 35 | druidWebStatFilter 36 | com.alibaba.druid.support.http.WebStatFilter 37 | 38 | exclusions 39 | /public/*,*.js,*.css,/druid*,*.jsp,*.swf 40 | 41 | 42 | principalSessionName 43 | sessionInfo 44 | 45 | 46 | profileEnable 47 | true 48 | 49 | 50 | 51 | druidWebStatFilter 52 | /* 53 | 54 | -------------------------------------------------------------------------------- /BookSystem_V2/src/main/webapp/add.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 添加图书 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | HiSEN 16 | 图书管理系统V2 - 添加图书 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 首页 25 | 图书具体信息 26 | 添加图书 27 | 信息 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 图书ID 37 | 38 | 39 | 图书名字 40 | 41 | 42 | 图书描述 43 | 44 | 45 | 图书数量 46 | 47 | Submit 48 | 49 | 50 | 51 | 52 | 53 | 54 | 55 | 76 | 77 | 78 | -------------------------------------------------------------------------------- /BookSystem_V2/src/main/webapp/index.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | SSM_BookSystem --- V2 7 | 开发工具:intelliJ IDEA 8 | 9 | 项目相关:maven-3.39、jdk-8、github 10 | 11 | 相关框架:Spring-4.17、SpringMVC-4.17、Mybatis-3.30 12 | 13 | 数 据 库:mysql(存放数据)、redis(缓存) 14 | 15 | 模板引擎:thymeleaf 16 | 17 | 更新:2017年5月18日 17:51:22 18 | 19 | 作者:hisenyuan 20 | 21 | 网站:hisen.me 22 | 23 | 图书系统:SSM_BookSystem V2(点击进入) 24 | 25 | 26 | -------------------------------------------------------------------------------- /BookSystem_V2/src/test/AppointmentServiceImplTest.java: -------------------------------------------------------------------------------- 1 | import com.hisen.dao.form.AppointmentForm; 2 | import com.hisen.service.AppointmentService; 3 | import org.junit.Test; 4 | import org.springframework.beans.factory.annotation.Autowired; 5 | 6 | /** 7 | * Created by hisenyuan on 2017/8/2 at 11:03. 8 | */ 9 | public class AppointmentServiceImplTest extends BaseTest { 10 | 11 | @Autowired 12 | private AppointmentService appointmentService; 13 | 14 | /** 15 | * 测试预约功能 16 | */ 17 | @Test 18 | public void insertApponit() { 19 | AppointmentForm form = new AppointmentForm(); 20 | form.setHoldDay("10"); 21 | form.setBookId(103); 22 | form.setUserNumber(20080808); 23 | int appoint = appointmentService.appoint(form); 24 | System.out.println(appoint); 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /BookSystem_V2/src/test/BaseTest.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Created by hisenyuan on 2017/5/3 at 19:40. 3 | */ 4 | 5 | import org.junit.runner.RunWith; 6 | import org.springframework.test.context.ContextConfiguration; 7 | import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; 8 | 9 | /** 10 | * 配置spring和junit整合,junit启动时加载springIOC容器 spring-test,junit 11 | */ 12 | @RunWith(SpringJUnit4ClassRunner.class) 13 | // 告诉junit spring配置文件 14 | @ContextConfiguration({ "classpath:spring/spring-dao.xml", "classpath:spring/spring-service.xml" }) 15 | public class BaseTest { 16 | 17 | } 18 | -------------------------------------------------------------------------------- /BookSystem_V2/src/test/UserDaoTest.java: -------------------------------------------------------------------------------- 1 | import com.hisen.dao.UserMapper; 2 | import com.hisen.entity.User; 3 | import org.junit.Test; 4 | import org.springframework.beans.factory.annotation.Autowired; 5 | 6 | /** 7 | * Created by hisenyuan on 2017/8/2 at 11:18. 8 | */ 9 | public class UserDaoTest extends BaseTest { 10 | @Autowired 11 | private UserMapper userMapper; 12 | 13 | /** 14 | * 测试添加用户 15 | */ 16 | @Test 17 | public void insert(){ 18 | User u = new User(); 19 | u.setUserName("阿星"); 20 | u.setUserAddress("北京市朝阳区"); 21 | u.setUserPhone("13820080001"); 22 | u.setUserNumber("20080001"); 23 | int insert = userMapper.insert(u); 24 | System.out.println(insert); 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /BookSystem_V3/src/main/java/com/hisen/aop/GetMethodInfoHandler.java: -------------------------------------------------------------------------------- 1 | package com.hisen.aop; 2 | 3 | import java.util.Arrays; 4 | import org.aspectj.lang.JoinPoint; 5 | 6 | /** 7 | * Created by hisenyuan on 2017/7/26 at 16:43. 8 | */ 9 | public class GetMethodInfoHandler { 10 | public void getInfo(JoinPoint joinPoint) { 11 | System.out.println(); 12 | // Object[] getArgs:返回目标方法的参数 13 | // Signature getSignature:返回目标方法的签名 14 | // Object getTarget:返回被织入增强处理的目标对象 15 | // Object getThis:返回AOP框架为目标对象生成的代理对象 16 | System.out.println("执行过的函数相关信息 >>>>> 方法" + joinPoint.getSignature()); 17 | System.out.println("执行过的函数相关信息 >>>>> 参数" + Arrays.asList(joinPoint.getArgs())); 18 | System.out.println("执行过的函数相关信息 >>>>> 目标" + joinPoint.getTarget()); 19 | System.out.println("执行过的函数相关信息 >>>>> 代理" + joinPoint.getThis()); 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /BookSystem_V3/src/main/java/com/hisen/aop/TimeHandler.java: -------------------------------------------------------------------------------- 1 | package com.hisen.aop; 2 | 3 | import java.text.SimpleDateFormat; 4 | import java.util.Date; 5 | 6 | /** 7 | * Created by hisenyuan on 2017/7/26 at 15:53. 8 | */ 9 | public class TimeHandler { 10 | /** 11 | * 打印时间 12 | */ 13 | public void printTime() { 14 | //此处准备记录运行日志 15 | String time = new SimpleDateFormat("yyyy-MM-dd hh24:mm:ss").format(new Date()); 16 | System.out.println(); 17 | System.out.println("Spring AOP >>>>> 当前时间 = " + time); 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /BookSystem_V3/src/main/java/com/hisen/bean/contains/CommonEnum.java: -------------------------------------------------------------------------------- 1 | package com.hisen.bean.contains; 2 | 3 | /** 4 | * 通用的枚举类型 5 | * Created by hisen on 17-8-26. 6 | * E-mail: hisenyuan@gmail.com 7 | */ 8 | public enum CommonEnum { 9 | LOGIN_FAILED(10001, "登录失败,账号或者密码错误"), 10 | LOGIN_SUCCESS(10002, "登录成功"), 11 | LOGIN_LOCKED(10003, "账号被锁定,请重置密码"), 12 | REQUEST_SUCCESS(10003, "请求成功"), 13 | REQUEST_FAILED(10004, "请求成功"), 14 | JWT_SECRET(10005,"XX#$%()(#*!()!KL<>?N<:{LWPW_hisen"), 15 | JWT_PAYLOAD(10006,"payload"), 16 | JWT_MAXAGE(10007,"3600000");// 60 * 60 * 1000 = 3600000 也就是一个小时 17 | private int code; 18 | private String msg; 19 | 20 | CommonEnum(int code, String msg) { 21 | this.code = code; 22 | this.msg = msg; 23 | } 24 | 25 | public int getCode() { 26 | return code; 27 | } 28 | 29 | public void setCode(int code) { 30 | this.code = code; 31 | } 32 | 33 | public String getMsg() { 34 | return msg; 35 | } 36 | 37 | public void setMsg(String msg) { 38 | this.msg = msg; 39 | } 40 | } 41 | -------------------------------------------------------------------------------- /BookSystem_V3/src/main/java/com/hisen/bean/dto/UserInfoLoginDto.java: -------------------------------------------------------------------------------- 1 | package com.hisen.bean.dto; 2 | 3 | /** 4 | * Created by hisen on 17-8-26. 5 | * E-mail: hisenyuan@gmail.com 6 | */ 7 | public class UserInfoLoginDto { 8 | 9 | private String name; 10 | private String pwd; 11 | private Integer userState; 12 | private Integer userType; 13 | 14 | public String getName() { 15 | return name; 16 | } 17 | 18 | public void setName(String name) { 19 | this.name = name; 20 | } 21 | 22 | public String getPwd() { 23 | return pwd; 24 | } 25 | 26 | public void setPwd(String pwd) { 27 | this.pwd = pwd; 28 | } 29 | 30 | public Integer getUserState() { 31 | return userState; 32 | } 33 | 34 | public void setUserState(Integer userState) { 35 | this.userState = userState; 36 | } 37 | 38 | public Integer getUserType() { 39 | return userType; 40 | } 41 | 42 | public void setUserType(Integer userType) { 43 | this.userType = userType; 44 | } 45 | } 46 | -------------------------------------------------------------------------------- /BookSystem_V3/src/main/java/com/hisen/bean/entity/Book.java: -------------------------------------------------------------------------------- 1 | package com.hisen.bean.entity; 2 | 3 | import java.io.Serializable; 4 | 5 | /** 6 | * 图书实体类,需要实现Serializable接口以便存放在redis中 7 | * Created by hisen on 17-4-24. 8 | */ 9 | public class Book implements Serializable { 10 | private long bookId; 11 | private String name; 12 | private int number; 13 | private String detail; 14 | 15 | @Override 16 | public String toString() { 17 | return "Book{" + 18 | "bookId=" + bookId + 19 | ", name='" + name + '\'' + 20 | ", number=" + number + 21 | ", detail='" + detail + '\'' + 22 | '}'; 23 | } 24 | 25 | public long getBookId() { 26 | return bookId; 27 | } 28 | 29 | public void setBookId(long bookId) { 30 | this.bookId = bookId; 31 | } 32 | 33 | public String getName() { 34 | return name; 35 | } 36 | 37 | public void setName(String name) { 38 | this.name = name; 39 | } 40 | 41 | public int getNumber() { 42 | return number; 43 | } 44 | 45 | public void setNumber(int number) { 46 | this.number = number; 47 | } 48 | 49 | public String getDetail() { 50 | return detail; 51 | } 52 | 53 | public void setDetail(String detail) { 54 | this.detail = detail; 55 | } 56 | } 57 | -------------------------------------------------------------------------------- /BookSystem_V3/src/main/java/com/hisen/bean/entity/JWTInfo.java: -------------------------------------------------------------------------------- 1 | package com.hisen.bean.entity; 2 | 3 | /** 4 | * Created by hisen on 17-8-27. 5 | * E-mail: hisenyuan@gmail.com 6 | */ 7 | public class JWTInfo { 8 | private String username; 9 | private String password; 10 | 11 | public String getUsername() { 12 | return username; 13 | } 14 | 15 | public void setUsername(String username) { 16 | this.username = username; 17 | } 18 | 19 | public String getPassword() { 20 | return password; 21 | } 22 | 23 | public void setPassword(String password) { 24 | this.password = password; 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /BookSystem_V3/src/main/java/com/hisen/bean/request/AppointmentRequest.java: -------------------------------------------------------------------------------- 1 | package com.hisen.bean.request; 2 | 3 | import com.hisen.bean.entity.Appointment; 4 | 5 | /** 6 | * Created by hisen on 17-8-27. 7 | * E-mail: hisenyuan@gmail.com 8 | */ 9 | public class AppointmentRequest extends Appointment{ 10 | private String holdDay; 11 | 12 | public String getHoldDay() { 13 | return holdDay; 14 | } 15 | 16 | public void setHoldDay(String holdDay) { 17 | this.holdDay = holdDay; 18 | } 19 | 20 | @Override 21 | public String toString() { 22 | return "AppointmentRequest{" + 23 | "holdDay='" + holdDay + '\'' + 24 | '}'; 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /BookSystem_V3/src/main/java/com/hisen/bean/request/CommonRequest.java: -------------------------------------------------------------------------------- 1 | package com.hisen.bean.request; 2 | 3 | import java.util.Date; 4 | 5 | /** 6 | * Created by hisen on 17-8-26. 7 | * E-mail: hisenyuan@gmail.com 8 | */ 9 | public class CommonRequest { 10 | private Date time = new Date(); 11 | 12 | public Date getTime() { 13 | return time; 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /BookSystem_V3/src/main/java/com/hisen/bean/request/UserLoginRequest.java: -------------------------------------------------------------------------------- 1 | package com.hisen.bean.request; 2 | 3 | /** 4 | * Created by hisen on 17-8-26. 5 | * E-mail: hisenyuan@gmail.com 6 | */ 7 | public class UserLoginRequest { 8 | private String name; 9 | private String pwd; 10 | 11 | public String getName() { 12 | return name; 13 | } 14 | 15 | public void setName(String name) { 16 | this.name = name; 17 | } 18 | 19 | public String getPwd() { 20 | return pwd; 21 | } 22 | 23 | public void setPwd(String pwd) { 24 | this.pwd = pwd; 25 | } 26 | 27 | @Override 28 | public String toString() { 29 | return "UserLoginRequest{" + 30 | "name='" + name + '\'' + 31 | ", pwd='" + pwd + '\'' + 32 | '}'; 33 | } 34 | } 35 | -------------------------------------------------------------------------------- /BookSystem_V3/src/main/java/com/hisen/bean/response/CommonResponse.java: -------------------------------------------------------------------------------- 1 | package com.hisen.bean.response; 2 | 3 | /** 4 | * Created by hisen on 17-8-26. 5 | * E-mail: hisenyuan@gmail.com 6 | */ 7 | public class CommonResponse { 8 | 9 | private int resCode; 10 | private String resMsg; 11 | 12 | public int getResCode() { 13 | return resCode; 14 | } 15 | 16 | public void setResCode(int resCode) { 17 | this.resCode = resCode; 18 | } 19 | 20 | public String getResMsg() { 21 | return resMsg; 22 | } 23 | 24 | public void setResMsg(String resMsg) { 25 | this.resMsg = resMsg; 26 | } 27 | } 28 | -------------------------------------------------------------------------------- /BookSystem_V3/src/main/java/com/hisen/bean/response/UserLoginResponse.java: -------------------------------------------------------------------------------- 1 | package com.hisen.bean.response; 2 | 3 | /** 4 | * Created by hisen on 17-8-26. 5 | * E-mail: hisenyuan@gmail.com 6 | */ 7 | public class UserLoginResponse extends CommonResponse { 8 | private String name; 9 | private Integer userState; 10 | private Integer userType; 11 | private String jwt; 12 | 13 | public String getJwt() { 14 | return jwt; 15 | } 16 | 17 | public void setJwt(String jwt) { 18 | this.jwt = jwt; 19 | } 20 | 21 | public String getName() { 22 | return name; 23 | } 24 | 25 | public void setName(String name) { 26 | this.name = name; 27 | } 28 | 29 | public Integer getUserState() { 30 | return userState; 31 | } 32 | 33 | public void setUserState(Integer userState) { 34 | this.userState = userState; 35 | } 36 | 37 | public Integer getUserType() { 38 | return userType; 39 | } 40 | 41 | public void setUserType(Integer userType) { 42 | this.userType = userType; 43 | } 44 | 45 | @Override 46 | public String toString() { 47 | return "UserLoginResponse{" + 48 | "name='" + name + '\'' + 49 | ", userState=" + userState + 50 | ", userType=" + userType + 51 | ", jwt='" + jwt + '\'' + 52 | '}'; 53 | } 54 | } 55 | -------------------------------------------------------------------------------- /BookSystem_V3/src/main/java/com/hisen/common/CookieUtil.java: -------------------------------------------------------------------------------- 1 | package com.hisen.common; 2 | 3 | import java.util.HashMap; 4 | import java.util.Map; 5 | import javax.servlet.http.Cookie; 6 | import javax.servlet.http.HttpServletRequest; 7 | import javax.servlet.http.HttpServletResponse; 8 | 9 | /** 10 | * Created by hisen on 17-8-26. 11 | * E-mail: hisenyuan@gmail.com 12 | */ 13 | public class CookieUtil { 14 | 15 | /** 16 | * 把cookie装入map,然后设置到response中 17 | */ 18 | public static void addCookieMap(HttpServletResponse response, Map map) { 19 | for (String key : map.keySet()) { 20 | String value = map.get(key); 21 | Cookie cookie = new Cookie(key.trim(), value.trim()); 22 | cookie.setMaxAge(30 * 60);// 设置为30min 23 | cookie.setPath("/"); 24 | response.addCookie(cookie); 25 | } 26 | } 27 | 28 | /** 29 | * 添加单个cookie 30 | * @param response 31 | * @param name 32 | * @param value 33 | */ 34 | public static void addCookie(HttpServletResponse response, String name, String value) { 35 | Cookie cookie = new Cookie(name.trim(), value.trim()); 36 | cookie.setMaxAge(30 * 60);// 设置为30min 37 | cookie.setPath("/"); 38 | response.addCookie(cookie); 39 | } 40 | 41 | /** 42 | * 对cookie进行封装 43 | */ 44 | public static Map ReadCookieMap(HttpServletRequest request) { 45 | Map cookieMap = new HashMap(); 46 | Cookie[] cookies = request.getCookies(); 47 | if (null != cookies) { 48 | for (Cookie cookie : cookies) { 49 | cookieMap.put(cookie.getName(), cookie); 50 | } 51 | } 52 | return cookieMap; 53 | } 54 | } 55 | -------------------------------------------------------------------------------- /BookSystem_V3/src/main/java/com/hisen/common/JWTUtil.java: -------------------------------------------------------------------------------- 1 | package com.hisen.common; 2 | 3 | import com.alibaba.fastjson.JSON; 4 | import com.auth0.jwt.JWT; 5 | import com.auth0.jwt.algorithms.Algorithm; 6 | import com.auth0.jwt.interfaces.Claim; 7 | import com.auth0.jwt.interfaces.DecodedJWT; 8 | import com.hisen.bean.contains.CommonEnum; 9 | import java.io.UnsupportedEncodingException; 10 | import java.util.Date; 11 | import java.util.HashMap; 12 | import java.util.Map; 13 | import org.joda.time.DateTime; 14 | 15 | /** 16 | * Created by hisenyuan on 2017/8/17 at 11:09. 17 | */ 18 | public class JWTUtil { 19 | 20 | 21 | 22 | private static final String SECRET = CommonEnum.JWT_SECRET.getMsg(); 23 | private static final String EXP = "exp"; 24 | private static final String PAYLOAD = CommonEnum.JWT_PAYLOAD.getMsg(); 25 | 26 | /** 27 | * 生成Token:jwt 28 | * @param object 传入的加密对象 - 放入PAYLOAD 29 | * @param maxAge 过期事件,单位毫秒 30 | * @param 31 | * @return 32 | */ 33 | public static String sign(T object, long maxAge) { 34 | Map map = new HashMap(); 35 | String jsonString = JSON.toJSONString(object); 36 | map.put("alg", "HS256"); 37 | map.put("typ", "JWT"); 38 | long exp = System.currentTimeMillis() + maxAge; 39 | System.out.println("JWTUtil 当前时间:"+new DateTime().toString("yyyy-MM-dd HH:mm:ss EE")); 40 | System.out.println("JWTUtil 过期时间:"+new DateTime(exp).toString("yyyy-MM-dd HH:mm:ss EE")); 41 | String token = null; 42 | try { 43 | token = JWT.create() 44 | .withHeader(map)//header 45 | .withClaim(PAYLOAD, jsonString)//存放的内容 json 46 | .withClaim(EXP, new DateTime(exp).toDate())//超时时间 47 | .sign(Algorithm.HMAC256(SECRET));//密钥 48 | } catch (UnsupportedEncodingException e) { 49 | e.printStackTrace(); 50 | } 51 | return token; 52 | } 53 | 54 | /** 55 | * 解密token 56 | * @param token jwt类型的token 57 | * @param classT 加密时的类型 58 | * @param 59 | * @return 返回解密后的对象 - 如果token过期返回空对象 60 | */ 61 | public static T unsign(String token, Class classT) { 62 | if (token == null) { 63 | return null; 64 | } 65 | DecodedJWT decode = JWT.decode(token); 66 | Map claims = decode.getClaims(); 67 | if (claims.containsKey(EXP) && claims.containsKey(PAYLOAD)){ 68 | long tokenTime = claims.get(EXP).asDate().getTime(); 69 | long nowTime = new Date().getTime(); 70 | // 判断令牌是否超时 71 | if (tokenTime > nowTime){ 72 | String json = claims.get(PAYLOAD).asString(); 73 | return JSON.parseObject(json, classT); 74 | } 75 | } 76 | 77 | // JWTVerifier verifier = null; 78 | // try { 79 | // verifier = JWT.require(Algorithm.HMAC256(SECRET)).build(); 80 | // DecodedJWT jwt = verifier.verify(token); // 如果超时,直接抛出运行时异常 81 | // Map claims = jwt.getClaims(); 82 | // if (claims.containsKey(EXP) && claims.containsKey(PAYLOAD)) { 83 | // long tokenTime = claims.get(EXP).asDate().getTime(); 84 | // long now = new Date().getTime(); 85 | // // 判断令牌是否已经超时 86 | // if (tokenTime > now) { 87 | // String json = claims.get(PAYLOAD).asString(); 88 | // // 把json转回对象,返回 89 | // return JSON.parseObject(json, classT); 90 | // } 91 | // } 92 | // } catch (UnsupportedEncodingException e) { 93 | // e.printStackTrace(); 94 | // } catch (TokenExpiredException e){ 95 | // e.printStackTrace(); 96 | // } 97 | return null; 98 | } 99 | } 100 | -------------------------------------------------------------------------------- /BookSystem_V3/src/main/java/com/hisen/common/MD5Util.java: -------------------------------------------------------------------------------- 1 | package com.hisen.common; 2 | 3 | import java.security.MessageDigest; 4 | import java.security.NoSuchAlgorithmException; 5 | 6 | /** 7 | * Created by hisen on 17-8-26. 8 | * E-mail: hisenyuan@gmail.com 9 | */ 10 | public class MD5Util { 11 | 12 | public static String toMd5(String str) { 13 | StringBuffer md5Code = new StringBuffer(); 14 | try { 15 | //获取加密方式为md5的算法对象 16 | MessageDigest instance = MessageDigest.getInstance("MD5"); 17 | byte[] digest = instance.digest(str.getBytes()); 18 | for (byte b : digest) { 19 | String hexString = Integer.toHexString(b & 0xff); 20 | if (hexString.length() < 2) { 21 | hexString = "0" + hexString; 22 | } 23 | md5Code = md5Code.append(b); 24 | } 25 | } catch (NoSuchAlgorithmException e) { 26 | e.printStackTrace(); 27 | } 28 | return md5Code.toString(); 29 | } 30 | 31 | public static void main(String[] args) { 32 | System.out.println(toMd5("hisen")); 33 | } 34 | } 35 | -------------------------------------------------------------------------------- /BookSystem_V3/src/main/java/com/hisen/controller/AppointmengtController.java: -------------------------------------------------------------------------------- 1 | package com.hisen.controller; 2 | 3 | import com.alibaba.fastjson.JSON; 4 | import com.hisen.bean.request.AppointmentRequest; 5 | import com.hisen.bean.entity.Appointment; 6 | import com.hisen.service.AppointmentService; 7 | import org.slf4j.Logger; 8 | import org.slf4j.LoggerFactory; 9 | import org.springframework.beans.factory.annotation.Autowired; 10 | import org.springframework.stereotype.Controller; 11 | import org.springframework.web.bind.annotation.PathVariable; 12 | import org.springframework.web.bind.annotation.RequestMapping; 13 | import org.springframework.web.bind.annotation.RequestMethod; 14 | import org.springframework.web.bind.annotation.ResponseBody; 15 | 16 | /** 17 | * Created by hisenyuan on 2017/8/2 at 10:27. 18 | */ 19 | @Controller 20 | @RequestMapping("/appointment") 21 | public class AppointmengtController { 22 | 23 | private Logger logger = LoggerFactory.getLogger(this.getClass()); 24 | @Autowired 25 | private AppointmentService appointmentService; 26 | 27 | /** 28 | * 借书的一个小功能 29 | * 本地测试成功,暂时不想写页面 30 | * localhost:8080/V2/appointment/appoint/103/20080808/20 31 | */ 32 | @RequestMapping(value = "/appoint/{bookId}/{userNumber}/{holdDay}", method = RequestMethod.GET, produces = "text/plain;charset=UTF-8") 33 | @ResponseBody 34 | public String appoint(@PathVariable("bookId") int bookId, 35 | @PathVariable("userNumber") int userNumber, @PathVariable("holdDay") String holdDay) { 36 | AppointmentRequest form = new AppointmentRequest(); 37 | form.setBookId(bookId); 38 | form.setUserNumber(userNumber); 39 | form.setHoldDay(holdDay); 40 | logger.info("借书入参 AppointmengtController >>>>> " + form.toString()); 41 | int appoint = appointmentService.appoint(form); 42 | String s = JSON.toJSONString(appoint > 0 ? "借书成功" : "借书失败"); 43 | return s; 44 | } 45 | 46 | @RequestMapping(value = "/return/{bookId}/{userNumber}", method = RequestMethod.GET, produces = "text/plain;charset=UTF-8") 47 | @ResponseBody 48 | public String returnBook(@PathVariable("bookId") int bookId, 49 | @PathVariable("userNumber") int userNumber) { 50 | Appointment form = new Appointment(); 51 | form.setBookId(bookId); 52 | form.setUserNumber(userNumber); 53 | logger.info("还书书入参 AppointmengtController >>>>> " + form.toString()); 54 | int i = appointmentService.returnBook(form); 55 | String s = JSON.toJSONString(i > 0 ? "还书成功" : "还书失败"); 56 | return s; 57 | } 58 | } 59 | -------------------------------------------------------------------------------- /BookSystem_V3/src/main/java/com/hisen/controller/BookController.java: -------------------------------------------------------------------------------- 1 | package com.hisen.controller; 2 | 3 | import com.github.pagehelper.PageHelper; 4 | import com.hisen.bean.entity.Book; 5 | import com.hisen.service.BookService; 6 | import java.util.List; 7 | import org.slf4j.Logger; 8 | import org.slf4j.LoggerFactory; 9 | import org.springframework.beans.factory.annotation.Autowired; 10 | import org.springframework.stereotype.Controller; 11 | import org.springframework.web.bind.annotation.PathVariable; 12 | import org.springframework.web.bind.annotation.RequestMapping; 13 | import org.springframework.web.bind.annotation.RequestMethod; 14 | import org.springframework.web.bind.annotation.ResponseBody; 15 | 16 | /** 17 | * Created by hisen on 17-4-24. 18 | */ 19 | @Controller 20 | @RequestMapping("/book") 21 | public class BookController { 22 | private Logger logger = LoggerFactory.getLogger(this.getClass()); 23 | 24 | @Autowired 25 | private BookService bookService; 26 | 27 | /** 28 | * 查询列表 - 默认查询 0-10 的数据 29 | * @return 30 | */ 31 | @RequestMapping(value = "/list", method = RequestMethod.GET) 32 | @ResponseBody //以json的形式返回对象 33 | private List list() { 34 | List list = bookService.getList(0, 10); 35 | return list; 36 | } 37 | 38 | /** 39 | * 图书的详细信息 40 | * @param bookId 图书id 41 | * @return 42 | */ 43 | @RequestMapping(value = "/detail/{bookId}", method = RequestMethod.GET) 44 | @ResponseBody 45 | private Book detail(@PathVariable("bookId") Long bookId) { 46 | Book book = bookService.getById(bookId); 47 | return book; 48 | } 49 | 50 | /** 51 | * 添加图书的方法 52 | * @param book 53 | * @return 54 | */ 55 | @RequestMapping(value = "/add", method = RequestMethod.POST, produces = "text/plain;charset=UTF-8") 56 | @ResponseBody 57 | private int add(Book book) { 58 | Book hasBook = bookService.getById(book.getBookId()); 59 | int i = -2; 60 | if (hasBook == null) { 61 | i = bookService.addBook(book); 62 | } 63 | return i; 64 | } 65 | 66 | /** 67 | * 删除图书 68 | * @param id 图书编号 69 | * @return 70 | */ 71 | @RequestMapping(value = "/del/{bookId}", method = RequestMethod.GET) 72 | @ResponseBody 73 | private String deleteBookById(@PathVariable("bookId") Long id) { 74 | int i = bookService.deleteBookById(id); 75 | return i > 0 ? "success" : "error"; 76 | } 77 | 78 | /** 79 | * 利用分页插件查询 80 | * @param start 81 | * @return 82 | */ 83 | @RequestMapping(value = "/list/{start}", method = RequestMethod.GET) 84 | @ResponseBody 85 | private List listPagePlug(@PathVariable("start") String start) { 86 | PageHelper.startPage(Integer.valueOf(start), 20); 87 | //默认一页10条 88 | List readingList = bookService.getListPlug(); 89 | return readingList; 90 | } 91 | } 92 | -------------------------------------------------------------------------------- /BookSystem_V3/src/main/java/com/hisen/controller/UserInfoController.java: -------------------------------------------------------------------------------- 1 | package com.hisen.controller; 2 | 3 | import static com.hisen.common.MD5Util.toMd5; 4 | 5 | import com.alibaba.fastjson.JSON; 6 | import com.hisen.bean.request.UserLoginRequest; 7 | import com.hisen.bean.response.UserLoginResponse; 8 | import com.hisen.common.CookieUtil; 9 | import com.hisen.service.UserInfoService; 10 | import java.util.Enumeration; 11 | import java.util.HashMap; 12 | import java.util.Map; 13 | import javax.servlet.http.HttpServletRequest; 14 | import javax.servlet.http.HttpServletResponse; 15 | import org.slf4j.Logger; 16 | import org.slf4j.LoggerFactory; 17 | import org.springframework.beans.factory.annotation.Autowired; 18 | import org.springframework.stereotype.Controller; 19 | import org.springframework.web.bind.annotation.RequestMapping; 20 | import org.springframework.web.bind.annotation.RequestMethod; 21 | import org.springframework.web.bind.annotation.ResponseBody; 22 | 23 | /** 24 | * Created by hisen on 17-8-26. 25 | * E-mail: hisenyuan@gmail.com 26 | */ 27 | @Controller 28 | public class UserInfoController { 29 | private Logger log = LoggerFactory.getLogger(this.getClass()); 30 | @Autowired 31 | private UserInfoService userInfoService; 32 | 33 | @RequestMapping(value = "/login", method = RequestMethod.POST, produces = "text/plain;charset=UTF-8") 34 | @ResponseBody 35 | public String login(HttpServletResponse res,HttpServletRequest req,UserLoginRequest request) { 36 | request.setPwd(toMd5(request.getPwd())); 37 | log.info("[UserInfoController] - [login] 入参-> "+request.toString()); 38 | UserLoginResponse login = userInfoService.login(request); 39 | log.info("[UserInfoController] - [login] 出参-> "+login.toString()); 40 | // // 放置cookies 41 | // Map map = new HashMap(); 42 | // map.put("jwt",login.getJwt()); 43 | // map.put("name",login.getName()); 44 | // CookieUtil.addCookieMap(res,map); 45 | return JSON.toJSONString(login); 46 | } 47 | @RequestMapping(value = "/test", method = RequestMethod.GET, produces = "text/plain;charset=UTF-8") 48 | @ResponseBody 49 | public String test(HttpServletResponse response){ 50 | // 放置cookies 51 | Map map = new HashMap(); 52 | map.put("md5","test"); 53 | map.put("pwd","hisen"); 54 | CookieUtil.addCookieMap(response,map); 55 | return "hello"; 56 | } 57 | @RequestMapping(value = "/add", method = RequestMethod.GET, produces = "text/plain;charset=UTF-8") 58 | @ResponseBody 59 | public String testJWT(HttpServletResponse res,HttpServletRequest req){ 60 | Boolean checkJWT = userInfoService.checkJWT(req); 61 | if (checkJWT) { 62 | return "成功"; 63 | } 64 | return "失败"; 65 | } 66 | @RequestMapping(value = "/register", method = RequestMethod.POST, produces = "text/plain;charset=UTF-8") 67 | @ResponseBody 68 | public String register(HttpServletRequest req,HttpServletResponse res,String name){ 69 | // 打印请求头 70 | Enumeration> enum1 = req.getHeaderNames(); 71 | while (enum1.hasMoreElements()) { 72 | String key = (String) enum1.nextElement(); 73 | String value = req.getHeader(key); 74 | System.out.println(key + "\t" + value); 75 | } 76 | // 取出jwt中的加密信息,与数据库比对,看是否正确 77 | Boolean aBoolean = userInfoService.checkJWT(req); 78 | return aBoolean ? "成功" : "失败"; 79 | } 80 | } 81 | -------------------------------------------------------------------------------- /BookSystem_V3/src/main/java/com/hisen/dao/AppointmentMapper.java: -------------------------------------------------------------------------------- 1 | package com.hisen.dao; 2 | 3 | import com.hisen.bean.entity.Appointment; 4 | import com.hisen.bean.entity.AppointmentExample; 5 | import java.util.List; 6 | import org.apache.ibatis.annotations.Param; 7 | 8 | public interface AppointmentMapper { 9 | /** 10 | * This method was generated by MyBatis Generator. 11 | * This method corresponds to the database table appointment 12 | * 13 | * @mbggenerated Thu Aug 03 16:47:27 CST 2017 14 | */ 15 | int countByExample(AppointmentExample example); 16 | 17 | /** 18 | * This method was generated by MyBatis Generator. 19 | * This method corresponds to the database table appointment 20 | * 21 | * @mbggenerated Thu Aug 03 16:47:27 CST 2017 22 | */ 23 | int deleteByExample(AppointmentExample example); 24 | 25 | /** 26 | * This method was generated by MyBatis Generator. 27 | * This method corresponds to the database table appointment 28 | * 29 | * @mbggenerated Thu Aug 03 16:47:27 CST 2017 30 | */ 31 | int deleteByPrimaryKey(Integer id); 32 | 33 | /** 34 | * This method was generated by MyBatis Generator. 35 | * This method corresponds to the database table appointment 36 | * 37 | * @mbggenerated Thu Aug 03 16:47:27 CST 2017 38 | */ 39 | int insert(Appointment record); 40 | 41 | /** 42 | * This method was generated by MyBatis Generator. 43 | * This method corresponds to the database table appointment 44 | * 45 | * @mbggenerated Thu Aug 03 16:47:27 CST 2017 46 | */ 47 | int insertSelective(Appointment record); 48 | 49 | /** 50 | * This method was generated by MyBatis Generator. 51 | * This method corresponds to the database table appointment 52 | * 53 | * @mbggenerated Thu Aug 03 16:47:27 CST 2017 54 | */ 55 | List selectByExample(AppointmentExample example); 56 | 57 | /** 58 | * This method was generated by MyBatis Generator. 59 | * This method corresponds to the database table appointment 60 | * 61 | * @mbggenerated Thu Aug 03 16:47:27 CST 2017 62 | */ 63 | Appointment selectByPrimaryKey(Integer id); 64 | 65 | /** 66 | * This method was generated by MyBatis Generator. 67 | * This method corresponds to the database table appointment 68 | * 69 | * @mbggenerated Thu Aug 03 16:47:27 CST 2017 70 | */ 71 | int updateByExampleSelective(@Param("record") Appointment record, 72 | @Param("example") AppointmentExample example); 73 | 74 | /** 75 | * This method was generated by MyBatis Generator. 76 | * This method corresponds to the database table appointment 77 | * 78 | * @mbggenerated Thu Aug 03 16:47:27 CST 2017 79 | */ 80 | int updateByExample(@Param("record") Appointment record, 81 | @Param("example") AppointmentExample example); 82 | 83 | /** 84 | * This method was generated by MyBatis Generator. 85 | * This method corresponds to the database table appointment 86 | * 87 | * @mbggenerated Thu Aug 03 16:47:27 CST 2017 88 | */ 89 | int updateByPrimaryKeySelective(Appointment record); 90 | 91 | /** 92 | * This method was generated by MyBatis Generator. 93 | * This method corresponds to the database table appointment 94 | * 95 | * @mbggenerated Thu Aug 03 16:47:27 CST 2017 96 | */ 97 | int updateByPrimaryKey(Appointment record); 98 | } -------------------------------------------------------------------------------- /BookSystem_V3/src/main/java/com/hisen/dao/BookDao.java: -------------------------------------------------------------------------------- 1 | package com.hisen.dao; 2 | 3 | import com.hisen.bean.entity.Book; 4 | import java.util.List; 5 | import org.apache.ibatis.annotations.Param; 6 | 7 | /** 8 | * Created by hisen on 17-4-24. 9 | */ 10 | public interface BookDao { 11 | Book queryById(long id); 12 | List queryAll(@Param("offset") int offset, @Param("limit") int limit); 13 | int addBook(Book book); 14 | int updateBook(Book book); 15 | int deleteBookById(long id); 16 | int countNum(); 17 | List queryAllPlug(); 18 | } 19 | -------------------------------------------------------------------------------- /BookSystem_V3/src/main/java/com/hisen/dao/RedisCache.java: -------------------------------------------------------------------------------- 1 | package com.hisen.dao; 2 | 3 | import java.util.concurrent.locks.ReadWriteLock; 4 | import java.util.concurrent.locks.ReentrantReadWriteLock; 5 | import org.apache.ibatis.cache.Cache; 6 | import org.slf4j.Logger; 7 | import org.slf4j.LoggerFactory; 8 | import org.springframework.data.redis.connection.jedis.JedisConnection; 9 | import org.springframework.data.redis.connection.jedis.JedisConnectionFactory; 10 | import org.springframework.data.redis.serializer.JdkSerializationRedisSerializer; 11 | import org.springframework.data.redis.serializer.RedisSerializer; 12 | import redis.clients.jedis.exceptions.JedisConnectionException; 13 | 14 | /** 15 | * 使用第三方内存数据库Redis作为二级缓存 16 | * 模仿Ehcache写的一个RedisCache缓存类 17 | * Created by hisenyuan on 2017/5/18 at 15:55. 18 | */ 19 | public class RedisCache implements Cache { 20 | 21 | private static final Logger logger = LoggerFactory.getLogger(RedisCache.class); 22 | private static JedisConnectionFactory jedisConnectionFactory; 23 | private final String id; 24 | /** 25 | * 写锁 26 | */ 27 | private final ReadWriteLock readWriteLock = new ReentrantReadWriteLock(); 28 | 29 | public RedisCache(final String id) { 30 | if (id == null) { 31 | throw new IllegalArgumentException("Cache instances require an ID"); 32 | } 33 | logger.info("MybatisRedisCache:id=" + id); 34 | this.id = id; 35 | } 36 | 37 | public String getId() { 38 | return this.id; 39 | } 40 | 41 | /** 42 | * 存放对象 43 | */ 44 | public void putObject(Object key, Object value) { 45 | JedisConnection connection = null; 46 | try { 47 | connection = jedisConnectionFactory.getConnection(); 48 | RedisSerializer serializer = new JdkSerializationRedisSerializer(); 49 | //设置超时时间,可以设置如下,此为三秒钟过期 50 | connection.setEx(serializer.serialize(key),30,serializer.serialize(value)); 51 | //connection.set(serializer.serialize(key), serializer.serialize(value)); 52 | } catch (JedisConnectionException e) { 53 | e.printStackTrace(); 54 | } finally { 55 | if (connection != null) { 56 | connection.close(); 57 | } 58 | } 59 | } 60 | 61 | /** 62 | * 获取对象 63 | */ 64 | public Object getObject(Object key) { 65 | Object result = null; 66 | JedisConnection connection = null; 67 | try { 68 | connection = jedisConnectionFactory.getConnection(); 69 | RedisSerializer serializer = new JdkSerializationRedisSerializer(); 70 | result = serializer.deserialize(connection.get(serializer.serialize(key))); 71 | } catch (JedisConnectionException e) { 72 | e.printStackTrace(); 73 | } finally { 74 | if (connection != null) { 75 | connection.close(); 76 | } 77 | } 78 | return result; 79 | } 80 | 81 | /** 82 | * 删除对象 83 | */ 84 | public Object removeObject(Object key) { 85 | JedisConnection connection = null; 86 | Object result = null; 87 | try { 88 | connection = jedisConnectionFactory.getConnection(); 89 | RedisSerializer serializer = new JdkSerializationRedisSerializer(); 90 | result = connection.expire(serializer.serialize(key), 0); 91 | } catch (JedisConnectionException e) { 92 | e.printStackTrace(); 93 | } finally { 94 | if (connection != null) { 95 | connection.close(); 96 | } 97 | } 98 | return result; 99 | } 100 | 101 | /** 102 | * 获得锁 103 | */ 104 | public ReadWriteLock getReadWriteLock() { 105 | return this.readWriteLock; 106 | } 107 | 108 | /** 109 | * 获取数据库大小 110 | */ 111 | public int getSize() { 112 | int result = 0; 113 | JedisConnection connection = null; 114 | try { 115 | connection = jedisConnectionFactory.getConnection(); 116 | result = Integer.valueOf(connection.dbSize().toString()); 117 | } catch (JedisConnectionException e) { 118 | e.printStackTrace(); 119 | } finally { 120 | if (connection != null) { 121 | connection.close(); 122 | } 123 | } 124 | return result; 125 | } 126 | 127 | /** 128 | * 配置连接工厂 129 | */ 130 | public static void setJedisConnectionFactory(JedisConnectionFactory jedisConnectionFactory) { 131 | RedisCache.jedisConnectionFactory = jedisConnectionFactory; 132 | } 133 | 134 | /** 135 | * 清空 136 | */ 137 | public void clear() { 138 | JedisConnection connection = null; 139 | try { 140 | connection = jedisConnectionFactory.getConnection(); 141 | connection.flushDb(); 142 | connection.flushAll(); 143 | } catch (JedisConnectionException e) { 144 | e.printStackTrace(); 145 | } finally { 146 | if (connection != null) { 147 | connection.close(); 148 | } 149 | } 150 | } 151 | } 152 | -------------------------------------------------------------------------------- /BookSystem_V3/src/main/java/com/hisen/dao/RedisCacheTransfer.java: -------------------------------------------------------------------------------- 1 | package com.hisen.dao; 2 | 3 | import org.springframework.beans.factory.annotation.Autowired; 4 | import org.springframework.data.redis.connection.jedis.JedisConnectionFactory; 5 | 6 | /** 7 | * 静态注入中间类 8 | * 完成RedisCache.jedisConnectionFactory的静态注入 9 | * Created by hisenyuan on 2017/5/18 at 16:05. 10 | */ 11 | public class RedisCacheTransfer { 12 | 13 | @Autowired 14 | public void setJedisConnectionFactory(JedisConnectionFactory jedisConnectionFactory) { 15 | RedisCache.setJedisConnectionFactory(jedisConnectionFactory); 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /BookSystem_V3/src/main/java/com/hisen/dao/UserInfoMapper.java: -------------------------------------------------------------------------------- 1 | package com.hisen.dao; 2 | 3 | import com.hisen.bean.entity.UserInfo; 4 | import com.hisen.bean.entity.UserInfoExample; 5 | import java.util.List; 6 | import org.apache.ibatis.annotations.Param; 7 | 8 | public interface UserInfoMapper { 9 | /** 10 | * This method was generated by MyBatis Generator. 11 | * This method corresponds to the database table user_info 12 | * 13 | * @mbggenerated Sat Aug 26 20:58:48 CST 2017 14 | */ 15 | int countByExample(UserInfoExample example); 16 | 17 | /** 18 | * This method was generated by MyBatis Generator. 19 | * This method corresponds to the database table user_info 20 | * 21 | * @mbggenerated Sat Aug 26 20:58:48 CST 2017 22 | */ 23 | int deleteByExample(UserInfoExample example); 24 | 25 | /** 26 | * This method was generated by MyBatis Generator. 27 | * This method corresponds to the database table user_info 28 | * 29 | * @mbggenerated Sat Aug 26 20:58:48 CST 2017 30 | */ 31 | int deleteByPrimaryKey(Integer id); 32 | 33 | /** 34 | * This method was generated by MyBatis Generator. 35 | * This method corresponds to the database table user_info 36 | * 37 | * @mbggenerated Sat Aug 26 20:58:48 CST 2017 38 | */ 39 | int insert(UserInfo record); 40 | 41 | /** 42 | * This method was generated by MyBatis Generator. 43 | * This method corresponds to the database table user_info 44 | * 45 | * @mbggenerated Sat Aug 26 20:58:48 CST 2017 46 | */ 47 | int insertSelective(UserInfo record); 48 | 49 | /** 50 | * This method was generated by MyBatis Generator. 51 | * This method corresponds to the database table user_info 52 | * 53 | * @mbggenerated Sat Aug 26 20:58:48 CST 2017 54 | */ 55 | List selectByExample(UserInfoExample example); 56 | 57 | /** 58 | * This method was generated by MyBatis Generator. 59 | * This method corresponds to the database table user_info 60 | * 61 | * @mbggenerated Sat Aug 26 20:58:48 CST 2017 62 | */ 63 | UserInfo selectByPrimaryKey(Integer id); 64 | 65 | /** 66 | * This method was generated by MyBatis Generator. 67 | * This method corresponds to the database table user_info 68 | * 69 | * @mbggenerated Sat Aug 26 20:58:48 CST 2017 70 | */ 71 | int updateByExampleSelective(@Param("record") UserInfo record, 72 | @Param("example") UserInfoExample example); 73 | 74 | /** 75 | * This method was generated by MyBatis Generator. 76 | * This method corresponds to the database table user_info 77 | * 78 | * @mbggenerated Sat Aug 26 20:58:48 CST 2017 79 | */ 80 | int updateByExample(@Param("record") UserInfo record, @Param("example") UserInfoExample example); 81 | 82 | /** 83 | * This method was generated by MyBatis Generator. 84 | * This method corresponds to the database table user_info 85 | * 86 | * @mbggenerated Sat Aug 26 20:58:48 CST 2017 87 | */ 88 | int updateByPrimaryKeySelective(UserInfo record); 89 | 90 | /** 91 | * This method was generated by MyBatis Generator. 92 | * This method corresponds to the database table user_info 93 | * 94 | * @mbggenerated Sat Aug 26 20:58:48 CST 2017 95 | */ 96 | int updateByPrimaryKey(UserInfo record); 97 | 98 | String checkJWT(String name); 99 | } -------------------------------------------------------------------------------- /BookSystem_V3/src/main/java/com/hisen/filter/CorsFilter.java: -------------------------------------------------------------------------------- 1 | package com.hisen.filter; 2 | 3 | import java.io.IOException; 4 | import java.util.Collection; 5 | import java.util.Enumeration; 6 | import javax.servlet.Filter; 7 | import javax.servlet.FilterChain; 8 | import javax.servlet.FilterConfig; 9 | import javax.servlet.ServletException; 10 | import javax.servlet.ServletRequest; 11 | import javax.servlet.ServletResponse; 12 | import javax.servlet.http.HttpServletRequest; 13 | import javax.servlet.http.HttpServletResponse; 14 | import org.slf4j.Logger; 15 | import org.slf4j.LoggerFactory; 16 | import org.springframework.stereotype.Component; 17 | 18 | /** 19 | * CorsFileter 功能描述:CORS过滤器 20 | * 允许跨域访问 21 | * Created by hisen on 17-8-27. 22 | * E-mail: hisenyuan@gmail.com 23 | */ 24 | @Component 25 | public class CorsFilter implements Filter { 26 | 27 | private Logger log = LoggerFactory.getLogger(this.getClass()); 28 | 29 | public void init(FilterConfig filterConfig) throws ServletException { 30 | 31 | } 32 | 33 | public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, 34 | FilterChain filterChain) throws IOException, ServletException { 35 | HttpServletRequest request = (HttpServletRequest) servletRequest; 36 | HttpServletResponse response = (HttpServletResponse) servletResponse; 37 | Enumeration enum1 = request.getHeaderNames(); 38 | log.info("before Filter Header is:"); 39 | while (enum1.hasMoreElements()) { 40 | String key = (String) enum1.nextElement(); 41 | String value = request.getHeader(key); 42 | log.info("{},{}",key,value); 43 | } 44 | // 设置允许访问的域名 45 | response.setHeader("Access-Control-Allow-Origin", "*"); 46 | // 设置允许的方式 47 | response.setHeader("Access-Control-Allow-Methods", "POST, GET, OPTIONS, DELETE"); 48 | // 设置preflight请求的结果能够被缓存多久 49 | response.setHeader("Access-Control-Max-Age", "3600"); 50 | // 设置允许的header类型 51 | response.setHeader("Access-Control-Allow-Headers", "x-requested-with,Authorization,name"); 52 | // 设置为true时允许浏览器读取response的内容 53 | response.setHeader("Access-Control-Allow-Credentials", "true"); 54 | 55 | Collection names = response.getHeaderNames(); 56 | log.info("after Filter Header is:"); 57 | for (String name:names) { 58 | String value = response.getHeader(name); 59 | log.info("{},{}",name,value); 60 | } 61 | filterChain.doFilter(servletRequest, servletResponse); 62 | } 63 | 64 | public void destroy() { 65 | 66 | } 67 | } 68 | -------------------------------------------------------------------------------- /BookSystem_V3/src/main/java/com/hisen/filter/JWTCheckInterceptor.java: -------------------------------------------------------------------------------- 1 | package com.hisen.filter; 2 | 3 | import com.alibaba.fastjson.JSON; 4 | import com.hisen.bean.contains.CommonEnum; 5 | import com.hisen.bean.entity.JWTInfo; 6 | import com.hisen.bean.response.CommonResponse; 7 | import com.hisen.common.JWTUtil; 8 | import java.io.PrintWriter; 9 | import javax.servlet.http.HttpServletRequest; 10 | import javax.servlet.http.HttpServletResponse; 11 | import org.slf4j.Logger; 12 | import org.slf4j.LoggerFactory; 13 | import org.springframework.web.servlet.HandlerInterceptor; 14 | import org.springframework.web.servlet.ModelAndView; 15 | 16 | /** 17 | * Created by hisen on 17-8-27. 18 | * E-mail: hisenyuan@gmail.com 19 | */ 20 | public class JWTCheckInterceptor implements HandlerInterceptor { 21 | 22 | private Logger log = LoggerFactory.getLogger(this.getClass()); 23 | 24 | public boolean preHandle(HttpServletRequest request, 25 | HttpServletResponse response, Object o) throws Exception { 26 | response.setCharacterEncoding("utf-8"); 27 | String jwt = request.getHeader("Authorization"); 28 | String name = request.getHeader("name"); 29 | log.info("JWTCheckInterceptor - jwt:{},name{}",jwt,name); 30 | if ("".equals(jwt) || "".equals(name) || null == jwt || null == name) { 31 | CommonResponse commonResponse = new CommonResponse(); 32 | commonResponse.setResCode(CommonEnum.REQUEST_FAILED.getCode()); 33 | commonResponse.setResMsg("非法操作"); 34 | responseMessage(response, response.getWriter(), commonResponse); 35 | return false; 36 | } 37 | // 解密信息 38 | JWTInfo jwtInfo = JWTUtil.unsign(jwt, JWTInfo.class); 39 | log.info("jwt解密之后:{}",JSON.toJSONString(jwtInfo)); 40 | if (jwtInfo == null) { 41 | CommonResponse commonResponse = new CommonResponse(); 42 | commonResponse.setResCode(CommonEnum.REQUEST_FAILED.getCode()); 43 | commonResponse.setResMsg("token超时,请重新登录"); 44 | responseMessage(response, response.getWriter(), commonResponse); 45 | return false; 46 | } 47 | if (name.equals(jwtInfo.getUsername())) { 48 | return true; 49 | } else { 50 | CommonResponse commonResponse = new CommonResponse(); 51 | commonResponse.setResCode(CommonEnum.REQUEST_FAILED.getCode()); 52 | commonResponse.setResMsg("token校验不通过,请重新登录"); 53 | responseMessage(response, response.getWriter(), commonResponse); 54 | return false; 55 | } 56 | } 57 | 58 | public void postHandle(HttpServletRequest request, 59 | HttpServletResponse response, Object o, ModelAndView modelAndView) 60 | throws Exception { 61 | } 62 | 63 | public void afterCompletion(HttpServletRequest httpServletRequest, 64 | HttpServletResponse httpServletResponse, Object o, Exception e) throws Exception { 65 | 66 | } 67 | 68 | //请求不通过,返回错误信息给客户端 69 | private void responseMessage(HttpServletResponse response, PrintWriter out, 70 | CommonResponse commonResponse) { 71 | response.setContentType("application/json; charset=utf-8"); 72 | String json = JSON.toJSONString(commonResponse); 73 | out.print(json); 74 | out.flush(); 75 | out.close(); 76 | } 77 | } 78 | -------------------------------------------------------------------------------- /BookSystem_V3/src/main/java/com/hisen/service/AppointmentService.java: -------------------------------------------------------------------------------- 1 | package com.hisen.service; 2 | 3 | import com.hisen.bean.request.AppointmentRequest; 4 | import com.hisen.bean.entity.Appointment; 5 | 6 | /** 7 | * Created by hisenyuan on 2017/8/2 at 10:36. 8 | */ 9 | public interface AppointmentService { 10 | int appoint(AppointmentRequest record); 11 | int returnBook(Appointment record); 12 | } 13 | -------------------------------------------------------------------------------- /BookSystem_V3/src/main/java/com/hisen/service/BookService.java: -------------------------------------------------------------------------------- 1 | package com.hisen.service; 2 | 3 | import com.hisen.bean.entity.Book; 4 | import java.util.List; 5 | 6 | /** 7 | * Created by hisen on 17-4-24. 8 | */ 9 | public interface BookService { 10 | 11 | /** 12 | * 根据图书id查询图书信息 13 | * @param bookId 图书id 14 | * @return 图书信息 15 | */ 16 | Book getById(long bookId); 17 | 18 | /** 19 | * 查询图书列表 20 | * @param start 开始的位置 21 | * @param pageNum 每次查询的次数 22 | * @return 图书信息列表 23 | */ 24 | List getList(int start, int pageNum); 25 | 26 | /** 27 | * 添加图书信息 28 | * @param book 图书对象 29 | * @return 30 | */ 31 | int addBook(Book book); 32 | 33 | /** 34 | * 修改图书信息 35 | * @param book 图书对象 36 | * @return 37 | */ 38 | int updateBook(Book book); 39 | 40 | /** 41 | * 删除图书 - 根据图书id 42 | * @param id 图书id 43 | * @return 44 | */ 45 | int deleteBookById(long id); 46 | 47 | /** 48 | * 通过分页插件查询 49 | * @return 50 | */ 51 | List getListPlug(); 52 | } 53 | -------------------------------------------------------------------------------- /BookSystem_V3/src/main/java/com/hisen/service/UserInfoService.java: -------------------------------------------------------------------------------- 1 | package com.hisen.service; 2 | 3 | import com.hisen.bean.request.UserLoginRequest; 4 | import com.hisen.bean.response.UserLoginResponse; 5 | import javax.servlet.http.HttpServletRequest; 6 | 7 | /** 8 | * Created by hisen on 17-8-26. 9 | * E-mail: hisenyuan@gmail.com 10 | */ 11 | public interface UserInfoService { 12 | public UserLoginResponse login(UserLoginRequest request); 13 | public Boolean checkJWT(HttpServletRequest req); 14 | } 15 | -------------------------------------------------------------------------------- /BookSystem_V3/src/main/java/com/hisen/service/impl/AppointmentServiceImpl.java: -------------------------------------------------------------------------------- 1 | package com.hisen.service.impl; 2 | 3 | import static com.google.common.base.Preconditions.checkNotNull; 4 | 5 | import com.hisen.bean.request.AppointmentRequest; 6 | import com.hisen.dao.AppointmentMapper; 7 | import com.hisen.dao.BookDao; 8 | import com.hisen.bean.entity.Appointment; 9 | import com.hisen.bean.entity.AppointmentExample; 10 | import com.hisen.bean.entity.Book; 11 | import com.hisen.service.AppointmentService; 12 | import java.util.List; 13 | import org.joda.time.DateTime; 14 | import org.slf4j.Logger; 15 | import org.slf4j.LoggerFactory; 16 | import org.springframework.beans.factory.annotation.Autowired; 17 | import org.springframework.stereotype.Service; 18 | import org.springframework.transaction.annotation.Transactional; 19 | 20 | /** 21 | * Created by hisenyuan on 2017/8/2 at 10:36. 22 | */ 23 | @Service 24 | public class AppointmentServiceImpl implements AppointmentService { 25 | 26 | private Logger logger = LoggerFactory.getLogger(this.getClass()); 27 | 28 | @Autowired 29 | private AppointmentMapper appointmentMapper; 30 | @Autowired 31 | private BookDao bookDao; 32 | 33 | /** 34 | * 预约图书 35 | */ 36 | //如果发生了异常,就进行回滚 37 | @Transactional(rollbackFor = {Exception.class}) 38 | public int appoint(AppointmentRequest record) { 39 | //利用google guava判空 40 | checkNotNull(record.getUserNumber(), "预约 - 用户号不能为空"); 41 | checkNotNull(record.getBookId(), "预约 - 图书编号不能为空"); 42 | //利用joda-time生成时间,并且计算时间 43 | DateTime dt = new DateTime(); 44 | record.setAppointmentTime(dt.toDate()); 45 | //预计持有时间 46 | int holdDay = Integer.valueOf(record.getHoldDay()); 47 | //利用持有时间计算预计归还时间 48 | record.setExpectReturnTime(dt.plusDays(holdDay).toDate()); 49 | Book book = bookDao.queryById(record.getBookId()); 50 | int num = book.getNumber(); 51 | int insert = 0; 52 | if (num >= 1) { 53 | insert = appointmentMapper.insert(record); 54 | book.setNumber(num - 1); 55 | logger.info("借书入参 AppointmentServiceImpl >>>>> " + record.toString()); 56 | bookDao.updateBook(book); 57 | // checkNotNull(null, "出现异常,事物回滚。用来测试事物控制"); 58 | } 59 | return insert; 60 | } 61 | 62 | /** 63 | * 归还图书 64 | */ 65 | @Transactional(rollbackFor = {Exception.class}) 66 | public int returnBook(Appointment record) { 67 | //利用google guava判空 68 | checkNotNull(record.getUserNumber(), "用户号不能为空"); 69 | checkNotNull(record.getBookId(), "图书编号不能为空"); 70 | //利用joda-time生成时间,并且计算时间 71 | DateTime dt = new DateTime(); 72 | //查询是否存在已经借出的书籍 73 | AppointmentExample example = new AppointmentExample(); 74 | example.createCriteria() 75 | .andBookIdEqualTo(Integer.valueOf(record.getBookId())) 76 | .andUserNumberEqualTo(Integer.valueOf(record.getUserNumber())); 77 | List appointments = appointmentMapper.selectByExample(example); 78 | 79 | logger.info("appointments >>>>> " + appointments.toString()); 80 | logger.info("appointments size >>>>> " + appointments.size()); 81 | logger.info("appointments status >>>>> " + appointments.get(0).getStatus()); 82 | int i = 0; 83 | if (appointments.size() > 0) { 84 | Appointment appointment = appointments.get(0); 85 | if (0 == appointment.getStatus()) { 86 | appointment.setRealReturnTime(new DateTime().toDate()); 87 | appointment.setStatus(1); 88 | logger.info("还书入参 AppointmentServiceImpl >>>>> " + appointment.toString()); 89 | i = appointmentMapper.updateByPrimaryKey(appointment); 90 | logger.info("appointments i >>>>> " + i); 91 | } 92 | } else { 93 | logger.info("AppointmentServiceImpl 为进入更新状态 >>>>> " + i); 94 | } 95 | return i; 96 | } 97 | } 98 | -------------------------------------------------------------------------------- /BookSystem_V3/src/main/java/com/hisen/service/impl/BookServiceImpl.java: -------------------------------------------------------------------------------- 1 | package com.hisen.service.impl; 2 | 3 | import com.hisen.dao.BookDao; 4 | import com.hisen.bean.entity.Book; 5 | import com.hisen.service.BookService; 6 | import java.util.List; 7 | import org.springframework.beans.factory.annotation.Autowired; 8 | import org.springframework.stereotype.Service; 9 | 10 | /** 11 | * Created by hisen on 17-4-24. 12 | */ 13 | @Service 14 | public class BookServiceImpl implements BookService { 15 | 16 | @Autowired 17 | private BookDao bookDao; 18 | 19 | public Book getById(long bookId) { 20 | return bookDao.queryById(bookId); 21 | } 22 | 23 | public List getList(int start, int pageNum) { 24 | return bookDao.queryAll(start, pageNum); 25 | } 26 | 27 | public int addBook(Book book) { 28 | return bookDao.addBook(book); 29 | } 30 | 31 | public int updateBook(Book book) { 32 | return bookDao.updateBook(book); 33 | } 34 | 35 | public int deleteBookById(long id) { 36 | return bookDao.deleteBookById(id); 37 | } 38 | 39 | public int countNum() { 40 | return bookDao.countNum(); 41 | } 42 | 43 | public List getListPlug() { 44 | return bookDao.queryAllPlug(); 45 | } 46 | } 47 | -------------------------------------------------------------------------------- /BookSystem_V3/src/main/java/com/hisen/service/impl/UserInfoServiceImpl.java: -------------------------------------------------------------------------------- 1 | package com.hisen.service.impl; 2 | 3 | import com.alibaba.fastjson.JSON; 4 | import com.hisen.bean.contains.CommonEnum; 5 | import com.hisen.bean.entity.JWTInfo; 6 | import com.hisen.bean.entity.UserInfo; 7 | import com.hisen.bean.entity.UserInfoExample; 8 | import com.hisen.bean.request.UserLoginRequest; 9 | import com.hisen.bean.response.UserLoginResponse; 10 | import com.hisen.common.CookieUtil; 11 | import com.hisen.common.JWTUtil; 12 | import com.hisen.dao.UserInfoMapper; 13 | import com.hisen.service.UserInfoService; 14 | import java.util.List; 15 | import java.util.Map; 16 | import javax.annotation.Resource; 17 | import javax.servlet.http.Cookie; 18 | import javax.servlet.http.HttpServletRequest; 19 | import org.slf4j.Logger; 20 | import org.slf4j.LoggerFactory; 21 | import org.springframework.stereotype.Service; 22 | 23 | /** 24 | * Created by hisen on 17-8-26. 25 | * E-mail: hisenyuan@gmail.com 26 | */ 27 | @Service 28 | public class UserInfoServiceImpl implements UserInfoService { 29 | private Logger log = LoggerFactory.getLogger(this.getClass()); 30 | 31 | @Resource 32 | UserInfoMapper mapper; 33 | 34 | public UserLoginResponse login(UserLoginRequest request) { 35 | log.info("[UserInfoServiceImpl]-[login]-入参:{}", JSON.toJSONString(request)); 36 | UserInfoExample example = new UserInfoExample(); 37 | example.createCriteria().andNameEqualTo(request.getName()).andPwdEqualTo(request.getPwd()); 38 | List userInfos = mapper.selectByExample(example); 39 | log.info("[UserLoginResponse]-[login]-[selectByExample]-出参:{}", JSON.toJSONString(userInfos)); 40 | UserInfo userInfo = userInfos.get(0); 41 | UserLoginResponse login = new UserLoginResponse(); 42 | if (userInfos == null || userInfos.size() == 0 || !request.getPwd().equals(userInfo.getPwd())) { 43 | login.setResCode(CommonEnum.LOGIN_FAILED.getCode()); 44 | login.setResMsg(CommonEnum.LOGIN_FAILED.getMsg()); 45 | return login; 46 | } 47 | if ("1".equals(userInfo.getUserState())) { 48 | login.setResCode(CommonEnum.LOGIN_LOCKED.getCode()); 49 | login.setResMsg(CommonEnum.LOGIN_LOCKED.getMsg()); 50 | return login; 51 | } 52 | // 生成token 53 | JWTInfo jwtInfo = new JWTInfo(); 54 | jwtInfo.setPassword(userInfo.getPwd()); 55 | jwtInfo.setUsername(userInfo.getName()); 56 | String jwt = JWTUtil.sign(jwtInfo, Long.valueOf(CommonEnum.JWT_MAXAGE.getMsg())); 57 | // 放入返回 58 | login.setJwt(jwt); 59 | login.setName(userInfo.getName()); 60 | login.setUserType(userInfo.getUserType()); 61 | login.setResCode(CommonEnum.LOGIN_SUCCESS.getCode()); 62 | login.setResMsg(CommonEnum.LOGIN_SUCCESS.getMsg()); 63 | log.info("[UserLoginResponse]-[login]-出参:{}", JSON.toJSONString(login)); 64 | return login; 65 | } 66 | 67 | public Boolean checkJWT(HttpServletRequest req) { 68 | String jwt = null; 69 | String name = null; 70 | Map stringCookieMap = CookieUtil.ReadCookieMap(req); 71 | log.info("[UserLoginResponse]-[checkJWT]-入参:{}", JSON.toJSONString(stringCookieMap)); 72 | // 从cookies中取数据 73 | if (!stringCookieMap.isEmpty()) { 74 | jwt = stringCookieMap.get("jwt").getValue(); 75 | name = stringCookieMap.get("name").getValue(); 76 | System.out.println("checkJWT 1:" + jwt + "\t" + name); 77 | } else { 78 | // 从Header中取数据 79 | jwt = req.getHeader("Authorization"); 80 | name = req.getHeader("name"); 81 | System.out.println("checkJWT 2:" + jwt + "\t" + name); 82 | } 83 | // 获得加密之前的数据 84 | JWTInfo jwtInfo = JWTUtil.unsign(jwt, JWTInfo.class); 85 | // 利用name去查询密码 86 | String checkJWT = mapper.checkJWT(jwtInfo.getUsername()); 87 | if ((jwtInfo.getPassword()).equals(checkJWT)) { 88 | return true; 89 | } 90 | return false; 91 | } 92 | } 93 | -------------------------------------------------------------------------------- /BookSystem_V3/src/main/resources/generatorConfig.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 8 | 9 | 10 | 11 | 12 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | -------------------------------------------------------------------------------- /BookSystem_V3/src/main/resources/jdbc.properties: -------------------------------------------------------------------------------- 1 | jdbc.driverClassName=com.mysql.jdbc.Driver 2 | jdbc.url=jdbc:mysql://127.0.0.1:3306/booksystem?useUnicode=true&characterEncoding=utf8 3 | jdbc.username=root 4 | jdbc.password=hisen -------------------------------------------------------------------------------- /BookSystem_V3/src/main/resources/logback.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | %d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n 7 | 8 | 9 | 10 | 11 | 12 | 13 | -------------------------------------------------------------------------------- /BookSystem_V3/src/main/resources/mapper/BookMapper.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | SELECT book_id,name,number,detail 12 | FROM 13 | book 14 | WHERE 15 | book_id = #{bookId} 16 | 17 | 18 | 19 | SELECT 20 | book_id, 21 | name, 22 | number, 23 | detail 24 | FROM 25 | book 26 | ORDER BY 27 | book_id 28 | LIMIT #{offset}, #{limit} 29 | 30 | 31 | 32 | SELECT 33 | book_id, 34 | name, 35 | number, 36 | detail 37 | FROM 38 | book 39 | ORDER BY 40 | book_id 41 | 42 | 43 | 44 | UPDATE book 45 | SET NAME = #{name}, 46 | NUMBER = #{number}, 47 | detail = #{detail} 48 | WHERE 49 | book_id = #{bookId} 50 | AND number > 0 51 | 52 | 53 | 54 | INSERT INTO book(`book_id`, `name`, `number`,`detail`) 55 | VALUES(#{bookId}, #{name}, #{number}, #{detail}) 56 | 57 | 58 | 59 | DELETE FROM book WHERE book_id = #{bookId} 60 | 61 | 62 | SELECT 63 | COUNT(*) 64 | FROM 65 | book 66 | 67 | -------------------------------------------------------------------------------- /BookSystem_V3/src/main/resources/mybatis-config.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | 45 | -------------------------------------------------------------------------------- /BookSystem_V3/src/main/resources/redis.properties: -------------------------------------------------------------------------------- 1 | # Redis settings 2 | redis.host=127.0.0.1 3 | redis.port=6379 4 | redis.pass= 5 | 6 | redis.maxIdle=300 7 | redis.maxActive=600 8 | redis.maxWait=1000 9 | redis.testOnBorrow=true -------------------------------------------------------------------------------- /BookSystem_V3/src/main/resources/spring/spring-aop.xml: -------------------------------------------------------------------------------- 1 | 2 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | -------------------------------------------------------------------------------- /BookSystem_V3/src/main/resources/spring/spring-service.xml: -------------------------------------------------------------------------------- 1 | 2 | 12 | 13 | 14 | 15 | 16 | 18 | 19 | 20 | 21 | 22 | 23 | -------------------------------------------------------------------------------- /BookSystem_V3/src/main/resources/spring/spring-web.xml: -------------------------------------------------------------------------------- 1 | 2 | 12 | 13 | 14 | 18 | 19 | 20 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | 45 | 46 | 47 | 48 | 49 | 50 | 51 | 52 | 53 | 54 | 55 | 56 | 57 | 58 | -------------------------------------------------------------------------------- /BookSystem_V3/src/main/webapp/WEB-INF/web.xml: -------------------------------------------------------------------------------- 1 | 5 | 6 | 7 | seckill-dispatcher 8 | org.springframework.web.servlet.DispatcherServlet 9 | 13 | 14 | contextConfigLocation 15 | classpath:spring/spring-*.xml 16 | 17 | 18 | 19 | seckill-dispatcher 20 | 21 | / 22 | 23 | 24 | 25 | 26 | 27 | DruidStatView 28 | com.alibaba.druid.support.http.StatViewServlet 29 | 30 | 31 | DruidStatView 32 | /druid/* 33 | 34 | 35 | druidWebStatFilter 36 | com.alibaba.druid.support.http.WebStatFilter 37 | 38 | exclusions 39 | /public/*,*.js,*.css,/druid*,*.jsp,*.swf 40 | 41 | 42 | principalSessionName 43 | sessionInfo 44 | 45 | 46 | profileEnable 47 | true 48 | 49 | 50 | 51 | druidWebStatFilter 52 | /* 53 | 54 | 55 | 56 | cors 57 | com.hisen.filter.CorsFilter 58 | 59 | 60 | cors 61 | /* 62 | 63 | 64 | -------------------------------------------------------------------------------- /BookSystem_V3/src/main/webapp/front/html/bookList.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | Bootstrap 101 Template 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | HiSEN 24 | 图书管理系统 - by ssm基础框架 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 首页 33 | 图书具体信息 34 | 添加图书信息 35 | 信息 36 | 37 | 38 | 39 | 40 | 41 | 42 | 图书列表 43 | 显示当前图书库存信息 44 | 45 | 46 | 47 | 48 | 49 | 50 | 51 | 52 | 53 | 图书编号 54 | 图书名字 55 | 图书数量 56 | 操作 57 | 58 | 59 | 60 | 61 | 62 | 63 | 64 | 65 | 66 | 67 | 68 | Prev 69 | 1 70 | 2 71 | 3 72 | 4 73 | 5 74 | Next 75 | 76 | 77 | 78 | 79 | 80 | 81 | × 82 | 83 | 84 | 注意! 85 | Warning! Best check yo self, you're not looking too good. alert link 87 | 88 | 89 | 90 | 91 | 92 | -------------------------------------------------------------------------------- /BookSystem_V3/src/main/webapp/front/html/login.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 登录 - 图书系统 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 账号 19 | 20 | 21 | 密码 22 | 23 | Submit 24 | 25 | 26 | 27 | 28 | 29 | 30 | -------------------------------------------------------------------------------- /BookSystem_V3/src/main/webapp/front/html/register.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 添加 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 账号 18 | 19 | 20 | 性别 21 | 22 | 23 | 年龄 24 | 25 | 26 | 密码 27 | 28 | 29 | 确认密码 30 | 31 | Submit 32 | 33 | 34 | 35 | 36 | 37 | 38 | -------------------------------------------------------------------------------- /BookSystem_V3/src/main/webapp/front/js/bookList.js: -------------------------------------------------------------------------------- 1 | $(document).ready(function () { 2 | $.ajax({ 3 | type: "GET", 4 | url: "http://localhost:8081/book/list/", 5 | dataType: 'json', 6 | global: "false", 7 | success: function (data) { 8 | for (var i = 0; i < data.length; i++) { 9 | $('table tbody').append( 10 | '' + 11 | '' + data[i].bookId + '' + 12 | '' + data[i].name + '' + 13 | '' + data[i].number + '' + 14 | '详情 | 删除' 17 | + 18 | '' 19 | ); 20 | } 21 | } 22 | }) 23 | }); -------------------------------------------------------------------------------- /BookSystem_V3/src/main/webapp/front/js/demo.js: -------------------------------------------------------------------------------- 1 | $(document).ready(function () { 2 | 3 | var thisUrl = document.URL; 4 | var id = thisUrl.split('=')[1]; 5 | $.ajax({ 6 | type: "GET", 7 | url: "http://localhost:8080/seckill/" + id + "/detail", 8 | dataType: 'json', 9 | global: "false", 10 | success: function (data) { 11 | var seckill = data; 12 | var seckillId = seckill.seckillId; 13 | var name = seckill.name; 14 | var number = seckill.number; 15 | var start_time = seckill.startTime; 16 | var start_date = new Date(start_time); 17 | var end_time = seckill.endTime; 18 | var end_date = new Date(end_time); 19 | var create_time = seckill.createTime; 20 | var create_date = new Date(create_time); 21 | $('.panel-title').append(name); 22 | // 进行页面的初始化 23 | seckill_m.detail.init({ 24 | seckillId:seckillId, 25 | start_time:start_time, 26 | end_time:end_time 27 | }); 28 | } 29 | }) 30 | }); 31 | var seckill_m = { 32 | Url:{ 33 | now:'http://localhost:8080/seckill/time/now/', 34 | exposer:function(seckillId){ 35 | return 'http://localhost:8080/seckill/'+seckillId+'/exposer/'; 36 | }, 37 | execution:function(seckillId,md5,phoneCookie){ 38 | return 'http://localhost:8080/seckill/'+seckillId+'/'+md5+'/'+phoneCookie+'/execution' 39 | } 40 | }, 41 | 42 | validatePhone: function (phoneNum) { 43 | if (phoneNum && phoneNum.length == 11 && !isNaN(phoneNum)) { 44 | return true; 45 | } else { 46 | return false; 47 | } 48 | }, 49 | detail: { 50 | //详情页初始化 51 | init: function (params) { 52 | var userPhoneCookie = $.cookie('userPhone'); 53 | if (!seckill_m.validatePhone(userPhoneCookie)) { 54 | $('#phoneModal').modal({ 55 | show: true, 56 | keybody: false, 57 | backdrop: 'static' 58 | }); 59 | $('#seckillPhoneBtn').click(function(){ 60 | var inputPhone = $('#killPhoneKey').val(); 61 | if(seckill_m.validatePhone(inputPhone)){ 62 | $.cookie('userPhone',inputPhone,{path:'/',expires:7}); 63 | window.location.reload(); 64 | }else{ 65 | $('#killPhoneMessage').hide().html('手机号码错误!').show(300); 66 | } 67 | }); 68 | } 69 | var startTime = params.start_time; 70 | var endTime = params.end_time; 71 | var seckillId = params.seckillId; 72 | $.get(seckill_m.Url.now,{},function(data){ 73 | var nowTime = data.data; 74 | if(nowTime > endTime) { 75 | //秒杀结束 76 | $('#seckill-box').html('秒杀结束'); 77 | 78 | }else if(nowTime < startTime){ 79 | //秒杀未开始,倒计时 80 | var killTime = new Date(startTime+1000); 81 | $('#seckill-box').countdown(killTime,function(event){ 82 | var format = event.strftime('秒杀倒计时: %D天 %H时 %M分 %S秒'); 83 | $('#seckill-box').html(format); 84 | 85 | }).on('finish.countdown',function(){ 86 | seckill_m.handlerSeckill(seckillId,userPhoneCookie); 87 | }); 88 | }else{ 89 | //秒杀正在进行中 90 | seckill_m.handlerSeckill(seckillId,userPhoneCookie); 91 | } 92 | }) 93 | 94 | } 95 | }, 96 | handlerSeckill:function(seckillId,phoneCookie) { 97 | $('#seckill-box').html('开始秒杀'); 98 | $.post(seckill_m.Url.exposer(seckillId),{},function(data){ 99 | if(data.success){ 100 | var exposer = data.data; 101 | if(exposer.exposed){ 102 | //获得秒杀接口地址 103 | var md5 = exposer.md5; 104 | var seckillId= exposer.seckillId; 105 | var Url = seckill_m.Url.execution(seckillId,md5,phoneCookie); 106 | //执行秒杀操作 107 | $('#killBtn').one('click',function(){ 108 | $.post(Url,{},function(data){ 109 | $('#seckill-box').html(''+data.data.stateInfo+''); 110 | }); 111 | }); 112 | } 113 | }else{ 114 | console.log('error'+data.error); 115 | } 116 | }); 117 | 118 | } 119 | }; 120 | 121 | 122 | -------------------------------------------------------------------------------- /BookSystem_V3/src/main/webapp/front/js/login.js: -------------------------------------------------------------------------------- 1 | /** 2 | * Created by hisen on 17-8-26. 3 | */ 4 | var frm = $('#form'); 5 | frm.submit(function (ev) { 6 | $.ajax({ 7 | type: "post", 8 | url: "http://localhost:8183/login", 9 | data: frm.serialize(), 10 | success:function(data) { 11 | console.log(data) 12 | var json = jQuery.parseJSON(data); 13 | var jwt = json.jwt; 14 | // 登录成功,存储令牌到本地 15 | localStorage["jwt"] = jwt; 16 | localStorage["name"] = json.name; 17 | console.log(jwt); 18 | }, 19 | error:function(data){ 20 | console.log(data) 21 | } 22 | }); 23 | ev.preventDefault(); 24 | }); 25 | 26 | -------------------------------------------------------------------------------- /BookSystem_V3/src/main/webapp/front/js/register.js: -------------------------------------------------------------------------------- 1 | /** 2 | * Created by hisen on 17-8-27. 3 | */ 4 | //获取本地存储的数据,每次请求都带上 5 | var jwt = localStorage["jwt"]; 6 | var name = localStorage["name"]; 7 | console.log("输出:"+jwt); 8 | var frm = $('#form'); 9 | frm.submit(function (ev) { 10 | $.ajax({ 11 | headers: { 12 | Authorization : jwt, 13 | 'name' : name 14 | }, 15 | type: "post", 16 | url: "http://localhost:8183/register", 17 | data: frm.serialize(), 18 | success:function(data) { 19 | console.log(data) 20 | }, 21 | error:function(data){ 22 | console.log(data) 23 | } 24 | }); 25 | ev.preventDefault(); 26 | }); -------------------------------------------------------------------------------- /BookSystem_V3/src/main/webapp/index.jsp: -------------------------------------------------------------------------------- 1 | 2 | 3 | Hello World! 4 | 5 | 6 | -------------------------------------------------------------------------------- /BookSystem_V3/src/test/AppointmentServiceImplTest.java: -------------------------------------------------------------------------------- 1 | import com.hisen.bean.request.AppointmentRequest; 2 | import com.hisen.service.AppointmentService; 3 | import org.junit.Test; 4 | import org.springframework.beans.factory.annotation.Autowired; 5 | 6 | /** 7 | * Created by hisenyuan on 2017/8/2 at 11:03. 8 | */ 9 | public class AppointmentServiceImplTest extends BaseTest { 10 | 11 | @Autowired 12 | private AppointmentService appointmentService; 13 | 14 | /** 15 | * 测试预约功能 16 | */ 17 | @Test 18 | public void insertApponit() { 19 | AppointmentRequest form = new AppointmentRequest(); 20 | form.setHoldDay("10"); 21 | form.setBookId(103); 22 | form.setUserNumber(20080808); 23 | int appoint = appointmentService.appoint(form); 24 | System.out.println(appoint); 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /BookSystem_V3/src/test/BaseTest.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Created by hisenyuan on 2017/5/3 at 19:40. 3 | */ 4 | 5 | import org.junit.runner.RunWith; 6 | import org.springframework.test.context.ContextConfiguration; 7 | import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; 8 | 9 | /** 10 | * 配置spring和junit整合,junit启动时加载springIOC容器 spring-test,junit 11 | */ 12 | @RunWith(SpringJUnit4ClassRunner.class) 13 | // 告诉junit spring配置文件 14 | @ContextConfiguration({ "classpath:spring/spring-dao.xml", "classpath:spring/spring-service.xml" }) 15 | public class BaseTest { 16 | 17 | } 18 | --------------------------------------------------------------------------------
77 | 《${requestScope.get('book').name}》 ${requestScope.get('book').detail} 78 |
66 | } 67 |
...