登录授权认证(2)(mybatis+spring)
作者:互联网
mybatis+spring环境搭建
里边添加了事务管理器,如果事务出错,则进行回滚等操作(这种方式是spring集成的,mybatis中可以使用过滤器进行事务的管理,后边springmvc中还有拦截器也是类似的功能)
<?xml version="1.0" encoding="UTF-8"?>
<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" default-autowire="byName">
<!--Impl包注解扫描 -->
<context:component-scan base-package="cn.wit.serviceImpl"></context:component-scan>
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver"></property>
<property name="url" value="jdbc:mysql://localhost:3306/wit"></property>
<property name="username" value="root"></property>
<property name="password" value="wityy"></property>
</bean>
<bean id="factory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource"></property>
<!--设置简写 -->
<property name="typeAliasesPackage" value="cn.wit.pojo"></property>
</bean>
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<!--扫描mapper包 -->
<property name="basePackage" value="cn.wit.mapper" ></property>
<property name="sqlSessionFactoryBeanName" value="factory"></property>
</bean>
<!--事务管理器 -->
<bean id="txManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"></property>
</bean>
<tx:advice id="txAdvice" transaction-manager="txManager">
<tx:attributes>
<tx:method name="ins*"/>
<tx:method name="del*"/>
<tx:method name="sel*"/>
<tx:method name="upd*"/>
</tx:attributes>
</tx:advice>
<aop:config>
<aop:pointcut expression="execution(* cn.wit.serviceImpl.*.*(..))" id="mypoint"/>
<aop:advisor advice-ref="txAdvice" pointcut-ref="mypoint"/>
</aop:config>
<aop:aspectj-autoproxy proxy-target-class="true"/>
</beans>
数据库设计
使用rbac思想进行数据库设计,即 用户-角色-功能,下面的car表为功能,这里用户和角色采用一对一的设计,users表里边除了id 登录名和密码之外有外键rid(用户对应的角色id),role里边独善其身,不与其他表产生直接关联,角色id和角色名name,car里边有它自己的属性(id name price slogan),role_car将car与role产生关联,id rid cid
采用上面的这种设计,当需要拿到登录用户的所有car时,通过登录后的rid在role_car表中进行查询,找出对应的car,然后将role_car和car两表联合,得出该用户所有的car数据
mapper事务
登录认证事务(直接用注解解决了)
package cn.wit.mapper;
import org.apache.ibatis.annotations.Select;
import cn.wit.pojo.Users;
public interface UsersMapper {
@Select("select *from users where username=#{username} and password=#{password}")
Users selUsers(Users users);
}
授权事务(使用mapper.xml文件需要导入dtd)
package cn.wit.mapper;
import java.util.List;
import cn.wit.pojo.Car;
import cn.wit.pojo.Users;
public interface CarMapper {
List<Car> selCar(Users users);
}
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="cn.wit.mapper.CarMapper">
<select id="selCar" parameterType="users" resultType="car">
select c.*,rc.rid from role_car rc
join car c on rc.cid=c.id
where rid=#{rid}
</select>
</mapper>
Service
通过Resource注解注入mapper
package cn.wit.serviceImpl;
import java.util.List;
import javax.annotation.Resource;
import org.springframework.stereotype.Service;
import cn.wit.mapper.CarMapper;
import cn.wit.mapper.UsersMapper;
import cn.wit.pojo.Car;
import cn.wit.pojo.Users;
import cn.wit.service.LoginService;
@Service
public class LoginServiceImpl implements LoginService{
@Resource
UsersMapper usersMapper;
@Resource
CarMapper carMapper;
@Override
public Users login(Users users) {
return usersMapper.selByUsers(users);
}
@Override
public List<Car> getCars(int rid) {
return carMapper.selCars(rid);
}
}
Servlet
package cn.wit.servlet;
import java.io.IOException;
import java.util.List;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import org.springframework.context.ApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;
import cn.wit.pojo.Car;
import cn.wit.pojo.Users;
import cn.wit.service.LoginService;
import cn.wit.serviceImpl.LoginServiceImpl;
/**
* Servlet implementation class LoginServlet
*/
@WebServlet("/login")
public class LoginServlet extends HttpServlet {
LoginService ls;
@Override
public void init() throws ServletException {
ApplicationContext ac = WebApplicationContextUtils.getWebApplicationContext(getServletContext());
ls= ac.getBean("loginServiceImpl",LoginServiceImpl.class);
}
@Override
protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
req.setCharacterEncoding("utf-8");
String username = req.getParameter("username");
String password = req.getParameter("password");
Users users=new Users(username,password);
Users login = ls.login(users);
if(login!=null){
List<Car> cars = ls.getCars(login.getRid());
System.out.println(cars);
HttpSession session = req.getSession();
session.setAttribute("cars", cars);
resp.sendRedirect("/car3/main.jsp");
}else{
resp.sendRedirect("/car3/login.jsp?error=yes");
}
}
}
视图
登录 login.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<script type="text/javascript">
var errori ='<%=request.getParameter("error")%>';
if(errori=='yes'){
alert("账号或密码错误!");
}
</script>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<form action="login" method="post" >
账号<input type="text" name="username"> <br>
密码<input type="text" name="password"> <br>
<input type="submit" value="登陆">
</form>
</body>
</html>
主页 main.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<table border="1px">
<tr>
<th>名字</th>
<th>价格</th>
<th>宣传语</th>
</tr>
<c:forEach items="${cars}" var="car">
<tr>
<td>${car.name }</td>
<td>${car.price }</td>
<td>${car.slogan }</td>
</tr>
</c:forEach>
</table>
</body>
</html>
标签:cn,spring,认证,wit,car,mybatis,import,login,Users 来源: https://blog.csdn.net/WA_MC/article/details/114437559