├── .classpath ├── .gitignore ├── .project ├── .settings ├── .jsdtscope ├── org.eclipse.jdt.core.prefs ├── org.eclipse.jst.j2ee.ejb.annotations.xdoclet.prefs ├── org.eclipse.ltk.core.refactoring.prefs ├── org.eclipse.m2e.core.prefs ├── org.eclipse.wst.common.component ├── org.eclipse.wst.common.project.facet.core.xml ├── org.eclipse.wst.jsdt.ui.superType.container ├── org.eclipse.wst.jsdt.ui.superType.name ├── org.eclipse.wst.validation.prefs └── org.eclipse.wst.ws.service.policy.prefs ├── README.md ├── WebContent ├── META-INF │ └── MANIFEST.MF ├── WEB-INF │ ├── lib │ │ ├── com.springsource.org.aopalliance-1.0.0.jar │ │ ├── com.springsource.org.apache.commons.fileupload-1.2.0.jar │ │ ├── com.springsource.org.apache.commons.io-1.4.0.jar │ │ ├── com.springsource.org.apache.commons.logging-1.1.1.jar │ │ ├── commons-codec-1.10.jar │ │ ├── fastjson-1.2.9.jar │ │ ├── jedis-2.9.0.jar │ │ ├── mysql-connector-java-5.1.36-bin.jar │ │ ├── org.springframework.aop-3.0.2.RELEASE.jar │ │ ├── org.springframework.asm-3.0.2.RELEASE.jar │ │ ├── org.springframework.beans-3.0.2.RELEASE.jar │ │ ├── org.springframework.context-3.0.2.RELEASE.jar │ │ ├── org.springframework.context.support-3.0.2.RELEASE.jar │ │ ├── org.springframework.core-3.0.2.RELEASE.jar │ │ ├── org.springframework.expression-3.0.2.RELEASE.jar │ │ ├── org.springframework.web-3.0.2.RELEASE.jar │ │ └── org.springframework.web.servlet-3.0.2.RELEASE.jar │ ├── springmvc-servlet.xml │ └── web.xml └── view │ └── article.jsp ├── build └── classes │ ├── .gitignore │ ├── com │ └── happyheng │ │ ├── dao │ │ ├── UserDao.class │ │ └── impl │ │ │ ├── BaseDaoImplement.class │ │ │ └── UserDaoImplement.class │ │ ├── entity │ │ ├── IdEntity.class │ │ ├── User.class │ │ └── result │ │ │ └── LoginResult.class │ │ ├── service │ │ ├── LoginService.class │ │ └── RegisterService.class │ │ ├── servlet │ │ ├── LoginServlet.class │ │ └── RegisterServlet.class │ │ ├── test │ │ └── ConnectionTest.class │ │ └── utils │ │ └── ConnectionFactory.class │ └── dbconfig.properties ├── pom.xml └── src ├── applicationContext.xml ├── com └── happyheng │ ├── cache │ └── NewsCountCache.java │ ├── controller │ ├── BaseController.java │ ├── NewsController.java │ ├── SportController.java │ └── UserController.java │ ├── dao │ ├── NewsCountDao.java │ ├── NewsDao.java │ ├── SportRecordDao.java │ ├── UserDao.java │ └── impl │ │ ├── BaseDaoImplement.java │ │ ├── NewsCountDaoImpl.java │ │ ├── NewsDaoImpl.java │ │ ├── NewsDaoRedisImplement.java │ │ ├── SportRecordDaoImpl.java │ │ └── UserDaoImpl.java │ ├── encrypt │ └── AESUtils.java │ ├── entity │ ├── IdEntity.java │ ├── Location.java │ ├── News.java │ ├── Sport.java │ ├── SportRecord.java │ ├── User.java │ └── result │ │ ├── LoginResult.java │ │ ├── NewsResult.java │ │ ├── SportListResult.java │ │ ├── SportMessageResult.java │ │ └── SportRecordResult.java │ ├── factory │ └── DaoFactory.java │ ├── filter │ ├── CharacterFilter.java │ └── VerifyFilter.java │ ├── mapper │ ├── NewsMapper.xml │ ├── sportRecordMapper.xml │ └── userMapper.xml │ ├── secret │ ├── VerifySecret.java │ └── impl │ │ └── AESSercret.java │ ├── service │ ├── LoginService.java │ ├── NewsService.java │ ├── RegisterService.java │ ├── SportIdService.java │ ├── SportListService.java │ ├── SportMessageService.java │ ├── SportRecordService.java │ └── impl │ │ ├── BaseService.java │ │ ├── LoginServiceImpl.java │ │ ├── NewsCountServiceImpl.java │ │ ├── NewsServiceImpl.java │ │ ├── RegisterServiceImpl.java │ │ ├── SportIdServiceImpl.java │ │ ├── SportListServiceImpl.java │ │ ├── SportMessageImpl.java │ │ └── SportRecordServiceImpl.java │ ├── test │ ├── ConnectionTest.java │ ├── RedisTest.java │ └── UserDaoTest.java │ └── utils │ ├── ConnectionFactory.java │ ├── ContextUtils.java │ ├── HexUtils.java │ ├── MyBatisUtil.java │ ├── PropertiesUtils.java │ ├── Redis.java │ └── TextUtils.java ├── conf.xml ├── dbconfig.properties └── key.properties /.classpath: -------------------------------------------------------------------------------- 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 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | /target/ 2 | -------------------------------------------------------------------------------- /.project: -------------------------------------------------------------------------------- 1 | 2 | 3 | Sport 4 | 5 | 6 | 7 | 8 | 9 | org.eclipse.wst.jsdt.core.javascriptValidator 10 | 11 | 12 | 13 | 14 | org.eclipse.jdt.core.javabuilder 15 | 16 | 17 | 18 | 19 | org.eclipse.wst.common.project.facet.core.builder 20 | 21 | 22 | 23 | 24 | org.eclipse.wst.validation.validationbuilder 25 | 26 | 27 | 28 | 29 | org.eclipse.m2e.core.maven2Builder 30 | 31 | 32 | 33 | 34 | 35 | org.eclipse.m2e.core.maven2Nature 36 | org.eclipse.jem.workbench.JavaEMFNature 37 | org.eclipse.wst.common.modulecore.ModuleCoreNature 38 | org.eclipse.wst.common.project.facet.core.nature 39 | org.eclipse.jdt.core.javanature 40 | org.eclipse.wst.jsdt.core.jsNature 41 | 42 | 43 | -------------------------------------------------------------------------------- /.settings/.jsdtscope: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | -------------------------------------------------------------------------------- /.settings/org.eclipse.jdt.core.prefs: -------------------------------------------------------------------------------- 1 | eclipse.preferences.version=1 2 | org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled 3 | org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.8 4 | org.eclipse.jdt.core.compiler.compliance=1.8 5 | org.eclipse.jdt.core.compiler.problem.assertIdentifier=error 6 | org.eclipse.jdt.core.compiler.problem.enumIdentifier=error 7 | org.eclipse.jdt.core.compiler.problem.forbiddenReference=warning 8 | org.eclipse.jdt.core.compiler.source=1.8 9 | -------------------------------------------------------------------------------- /.settings/org.eclipse.jst.j2ee.ejb.annotations.xdoclet.prefs: -------------------------------------------------------------------------------- 1 | XDOCLETBUILDERACTIVE=true 2 | XDOCLETHOME= 3 | XDOCLETUSEGLOBAL=true 4 | XDOCLETVERSION=1.2.1 5 | eclipse.preferences.version=1 6 | -------------------------------------------------------------------------------- /.settings/org.eclipse.ltk.core.refactoring.prefs: -------------------------------------------------------------------------------- 1 | eclipse.preferences.version=1 2 | org.eclipse.ltk.core.refactoring.enable.project.refactoring.history=false 3 | -------------------------------------------------------------------------------- /.settings/org.eclipse.m2e.core.prefs: -------------------------------------------------------------------------------- 1 | activeProfiles= 2 | eclipse.preferences.version=1 3 | resolveWorkspaceProjects=true 4 | version=1 5 | -------------------------------------------------------------------------------- /.settings/org.eclipse.wst.common.component: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | -------------------------------------------------------------------------------- /.settings/org.eclipse.wst.common.project.facet.core.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | -------------------------------------------------------------------------------- /.settings/org.eclipse.wst.jsdt.ui.superType.container: -------------------------------------------------------------------------------- 1 | org.eclipse.wst.jsdt.launching.baseBrowserLibrary -------------------------------------------------------------------------------- /.settings/org.eclipse.wst.jsdt.ui.superType.name: -------------------------------------------------------------------------------- 1 | Window -------------------------------------------------------------------------------- /.settings/org.eclipse.wst.validation.prefs: -------------------------------------------------------------------------------- 1 | disabled=06target 2 | eclipse.preferences.version=1 3 | -------------------------------------------------------------------------------- /.settings/org.eclipse.wst.ws.service.policy.prefs: -------------------------------------------------------------------------------- 1 | eclipse.preferences.version=1 2 | org.eclipse.wst.ws.service.policy.projectEnabled=false 3 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # 描述 2 | 一个基于Java Web的服务器端程序,实现了对应Android程序所需的接口。其对应的Android程序源码为[Sport_Android](https://github.com/happyheng/Sport_Android)。 3 | 4 | #技术栈 5 | [Spring](http://projects.spring.io/spring-framework/#quick-start) 在项目中充当了管理容器的角色 6 | 7 | [Spring MVC](http://projects.spring.io/spring-framework/#quick-start) 提供了构建Web应用程序的全能MVC模块 8 | 9 | [MyBatis](http://www.mybatis.org/mybatis-3/index.html) 一个支持普通SQL查询,存储过程和高级映射的优秀持久层框架 10 | 11 | [Redis](http://redis.io) 一个基于Key-Value的内存数据库 12 | 13 | #配置信息 14 | ### 数据库配置 15 | 本项目使用MySQL数据库,其数据库建表语句为 16 | 17 | ``` 18 | -- User表 19 | create table tal_user( 20 | id int(11) unsigned not null auto_increment, 21 | name varchar(50) not null unique, 22 | password varchar(50) not null, 23 | nickname varchar(50) not null, 24 | token varchar(50), 25 | stime TIMESTAMP, 26 | primary key(id) 27 | ) ENGINE=InnoDB DEFAULT CHARSET=UTF8; 28 | 29 | -- 运动表 30 | create table tal_sport( 31 | id int(11) unsigned not null auto_increment, 32 | userid int(11) unsigned not null, 33 | primary key(id) 34 | )ENGINE=InnoDB DEFAULT CHARSET=UTF8; 35 | 36 | -- 运动信息表 37 | create table tal_sport_message( 38 | id int(11) unsigned not null auto_increment, 39 | sportId int(11) unsigned, 40 | posx float, 41 | posy float, 42 | stime TIMESTAMP, 43 | location varchar(100), 44 | primary key (id) 45 | )ENGINE=InnoDB DEFAULT CHARSET=UTF8; 46 | 47 | -- 资讯表 48 | create table tal_news( 49 | id int(11) unsigned not null auto_increment, 50 | name varchar(50) not null, 51 | simplecontent varchar(100), 52 | thumbnail varchar(255), 53 | url varchar(255), 54 | stime TIMESTAMP, 55 | primary key (id) 56 | )ENGINE=InnoDB DEFAULT CHARSET=UTF8; 57 | 58 | ``` 59 | ### 数据库连接配置 60 | 连接数据库的配置信息存储在项目的/src/dbconfig.properties中,需将其改成自己的数据库连接配置。 61 | 62 | 需要注意的是,本项目使用Meven进行项目构建,所以在运行项目之前需要确保Maven可以正常使用。 63 | 64 | 65 | 66 | -------------------------------------------------------------------------------- /WebContent/META-INF/MANIFEST.MF: -------------------------------------------------------------------------------- 1 | Manifest-Version: 1.0 2 | Class-Path: 3 | 4 | -------------------------------------------------------------------------------- /WebContent/WEB-INF/lib/com.springsource.org.aopalliance-1.0.0.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/happyheng/Sport_Service/342ff4a4e45f8154a90b75c00c072971d33ce0c7/WebContent/WEB-INF/lib/com.springsource.org.aopalliance-1.0.0.jar -------------------------------------------------------------------------------- /WebContent/WEB-INF/lib/com.springsource.org.apache.commons.fileupload-1.2.0.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/happyheng/Sport_Service/342ff4a4e45f8154a90b75c00c072971d33ce0c7/WebContent/WEB-INF/lib/com.springsource.org.apache.commons.fileupload-1.2.0.jar -------------------------------------------------------------------------------- /WebContent/WEB-INF/lib/com.springsource.org.apache.commons.io-1.4.0.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/happyheng/Sport_Service/342ff4a4e45f8154a90b75c00c072971d33ce0c7/WebContent/WEB-INF/lib/com.springsource.org.apache.commons.io-1.4.0.jar -------------------------------------------------------------------------------- /WebContent/WEB-INF/lib/com.springsource.org.apache.commons.logging-1.1.1.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/happyheng/Sport_Service/342ff4a4e45f8154a90b75c00c072971d33ce0c7/WebContent/WEB-INF/lib/com.springsource.org.apache.commons.logging-1.1.1.jar -------------------------------------------------------------------------------- /WebContent/WEB-INF/lib/commons-codec-1.10.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/happyheng/Sport_Service/342ff4a4e45f8154a90b75c00c072971d33ce0c7/WebContent/WEB-INF/lib/commons-codec-1.10.jar -------------------------------------------------------------------------------- /WebContent/WEB-INF/lib/fastjson-1.2.9.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/happyheng/Sport_Service/342ff4a4e45f8154a90b75c00c072971d33ce0c7/WebContent/WEB-INF/lib/fastjson-1.2.9.jar -------------------------------------------------------------------------------- /WebContent/WEB-INF/lib/jedis-2.9.0.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/happyheng/Sport_Service/342ff4a4e45f8154a90b75c00c072971d33ce0c7/WebContent/WEB-INF/lib/jedis-2.9.0.jar -------------------------------------------------------------------------------- /WebContent/WEB-INF/lib/mysql-connector-java-5.1.36-bin.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/happyheng/Sport_Service/342ff4a4e45f8154a90b75c00c072971d33ce0c7/WebContent/WEB-INF/lib/mysql-connector-java-5.1.36-bin.jar -------------------------------------------------------------------------------- /WebContent/WEB-INF/lib/org.springframework.aop-3.0.2.RELEASE.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/happyheng/Sport_Service/342ff4a4e45f8154a90b75c00c072971d33ce0c7/WebContent/WEB-INF/lib/org.springframework.aop-3.0.2.RELEASE.jar -------------------------------------------------------------------------------- /WebContent/WEB-INF/lib/org.springframework.asm-3.0.2.RELEASE.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/happyheng/Sport_Service/342ff4a4e45f8154a90b75c00c072971d33ce0c7/WebContent/WEB-INF/lib/org.springframework.asm-3.0.2.RELEASE.jar -------------------------------------------------------------------------------- /WebContent/WEB-INF/lib/org.springframework.beans-3.0.2.RELEASE.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/happyheng/Sport_Service/342ff4a4e45f8154a90b75c00c072971d33ce0c7/WebContent/WEB-INF/lib/org.springframework.beans-3.0.2.RELEASE.jar -------------------------------------------------------------------------------- /WebContent/WEB-INF/lib/org.springframework.context-3.0.2.RELEASE.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/happyheng/Sport_Service/342ff4a4e45f8154a90b75c00c072971d33ce0c7/WebContent/WEB-INF/lib/org.springframework.context-3.0.2.RELEASE.jar -------------------------------------------------------------------------------- /WebContent/WEB-INF/lib/org.springframework.context.support-3.0.2.RELEASE.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/happyheng/Sport_Service/342ff4a4e45f8154a90b75c00c072971d33ce0c7/WebContent/WEB-INF/lib/org.springframework.context.support-3.0.2.RELEASE.jar -------------------------------------------------------------------------------- /WebContent/WEB-INF/lib/org.springframework.core-3.0.2.RELEASE.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/happyheng/Sport_Service/342ff4a4e45f8154a90b75c00c072971d33ce0c7/WebContent/WEB-INF/lib/org.springframework.core-3.0.2.RELEASE.jar -------------------------------------------------------------------------------- /WebContent/WEB-INF/lib/org.springframework.expression-3.0.2.RELEASE.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/happyheng/Sport_Service/342ff4a4e45f8154a90b75c00c072971d33ce0c7/WebContent/WEB-INF/lib/org.springframework.expression-3.0.2.RELEASE.jar -------------------------------------------------------------------------------- /WebContent/WEB-INF/lib/org.springframework.web-3.0.2.RELEASE.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/happyheng/Sport_Service/342ff4a4e45f8154a90b75c00c072971d33ce0c7/WebContent/WEB-INF/lib/org.springframework.web-3.0.2.RELEASE.jar -------------------------------------------------------------------------------- /WebContent/WEB-INF/lib/org.springframework.web.servlet-3.0.2.RELEASE.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/happyheng/Sport_Service/342ff4a4e45f8154a90b75c00c072971d33ce0c7/WebContent/WEB-INF/lib/org.springframework.web.servlet-3.0.2.RELEASE.jar -------------------------------------------------------------------------------- /WebContent/WEB-INF/springmvc-servlet.xml: -------------------------------------------------------------------------------- 1 | 2 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | -------------------------------------------------------------------------------- /WebContent/WEB-INF/web.xml: -------------------------------------------------------------------------------- 1 | 2 | 6 | WebStudy 7 | 8 | index.html 9 | index.htm 10 | index.jsp 11 | default.html 12 | default.htm 13 | default.jsp 14 | 15 | 16 | 17 | characterFilter 18 | com.happyheng.filter.CharacterFilter 19 | 20 | 21 | 22 | characterFilter 23 | /* 24 | 25 | 26 | 27 | verifyFilter 28 | com.happyheng.filter.VerifyFilter 29 | 30 | 31 | 32 | verifyFilter 33 | /* 34 | 35 | 36 | 37 | 38 | springmvc 39 | org.springframework.web.servlet.DispatcherServlet 40 | 41 | 42 | springmvc 43 | / 44 | 45 | 46 | 47 | -------------------------------------------------------------------------------- /WebContent/view/article.jsp: -------------------------------------------------------------------------------- 1 | <%@ page language="java" contentType="text/html; charset=UTF-8" 2 | pageEncoding="UTF-8"%> 3 | 4 | 5 | 6 | 7 | 跑步,是皮肤的保鲜剂还是大杀器? 8 | 68 | 69 | 70 |
71 |
72 |

跑步,是皮肤的保鲜剂还是大杀器?

73 |
74 | 75 |

对于天生爱美的女性,跑步是皮肤保鲜剂吗?是让皮肤变好的最直接有效的方式吗?

76 | 77 |

身边常有人说:“每次跑步之后,脸上总会有点痒痒的感觉,第二天就冒了两颗痘痘!看来我的肤质不适合跑步啊?” 78 | 但也有人说:“跑步出汗排毒,带走了杂质和油脂,皮肤越发光滑紧致了!”

79 | 80 | 82 | 83 |

那,对于天生爱美的女性,难道跑步并不是好的健身选择?!难道每次跑步都要顾虑对皮肤的影响?当然不是!

84 |

看看陈意涵,再看女性跑步新标杆—张钧甯。

85 |

世界上最华丽的时装也比不上健康、光洁、细腻、嫩滑、晶莹、红润和富有弹性的皮肤。而跑步又是当今最潮、最容易开展也是最受民众瞩目的健身运动方式。

86 | 87 |

阅读量:<%=request.getAttribute("read")%>

88 |
89 | 90 | 91 | 92 | -------------------------------------------------------------------------------- /build/classes/.gitignore: -------------------------------------------------------------------------------- 1 | /com/ 2 | -------------------------------------------------------------------------------- /build/classes/com/happyheng/dao/UserDao.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/happyheng/Sport_Service/342ff4a4e45f8154a90b75c00c072971d33ce0c7/build/classes/com/happyheng/dao/UserDao.class -------------------------------------------------------------------------------- /build/classes/com/happyheng/dao/impl/BaseDaoImplement.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/happyheng/Sport_Service/342ff4a4e45f8154a90b75c00c072971d33ce0c7/build/classes/com/happyheng/dao/impl/BaseDaoImplement.class -------------------------------------------------------------------------------- /build/classes/com/happyheng/dao/impl/UserDaoImplement.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/happyheng/Sport_Service/342ff4a4e45f8154a90b75c00c072971d33ce0c7/build/classes/com/happyheng/dao/impl/UserDaoImplement.class -------------------------------------------------------------------------------- /build/classes/com/happyheng/entity/IdEntity.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/happyheng/Sport_Service/342ff4a4e45f8154a90b75c00c072971d33ce0c7/build/classes/com/happyheng/entity/IdEntity.class -------------------------------------------------------------------------------- /build/classes/com/happyheng/entity/User.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/happyheng/Sport_Service/342ff4a4e45f8154a90b75c00c072971d33ce0c7/build/classes/com/happyheng/entity/User.class -------------------------------------------------------------------------------- /build/classes/com/happyheng/entity/result/LoginResult.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/happyheng/Sport_Service/342ff4a4e45f8154a90b75c00c072971d33ce0c7/build/classes/com/happyheng/entity/result/LoginResult.class -------------------------------------------------------------------------------- /build/classes/com/happyheng/service/LoginService.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/happyheng/Sport_Service/342ff4a4e45f8154a90b75c00c072971d33ce0c7/build/classes/com/happyheng/service/LoginService.class -------------------------------------------------------------------------------- /build/classes/com/happyheng/service/RegisterService.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/happyheng/Sport_Service/342ff4a4e45f8154a90b75c00c072971d33ce0c7/build/classes/com/happyheng/service/RegisterService.class -------------------------------------------------------------------------------- /build/classes/com/happyheng/servlet/LoginServlet.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/happyheng/Sport_Service/342ff4a4e45f8154a90b75c00c072971d33ce0c7/build/classes/com/happyheng/servlet/LoginServlet.class -------------------------------------------------------------------------------- /build/classes/com/happyheng/servlet/RegisterServlet.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/happyheng/Sport_Service/342ff4a4e45f8154a90b75c00c072971d33ce0c7/build/classes/com/happyheng/servlet/RegisterServlet.class -------------------------------------------------------------------------------- /build/classes/com/happyheng/test/ConnectionTest.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/happyheng/Sport_Service/342ff4a4e45f8154a90b75c00c072971d33ce0c7/build/classes/com/happyheng/test/ConnectionTest.class -------------------------------------------------------------------------------- /build/classes/com/happyheng/utils/ConnectionFactory.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/happyheng/Sport_Service/342ff4a4e45f8154a90b75c00c072971d33ce0c7/build/classes/com/happyheng/utils/ConnectionFactory.class -------------------------------------------------------------------------------- /build/classes/dbconfig.properties: -------------------------------------------------------------------------------- 1 | driver = com.mysql.jdbc.Driver 2 | dburl = jdbc\:mysql\://127.0.0.1\:3306/sport_db 3 | user = root 4 | password = mytestcon -------------------------------------------------------------------------------- /pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 4.0.0 3 | Sport 4 | Sport 5 | 0.0.1-SNAPSHOT 6 | war 7 | 8 | src 9 | 10 | 11 | src 12 | 13 | **/*.java 14 | 15 | 16 | 17 | 18 | 19 | maven-war-plugin 20 | 2.6 21 | 22 | WebContent 23 | false 24 | 25 | 26 | 27 | maven-compiler-plugin 28 | 3.3 29 | 30 | 1.8 31 | 1.8 32 | 33 | 34 | 35 | 36 | 37 | 38 | org.mybatis 39 | mybatis 40 | 3.1.1 41 | 42 | 43 | org.mybatis 44 | mybatis-spring 45 | 1.1.0 46 | 47 | 48 | commons-dbcp 49 | commons-dbcp 50 | 1.4 51 | 52 | 53 | -------------------------------------------------------------------------------- /src/applicationContext.xml: -------------------------------------------------------------------------------- 1 | 2 | 11 | 12 | 14 | 15 | 16 | 17 | 19 | 20 | 21 | 22 | 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 | 77 | 78 | 79 | 80 | 81 | 82 | 83 | 84 | 85 | 86 | 87 | 88 | 89 | 90 | 91 | 92 | 93 | 94 | 95 | -------------------------------------------------------------------------------- /src/com/happyheng/cache/NewsCountCache.java: -------------------------------------------------------------------------------- 1 | package com.happyheng.cache; 2 | 3 | import com.happyheng.dao.NewsCountDao; 4 | import com.happyheng.utils.Redis; 5 | 6 | import redis.clients.jedis.Jedis; 7 | 8 | public class NewsCountCache { 9 | 10 | public static final String KEY_ARTICLE_COUNT = "articleCount"; 11 | public static final String KEY_FIELD_COUNT = "art:"; 12 | 13 | private NewsCountDao countDao; 14 | 15 | public NewsCountDao getCountDao() { 16 | return countDao; 17 | } 18 | 19 | public void setCountDao(NewsCountDao countDao) { 20 | this.countDao = countDao; 21 | } 22 | 23 | public String getAndAddCountFromCache(String id) throws Exception { 24 | 25 | Jedis jedis = Redis.getConnection(); 26 | String fieldCountKey = KEY_FIELD_COUNT + id; 27 | 28 | // 1、查看key中是否有 29 | if (!jedis.exists(KEY_ARTICLE_COUNT) || !jedis.hexists(KEY_ARTICLE_COUNT, fieldCountKey)) { 30 | 31 | // 2、从数据库中进行读取并存入Redis中 32 | // Connection connection = 33 | // ConnectionFactory.getInstance().makeConnection(); 34 | // NewsCountDao dao = new NewsCountDaoImplement(); 35 | Integer readCount = countDao.getNewsReadCount(Integer.valueOf(id)); 36 | 37 | if (readCount == null) { 38 | throw new Exception(); 39 | } 40 | 41 | jedis.hset(KEY_ARTICLE_COUNT, fieldCountKey, String.valueOf(readCount)); 42 | } 43 | 44 | // 3、先+1,然后获取 45 | jedis.hincrBy(KEY_ARTICLE_COUNT, fieldCountKey, 1); 46 | String count = jedis.hget(KEY_ARTICLE_COUNT, fieldCountKey); 47 | return count; 48 | 49 | } 50 | } 51 | -------------------------------------------------------------------------------- /src/com/happyheng/controller/BaseController.java: -------------------------------------------------------------------------------- 1 | package com.happyheng.controller; 2 | 3 | public class BaseController { 4 | protected static final String RESULT_KEY = "result"; 5 | private static final String APPLICATION_XML = "applicationContext.xml"; 6 | 7 | // protected ApplicationContext context; 8 | // 9 | // public BaseController(){ 10 | //// context = new ClassPathXmlApplicationContext(APPLICATION_XML); 11 | // } 12 | } 13 | -------------------------------------------------------------------------------- /src/com/happyheng/controller/NewsController.java: -------------------------------------------------------------------------------- 1 | package com.happyheng.controller; 2 | 3 | import java.io.PrintWriter; 4 | 5 | import javax.servlet.http.HttpServletRequest; 6 | import javax.servlet.http.HttpServletResponse; 7 | 8 | import org.springframework.stereotype.Controller; 9 | import org.springframework.web.bind.annotation.RequestMapping; 10 | import org.springframework.web.bind.annotation.RequestMethod; 11 | import org.springframework.web.servlet.ModelAndView; 12 | 13 | import com.alibaba.fastjson.JSON; 14 | import com.happyheng.entity.result.NewsResult; 15 | import com.happyheng.service.NewsService; 16 | import com.happyheng.service.impl.NewsCountServiceImpl; 17 | import com.happyheng.utils.ContextUtils; 18 | 19 | @Controller 20 | public class NewsController extends BaseController { 21 | // public NewsController() { 22 | // super(); 23 | // } 24 | 25 | @RequestMapping(value = "/News", method = RequestMethod.POST) 26 | public void getNews(HttpServletRequest req, HttpServletResponse resp) throws Exception { 27 | int begin = 0; 28 | int id = 0; 29 | if (req.getAttribute("begin") != null) { 30 | begin = (int) req.getAttribute("begin"); 31 | } 32 | if (req.getAttribute("id") != null) { 33 | id = (int) req.getAttribute("id"); 34 | } 35 | 36 | int count = (int) req.getAttribute("count"); 37 | 38 | NewsService service = (NewsService) ContextUtils.getContext().getBean("newsService"); 39 | NewsResult result = service.getNews(begin, id, count); 40 | 41 | String resultJson = JSON.toJSONString(result); 42 | 43 | resp.setContentType("text/html;charset=utf-8"); 44 | resp.setCharacterEncoding("utf-8"); 45 | PrintWriter printWriter = resp.getWriter(); 46 | printWriter.write(resultJson); 47 | printWriter.close(); 48 | } 49 | 50 | @RequestMapping(value = "/NewsDetail") 51 | public ModelAndView getNewsDetail(HttpServletRequest req, HttpServletResponse resp) throws Exception { 52 | 53 | String newsId = req.getParameter("id"); 54 | 55 | NewsCountServiceImpl service = (NewsCountServiceImpl)ContextUtils.getContext().getBean("newsCountService"); 56 | String count = service.addAndGetReadCount(newsId); 57 | req.setAttribute("read", count); 58 | 59 | System.out.println("输出的结果为" + count); 60 | ModelAndView view = new ModelAndView("article"); 61 | return view; 62 | } 63 | 64 | } 65 | -------------------------------------------------------------------------------- /src/com/happyheng/controller/SportController.java: -------------------------------------------------------------------------------- 1 | package com.happyheng.controller; 2 | 3 | import java.io.PrintWriter; 4 | import java.math.BigDecimal; 5 | import java.util.HashMap; 6 | import java.util.Map; 7 | 8 | import javax.servlet.http.HttpServletRequest; 9 | import javax.servlet.http.HttpServletResponse; 10 | 11 | import org.springframework.stereotype.Controller; 12 | import org.springframework.web.bind.annotation.RequestMapping; 13 | import org.springframework.web.bind.annotation.RequestMethod; 14 | 15 | import com.alibaba.fastjson.JSON; 16 | import com.happyheng.entity.result.SportListResult; 17 | import com.happyheng.entity.result.SportMessageResult; 18 | import com.happyheng.entity.result.SportRecordResult; 19 | import com.happyheng.service.SportIdService; 20 | import com.happyheng.service.SportListService; 21 | import com.happyheng.service.SportMessageService; 22 | import com.happyheng.service.SportRecordService; 23 | import com.happyheng.service.impl.BaseService; 24 | import com.happyheng.utils.ContextUtils; 25 | 26 | @Controller 27 | public class SportController extends BaseController { 28 | // public SportController() { 29 | // super(); 30 | // } 31 | 32 | @RequestMapping(value = "/SportId", method = RequestMethod.POST) 33 | public void getSportId(HttpServletRequest req, HttpServletResponse resp) throws Exception { 34 | String userKey = (String) req.getAttribute("ukey"); 35 | 36 | // 得到返回的结果 37 | SportIdService service = (SportIdService) ContextUtils.getContext().getBean("sportIdService"); 38 | SportRecordResult result = service.getSportId(userKey); 39 | 40 | Map responseMap = new HashMap<>(); 41 | // 说明插入成功 42 | if (result.getCode() == BaseService.RESULT_CODE_SUCCESS) { 43 | responseMap.put("id", result.getSportId()); 44 | } 45 | responseMap.put(RESULT_KEY, result.getCode()); 46 | 47 | String JsonResult = JSON.toJSONString(responseMap); 48 | System.out.println("结果为" + JsonResult); 49 | 50 | PrintWriter printWriter = resp.getWriter(); 51 | printWriter.write(JsonResult); 52 | printWriter.close(); 53 | } 54 | 55 | @RequestMapping(value = "/Record", method = RequestMethod.POST) 56 | public void record(HttpServletRequest req, HttpServletResponse resp) throws Exception { 57 | int sportId = (int) req.getAttribute("id"); 58 | double posx = ((BigDecimal) req.getAttribute("posx")).doubleValue(); 59 | double posy = ((BigDecimal) req.getAttribute("posy")).doubleValue(); 60 | String location = (String) req.getAttribute("location"); 61 | 62 | SportRecordService service = (SportRecordService) ContextUtils.getContext().getBean("sportRecordService"); 63 | 64 | Map responseMap = new HashMap<>(); 65 | // 不为空,则说明是来插入信息并获取sportId的 66 | int resultCode = service.record(sportId, posx, posy, location); 67 | responseMap.put(RESULT_KEY, resultCode); 68 | 69 | String result = JSON.toJSONString(responseMap); 70 | System.out.println("结果为" + result); 71 | 72 | PrintWriter printWriter = resp.getWriter(); 73 | printWriter.write(result); 74 | printWriter.close(); 75 | } 76 | 77 | @RequestMapping(value = "/SportList", method = RequestMethod.POST) 78 | public void getSportList(HttpServletRequest req, HttpServletResponse resp) throws Exception { 79 | String userKey = (String) req.getAttribute("userkey"); 80 | 81 | SportListService service = (SportListService)ContextUtils.getContext().getBean("sportListService"); 82 | SportListResult result = service.getSportList(userKey); 83 | 84 | String resultJson = JSON.toJSONString(result); 85 | System.out.println("结果为" + resultJson); 86 | 87 | PrintWriter printWriter = resp.getWriter(); 88 | printWriter.write(resultJson); 89 | printWriter.close(); 90 | } 91 | 92 | @RequestMapping(value = "/SportMessage", method = RequestMethod.POST) 93 | public void getSportMessage(HttpServletRequest req, HttpServletResponse resp) throws Exception { 94 | String sportId = (String) req.getAttribute("sportId"); 95 | 96 | SportMessageService service = (SportMessageService)ContextUtils.getContext().getBean("sportMessageService"); 97 | SportMessageResult result = service.getSportMessage("", sportId); 98 | 99 | String resultJson = JSON.toJSONString(result); 100 | System.out.println("结果为" + resultJson); 101 | 102 | PrintWriter printWriter = resp.getWriter(); 103 | printWriter.write(resultJson); 104 | printWriter.close(); 105 | } 106 | 107 | } 108 | -------------------------------------------------------------------------------- /src/com/happyheng/controller/UserController.java: -------------------------------------------------------------------------------- 1 | package com.happyheng.controller; 2 | 3 | import java.io.PrintWriter; 4 | import java.util.HashMap; 5 | import java.util.Map; 6 | 7 | import javax.servlet.http.HttpServletRequest; 8 | import javax.servlet.http.HttpServletResponse; 9 | 10 | import org.springframework.stereotype.Controller; 11 | import org.springframework.web.bind.annotation.RequestMapping; 12 | import org.springframework.web.bind.annotation.RequestMethod; 13 | 14 | import com.alibaba.fastjson.JSON; 15 | import com.happyheng.entity.result.LoginResult; 16 | import com.happyheng.service.LoginService; 17 | import com.happyheng.service.RegisterService; 18 | import com.happyheng.utils.ContextUtils; 19 | 20 | @Controller 21 | public class UserController extends BaseController { 22 | 23 | // public UserController() { 24 | // super(); 25 | // } 26 | 27 | @RequestMapping(value="/Register",method=RequestMethod.POST) 28 | public void register(HttpServletRequest req, HttpServletResponse resp) throws Exception { 29 | String userName = (String) req.getAttribute("uname"); 30 | String passWord = (String) req.getAttribute("upwd"); 31 | String nickName = (String) req.getAttribute("nkname"); 32 | 33 | System.out.println("请求的userName为" + userName + "\n请求的passWord为" + passWord + "\nnickName为" + nickName); 34 | 35 | RegisterService service = (RegisterService) ContextUtils.getContext().getBean("registerService"); 36 | int code = service.register(userName, passWord, nickName); 37 | System.out.println("插入的code为" + code); 38 | 39 | // 将结果序列化成json 40 | Map map = new HashMap<>(); 41 | map.put(RESULT_KEY, code); 42 | String result = JSON.toJSONString(map); 43 | 44 | PrintWriter printWriter = resp.getWriter(); 45 | printWriter.write(result); 46 | printWriter.close(); 47 | } 48 | 49 | @RequestMapping(value = "/Login",method=RequestMethod.POST) 50 | public void login(HttpServletRequest req, HttpServletResponse resp) throws Exception { 51 | 52 | String userName = (String) req.getAttribute("uname"); 53 | String passWord = (String) req.getAttribute("upwd"); 54 | 55 | System.out.println("请求的userName为" + userName + "\n请求的passWord为" + passWord); 56 | 57 | LoginService service = (LoginService) ContextUtils.getContext().getBean("loginService"); 58 | LoginResult loginResult = service.login(userName, passWord); 59 | 60 | Map map = new HashMap<>(); 61 | map.put(RESULT_KEY, loginResult.getCode()); 62 | // 如果成功,还需要加上token 63 | if (loginResult.getCode() == 0) { 64 | Map dataMap = new HashMap<>(); 65 | dataMap.put("token", loginResult.getToken()); 66 | 67 | map.put("data", dataMap); 68 | } 69 | 70 | String result = JSON.toJSONString(map); 71 | System.out.println("结果为" + result); 72 | 73 | PrintWriter printWriter = resp.getWriter(); 74 | printWriter.write(result); 75 | printWriter.close(); 76 | 77 | } 78 | } 79 | -------------------------------------------------------------------------------- /src/com/happyheng/dao/NewsCountDao.java: -------------------------------------------------------------------------------- 1 | package com.happyheng.dao; 2 | 3 | import java.sql.SQLException; 4 | 5 | import org.apache.ibatis.annotations.Select; 6 | 7 | public interface NewsCountDao { 8 | 9 | /** 10 | * 获取指定的readCount 11 | * @param connection 12 | * @param id 13 | * @return 14 | * @throws SQLException 15 | */ 16 | @Select("select readcount from tal_news where id = #{id}") 17 | public Integer getNewsReadCount(int id) throws SQLException; 18 | } 19 | -------------------------------------------------------------------------------- /src/com/happyheng/dao/NewsDao.java: -------------------------------------------------------------------------------- 1 | package com.happyheng.dao; 2 | 3 | import java.sql.SQLException; 4 | import java.util.List; 5 | 6 | import org.apache.ibatis.annotations.Select; 7 | 8 | import com.happyheng.entity.News; 9 | 10 | public interface NewsDao { 11 | 12 | /** 13 | * 通过开始位置和count来得到News实体 14 | * @param begin 15 | * @param count 16 | * @return 17 | */ 18 | @Select("select * from tal_news order by id limit #{1} offset #{0}") 19 | List getNewsByIndex(int begin, int count)throws SQLException; 20 | 21 | /** 22 | * 通过id和count来得到News实体 23 | * @param newsId 24 | * @param count 25 | * @return 26 | */ 27 | @Select("select * from tal_news where id > #{0} limit #{1}") 28 | List getNewsById(int newsId, int count) throws SQLException; 29 | 30 | 31 | } 32 | -------------------------------------------------------------------------------- /src/com/happyheng/dao/SportRecordDao.java: -------------------------------------------------------------------------------- 1 | package com.happyheng.dao; 2 | 3 | import java.sql.SQLException; 4 | import java.util.List; 5 | 6 | import org.apache.ibatis.annotations.Insert; 7 | import org.apache.ibatis.annotations.Select; 8 | import org.apache.ibatis.annotations.SelectKey; 9 | import com.happyheng.entity.Location; 10 | import com.happyheng.entity.Sport; 11 | import com.happyheng.entity.SportRecord; 12 | 13 | public interface SportRecordDao { 14 | 15 | /** 16 | * 将userId插入到用户运动表中,获取自增的运动id 17 | * @param connection 18 | * @param userId 如果获取失败,返回0 19 | * @return 20 | * @throws SQLException 21 | */ 22 | @Insert("insert into tal_sport (userid) values (#{userid})") 23 | @SelectKey(statement = "select last_insert_id() as id", keyProperty = "id", before = false, resultType = long.class) 24 | public Integer insertSport(Sport sport) throws SQLException; 25 | 26 | 27 | /** 28 | * 将SportRecord保存至数据库中 29 | * 30 | * @param connection 31 | * @param record 32 | * @throws SQLException 33 | */ 34 | @Insert("insert into tal_sport_message (sportId,posx,posy,location) values(#{sportId},#{posX},#{posY},#{location})") 35 | public void insert(SportRecord record) throws SQLException; 36 | 37 | 38 | /** 39 | * 根据用户的id获取对已经的运动的所有信息(关联表查询) 40 | * @param connection 41 | * @param id 42 | * @return 43 | * @throws SQLException 44 | */ 45 | @Select("select * from tal_sport_message where ") 46 | public List queryRecordList(int id) throws SQLException; 47 | 48 | /** 49 | * 根据用户的userKey查出用户的sportId 50 | * @param userKey 51 | * @return 52 | * @throws SQLException 53 | */ 54 | @Select("select id from tal_sport where userid in (select id from tal_user where token = #{userKey})") 55 | public List getSportList(String userKey) throws SQLException; 56 | 57 | /** 58 | * 根据运动id查出所有的运动信息 59 | * @param sportId 60 | * @return 61 | * @throws SQLException 62 | */ 63 | @Select("select posx,posy from tal_sport_message where sportId = #{sportId}") 64 | public List getSportMessage(String sportId) throws SQLException; 65 | 66 | } 67 | -------------------------------------------------------------------------------- /src/com/happyheng/dao/UserDao.java: -------------------------------------------------------------------------------- 1 | package com.happyheng.dao; 2 | 3 | 4 | import org.apache.ibatis.annotations.Insert; 5 | import org.apache.ibatis.annotations.Select; 6 | import org.apache.ibatis.annotations.Update; 7 | 8 | import com.happyheng.entity.User; 9 | 10 | public interface UserDao { 11 | 12 | /** 13 | * 将User保存至数据库中 14 | * @param user 15 | */ 16 | @Insert("insert into tal_user (name,password,nickname) values (#{name},#{password},#{nickname})") 17 | public void save(User user); 18 | 19 | /** 20 | * 查询数据库中是否有对应的UserName,如果有,返回对应id,没有,返回0 21 | * @param userName 22 | * @return 23 | */ 24 | @Select("select id from tal_user where name = #{userName}") 25 | public Integer queryUserName(String userName); 26 | 27 | /** 28 | * 根据User查询数据库中相应的id的password是否正确。如果正确,返回对应的id,否则返回0 29 | * @param id 30 | * @param password 31 | * @return 32 | */ 33 | @Select("select id from tal_user where id=#{0} and password=#{1}") 34 | public Integer queryPassWord(int id, String password); 35 | 36 | 37 | /** 38 | * 向指定Id的User中更新token 39 | * @param userId 40 | * @param token 41 | */ 42 | @Update("update tal_user set token=#{1} where id = #{0}") 43 | public int updateToken(int userId, String token); 44 | 45 | /** 46 | * 根据token获取到用户的id,如果没有,返回0 47 | * @param token 48 | * @return 49 | */ 50 | @Select("select id from tal_user where token = #{token}") 51 | public Integer getUserId(String token); 52 | } 53 | -------------------------------------------------------------------------------- /src/com/happyheng/dao/impl/BaseDaoImplement.java: -------------------------------------------------------------------------------- 1 | package com.happyheng.dao.impl; 2 | 3 | public class BaseDaoImplement { 4 | 5 | 6 | /** 7 | * 下为返回的码 8 | */ 9 | 10 | 11 | 12 | 13 | 14 | /** 15 | * 下为SQL的错误码 16 | */ 17 | //MySql重复插入的错误码 18 | public static final int SQL_ERROR_CODE_DUPLICATE = 1062; 19 | } 20 | -------------------------------------------------------------------------------- /src/com/happyheng/dao/impl/NewsCountDaoImpl.java: -------------------------------------------------------------------------------- 1 | package com.happyheng.dao.impl; 2 | 3 | import java.sql.SQLException; 4 | 5 | import org.mybatis.spring.support.SqlSessionDaoSupport; 6 | 7 | import com.happyheng.dao.NewsCountDao; 8 | 9 | public class NewsCountDaoImpl extends SqlSessionDaoSupport implements NewsCountDao { 10 | 11 | private static final String BASE_STATEMENT = "com.happyheng.mapper.userMapper."; 12 | 13 | @Override 14 | public Integer getNewsReadCount(int id) throws SQLException { 15 | return getSqlSession().selectOne(BASE_STATEMENT+"selectNewsCount",id); 16 | } 17 | 18 | } 19 | -------------------------------------------------------------------------------- /src/com/happyheng/dao/impl/NewsDaoImpl.java: -------------------------------------------------------------------------------- 1 | package com.happyheng.dao.impl; 2 | 3 | import java.sql.SQLException; 4 | import java.util.HashMap; 5 | import java.util.List; 6 | import java.util.Map; 7 | 8 | import org.mybatis.spring.support.SqlSessionDaoSupport; 9 | 10 | import com.happyheng.dao.NewsDao; 11 | import com.happyheng.entity.News; 12 | 13 | public class NewsDaoImpl extends SqlSessionDaoSupport implements NewsDao { 14 | 15 | private static final String BASE_STATEMENT = "com.happyheng.mapper.NewsMapper."; 16 | 17 | @Override 18 | public List getNewsByIndex(int begin, int count) throws SQLException { 19 | 20 | Map map = new HashMap<>(); 21 | map.put("begin", begin); 22 | map.put("count", count); 23 | 24 | return getSqlSession().selectList(BASE_STATEMENT + "selectNewsByIndex", map); 25 | } 26 | 27 | @Override 28 | public List getNewsById(int newsId, int count) throws SQLException { 29 | Map map = new HashMap<>(); 30 | map.put("id", newsId); 31 | map.put("count", count); 32 | return getSqlSession().selectList(BASE_STATEMENT + "selectNewsById", map); 33 | } 34 | 35 | } 36 | -------------------------------------------------------------------------------- /src/com/happyheng/dao/impl/NewsDaoRedisImplement.java: -------------------------------------------------------------------------------- 1 | package com.happyheng.dao.impl; 2 | 3 | import java.sql.SQLException; 4 | import java.util.ArrayList; 5 | import java.util.List; 6 | import java.util.Set; 7 | 8 | import com.alibaba.fastjson.JSON; 9 | import com.happyheng.dao.NewsDao; 10 | import com.happyheng.entity.News; 11 | import com.happyheng.utils.Redis; 12 | 13 | import redis.clients.jedis.Jedis; 14 | 15 | public class NewsDaoRedisImplement implements NewsDao { 16 | 17 | // 默认缓存的条数 18 | public static final int CACHE_NUM = 100; 19 | // 缓存的有序列表Key 20 | public static final String KEY_LIST = "article:list"; 21 | // 缓存的Key的时间 22 | public static final int CACHE_TIME = 30; 23 | 24 | private Jedis mJedis; 25 | private NewsDao newsDao; 26 | 27 | public NewsDao getNewsDao() { 28 | return newsDao; 29 | } 30 | 31 | public void setNewsDao(NewsDao newsDao) { 32 | this.newsDao = newsDao; 33 | } 34 | 35 | public NewsDaoRedisImplement() { 36 | mJedis = Redis.getConnection(); 37 | } 38 | 39 | @Override 40 | public List getNewsByIndex(int begin, int count) throws SQLException { 41 | 42 | // 1、判断数据是否存在,不存在即填充 43 | initData(); 44 | 45 | // 2、然后取出 46 | List result = getNewsByIndexFromRedis(begin, count); 47 | return result; 48 | } 49 | 50 | @Override 51 | public List getNewsById(int newsId, int count) throws SQLException { 52 | // 1、判断数据是否存在,不存在即填充 53 | initData(); 54 | 55 | // 2、然后取出 56 | List result = getNewsByIdFromRedis(newsId, count); 57 | return result; 58 | } 59 | 60 | private void initData() throws SQLException { 61 | // 1、首先判断缓存中是否有,如果有,就直接返回 62 | boolean isExist = mJedis.exists(KEY_LIST); 63 | 64 | if (!isExist) { 65 | System.out.println("Redis中没有数据,将数据库数据读入缓存中"); 66 | // 2、如果没有,从数据库中取出,并缓存至Redis中,并设置其缓存时间 67 | getDataFromDao(); 68 | mJedis.expire(KEY_LIST, CACHE_TIME); 69 | } else { 70 | System.out.println("Redis中存在数据"); 71 | } 72 | } 73 | 74 | private void getDataFromDao() throws SQLException { 75 | List list = newsDao.getNewsByIndex(0, CACHE_NUM); 76 | for (News news : list) { 77 | mJedis.zadd(KEY_LIST, news.getId(), JSON.toJSONString(news)); 78 | } 79 | } 80 | 81 | private List getNewsByIndexFromRedis(int begin, int count) { 82 | List newList = new ArrayList<>(); 83 | Set result = mJedis.zrange(KEY_LIST, begin, begin + count - 1); 84 | 85 | for (String newsJson : result) { 86 | News news = JSON.parseObject(newsJson, News.class); 87 | newList.add(news); 88 | } 89 | return newList; 90 | } 91 | 92 | private List getNewsByIdFromRedis(int newsId, int count) { 93 | List newList = new ArrayList<>(); 94 | Set result = mJedis.zrangeByScore(KEY_LIST, String.valueOf(newsId), "+inf", 0, count); 95 | 96 | for (String newsJson : result) { 97 | News news = JSON.parseObject(newsJson, News.class); 98 | newList.add(news); 99 | } 100 | return newList; 101 | } 102 | 103 | } 104 | -------------------------------------------------------------------------------- /src/com/happyheng/dao/impl/SportRecordDaoImpl.java: -------------------------------------------------------------------------------- 1 | package com.happyheng.dao.impl; 2 | 3 | import java.sql.SQLException; 4 | import java.util.List; 5 | 6 | import org.mybatis.spring.support.SqlSessionDaoSupport; 7 | 8 | import com.happyheng.dao.SportRecordDao; 9 | import com.happyheng.entity.Location; 10 | import com.happyheng.entity.Sport; 11 | import com.happyheng.entity.SportRecord; 12 | 13 | public class SportRecordDaoImpl extends SqlSessionDaoSupport implements SportRecordDao { 14 | 15 | private static final String BASE_STATEMENT = "com.happyheng.mapper.sportRecordMapper."; 16 | 17 | @Override 18 | public Integer insertSport(Sport sport) throws SQLException { 19 | return getSqlSession().insert(BASE_STATEMENT + "insertSport" ,sport); 20 | } 21 | 22 | @Override 23 | public void insert(SportRecord record) throws SQLException { 24 | getSqlSession().insert(BASE_STATEMENT + "addSportMessage" ,record); 25 | } 26 | 27 | @Override 28 | public List queryRecordList(int id) throws SQLException { 29 | 30 | return null; 31 | } 32 | 33 | @Override 34 | public List getSportList(String userKey) throws SQLException { 35 | return getSqlSession().selectList(BASE_STATEMENT + "getSportList" ,userKey); 36 | } 37 | 38 | @Override 39 | public List getSportMessage(String sportId) throws SQLException { 40 | 41 | return getSqlSession().selectList(BASE_STATEMENT + "getSportMessage",sportId); 42 | } 43 | 44 | } 45 | -------------------------------------------------------------------------------- /src/com/happyheng/dao/impl/UserDaoImpl.java: -------------------------------------------------------------------------------- 1 | package com.happyheng.dao.impl; 2 | 3 | import java.util.HashMap; 4 | import java.util.Map; 5 | 6 | import org.mybatis.spring.support.SqlSessionDaoSupport; 7 | 8 | import com.happyheng.dao.UserDao; 9 | import com.happyheng.entity.User; 10 | 11 | public class UserDaoImpl extends SqlSessionDaoSupport implements UserDao{ 12 | 13 | private static final String BASE_STATEMENT = "com.happyheng.mapper.userMapper."; 14 | 15 | @Override 16 | public void save(User user) { 17 | getSqlSession().update(BASE_STATEMENT+"saveUser",user); 18 | } 19 | 20 | @Override 21 | public Integer queryUserName(String userName) { 22 | return getSqlSession().selectOne(BASE_STATEMENT+"selectUserByName",userName); 23 | } 24 | 25 | @Override 26 | public Integer queryPassWord(int id, String password) { 27 | 28 | Map map = new HashMap<>(); 29 | map.put("id", String.valueOf(id)); 30 | map.put("password", password); 31 | return getSqlSession().selectOne(BASE_STATEMENT+"selectUserByPassword", map); 32 | } 33 | 34 | @Override 35 | public int updateToken(int userId, String token) { 36 | Map map = new HashMap<>(); 37 | map.put("id", String.valueOf(userId)); 38 | map.put("token", token); 39 | return getSqlSession().update(BASE_STATEMENT+"updateUserToken",map); 40 | } 41 | 42 | @Override 43 | public Integer getUserId(String token) { 44 | System.out.println("token为"+token); 45 | return getSqlSession().selectOne(BASE_STATEMENT+"selectUserByToken",token); 46 | } 47 | 48 | } 49 | -------------------------------------------------------------------------------- /src/com/happyheng/encrypt/AESUtils.java: -------------------------------------------------------------------------------- 1 | package com.happyheng.encrypt; 2 | 3 | import java.security.Key; 4 | 5 | import javax.crypto.Cipher; 6 | import javax.crypto.spec.SecretKeySpec; 7 | 8 | import org.apache.commons.codec.binary.Hex; 9 | 10 | import com.happyheng.utils.HexUtils; 11 | 12 | /** 13 | * AES进行加解密的工具类 14 | * @author liuheng 15 | * 16 | */ 17 | public class AESUtils { 18 | 19 | /** 20 | * AES加密的方法 21 | * @param byteskey AES的key 22 | * @param text 待加密的字符串 23 | * @return 返回转换成16进制的加密字符串,如果加密失败,返回null 24 | */ 25 | public static String encrypt(byte[] byteskey,String text) { 26 | try { 27 | //转换key 28 | Key convertSecretKey = new SecretKeySpec(byteskey, "AES"); 29 | //加密 30 | Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding"); 31 | 32 | cipher.init(Cipher.ENCRYPT_MODE, convertSecretKey); 33 | byte[] encryptResult = cipher.doFinal(text.getBytes()); 34 | String encryptString = HexUtils.bytesToHexString(encryptResult); 35 | 36 | System.out.println("jdk des encrypt:" + encryptString); 37 | return encryptString; 38 | 39 | } catch (Exception e) { 40 | e.printStackTrace(); 41 | return null; 42 | } 43 | } 44 | 45 | /** 46 | * AES解密的方法 47 | * @param byteskey AES的key 48 | * @param encryptString 加密并转换成16进制的字符串 49 | * @return 返回解密的字符串,如果失败,返回null 50 | */ 51 | public static String decrypt(byte[] byteskey,String encryptString) { 52 | try { 53 | //转换key 54 | Key convertSecretKey = new SecretKeySpec(byteskey, "AES"); 55 | // 解密 56 | Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding"); 57 | cipher.init(Cipher.DECRYPT_MODE, convertSecretKey); 58 | byte[] decryptResult = cipher.doFinal(HexUtils.hexStringToBytes(encryptString)); 59 | String decryptString = new String(decryptResult); 60 | System.out.println("jdk des decrypt:" + decryptString); 61 | return decryptString; 62 | 63 | } catch (Exception e) { 64 | e.printStackTrace(); 65 | return null; 66 | } 67 | } 68 | 69 | } 70 | -------------------------------------------------------------------------------- /src/com/happyheng/entity/IdEntity.java: -------------------------------------------------------------------------------- 1 | package com.happyheng.entity; 2 | 3 | public abstract class IdEntity { 4 | protected Long id; 5 | 6 | public Long getId() { 7 | return id; 8 | } 9 | 10 | public void setId(Long id) { 11 | this.id = id; 12 | } 13 | 14 | } -------------------------------------------------------------------------------- /src/com/happyheng/entity/Location.java: -------------------------------------------------------------------------------- 1 | package com.happyheng.entity; 2 | 3 | public class Location { 4 | 5 | private double posx; 6 | private double posy; 7 | 8 | public double getPosx() { 9 | return posx; 10 | } 11 | 12 | public void setPosx(double posx) { 13 | this.posx = posx; 14 | } 15 | 16 | public double getPosy() { 17 | return posy; 18 | } 19 | 20 | public void setPosy(double posy) { 21 | this.posy = posy; 22 | } 23 | 24 | } 25 | -------------------------------------------------------------------------------- /src/com/happyheng/entity/News.java: -------------------------------------------------------------------------------- 1 | package com.happyheng.entity; 2 | 3 | public class News extends IdEntity { 4 | private String name; 5 | private String simplecontent; 6 | private String thumbnail; 7 | private String url; 8 | public String getName() { 9 | return name; 10 | } 11 | public void setName(String name) { 12 | this.name = name; 13 | } 14 | public String getSimplecontent() { 15 | return simplecontent; 16 | } 17 | public void setSimplecontent(String simplecontent) { 18 | this.simplecontent = simplecontent; 19 | } 20 | public String getThumbnail() { 21 | return thumbnail; 22 | } 23 | public void setThumbnail(String thumbnail) { 24 | this.thumbnail = thumbnail; 25 | } 26 | public String getUrl() { 27 | return url; 28 | } 29 | public void setUrl(String url) { 30 | this.url = url; 31 | } 32 | @Override 33 | public String toString() { 34 | return "News [name=" + name + ", simplecontent=" + simplecontent + ", thumbnail=" + thumbnail + ", url=" + url 35 | + "]"; 36 | } 37 | 38 | 39 | } 40 | -------------------------------------------------------------------------------- /src/com/happyheng/entity/Sport.java: -------------------------------------------------------------------------------- 1 | package com.happyheng.entity; 2 | 3 | public class Sport extends IdEntity { 4 | private int userid; 5 | 6 | public int getUserid() { 7 | return userid; 8 | } 9 | 10 | public void setUserid(int userid) { 11 | this.userid = userid; 12 | } 13 | 14 | } 15 | -------------------------------------------------------------------------------- /src/com/happyheng/entity/SportRecord.java: -------------------------------------------------------------------------------- 1 | package com.happyheng.entity; 2 | 3 | public class SportRecord extends IdEntity { 4 | private int sportId; 5 | private double posX; 6 | private double posY; 7 | private long time; 8 | private String location; 9 | 10 | public int getSportId() { 11 | return sportId; 12 | } 13 | public void setSportId(int sportId) { 14 | this.sportId = sportId; 15 | } 16 | 17 | public double getPosX() { 18 | return posX; 19 | } 20 | public void setPosX(double posX) { 21 | this.posX = posX; 22 | } 23 | public double getPosY() { 24 | return posY; 25 | } 26 | public void setPosY(double posY) { 27 | this.posY = posY; 28 | } 29 | 30 | public long getTime() { 31 | return time; 32 | } 33 | public void setTime(long time) { 34 | this.time = time; 35 | } 36 | public String getLocation() { 37 | return location; 38 | } 39 | public void setLocation(String location) { 40 | this.location = location; 41 | } 42 | 43 | } 44 | -------------------------------------------------------------------------------- /src/com/happyheng/entity/User.java: -------------------------------------------------------------------------------- 1 | package com.happyheng.entity; 2 | 3 | public class User extends IdEntity { 4 | private String name; 5 | private String password; 6 | private String nickname; 7 | private String token; 8 | 9 | public String getName() { 10 | return name; 11 | } 12 | public void setName(String name) { 13 | this.name = name; 14 | } 15 | public String getPassword() { 16 | return password; 17 | } 18 | public void setPassword(String password) { 19 | this.password = password; 20 | } 21 | public String getNickname() { 22 | return nickname; 23 | } 24 | public void setNickname(String nickname) { 25 | this.nickname = nickname; 26 | } 27 | public String getToken() { 28 | return token; 29 | } 30 | public void setToken(String token) { 31 | this.token = token; 32 | } 33 | 34 | 35 | 36 | } 37 | -------------------------------------------------------------------------------- /src/com/happyheng/entity/result/LoginResult.java: -------------------------------------------------------------------------------- 1 | package com.happyheng.entity.result; 2 | 3 | public class LoginResult { 4 | 5 | private int code; 6 | private String token; 7 | 8 | public int getCode() { 9 | return code; 10 | } 11 | public void setCode(int code) { 12 | this.code = code; 13 | } 14 | public String getToken() { 15 | return token; 16 | } 17 | public void setToken(String token) { 18 | this.token = token; 19 | } 20 | 21 | 22 | } 23 | -------------------------------------------------------------------------------- /src/com/happyheng/entity/result/NewsResult.java: -------------------------------------------------------------------------------- 1 | package com.happyheng.entity.result; 2 | 3 | import java.util.List; 4 | 5 | import com.happyheng.entity.News; 6 | 7 | public class NewsResult { 8 | private int code; 9 | private List data; 10 | 11 | 12 | public int getCode() { 13 | return code; 14 | } 15 | public void setCode(int code) { 16 | this.code = code; 17 | } 18 | public List getData() { 19 | return data; 20 | } 21 | 22 | public void setData(List data) { 23 | this.data = data; 24 | } 25 | 26 | @Override 27 | public String toString() { 28 | return "NewsResult [code=" + code + ", data=" + data + "]"; 29 | } 30 | } 31 | -------------------------------------------------------------------------------- /src/com/happyheng/entity/result/SportListResult.java: -------------------------------------------------------------------------------- 1 | package com.happyheng.entity.result; 2 | 3 | import java.util.List; 4 | 5 | 6 | public class SportListResult { 7 | private int result; 8 | private List data; 9 | 10 | 11 | public int getResult() { 12 | return result; 13 | } 14 | public void setResult(int result) { 15 | this.result = result; 16 | } 17 | public List getData() { 18 | return data; 19 | } 20 | public void setData(List data) { 21 | this.data = data; 22 | } 23 | 24 | } 25 | -------------------------------------------------------------------------------- /src/com/happyheng/entity/result/SportMessageResult.java: -------------------------------------------------------------------------------- 1 | package com.happyheng.entity.result; 2 | 3 | import java.util.List; 4 | 5 | import com.happyheng.entity.Location; 6 | 7 | public class SportMessageResult { 8 | 9 | private int result; 10 | private List data; 11 | 12 | public int getResult() { 13 | return result; 14 | } 15 | public void setResult(int result) { 16 | this.result = result; 17 | } 18 | public List getData() { 19 | return data; 20 | } 21 | public void setData(List data) { 22 | this.data = data; 23 | } 24 | 25 | 26 | } 27 | -------------------------------------------------------------------------------- /src/com/happyheng/entity/result/SportRecordResult.java: -------------------------------------------------------------------------------- 1 | package com.happyheng.entity.result; 2 | 3 | public class SportRecordResult { 4 | 5 | private int code; 6 | private long sportId; 7 | 8 | public int getCode() { 9 | return code; 10 | } 11 | public void setCode(int code) { 12 | this.code = code; 13 | } 14 | public long getSportId() { 15 | return sportId; 16 | } 17 | public void setSportId(long sportId) { 18 | this.sportId = sportId; 19 | } 20 | 21 | } 22 | -------------------------------------------------------------------------------- /src/com/happyheng/factory/DaoFactory.java: -------------------------------------------------------------------------------- 1 | package com.happyheng.factory; 2 | 3 | import com.happyheng.dao.NewsCountDao; 4 | import com.happyheng.dao.NewsDao; 5 | import com.happyheng.dao.SportRecordDao; 6 | import com.happyheng.dao.UserDao; 7 | import com.happyheng.utils.MyBatisUtil; 8 | 9 | public class DaoFactory { 10 | 11 | public UserDao getUserDao() { 12 | return MyBatisUtil.getSqlSession(true).getMapper(UserDao.class); 13 | } 14 | 15 | public SportRecordDao getSportRecordDao() { 16 | return MyBatisUtil.getSqlSession(true).getMapper(SportRecordDao.class); 17 | } 18 | 19 | public NewsDao getNewsDao() { 20 | return MyBatisUtil.getSqlSession(true).getMapper(NewsDao.class); 21 | } 22 | 23 | public NewsCountDao getNewsCountDao() { 24 | return MyBatisUtil.getSqlSession(true).getMapper(NewsCountDao.class); 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /src/com/happyheng/filter/CharacterFilter.java: -------------------------------------------------------------------------------- 1 | package com.happyheng.filter; 2 | 3 | import java.io.IOException; 4 | 5 | import javax.servlet.Filter; 6 | import javax.servlet.FilterChain; 7 | import javax.servlet.FilterConfig; 8 | import javax.servlet.ServletException; 9 | import javax.servlet.ServletRequest; 10 | import javax.servlet.ServletResponse; 11 | 12 | public class CharacterFilter implements Filter { 13 | 14 | //默认的字符集 15 | public static final String DEFAULT_ENCODING = "UTF8"; 16 | 17 | @Override 18 | public void init(FilterConfig paramFilterConfig) throws ServletException { 19 | 20 | } 21 | 22 | @Override 23 | public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) 24 | throws IOException, ServletException { 25 | request.setCharacterEncoding(DEFAULT_ENCODING); 26 | response.setCharacterEncoding(DEFAULT_ENCODING); 27 | 28 | chain.doFilter(request, response); 29 | } 30 | 31 | @Override 32 | public void destroy() { 33 | 34 | } 35 | 36 | } 37 | -------------------------------------------------------------------------------- /src/com/happyheng/filter/VerifyFilter.java: -------------------------------------------------------------------------------- 1 | package com.happyheng.filter; 2 | 3 | import java.io.IOException; 4 | import java.io.InputStream; 5 | import java.net.URLDecoder; 6 | import java.util.Properties; 7 | 8 | import javax.servlet.Filter; 9 | import javax.servlet.FilterChain; 10 | import javax.servlet.FilterConfig; 11 | import javax.servlet.ServletException; 12 | import javax.servlet.ServletRequest; 13 | import javax.servlet.ServletResponse; 14 | 15 | import org.apache.commons.codec.DecoderException; 16 | import org.apache.commons.codec.binary.Hex; 17 | 18 | import com.alibaba.fastjson.JSON; 19 | import com.alibaba.fastjson.JSONObject; 20 | import com.happyheng.secret.VerifySecret; 21 | import com.happyheng.secret.impl.AESSercret; 22 | import com.happyheng.utils.HexUtils; 23 | import com.happyheng.utils.PropertiesUtils; 24 | 25 | public class VerifyFilter implements Filter { 26 | 27 | public static final String KEY_ENCRYPT = "s"; 28 | 29 | private byte[] key; 30 | private VerifySecret secret; 31 | 32 | @Override 33 | public void init(FilterConfig paramFilterConfig) throws ServletException { 34 | 35 | PropertiesUtils propertiesUtils = new PropertiesUtils("key.properties"); 36 | String hexKey = propertiesUtils.getProperties("key"); 37 | key = HexUtils.hexStringToBytes(hexKey); 38 | 39 | secret = new AESSercret(); 40 | } 41 | 42 | @Override 43 | public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) 44 | throws IOException, ServletException { 45 | 46 | String securityString = request.getParameter(KEY_ENCRYPT); 47 | if (securityString == null) { 48 | chain.doFilter(request, response); 49 | } else { 50 | String decryptString = secret.decryption(key, securityString); 51 | 52 | if (decryptString != null) { 53 | System.out.println("获取的加密String为" + decryptString); 54 | 55 | JSONObject requestJson = JSON.parseObject(decryptString); 56 | for (String key : requestJson.keySet()) { 57 | request.setAttribute(key, requestJson.get(key)); 58 | System.out.println("取得的数据key----"+key+"----value为"+ requestJson.get(key)); 59 | } 60 | 61 | // 验证通过 62 | chain.doFilter(request, response); 63 | } else { 64 | response.getWriter().println("验证失败"); 65 | } 66 | 67 | } 68 | 69 | } 70 | 71 | @Override 72 | public void destroy() { 73 | 74 | } 75 | 76 | } 77 | -------------------------------------------------------------------------------- /src/com/happyheng/mapper/NewsMapper.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 9 | 10 | 13 | 14 | 17 | 18 | -------------------------------------------------------------------------------- /src/com/happyheng/mapper/sportRecordMapper.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | INSERT INTO tal_sport (userid) values (#{userid}) 8 | 9 | 10 | 11 | 12 | INSERT INTO tal_sport_message (sportId,posx,posy) VALUES (#{sportId},#{posX},#{posY}) 13 | 14 | 15 | 16 | 19 | 20 | 21 | 22 | 25 | 26 | 27 | 30 | 31 | -------------------------------------------------------------------------------- /src/com/happyheng/mapper/userMapper.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | insert into tal_user (name,password,nickname) values (#{name},#{password},#{nickname}) 8 | 9 | 10 | 11 | 14 | 15 | 16 | 19 | 20 | 21 | 22 | update tal_user set token = #{token} where id=#{id} 23 | 24 | 25 | 26 | 29 | 30 | -------------------------------------------------------------------------------- /src/com/happyheng/secret/VerifySecret.java: -------------------------------------------------------------------------------- 1 | package com.happyheng.secret; 2 | 3 | public interface VerifySecret { 4 | 5 | /** 6 | * 将传入的String解密的方法 7 | * @param encrypteString 8 | * @return 9 | */ 10 | public String decryption(byte[] key,String encrypteString); 11 | 12 | } 13 | -------------------------------------------------------------------------------- /src/com/happyheng/secret/impl/AESSercret.java: -------------------------------------------------------------------------------- 1 | package com.happyheng.secret.impl; 2 | 3 | import com.happyheng.encrypt.AESUtils; 4 | import com.happyheng.secret.VerifySecret; 5 | 6 | public class AESSercret implements VerifySecret { 7 | 8 | @Override 9 | public String decryption(byte[] key, String encrypteString) { 10 | return AESUtils.decrypt(key, encrypteString); 11 | } 12 | 13 | } 14 | -------------------------------------------------------------------------------- /src/com/happyheng/service/LoginService.java: -------------------------------------------------------------------------------- 1 | package com.happyheng.service; 2 | 3 | import com.happyheng.entity.result.LoginResult; 4 | 5 | public interface LoginService { 6 | public LoginResult login(String userName, String passWord); 7 | } 8 | -------------------------------------------------------------------------------- /src/com/happyheng/service/NewsService.java: -------------------------------------------------------------------------------- 1 | package com.happyheng.service; 2 | 3 | import com.happyheng.entity.result.NewsResult; 4 | 5 | public interface NewsService { 6 | public NewsResult getNews(int begin, int id, int count); 7 | } 8 | -------------------------------------------------------------------------------- /src/com/happyheng/service/RegisterService.java: -------------------------------------------------------------------------------- 1 | package com.happyheng.service; 2 | 3 | public interface RegisterService { 4 | 5 | public int register(String userName, String passWord, String nickName); 6 | } 7 | -------------------------------------------------------------------------------- /src/com/happyheng/service/SportIdService.java: -------------------------------------------------------------------------------- 1 | package com.happyheng.service; 2 | 3 | import com.happyheng.entity.result.SportRecordResult; 4 | 5 | public interface SportIdService { 6 | public SportRecordResult getSportId(String userKey); 7 | } 8 | -------------------------------------------------------------------------------- /src/com/happyheng/service/SportListService.java: -------------------------------------------------------------------------------- 1 | package com.happyheng.service; 2 | 3 | import com.happyheng.entity.result.SportListResult; 4 | 5 | public interface SportListService { 6 | public SportListResult getSportList(String userKey); 7 | } 8 | -------------------------------------------------------------------------------- /src/com/happyheng/service/SportMessageService.java: -------------------------------------------------------------------------------- 1 | package com.happyheng.service; 2 | 3 | import com.happyheng.entity.result.SportMessageResult; 4 | 5 | public interface SportMessageService { 6 | public SportMessageResult getSportMessage(String userKey,String sportId); 7 | } 8 | -------------------------------------------------------------------------------- /src/com/happyheng/service/SportRecordService.java: -------------------------------------------------------------------------------- 1 | package com.happyheng.service; 2 | 3 | public interface SportRecordService { 4 | public int record(int sportId, double posx, double posy, String location); 5 | } 6 | -------------------------------------------------------------------------------- /src/com/happyheng/service/impl/BaseService.java: -------------------------------------------------------------------------------- 1 | package com.happyheng.service.impl; 2 | 3 | /** 4 | * 逻辑处理类的基类 5 | * 6 | * @author liuheng 7 | * 8 | */ 9 | public class BaseService { 10 | // 请求成功的返回值 11 | public static int RESULT_CODE_SUCCESS = 0; 12 | // 请求失败的返回值 13 | public static int RESULT_CODE_ERROR = 100; 14 | 15 | } 16 | -------------------------------------------------------------------------------- /src/com/happyheng/service/impl/LoginServiceImpl.java: -------------------------------------------------------------------------------- 1 | package com.happyheng.service.impl; 2 | 3 | import java.sql.Connection; 4 | import java.sql.SQLException; 5 | 6 | import com.happyheng.dao.UserDao; 7 | import com.happyheng.entity.result.LoginResult; 8 | import com.happyheng.service.LoginService; 9 | import com.happyheng.utils.ConnectionFactory; 10 | 11 | 12 | public class LoginServiceImpl implements LoginService{ 13 | 14 | private UserDao userDao; 15 | 16 | public UserDao getUserDao() { 17 | return userDao; 18 | } 19 | 20 | public void setUserDao(UserDao userDao) { 21 | this.userDao = userDao; 22 | } 23 | 24 | private static final int RESULT_NULL_USERNAME = 1,RESULT_WRONG_PASSWORD = 2; 25 | 26 | @Override 27 | public LoginResult login(String userName, String passWord) { 28 | 29 | Connection connection = null; 30 | LoginResult result = new LoginResult(); 31 | 32 | //1、先判断是否有相应的用户名 33 | Integer id = userDao.queryUserName(userName); 34 | if (id == null) { 35 | result.setCode(RESULT_NULL_USERNAME); 36 | return result; 37 | } 38 | 39 | //2、在判断密码是否正确 40 | Integer userId = userDao.queryPassWord(id, passWord); 41 | if (userId == null) { 42 | result.setCode(RESULT_WRONG_PASSWORD); 43 | return result; 44 | } 45 | 46 | //3、设置相应的token 47 | long currentTime = System.currentTimeMillis(); 48 | String token = userId+"_"+currentTime; 49 | 50 | int updateResult = userDao.updateToken(userId, token); 51 | if (updateResult == 0) { 52 | result.setCode(100); 53 | 54 | } else { 55 | result.setCode(0); 56 | result.setToken(token); 57 | } 58 | 59 | return result; 60 | } 61 | 62 | } 63 | -------------------------------------------------------------------------------- /src/com/happyheng/service/impl/NewsCountServiceImpl.java: -------------------------------------------------------------------------------- 1 | package com.happyheng.service.impl; 2 | 3 | import com.happyheng.cache.NewsCountCache; 4 | 5 | /** 6 | * 可以为读取文章的个数+1的Service 7 | * @author liuheng 8 | * 9 | */ 10 | public class NewsCountServiceImpl extends BaseService { 11 | 12 | private NewsCountCache cacheDao; 13 | 14 | public NewsCountCache getCacheDao() { 15 | return cacheDao; 16 | } 17 | 18 | public void setCacheDao(NewsCountCache cacheDao) { 19 | this.cacheDao = cacheDao; 20 | } 21 | 22 | /** 23 | * 得到对应文章的阅读数并将Count+1 24 | * @param id 25 | */ 26 | public String addAndGetReadCount(String id) { 27 | //NewsCountCache cache = new NewsCountCache(); 28 | String readCount = "0"; 29 | 30 | try { 31 | readCount = cacheDao.getAndAddCountFromCache(id); 32 | } catch (Exception e) { 33 | e.printStackTrace(); 34 | } 35 | return readCount; 36 | } 37 | } 38 | -------------------------------------------------------------------------------- /src/com/happyheng/service/impl/NewsServiceImpl.java: -------------------------------------------------------------------------------- 1 | package com.happyheng.service.impl; 2 | 3 | import java.sql.Connection; 4 | import java.sql.SQLException; 5 | import java.util.List; 6 | 7 | import com.happyheng.dao.NewsDao; 8 | import com.happyheng.entity.News; 9 | import com.happyheng.entity.result.NewsResult; 10 | import com.happyheng.service.NewsService; 11 | import com.happyheng.utils.ConnectionFactory; 12 | 13 | public class NewsServiceImpl extends BaseService implements NewsService{ 14 | 15 | public static final String BASE_NEWS_URL = "http://www.happyheng.top:8080/Sport/NewsDetail?id="; 16 | 17 | private NewsDao newsDao; 18 | 19 | public NewsDao getNewsDao() { 20 | return newsDao; 21 | } 22 | 23 | public void setNewsDao(NewsDao newsDao) { 24 | this.newsDao = newsDao; 25 | } 26 | 27 | @Override 28 | public NewsResult getNews(int begin, int id, int count) { 29 | NewsResult result = new NewsResult(); 30 | //Connection connection = ConnectionFactory.getInstance().makeConnection(); 31 | 32 | try { 33 | 34 | List newsList; 35 | if (id != 0) { 36 | newsList = newsDao.getNewsById(id, count); 37 | } else { 38 | newsList = newsDao.getNewsByIndex(begin, count); 39 | } 40 | 41 | //为所有的news添加url 42 | for(News news:newsList){ 43 | news.setUrl(BASE_NEWS_URL+news.getId()); 44 | } 45 | 46 | result.setCode(RESULT_CODE_SUCCESS); 47 | result.setData(newsList); 48 | } catch (SQLException e) { 49 | result.setCode(RESULT_CODE_ERROR); 50 | e.printStackTrace(); 51 | } 52 | return result; 53 | } 54 | } 55 | -------------------------------------------------------------------------------- /src/com/happyheng/service/impl/RegisterServiceImpl.java: -------------------------------------------------------------------------------- 1 | package com.happyheng.service.impl; 2 | 3 | import java.sql.Connection; 4 | import java.sql.SQLException; 5 | 6 | import com.happyheng.dao.UserDao; 7 | import com.happyheng.dao.impl.BaseDaoImplement; 8 | import com.happyheng.entity.User; 9 | import com.happyheng.service.RegisterService; 10 | import com.happyheng.utils.ConnectionFactory; 11 | 12 | /** 13 | * 负责注册插入数据库操作的逻辑类,包括错误判断,出错时数据库回滚等操作 14 | * 15 | * @author liuheng 16 | * 17 | */ 18 | public class RegisterServiceImpl implements RegisterService{ 19 | 20 | private UserDao userDao; 21 | 22 | public UserDao getUserDao() { 23 | return userDao; 24 | } 25 | 26 | public void setUserDao(UserDao userDao) { 27 | this.userDao = userDao; 28 | } 29 | 30 | @Override 31 | public int register(String userName, String passWord, String nickName) { 32 | Connection connection = ConnectionFactory.getInstance().makeConnection(); 33 | try { 34 | //使用事务,先禁止其自动提交 35 | connection.setAutoCommit(false); 36 | 37 | User user = new User(); 38 | user.setName(userName); 39 | user.setPassword(passWord); 40 | user.setNickname(nickName); 41 | 42 | userDao.save(user); 43 | 44 | connection.commit(); 45 | return 0; 46 | 47 | } catch (SQLException e) { 48 | System.out.println("插入失败" + "\n错误码为" + e.getErrorCode() + "\n错误信息为" + e.getMessage()); 49 | 50 | //插入失败,需要回滚 51 | try { 52 | connection.rollback(); 53 | } catch (SQLException e1) { 54 | // TODO Auto-generated catch block 55 | e1.printStackTrace(); 56 | } 57 | 58 | // mysql重复插入的错误码为1062,如果是重复插入,Message会为Duplicate entry 'xxx' for key 59 | // 'name' 60 | // 即最后一个为列名,我们可以根据此来得到重复插入的列的名称 61 | if (e.getErrorCode() == BaseDaoImplement.SQL_ERROR_CODE_DUPLICATE) { 62 | String errorMessage = e.getMessage(); 63 | // 说明用户名重复了,根据接口,应该返回1 64 | if (errorMessage.endsWith("'name'")) { 65 | return 1; 66 | } 67 | // 其它应该就是nickname重复了,根据接口,应该返回2 68 | else { 69 | return 2; 70 | } 71 | 72 | } else { 73 | return 100; 74 | } 75 | } 76 | } 77 | 78 | } 79 | -------------------------------------------------------------------------------- /src/com/happyheng/service/impl/SportIdServiceImpl.java: -------------------------------------------------------------------------------- 1 | package com.happyheng.service.impl; 2 | 3 | import com.happyheng.dao.SportRecordDao; 4 | import com.happyheng.dao.UserDao; 5 | import com.happyheng.entity.Sport; 6 | import com.happyheng.entity.result.SportRecordResult; 7 | import com.happyheng.service.SportIdService; 8 | 9 | public class SportIdServiceImpl extends BaseService implements SportIdService { 10 | 11 | private UserDao userDao; 12 | private SportRecordDao recordDao; 13 | 14 | public UserDao getUserDao() { 15 | return userDao; 16 | } 17 | 18 | public void setUserDao(UserDao userDao) { 19 | this.userDao = userDao; 20 | } 21 | 22 | public SportRecordDao getRecordDao() { 23 | return recordDao; 24 | } 25 | 26 | public void setRecordDao(SportRecordDao recordDao) { 27 | this.recordDao = recordDao; 28 | } 29 | 30 | @Override 31 | public SportRecordResult getSportId(String userKey) { 32 | // Connection connection = null; 33 | // connection = ConnectionFactory.getInstance().makeConnection(); 34 | 35 | SportRecordResult recordResult = new SportRecordResult(); 36 | 37 | try { 38 | 39 | // 1、先使用userKey得到对应的userId 40 | Integer userId = userDao.getUserId(userKey); 41 | 42 | if (userId != null) { 43 | // 2、给Sport表中插入userId,获取sportId 44 | Sport sport = new Sport(); 45 | sport.setUserid(userId); 46 | Integer insertSportResult = recordDao.insertSport(sport); 47 | 48 | if (insertSportResult != null) { 49 | recordResult.setCode(RESULT_CODE_SUCCESS); 50 | recordResult.setSportId(sport.getId()); 51 | return recordResult; 52 | } 53 | 54 | } 55 | 56 | } catch (Exception exception) { 57 | exception.printStackTrace(); 58 | } 59 | recordResult.setCode(RESULT_CODE_ERROR); 60 | return recordResult; 61 | } 62 | } 63 | -------------------------------------------------------------------------------- /src/com/happyheng/service/impl/SportListServiceImpl.java: -------------------------------------------------------------------------------- 1 | package com.happyheng.service.impl; 2 | 3 | import java.sql.SQLException; 4 | import java.util.List; 5 | 6 | import com.happyheng.dao.SportRecordDao; 7 | import com.happyheng.entity.result.SportListResult; 8 | import com.happyheng.service.SportListService; 9 | 10 | public class SportListServiceImpl extends BaseService implements SportListService { 11 | 12 | private SportRecordDao recordDao; 13 | 14 | public SportRecordDao getRecordDao() { 15 | return recordDao; 16 | } 17 | 18 | public void setRecordDao(SportRecordDao recordDao) { 19 | this.recordDao = recordDao; 20 | } 21 | 22 | @Override 23 | public SportListResult getSportList(String userKey) { 24 | 25 | SportListResult result = new SportListResult(); 26 | 27 | try { 28 | List list = recordDao.getSportList(userKey); 29 | if (list == null || list.isEmpty()) { 30 | result.setResult(RESULT_CODE_ERROR); 31 | } else { 32 | result.setResult(RESULT_CODE_SUCCESS); 33 | result.setData(list); 34 | } 35 | 36 | } catch (SQLException e) { 37 | e.printStackTrace(); 38 | result.setResult(RESULT_CODE_ERROR); 39 | } 40 | 41 | return result; 42 | } 43 | 44 | } 45 | -------------------------------------------------------------------------------- /src/com/happyheng/service/impl/SportMessageImpl.java: -------------------------------------------------------------------------------- 1 | package com.happyheng.service.impl; 2 | 3 | import java.sql.SQLException; 4 | import java.util.List; 5 | 6 | import com.happyheng.dao.SportRecordDao; 7 | import com.happyheng.entity.Location; 8 | import com.happyheng.entity.result.SportMessageResult; 9 | import com.happyheng.service.SportMessageService; 10 | 11 | public class SportMessageImpl extends BaseService implements SportMessageService { 12 | 13 | private SportRecordDao recordDao; 14 | 15 | public SportRecordDao getRecordDao() { 16 | return recordDao; 17 | } 18 | 19 | public void setRecordDao(SportRecordDao recordDao) { 20 | this.recordDao = recordDao; 21 | } 22 | 23 | 24 | @Override 25 | public SportMessageResult getSportMessage(String userKey, String sportId) { 26 | 27 | SportMessageResult result = new SportMessageResult(); 28 | 29 | try { 30 | List listData = recordDao.getSportMessage(sportId); 31 | 32 | if (listData != null && !listData.isEmpty()) { 33 | result.setResult(RESULT_CODE_SUCCESS); 34 | result.setData(listData); 35 | return result; 36 | } 37 | 38 | } catch (SQLException e) { 39 | e.printStackTrace(); 40 | } 41 | result.setResult(RESULT_CODE_ERROR); 42 | return result; 43 | } 44 | 45 | } 46 | -------------------------------------------------------------------------------- /src/com/happyheng/service/impl/SportRecordServiceImpl.java: -------------------------------------------------------------------------------- 1 | package com.happyheng.service.impl; 2 | 3 | import java.sql.Connection; 4 | import com.happyheng.dao.SportRecordDao; 5 | import com.happyheng.entity.SportRecord; 6 | import com.happyheng.service.SportRecordService; 7 | import com.happyheng.utils.ConnectionFactory; 8 | 9 | /** 10 | * 运动信息记录的逻辑处理类,负责将请求写入到数据库,并根据情况决定返回值 11 | * 12 | * @author liuheng 13 | * 14 | */ 15 | public class SportRecordServiceImpl extends BaseService implements SportRecordService{ 16 | 17 | private SportRecordDao recordDao; 18 | 19 | public SportRecordDao getRecordDao() { 20 | return recordDao; 21 | } 22 | 23 | public void setRecordDao(SportRecordDao recordDao) { 24 | this.recordDao = recordDao; 25 | } 26 | 27 | /** 28 | * 根据运动的信息及其id,插入到运动信息表中。 29 | * 30 | * @param sportId 31 | * @param posx 32 | * @param posy 33 | * @param time 34 | * @param location 35 | */ 36 | @Override 37 | public int record(int sportId, double posx, double posy, String location) { 38 | 39 | try { 40 | SportRecord record = new SportRecord(); 41 | record.setSportId(sportId); 42 | record.setPosX(posx); 43 | record.setPosY(posy); 44 | record.setLocation(location); 45 | 46 | // 将运动信息写入 47 | recordDao.insert(record); 48 | return 0; 49 | 50 | } catch (Exception e) { 51 | e.printStackTrace(); 52 | 53 | } 54 | return 100; 55 | } 56 | 57 | } 58 | -------------------------------------------------------------------------------- /src/com/happyheng/test/ConnectionTest.java: -------------------------------------------------------------------------------- 1 | package com.happyheng.test; 2 | 3 | import java.sql.Connection; 4 | 5 | import com.happyheng.dao.UserDao; 6 | import com.happyheng.utils.ConnectionFactory; 7 | 8 | public class ConnectionTest { 9 | 10 | public static void main(String[] args) throws Exception{ 11 | // // TODO Auto-generated method stub 12 | // ConnectionFactory factory = ConnectionFactory.getInstance(); 13 | // Connection connection = factory.makeConnection(); 14 | // 15 | // User user = new User(); 16 | // user.setName("happyheng1"); 17 | // user.setPassword("123456"); 18 | // user.setNickname("happyhappy1"); 19 | // 20 | // UserDao userDao = new UserDaoImplement(); 21 | //int code = userDao.save(connection, user); 22 | 23 | //System.out.println("返回的code为"+code); 24 | } 25 | 26 | } 27 | -------------------------------------------------------------------------------- /src/com/happyheng/test/RedisTest.java: -------------------------------------------------------------------------------- 1 | package com.happyheng.test; 2 | 3 | import com.happyheng.entity.News; 4 | import com.happyheng.entity.result.NewsResult; 5 | import com.happyheng.service.impl.NewsServiceImpl; 6 | import com.happyheng.utils.Redis; 7 | 8 | import redis.clients.jedis.Jedis; 9 | 10 | public class RedisTest { 11 | 12 | public static void main(String[] args) { 13 | // // 连接本地的 Redis 服务 14 | // Jedis jedis = Redis.getConnection(); 15 | // System.out.println("Connection to server sucessfully"); 16 | // // 查看服务是否运行 17 | // System.out.println("Server is running: " + jedis.ping()); 18 | // 19 | // jedis.set("bar", "1"); 20 | // String result = jedis.get("bar"); 21 | // System.out.println("存储的结果为"+result); 22 | 23 | NewsServiceImpl service = new NewsServiceImpl(); 24 | NewsResult result = service.getNews(1, 0, 10); 25 | 26 | for (News news : result.getData()) { 27 | System.out.println("得到的结果为"+news); 28 | } 29 | } 30 | 31 | } 32 | -------------------------------------------------------------------------------- /src/com/happyheng/test/UserDaoTest.java: -------------------------------------------------------------------------------- 1 | package com.happyheng.test; 2 | 3 | import org.apache.ibatis.session.SqlSession; 4 | 5 | import com.happyheng.dao.UserDao; 6 | import com.happyheng.entity.User; 7 | import com.happyheng.utils.MyBatisUtil; 8 | 9 | public class UserDaoTest { 10 | 11 | public static void main(String[] args) { 12 | // testSave(); 13 | //testQueryUserName(); 14 | //testQueryPassword(); 15 | //testUpdateToken(); 16 | testGetUserId(); 17 | } 18 | 19 | public static void testSave() { 20 | SqlSession session = MyBatisUtil.getSqlSession(true); 21 | UserDao dao = session.getMapper(UserDao.class); 22 | 23 | User user = new User(); 24 | user.setName("liuheng789"); 25 | user.setPassword("123456"); 26 | user.setNickname("lalalalala"); 27 | 28 | dao.save(user); 29 | session.close(); 30 | } 31 | 32 | public static void testQueryUserName() { 33 | SqlSession session = MyBatisUtil.getSqlSession(true); 34 | UserDao dao = session.getMapper(UserDao.class); 35 | 36 | Integer id = dao.queryUserName("liuheng"); 37 | if (id != null) { 38 | System.out.println("查询的id为" + id); 39 | } else { 40 | System.out.println("为空"); 41 | } 42 | session.close(); 43 | } 44 | 45 | public static void testQueryPassword() { 46 | SqlSession session = MyBatisUtil.getSqlSession(true); 47 | UserDao dao = session.getMapper(UserDao.class); 48 | 49 | Integer id = dao.queryPassWord(1, "123456"); 50 | if (id != null) { 51 | System.out.println("查询的id为" + id); 52 | } else { 53 | System.out.println("为空"); 54 | } 55 | session.close(); 56 | } 57 | 58 | public static void testUpdateToken() { 59 | SqlSession session = MyBatisUtil.getSqlSession(true); 60 | UserDao dao = session.getMapper(UserDao.class); 61 | 62 | Integer result = dao.updateToken(1, "1_1470576360275"); 63 | System.out.println("影响的条数为"+result); 64 | } 65 | 66 | public static void testGetUserId() { 67 | SqlSession session = MyBatisUtil.getSqlSession(true); 68 | UserDao dao = session.getMapper(UserDao.class); 69 | 70 | Integer id = dao.getUserId("2_1470575831125"); 71 | if (id != null) { 72 | System.out.println("查询的结果为"+id); 73 | } else { 74 | System.out.println("结果为空"); 75 | } 76 | session.close(); 77 | } 78 | 79 | } 80 | -------------------------------------------------------------------------------- /src/com/happyheng/utils/ConnectionFactory.java: -------------------------------------------------------------------------------- 1 | package com.happyheng.utils; 2 | 3 | import java.io.InputStream; 4 | import java.sql.Connection; 5 | import java.sql.DriverManager; 6 | import java.util.Properties; 7 | 8 | /** 9 | * 连接数据库的工厂类 10 | * 11 | * @author liuheng 12 | * 13 | */ 14 | public class ConnectionFactory { 15 | 16 | 17 | // 下面的四个成员变量用于保存从数据库中读取的配置信息 18 | private static String driver; 19 | private static String dburl; 20 | private static String user; 21 | private static String password; 22 | 23 | private static ConnectionFactory mConnectionFactory = new ConnectionFactory(); 24 | 25 | private ConnectionFactory() { 26 | 27 | } 28 | 29 | //使用静态代码块来初始化四个配置信息 30 | static { 31 | 32 | Properties properties = new Properties(); 33 | 34 | try { 35 | InputStream inputStream = ConnectionFactory.class.getClassLoader() 36 | .getResourceAsStream("dbconfig.properties"); 37 | properties.load(inputStream); 38 | 39 | } catch (Exception e) { 40 | e.printStackTrace(); 41 | } 42 | driver = properties.getProperty("driver"); 43 | dburl = properties.getProperty("dburl"); 44 | user = properties.getProperty("user"); 45 | password = properties.getProperty("password"); 46 | } 47 | 48 | //获取ConnectionFactory单例 49 | public static ConnectionFactory getInstance() { 50 | return mConnectionFactory; 51 | } 52 | 53 | private Connection mConnection; 54 | 55 | public Connection makeConnection(){ 56 | try { 57 | Class.forName(driver); 58 | mConnection = DriverManager.getConnection(dburl, user, password); 59 | } catch (Exception e) { 60 | e.printStackTrace(); 61 | } 62 | return mConnection; 63 | } 64 | 65 | } 66 | -------------------------------------------------------------------------------- /src/com/happyheng/utils/ContextUtils.java: -------------------------------------------------------------------------------- 1 | package com.happyheng.utils; 2 | 3 | import org.springframework.context.ApplicationContext; 4 | import org.springframework.context.support.ClassPathXmlApplicationContext; 5 | 6 | public class ContextUtils { 7 | private static final String APPLICATION_XML = "applicationContext.xml"; 8 | 9 | private static ApplicationContext context; 10 | 11 | public static ApplicationContext getContext(){ 12 | if (context == null) { 13 | context = new ClassPathXmlApplicationContext(APPLICATION_XML); 14 | } 15 | return context; 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /src/com/happyheng/utils/HexUtils.java: -------------------------------------------------------------------------------- 1 | package com.happyheng.utils; 2 | 3 | public class HexUtils { 4 | public static String bytesToHexString(byte[] src){ 5 | StringBuilder stringBuilder = new StringBuilder(""); 6 | if (src == null || src.length <= 0) { 7 | return null; 8 | } 9 | for (int i = 0; i < src.length; i++) { 10 | int v = src[i] & 0xFF; 11 | String hv = Integer.toHexString(v); 12 | if (hv.length() < 2) { 13 | stringBuilder.append(0); 14 | } 15 | stringBuilder.append(hv); 16 | } 17 | return stringBuilder.toString(); 18 | } 19 | /** 20 | * Convert hex string to byte[] 21 | * @param hexString the hex string 22 | * @return byte[] 23 | */ 24 | public static byte[] hexStringToBytes(String hexString) { 25 | if (hexString == null || hexString.equals("")) { 26 | return null; 27 | } 28 | hexString = hexString.toUpperCase(); 29 | int length = hexString.length() / 2; 30 | char[] hexChars = hexString.toCharArray(); 31 | byte[] d = new byte[length]; 32 | for (int i = 0; i < length; i++) { 33 | int pos = i * 2; 34 | d[i] = (byte) (charToByte(hexChars[pos]) << 4 | charToByte(hexChars[pos + 1])); 35 | } 36 | return d; 37 | } 38 | /** 39 | * Convert char to byte 40 | * @param c char 41 | * @return byte 42 | */ 43 | private static byte charToByte(char c) { 44 | return (byte) "0123456789ABCDEF".indexOf(c); 45 | } 46 | } 47 | -------------------------------------------------------------------------------- /src/com/happyheng/utils/MyBatisUtil.java: -------------------------------------------------------------------------------- 1 | package com.happyheng.utils; 2 | 3 | import java.io.InputStream; 4 | 5 | import org.apache.ibatis.session.SqlSession; 6 | import org.apache.ibatis.session.SqlSessionFactory; 7 | import org.apache.ibatis.session.SqlSessionFactoryBuilder; 8 | 9 | public class MyBatisUtil { 10 | 11 | /** 12 | * 获取SqlSessionFactory 13 | * @return SqlSessionFactory 14 | */ 15 | public static SqlSessionFactory getSqlSessionFactory() { 16 | String resource = "conf.xml"; 17 | InputStream is = MyBatisUtil.class.getClassLoader().getResourceAsStream(resource); 18 | SqlSessionFactory factory = new SqlSessionFactoryBuilder().build(is); 19 | return factory; 20 | } 21 | 22 | /** 23 | * 获取SqlSession 24 | * @return SqlSession 25 | */ 26 | public static SqlSession getSqlSession() { 27 | return getSqlSessionFactory().openSession(); 28 | } 29 | 30 | /** 31 | * 获取SqlSession 32 | * @param isAutoCommit 33 | * true 表示创建的SqlSession对象在执行完SQL之后会自动提交事务 34 | * false 表示创建的SqlSession对象在执行完SQL之后不会自动提交事务,这时就需要我们手动调用sqlSession.commit()提交事务 35 | * @return SqlSession 36 | */ 37 | public static SqlSession getSqlSession(boolean isAutoCommit) { 38 | return getSqlSessionFactory().openSession(isAutoCommit); 39 | } 40 | } 41 | -------------------------------------------------------------------------------- /src/com/happyheng/utils/PropertiesUtils.java: -------------------------------------------------------------------------------- 1 | package com.happyheng.utils; 2 | 3 | import java.io.InputStream; 4 | import java.util.Properties; 5 | 6 | public class PropertiesUtils { 7 | 8 | private Properties properties; 9 | 10 | public PropertiesUtils(String fileName) { 11 | 12 | properties = new Properties(); 13 | 14 | try { 15 | InputStream inputStream = ConnectionFactory.class.getClassLoader().getResourceAsStream(fileName); 16 | properties.load(inputStream); 17 | 18 | } catch (Exception e) { 19 | e.printStackTrace(); 20 | } 21 | } 22 | 23 | public String getProperties(String propertiesName) { 24 | return properties.getProperty(propertiesName); 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /src/com/happyheng/utils/Redis.java: -------------------------------------------------------------------------------- 1 | package com.happyheng.utils; 2 | 3 | import redis.clients.jedis.Jedis; 4 | 5 | public class Redis { 6 | public static Jedis mJedis = null; 7 | 8 | public static Jedis getConnection() { 9 | if (mJedis == null) { 10 | mJedis = new Jedis("localhost"); 11 | } 12 | return mJedis; 13 | } 14 | 15 | } 16 | -------------------------------------------------------------------------------- /src/com/happyheng/utils/TextUtils.java: -------------------------------------------------------------------------------- 1 | package com.happyheng.utils; 2 | 3 | public class TextUtils { 4 | 5 | /** 6 | * 判断一个String是否为空的方法 7 | * @param text 8 | * @return 9 | */ 10 | public static boolean isEmpty(String text) { 11 | if (text == null || text.isEmpty()) { 12 | return true; 13 | } else { 14 | return false; 15 | } 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /src/conf.xml: -------------------------------------------------------------------------------- 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 | -------------------------------------------------------------------------------- /src/dbconfig.properties: -------------------------------------------------------------------------------- 1 | driver = com.mysql.jdbc.Driver 2 | dburl = jdbc\:mysql\://127.0.0.1\:3306/sport_db 3 | user = root 4 | password = mytestcon -------------------------------------------------------------------------------- /src/key.properties: -------------------------------------------------------------------------------- 1 | key=5de7e29919fad4d5a17f97a3b6215a68 --------------------------------------------------------------------------------