web.xml文件
/p>
"-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
"http://java.sun.com/dtd/web-app_2_3.dtd" >
finishFight
/WEB-INF/login.html
com.hp.listener.LoginSessionListner
15
characterEncodingFilter
org.springframework.web.filter.CharacterEncodingFilter
encoding
UTF-8
forceEncoding
true
characterEncodingFilter
/*
springMVC
org.springframework.web.servlet.DispatcherServlet
contextConfigLocation
classpath:config/spring-mvc.xml
springMVC
/
default
*.js
*.css
*.png
*.jpg
*.svg
*.ttf
*.woff
*.woff2
*.eot
*.gif
*.html
用来处理Session的.Java文件
package com.hp.listener;
import javax.servlet.http.HttpSessionEvent;
import javax.servlet.http.HttpSessionListener;
import java.util.HashMap;
import java.util.Map;
/**
* @program: finishFight
* @description: 里面的两个map实现了单点登录
* @author: li hui
* @create: 2020-11-23 16:23
*/
public class LoginSessionListner implements HttpSessionListener {
public static Map sessionMap = new HashMap();
public static Map map = new HashMap();
/**
* @Author: li hui
* @Param: [httpSessionEvent]
* @return: void
* @date: 2020/11/23
* @Description: 本项目中,每次创建session都会执行一次sessionCreated
*/
@Override
public void sessionCreated(HttpSessionEvent httpSessionEvent) {
System.out.println("创建一个session,这个sessionID为:"+httpSessionEvent.getSession().getId());
sessionMap.put(httpSessionEvent.getSession().getId(),httpSessionEvent.getSession());
}
/**
* @Author: li hui
* @Param: [httpSessionEvent]
* @return: void
* @date: 2020/11/23
* @Description: 本项目中,每次销毁session都会执行一次sessionDestroyed
*/
@Override
public void sessionDestroyed(HttpSessionEvent httpSessionEvent) {
System.out.println("销毁一个session,这个sessionID为:"+httpSessionEvent.getSession().getId());
sessionMap.remove(httpSessionEvent.getSession().getId());
//lambda 表达式
/*map.forEach((key, value) -> {
System.out.println("key:"+key+" "+"value:"+value);
if(value.equals(httpSessionEvent.getSession().getId())) {
System.out.println("key:"+key+"---------"+"value:"+value);
map.remove(key);
}
});*/
}
}
spring-mvc.xml文件
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:context="http://www.springframework.org/schema/context"
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">
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
spring-mvc.xml引入的Redis的配置文件(redis.properties)
#redis的服务器地址
redis.host=127.0.0.1
#redis的服务端口
redis.port=6379
#密码
redis.pass=root
#链接数据库
redis.default.db=0
#客户端超时时间单位是毫秒
redis.timeout=100000
#最大连接数
redis.maxActive=300
#最大空闲数
redis.maxIdle=100
#最大建立连接等待时间
redis.maxWait=1000
#指明是否在从池中取出连接前进行检验,如果检验失败,则从池中去除连接并尝试取出另一个
#DBSync.testOnBorrow=true
redis.clientName=local
springMVC的拦截的.Java文件package com.hp.interceptor;
import com.hp.util.RedisUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.Map;
/**
* @program: finishFight
* @description:
* @author: li hui
* @create: 2020-11-17 18:12
*/
public class LoginInterceptor implements HandlerInterceptor{
@Autowired
private RedisUtil redisUtil;
@Override
public boolean preHandle(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o) throws Exception {
String username = (String) httpServletRequest.getSession().getAttribute("username");
String session = httpServletRequest.getSession().getId();
if(username!=null){
redisUtil.set(username,session,60*15);
return true;
}else{
httpServletResponse.sendRedirect("/login");
return false;
}
}
@Override
public void postHandle(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, ModelAndView modelAndView) throws Exception {
}
@Override
public void afterCompletion(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, Exception e) throws Exception {
}
}
mybatis-config.xml文件
/p>
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd">
mybatis的插件的.Java文件
package com.hp.interceptor;
import com.hp.util.ReflectHelper;
import org.apache.ibatis.executor.statement.BaseStatementHandler;
import org.apache.ibatis.executor.statement.RoutingStatementHandler;
import org.apache.ibatis.executor.statement.StatementHandler;
import org.apache.ibatis.mapping.BoundSql;
import org.apache.ibatis.mapping.MappedStatement;
import org.apache.ibatis.plugin.*;
import java.sql.Connection;
import java.util.Map;
import java.util.Properties;
/**
* @program: finishFight
* @description: 用到了一个类 (ReflectHelper类)
* @author: li hui
* @create: 2020-11-17 16:39
*/
@Intercepts({
@Signature(type = StatementHandler.class, method = "prepare", args = {Connection.class, Integer.class})
})
public class MybatisInterceptor implements Interceptor {
@Override
public Object intercept(Invocation invocation) throws Throwable {
RoutingStatementHandler rsh=(RoutingStatementHandler)invocation.getTarget();
BaseStatementHandler delegate = (BaseStatementHandler) ReflectHelper.getValueByFieldName(rsh, "delegate");
MappedStatement mappedStatement = (MappedStatement) ReflectHelper.getValueByFieldName(delegate, "mappedStatement");
String id=mappedStatement.getId().toString();
BoundSql boundSql = delegate.getBoundSql();
Map map = (Map) rsh.getParameterHandler().getParameterObject();
if(id.substring(id.lastIndexOf(".")+1, id.length()).equals("query")) {
Integer limit = (Integer.valueOf(map.get("limit")==null ? "10" : map.get("limit").toString()));
Integer page = (Integer.valueOf(map.get("page")==null ? "1" : map.get("page").toString())-1)*limit;
ReflectHelper.setValueByFieldName(boundSql, "sql", rsh.getBoundSql().getSql()+ " limit "+page+","+limit);
}
//System.out.println("这是我获取的xml执行的id:"+id);
//System.out.println("这是传过来的参数:"+boundSql.getParameterObject());
//System.out.println("执行的sql:"+rsh.getBoundSql().getSql().replace("\n"," ").replace("\t"," ").replaceAll("\\s{1,}", " "));
return invocation.proceed();
}
@Override
public Object plugin(Object o) {
return Plugin.wrap(o,this);
}
@Override
public void setProperties(Properties properties) {
}
}
ReflectHelper类的.java文件
package com.hp.util;
import java.lang.reflect.Field;
/**
* 反射工具
* @version
*/
public class ReflectHelper {
/**
* @param obj
* @param fieldName
* @return
*获取需要反射的类
*/
public static Field getFieldByFieldName(Object obj, String fieldName) {
for (Class> superClass = obj.getClass(); superClass != Object.class; superClass = superClass.getSuperclass()) {
try {
return superClass.getDeclaredField(fieldName);
} catch (NoSuchFieldException e) {
}
}
return null;
}
/**
* @param obj
* @param fieldName
* @return
* @throws SecurityException
* @throws NoSuchFieldException
* @throws IllegalArgumentException
* @throws IllegalAccessException
* 获取某个参数的内容
*/
public static Object getValueByFieldName(Object obj, String fieldName)
throws SecurityException, NoSuchFieldException,
IllegalArgumentException, IllegalAccessException {
Field field = getFieldByFieldName(obj, fieldName);
Object value = null;
if(field!=null){
if (field.isAccessible()) {
value = field.get(obj);
} else {
field.setAccessible(true);
value = field.get(obj);
field.setAccessible(false);
}
}
return value;
}
/**
* @param obj
* @param fieldName
* @param value
* @throws SecurityException
* @throws NoSuchFieldException
* @throws IllegalArgumentException
* @throws IllegalAccessException
* 改变需要改变的内容
*/
public static void setValueByFieldName(Object obj, String fieldName,
Object value) throws SecurityException, NoSuchFieldException,
IllegalArgumentException, IllegalAccessException {
Field field = obj.getClass().getDeclaredField(fieldName);
if (field.isAccessible()) {
field.set(obj, value);
} else {
field.setAccessible(true);
field.set(obj, value);
field.setAccessible(false);
}
}
}
mybatis的mapper文件的配置
/p>
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
user
id,
username,
password,
phone,
sex
#{item.id},
#{item.username},
#{item.password},
#{item.phone},
#{item.sex},
select
from
${key} = #{val}
select
from
username like '%${like}%'
insert into
${key}
values
#{val}
insert into
()
values
()
update
${key} = #{val}
where id = #{map.id}
delete
from
where id=#{id}
delete
from
id in
#{item}
select
count(*) count
from
username like '%${like}%'