SSM作品项目总结(游戏商店)
一. 项目用到的技术
- 语言:java,html + ajax,js
- 工具:Intellij IDEA
- 数据库可视化: navicat
- web服务器:Tomcat
- 框架:(jquery,bootstrap,Mybatis,Spring,SpringMvc)
- 项目的管理和构建工具:Maven
- 设计模式:MVC
二.项目主要功能
- 用户登录
- 用户注册
- 游戏商店(公共界面)—— 分页查询
- 用户可将其加入个人购物车
- 用户提交订单
- 查询游戏
- 分类查询
- 个人中心页面(个性签名,修改密码,个人信息查看)
- 用户管理(修改权限,删除用户)
- 游戏管理(增加游戏,修改游戏类型,修改游戏价格,增加游戏类型)
- 点击注销
三.项目展示
-
登录页面
-
注册页面
-
普通用户界面
-
个人中心页面
-
游戏评论功能
-
管理员界面
-
增加游戏
-
查询游戏(类型修改,价格修改)
-
增加游戏类型
-
用户管理(修改权限,删除用户)
四.项目讲解
-
项目结构
-
数据库表
-
maven依赖
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0modelVersion>
<groupId>org.examplegroupId>
<artifactId>SpringExampleartifactId>
<version>1.0-SNAPSHOTversion>
<packaging>warpackaging>
<dependencies>
<dependency>
<groupId>org.springframeworkgroupId>
<artifactId>spring-contextartifactId>
<version>5.0.5.RELEASEversion>
dependency>
<dependency>
<groupId>org.aspectjgroupId>
<artifactId>aspectjweaverartifactId>
<version>1.8.7version>
dependency>
<dependency>
<groupId>org.springframeworkgroupId>
<artifactId>spring-jdbcartifactId>
<version>5.0.5.RELEASEversion>
dependency>
<dependency>
<groupId>org.springframeworkgroupId>
<artifactId>spring-txartifactId>
<version>5.0.5.RELEASEversion>
dependency>
<dependency>
<groupId>org.springframeworkgroupId>
<artifactId>spring-testartifactId>
<version>5.0.5.RELEASEversion>
dependency>
<dependency>
<groupId>org.springframeworkgroupId>
<artifactId>spring-webmvcartifactId>
<version>5.0.5.RELEASEversion>
dependency>
<dependency>
<groupId>javax.servletgroupId>
<artifactId>servlet-apiartifactId>
<version>2.5version>
<scope>providedscope>
dependency>
<dependency>
<groupId>javax.servlet.jspgroupId>
<artifactId>jsp-apiartifactId>
<version>2.0version>
<scope>providedscope>
dependency>
<dependency>
<groupId>org.mybatisgroupId>
<artifactId>mybatisartifactId>
<version>3.4.5version>
dependency>
<dependency>
<groupId>org.mybatisgroupId>
<artifactId>mybatis-springartifactId>
<version>1.3.1version>
dependency>
<dependency>
<groupId>mysqlgroupId>
<artifactId>mysql-connector-javaartifactId>
<version>5.1.47version>
dependency>
<dependency>
<groupId>c3p0groupId>
<artifactId>c3p0artifactId>
<version>0.9.1.2version>
dependency>
<dependency>
<groupId>junitgroupId>
<artifactId>junitartifactId>
<version>4.12version>
dependency>
<dependency>
<groupId>jstlgroupId>
<artifactId>jstlartifactId>
<version>1.2version>
dependency>
<dependency>
<groupId>com.alibabagroupId>
<artifactId>fastjsonartifactId>
<version>1.2.62version>
dependency>
<dependency>
<groupId>com.github.pagehelpergroupId>
<artifactId>pagehelperartifactId>
<version>3.7.5version>
dependency>
<dependency>
<groupId>com.github.jsqlparsergroupId>
<artifactId>jsqlparserartifactId>
<version>0.9.1version>
dependency>
dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.tomcat.mavengroupId>
<artifactId>tomcat7-maven-pluginartifactId>
<version>2.2version>
<configuration>
<port>80port>
<path>/path>
configuration>
plugin>
plugins>
build>
project>
- 用户controller层
package com.controller;
import com.alibaba.fastjson.JSON;
import com.domain.User;
import com.service.UserService;
import com.sun.org.apache.xpath.internal.operations.Mod;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;
import javax.annotation.Resource;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.List;
@Controller
@RequestMapping("/user")
public class UserController {
@Resource(name = "userService")
private UserService userService;
@RequestMapping("/login")
public ModelAndView login(String username, String password, HttpSession session, String isRem, HttpServletResponse resp) {
User user = userService.login(username, password);
if(user == null){
session.setAttribute("isLog",1);
return new ModelAndView("redirect:/jsp/login.jsp");
}
if("1".equals(isRem)){
Cookie c_username = new Cookie("username",username);
Cookie c_password = new Cookie("password",password);
c_username.setMaxAge(60*60*24*7);
c_password.setMaxAge(60*60*24*7);
resp.addCookie(c_username);
resp.addCookie(c_password);
}
session.removeAttribute("isLog");
session.setAttribute("user",user);
return user.getIsRooter() == 1 ? new ModelAndView("redirect:/jsp/adminPage.jsp") : new ModelAndView("redirect:/jsp/mainPage.jsp");
}
@RequestMapping("/register")
public ModelAndView register(String username,String password,HttpSession session){
User user = userService.login(username, password);
if(user == null){
session.removeAttribute("hasExist");
userService.register(username, password);
return new ModelAndView("redirect:/jsp/login.jsp");
}else{
session.setAttribute("hasExist",1);
return new ModelAndView("register");
}
}
@RequestMapping("updateP")
public void updatePassword(String newPassword,String prePassword,String userId,HttpServletResponse resp) throws IOException {
userService.updatePassword(newPassword, prePassword,userId);
resp.setContentType("text/json;charset=utf-8");
resp.getWriter().write("修改密码成功");
}
@RequestMapping("/updateS")
public void updateSignal(String newSignal,String userId,HttpServletResponse resp,HttpSession session) throws IOException {
userService.updateSignal(newSignal, userId);
User user = userService.selectUserById(userId);
session.setAttribute("user",user);
resp.setContentType("text/json;charset=utf-8");
resp.getWriter().write("修改个性签名成功");
}
@RequestMapping("/logOut")
public void logOut(HttpSession session,HttpServletResponse resp) throws IOException {
session.removeAttribute("user");
session.removeAttribute("isLog");
resp.setContentType("text/json;charset=utf-8");
resp.getWriter().write("注销成功");
}
@RequestMapping("/selectAllUser")
public void selectAllUser (HttpServletResponse resp) throws IOException {
List<User> users = userService.selectAllUser();
String jsonString = JSON.toJSONString(users);
resp.setContentType("text/json;charset=utf-8");
resp.getWriter().write(jsonString);
}
@RequestMapping("/deleteUser")
public void deleteUser (HttpServletResponse resp,String userName) throws IOException {
userService.deleteUser(userName);
resp.setContentType("text/json;charset=utf-8");
resp.getWriter().write("delete successfully");
}
@RequestMapping("/updateRoot")
public void updateRoot (HttpServletResponse resp,String isR,String userName) throws IOException {
userService.updateRoot(isR,userName);
resp.setContentType("text/json;charset=utf-8");
resp.getWriter().write("update successfully");
}
}
- 用户service层
package com.service.Impl;
import com.domain.User;
import com.mapper.UserMapper;
import com.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.util.List;
@Service("userService")
public class UserServiceImpl implements UserService {
@Autowired
private UserMapper userMapper;
public User login(String username, String password) {
User user = userMapper.login(username, password);
return user;
}
public void register(String username, String password) {
userMapper.register(username, password);
}
public void updatePassword(String newPassword, String prePassword,String userId) {
userMapper.updatePassword(newPassword, prePassword, userId);
}
public void updateSignal(String newSignal, String userId) {
userMapper.updateSignal(newSignal, userId);
}
public User selectUserById(String userId) {
return userMapper.selectUserById(userId);
}
public List<User> selectAllUser() {
return userMapper.selectAllUser();
}
public void deleteUser(String userName) {
userMapper.deleteUser(userName);
}
public void updateRoot(String isR, String userName) {
userMapper.updateRoot(isR, userName);
}
}
- 用户mapper层
package com.mapper;
import com.domain.User;
import org.apache.ibatis.annotations.*;
import org.springframework.stereotype.Repository;
import java.util.List;
public interface UserMapper {
public User login(@Param("username") String username, @Param("password") String password);
@Insert("insert into user (username, password, is_rooter, is_delete,user_signal) values(#{username},#{password},0,0,'') ")
public void register(@Param("username") String username, @Param("password") String password);
@Update("update user set password = #{newPassword} where password = #{prePassword} and id = #{userId}")
public void updatePassword(@Param("newPassword") String newPassword,@Param("prePassword") String prePassword,@Param("userId") String userId);
@Update("update user set user_signal = #{newSignal} where id = #{userId}")
public void updateSignal(@Param("newSignal") String newSignal, @Param("userId") String userId);
public User selectUserById(@Param("userId") String userId);
public List<User> selectAllUser();
@Update("update user set is_delete = 1 where username = #{userName}")
public void deleteUser(@Param("userName") String userName);
@Update("update user set is_rooter = #{isR} where username = #{userName}")
public void updateRoot(@Param("isR") String isR, @Param("userName") String userName);
}
- 用户sql映射文件
DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.mapper.UserMapper">
<resultMap id="UserMap" type="user">
<result column="is_delete" property="isDelete">result>
<result column="is_rooter" property="isRooter">result>
<result column="user_signal" property="userSignal">result>
resultMap>
<select id="login" resultMap="UserMap">
select * from user where username = #{username} and password = #{password}
select>
<select id="selectUserById" resultMap="UserMap">
select * from user where id = #{userId}
select>
<select id="selectAllUser" resultMap="UserMap">
select * from user where is_delete = 0
select>
mapper>
- Mybatis核心配置文件
DOCTYPE configuration
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
<typeAliases>
<package name="com.domain">package>
typeAliases>
<plugins>
<plugin interceptor="com.github.pagehelper.PageHelper">
<property name="dialect" value="mysql">property>
plugin>
plugins>
configuration>
- Spring配置文件
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd
">
<context:component-scan base-package="com">
<context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
context:component-scan>
<context:property-placeholder location="classpath:jdbc.properties"/>
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="driverClass" value="${jdbc.driver}"/>
<property name="jdbcUrl" value="${jdbc.url}"/>
<property name="user" value="${jdbc.username}"/>
<property name="password" value="${jdbc.password}"/>
bean>
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource"/>
<property name="configLocation" value="classpath:SqlMapConfig.xml"/>
bean>
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="com.mapper"/>
bean>
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource">property>
bean>
<tx:advice id="txAdvice">
<tx:attributes>
<tx:method name="*"/>
tx:attributes>
tx:advice>
<aop:config>
<aop:advisor advice-ref="txAdvice" pointcut="execution(* com.service.Impl.*.*(..))"/>
aop:config>
beans>
- SpringMvc核心配置文件
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd
">
<context:component-scan base-package="com.controller"/>
<mvc:annotation-driven/>
<bean id="resourceViewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/jsp/">property>
<property name="suffix" value=".jsp">property>
bean>
<mvc:default-servlet-handler/>
<mvc:interceptors>
<mvc:interceptor>
<mvc:mapping path="/**"/>
<mvc:exclude-mapping path="/user/login"/>
<mvc:exclude-mapping path="/user/register"/>
<bean class="com.interceptor.MyInterceptor"/>
mvc:interceptor>
mvc:interceptors>
beans>