其他分享
首页 > 其他分享> > 登录认证授权(3)(mybatis+spring+springmvc)

登录认证授权(3)(mybatis+spring+springmvc)

作者:互联网

环境搭建

mybatis+spring搭建

applicationContext.xml,和上一篇文章不同的是 弄了一个配置文件(数据库配置信息)

jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/wit
jdbc.username=root
jdbc.password=wityy
<?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">
        
        <context:component-scan base-package="cn.wit.serviceImpl"></context:component-scan>
        <context:property-placeholder location="classpath:db.properties"/>
        <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
        	<property name="driverClassName" value="${jdbc.driver}"></property>
        	<property name="url" value="${jdbc.url}"></property>
        	<property name="username" value="${jdbc.username}"></property>
        	<property name="password" value="${jdbc.password}"></property>
        </bean>
        
        
        <bean id="factory" class="org.mybatis.spring.SqlSessionFactoryBean">
        	<!--设置简写  -->
        	<property name="typeAliasesPackage" value="cn.wit.pojo"></property>
        	<property name="dataSource" ref="dataSource"></property>
        </bean>
        
        <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        	<property name="sqlSessionFactoryBeanName" value="factory"></property>
        	<property name="basePackage" value="cn.wit.mapper"></property>
        </bean>
        
        
        <bean id="txManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"></bean>
        <aop:config>
        	<aop:pointcut expression="execution(* cn.wit.serviceImpl.*.*(..))" id="mypoint"/>
        	<aop:advisor advice-ref="txAdvice" pointcut-ref="mypoint"/>
        </aop:config>
        <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>
        
        
</beans>

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: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="cn.wit.controller"></context:component-scan>
	<mvc:annotation-driven></mvc:annotation-driven>
	
	<mvc:resources location="/js/" mapping="/js/**"></mvc:resources>
	<mvc:resources location="/css/" mapping="/css/**"></mvc:resources>
	<mvc:resources location="/images/" mapping="/images/**"></mvc:resources>
	

        
</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);
	}
	
}

controller

注入Impl,RequestMappering注解接受请求,返回值默认是jsp页面

package cn.wit.controller;

import java.util.List;


import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import cn.wit.pojo.Car;
import cn.wit.pojo.Users;
import cn.wit.service.LoginService;

@Controller
public class LoginController {
	@Resource
	private LoginService loginServiceImpl;
	
	@RequestMapping("login")
	public String login(String username,String password,HttpServletRequest req){
		Users users = loginServiceImpl.login(new Users(username,password));
		if(users!=null){
			List<Car> cars = loginServiceImpl.cars(users.getRid());
			HttpSession session = req.getSession();
			session.setAttribute("cars", cars);
			return "redirect:/main.jsp";
		}else{
			return "redirect:/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,springmvc,spring,wit,car,mybatis,import,users,Users
来源: https://blog.csdn.net/WA_MC/article/details/114440052