一,普通用户(User)
1)注册:
注册的时候由于注册的用户分为普通用户和管理员用户,所以关联到另一张角色表,并且是用户角色权限表是通过用户表和角色表的主键构成,不能直接在用户表里添加字段,因为登录的时候会判断是普通用户还是管理员,所以需要进行判断登录,但是刚注册的用户是没法拿到权限的,因为它的主键id是自增的不是自己设置,所以这里用到了一个主键回填的技术
<!-- 注册 使用主键回填 -->
<insert id="register" parameterType="entity.User">
<selectKey keyProperty="id" keyColumn="ID" resultType="Integer" order="AFTER">
select last_insert_id()
</selectKey>
insert into sys_user(tel,password,email) values (#{tel},#{password},#{email})
</insert>
<!-- 注册的时候将用户的id值添加到角色管理中 -->
<insert id="addRoler" parameterType="int">
insert into sys_user_roler(id,rid) values (#{id},2)
</insert>
使用主键回填在调用方法执行完sql就可以拿到他的id,由于一般注册的都是普通用户,就可以使用拿着这个id向用户权限管理表中添加数据,默认为普通用户,在controller层调用完注册的方法,在通过user对象拿到他的id,再调用用户权限的方法,将数据插入到用户权限表中,这样用户就会有一个普通用户的权限。int a = userService.register(user) ;
//通过主键回填的方法获取到了id,可以将这个id添加到用户的权限管理中
int count = userService.addRoler(user.getId()) ;
这就通过主键回填的方法,同时完成了同时向两个数据表中添加数据
这是注册中的一个重点
2)登录
通过上面的注册,在登录时首先会对用户的用户名(考虑安全,并且密码会编译,所以不携带密码)去数据表中查询,查询到再拿着数据库里查到的密码和前台输入的密码进行比对,比对成功后将密码从user中删除掉(通过在user的实体类中创建一个有参构造的删除方法就是this。password=null),再将用户的user的信息添加到域中已方便后续的使用(user.getseSsion.setAttribute("user",user)),然后接着再会进行再一次的判断是普通用户还是管理员身份,会通过一个工具类(就是用于给前端页面响应状态码(int code),提示语(String msg),携带数据的(Object data)),返回不同的状态码,前台页面根据不同的状态码进行页面的转发,去普通用户还是管理员页面,如果比对不成功就返回用户名或密码不正确,去过没查到用户名就直接返回没有此用户。if(user != null){
//判断密码是否一样
if(password.equals(user.getPassword())){
user.deletePassword();
//添加到域中
request.getSession().setAttribute("user",user);
//判断是普通用户还是管理员
if(user.getRoler().getRname().equals("普通用户")){
msg.setCode(1);
}else{
msg.setCode(0);
}
}else{
msg.setInfo("用户名或密码错误");
}
}else{
msg.setInfo("用户名不存在!") ;
}
登录的sql也是三表联查,用户表,权限表,用户权限表
<select id="login" resultMap="userMapAndRoler" >
SELECT u.*,r.* FROM sys_user u,sys_roler r,sys_user_roler ur
WHERE u.id=ur.id
AND r.rid= ur.rid
AND tel=#{tel}
</select>
3)登出
就是将用户的信息从域中获取到,然后从域中删除掉
request.getSession().removeAttribute("user");
4)登录成功(选择地址)
登录成功就可以点击短租自驾跳转到城市地址的选择,选完取车地址还车地址,再通过取车地址来查询当前所在地的车辆信息
先根据pid来查询所有的地址,前台页面刚触发时默认的是0所以就会拿到前边所有的省份,而再根据前端页面选择的省份id来查询它下级的所有的市级地点,
<sql id="syscity_all">
id,name,pid
</sql>
<!-- 查询所有-->
<select id="selectCity" parameterType="int" resultType="entity.City">
select
<include refid="syscity_all" />
from sys_city where pid=#{pid}
</select>
<!-- 根据id查询一个地址的信息-->
<select id="selectById" parameterType="int" resultType="entity.City">
select
<include refid="syscity_all" />
from sys_city where id=#{id}
</selec>
这样就选择完地址--前往选车
5)选择车辆
选车页面需要获取你的取车地址,还车地址,还要查到当前取车点的所有车辆信息,并且按照汽车的座位数排序,按照租金排序,点击预定
取车地址,还车地址:
根据选择时候的getid,backid俩查询取车地址,还车地址,它的id是前端页面上获取到的,前端页面中将跳转到的页面时携带着getid,backid。
查询当前地址的车辆信息
<resultMap id="carMapp" type="entity.Car">
<id property="id" column="id" />
<result property="name" column="name" />
<result property="type" column="type" />
<result property="sitnum" column="sitnum" />
<result property="price" column="price" />
<result property="number" column="number" />
<result property="picture" column="picture" />
</resultMap>
<resultMap id="carAndCityMapp" extends="carMapp" type="entity.Car">
<association property="city" javaType="entity.City">
<id property="id" column="id" />
<result property="name" column="name" />
<result property="pid" column="pid" />
</association>
</resultMap>
<!-- 2. 根据城市查询车辆 租金升序排列-->
<select id="selectCityById" resultMap="carAndCityMapp">
select * from sys_car,sys_city
where sys_car.cid=sys_city.id and sys_car.cid=#{cid}
order by sys_car.price asc;
</select>
<!-- 4. 根据汽车id查询,按数量排序-->
<select id="selectCarByNumber" parameterType="int" resultMap="carAndCityMapp">
select *
from sys_car,sys_city
where sys_car.cid = sys_city.id
and sys_car.cid=#{gitId}
order by sys_car.sitnum desc;
</select>
6)提交订单的页面
点击预定--跳转到下一页面,要展示出取出的地址,还车的地址,以及这个车辆的信息,和订单的金额。
根据跳转的路径下携带参数getid,backid查到取出还车地址,在根据点击某一个车后的预定来获取到当前车辆的id,并将车辆的信息展示出来,在前台页面计算出它的总价格,
提交订单跳转到订单的页面(提交订单的时候需要判断一下用户当前是否已经登录,从域中获取user,判断user是否为空,来判断用户是否登录)
7)订单页面(订单管理,个人信息)
点击提交订单就是向库里添加订单数据-添加成功后重新加载页面
<insert id="addOrder">
insert into sys_order(cid,uid,getid,backid,oprice,statu,flag)
values (#{arg0},#{arg1},#{arg2},#{arg3},#{arg4},#{arg5},#{arg6})
</insert>
跳进去默认显示当前登录用户的所有订单(订单号,订单金额,车型,取车地址,还车地址,订单状态)
这些个字段是分别在三个表中sys_order(订单表)sys_city(地址表),sys_car(汽车表)而地址分为取车地址和还还车地址,因此需要级联两次,并且这个数据都需要根据用户的id来查询(用户的id是从域中拿到的,登录完将user添加到域中了),所以也需要sys_user(用户表),
所以sql是:
<!-- 根据用户的id查询当前用户的全部订单 -->
<select id="selectAllOrder" resultMap="backcityMapp" parameterType="int">
SELECT
so.id oid,
so.oprice,
so.statu,
sc.*,
sc1.id gid,
sc1.name gname,
sc1.pid gpid,
sc2.id bid,
sc2.name bname,
sc2.pid bpid,
su.*
FROM
sys_order so, sys_car sc,sys_city sc1,sys_city sc2,sys_user su
WHERE so.cid=sc.id
AND so.uid=su.id
AND so.getid=sc1.id
AND so.backid=sc2.id
AND so.flag=1
AND su.id=#{id}
</select>
有用户删除订单的操作--是根据订单的id来删除
<!-- 删除订单 就是将标记flag改为0 -->
<update id="deleteOrder" parameterType="int">
update sys_order set flag=0 where id=#{id}
</update>
8)个人信息(展示当前用户的用户名和密码,有修改的操作)
根据用户的id来查询当前的用户信息,或者可以直接从域中拿到当前用户的信息
修改:就是update用户的信息
根据当前用户的id,来更新它的信息
使用动态sql来进行修改
<!-- 通过id查询修改一个用户的信息 用动态sql -->
<update id="updateMyinfo" parameterType="entity.User">
update sys_user
<set>
<if test="tel!=null">
tel=#{tel}
</if>
<if test="email!=null">
email=#{email}
</if>
<if test="password!=null">
password=#{password}
</if>
</set>
<where>
<if test="id!=null">
id=#{id}
</if>
</where>
</update>
二,后台管理(Admin)
后台管理分为三大块:用户管理,车辆管理,订单管理
A)用户管理里展示所有的用户信息,有修改和删除的操作
B)车辆管理里展示所有的车辆信息,有增加,删除,修改
C)订单管理里展示所有的订单信息,有删除操作
1)管理员的注册直接从库里来注册
1)用户管理
点击用户管理下的信息管理,就查询出所有的用户信息来渲染,
每条数据后有一个修改删除的操作,点击每条数据,通过当前用户的id来删除(将他的flag标记修改),通过id来修改数据,都是通过弹模态框来操作,都是一些基本操作。修改的时候需要从前台页面传回来json数据,需要用@RequestBody转换接受数据
2)车辆管理的操作
查询展示出所有的车辆信息,修改和删除基本操作通过id来进行操作,添加的时候需要向里面添加一个车辆图片,车辆图片的上传需要用到文件的上传
需要在spring-MVC中添加配置
<!-- 配置文件上传解析器 -->
<!-- 这里的id为特定值-->
<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
<!-- 定义最大上传文件大小 10G
1GB=1024MB,1MB=1024KB,1KB=1024B,1B(字节)=8bits(比特) -->
<property name="maxUploadSize" value="104857600" />
</bean>
上传文件时的请求是POST 类型是 MultipartFile
a)获取文件的上传路径
String realPath = request.getSession().getServletContext().getRealPath("/upload") ;
b)将这个realPath放到一个目录下
File file = new File(realPath)
c)获取它的原文件名
String originalFilename = carPic.getOriginalFilename();
d)构建它的新名字--使用UUID随机生成器生成,再从原名上取下后缀
String fileName = UUID.randomUUID().toString()+originalFilename.substring(originalFilename.lastIndexOf("."));
e) 将carPic含的文件流写入到file目录下的filename中
carPic.transferTo(new File(file,fileName));
这就是文件上传的操作
f)拼接图片路径
String path = "/upload/"+fileName ;
g)调用方法进行向数据库添加--添加的是这个图片的地址
eg:下面的一个插入-可以使用car对象作为参数,car来封装进行传参
//添加车辆---并且上传车辆图片
@RequestMapping(value = "/addcar",method = RequestMethod.POST)
// @ResponseBody
public String addCar(HttpServletRequest request,String name, String type, int sitnum,int number,
double price, MultipartFile carPic){
//1.获取文件上传的物理路径
String realPath = request.getSession().getServletContext().getRealPath("/upload");
//测试--文件的路径
System.out.println("文件的路径: "+realPath);
//将它放到一个文件目录下
File file = new File(realPath);
//获取它的原文件名
String originalFilename = carPic.getOriginalFilename();
System.out.println("原名是: "+originalFilename);
//构建它的新名字--使用UUID随机生成器生成,再从原名上取下后缀
String fileName = UUID.randomUUID().toString()+originalFilename.substring(originalFilename.lastIndexOf("."));
//将carPic含的文件流写入到file目录下的filename中
try {
carPic.transferTo(new File(file,fileName));
} catch (IOException e) {
e.printStackTrace();
}
//拼接图片路径
String path = "/upload/"+fileName ;
//直接调用方法
int count = carService.addCar(name, type, sitnum, price, number, path);
//判断是否插入成功
if(count == 1){
ActionResult actionResult = new ActionResult(200,"添加成功","");
return "/admin/carList" ;
}else {
ActionResult actionResult = new ActionResult(500,"添加失败","");
return "添加失败" ;
}
}
3)订单管理
展示全部订单--查询全部是多表联查
sys_order,sys_city,sys_car,与用户里面展示的全部订单就少了用户的那个表,不用根据用户的id来查
sql也一致,只是少了用户表和用户id的条件
订单的删除就是通过点击那个订单,获取到当前的订单id来删除(就是假删除,将它的flag值进行修改)
环境配置好之后,
使用注解注入
service层
@service(“userService”)
controller层
@controller()
@RequestMapping(value = "/update",method = RequestMethod.PUT)
// @RequestBody 是接受从前台页面传过来的json数据
// @ResponseBody 是从后台向前端页面响应json数据
@ResponseBody
public ActionResult update(@RequestBody User user){
配置文件
poml.xml依赖
<?xml version="1.0" encoding="UTF-8"?>
<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.0</modelVersion>
<groupId>com.qf.j2005</groupId>
<artifactId>SSM_Group5_Car</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>war</packaging>
<dependencies>
<!-- 添加api依赖 servlet -->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.0.1</version>
<scope>provided</scope>
</dependency>
<!-- 上传组件依赖 上传文件用的 -->
<dependency>
<groupId>commons-fileupload</groupId>
<artifactId>commons-fileupload</artifactId>
<version>1.3.2</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>4.3.6.RELEASE</version>
</dependency>
<!-- 单元测试 -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
<!-- jdbc -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>4.3.6.RELEASE</version>
</dependency>
<!-- mysql数据库 -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.46</version>
</dependency>
<!-- druid 德鲁伊 -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.1.9</version>
</dependency>
<!-- json -->
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.8.8</version>
</dependency>
<!-- mybatis -->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.4.6</version>
</dependency>
<!-- mybatie/spring整合包 -->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
<version>2.0.1</version>
</dependency>
<!--jstl-->
<dependency>
<groupId>jstl</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.tomcat.maven</groupId>
<artifactId>tomcat7-maven-plugin</artifactId>
<version>2.2</version>
<configuration>
<port>8080</port>
<path>/</path>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.6.1</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
<compilerVersion>1.8</compilerVersion>
<encoding>UTF-8</encoding>
</configuration>
</plugin>
</plugins>
</build>
</project>
db.properties
jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/cars?characterEncoding=utf-8
jdbc.user=root
jdbc.pass=root
mybaties.xml
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
<!-- <!– 这个里面在双标签中如果没有数据的时候可以改为单标签–>-->
<!-- <!– 配置日志文件–>-->
<settings>
<setting name="logImpl" value="STDOUT_LOGGING"/>
</settings>
<!-- 配置别名 也就是在这里配置了别名在Usermapper中设置返回来类型时可以简写 -->
<!-- <typeAliases>-->
<!-- <typeAlias type="entity"/>-->
<!-- </typeAliases>-->
<!-- <!–分页插件–>-->
<!-- <plugins>-->
<!-- <plugin interceptor="com.github.pagehelper.PageInterceptor"/>-->
<!-- </plugins>-->
</configuration>
Spring_core.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
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
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd">
<!-- 扫描service包-->
<context:component-scan base-package="service" />
<!-- 扫描进来文件-->
<context:property-placeholder location="classpath:db.properties" />
<!-- 数据源-->
<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">
<property name="driverClassName" value="${jdbc.driver}" />
<property name="url" value="${jdbc.url}" />
<property name="username" value="${jdbc.user}" />
<property name="password" value="${jdbc.pass}" />
</bean>
<!-- 整合mybaties-->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="mapperLocations" value="classpath:mapper/*.xml" />
<property name="configLocation" value="classpath:mybaties.xml" />
</bean>
<!-- 扫描dao包-->
<bean id="mapperScannerConfigurer" class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="dao" />
<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory" />
</bean>
<!-- 事务管理器 -->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<!-- 数据源 -->
<property name="dataSource" ref="dataSource" />
</bean>
<!--开启事务注解配置-->
<mvc:annotation-driven />
</beans>
Spring_mvc.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
<!-- 配置组件扫描 -->
<context:component-scan base-package="controller" />
<!-- 配置MVC注解扫描 -->
<mvc:annotation-driven />
<!-- 释放静态资源-->
<mvc:default-servlet-handler />
<!-- 配置视图解析器, -->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/view/" /><!-- 前缀 -->
<property name="suffix" value=".jsp" /><!-- 后缀 -->
</bean>
<!--5、静态资源地址映射-->
<mvc:resources mapping="/css/*" location="/WEB-INF/view/css/"/>
<mvc:resources mapping="/js/*" location="/WEB-INF/view/js/"/>
<mvc:resources mapping="/layui/**" location="/WEB-INF/view/layui/"/>
<mvc:resources mapping="/fonts/*" location="/WEB-INF/view/fonts/"/>
<mvc:resources mapping="/images/*" location="/WEB-INF/view/images/"/>
<mvc:resources mapping="/image/*" location="/WEB-INF/view/image/"/>
<!-- 配置文件上传解析器 -->
<!-- 这里的id为特定值-->
<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
<!-- 定义最大上传文件大小 10G
1GB=1024MB,1MB=1024KB,1KB=1024B,1B(字节)=8bits(比特) -->
<property name="maxUploadSize" value="104857600" />
</bean>
</beans>
web.xml配置
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/javaee"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
id="WebApp_ID" version="3.0">
<!-- 编码格式 characterencodingfilter-->
<filter>
<filter-name>CharacterEncodingFilter</filter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>utf-8</param-value>
</init-param>
<!-- 请求的编码-->
<init-param>
<param-name>forceRequestEncoding</param-name>
<param-value>true</param-value>
</init-param>
<!-- 响应的编码-->
<init-param>
<param-name>forceResponseEncoding</param-name>
<param-value>true</param-value>
</init-param>
</filter>
<!-- 访问路径-->
<filter-mapping>
<filter-name>CharacterEncodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<!-- 监听-->
<!--配置一个核心监听器,当tomcat(web容器,应用服务器,web服务器)启动的时候创建spring 工厂类对象,绑定到tomcat上-->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!-- 设置全局初始化参数-->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring_core.xml</param-value>
</context-param>
<!--配置前端控制器springMVC-->
<servlet>
<servlet-name>spring-mvc</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<!--指定spring mvc 主配置文件的位置和名称-->
<!-- 设置全局参数-->
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring_mvc.xml</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>spring-mvc</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<!-- 配置请求方式-->
<filter>
<filter-name>HiddenHttpMethodFilter</filter-name>
<filter-class>org.springframework.web.filter.HiddenHttpMethodFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>HiddenHttpMethodFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
</web-app>