项目介绍
- 技术选型:
SSM+Maven+SpringSecurity
后台管理项目
项目源码
链接: 源码及sql文件 提取码: x3wd 复
后文不用看,留个自我纪念
Domain层
- 用于将前台页面传来的字符串转换为Date类型
@DateTimeFormat(pattern="yyyy-MM-dd HH:mm")
private Date departureTime; // 出发时间
- 某些表示状态的字段可以使用int类型,然后在String类型取值(get方法时)时进行赋值处理
private String id;
private String orderNum;
private Date orderTime;
private String orderTimeStr;//时间Date转String类型
private int orderStatus;
private String orderStatusStr;//订单状态(0 未支付 1 已支付)
private int peopleCount;
private Product product;
private List<Traveller> travellers;
private Member member;
private Integer payType;
private String payTypeStr;//支付方式(0 支付宝 1 微信 2其它)
private String orderDesc;
//eg:使用工具类完成Date到String的转换
public String getOrderTimeStr() {
if (orderTime!=null){
orderTimeStr=DateUtil.datetoString(orderTime,"yyyy-MM-dd HH:mm");
}
return orderTimeStr;
}
public String getPayTypeStr() {
if (payType==0){
payTypeStr="支付宝";
}else if (payType==1){
payTypeStr="微信";
}else if (payType==2){
payTypeStr="其它";
}
return payTypeStr;
}
- 转换完成后存入ModleandView后 在jsp页面中使用转换后的数据
Controller层
jsp页面
Dao层
- 多表关联
@Select("select * from orders")
//多表关联时的配置
@Results({
@Result(id = true, property = "id", column = "id"),
@Result(property = "orderNum", column = "orderNum"),
@Result(property = "orderTime", column = "orderTime"),
@Result(property = "orderStatus", column = "orderStatus"),
@Result(property = "peopleCount", column = "peopleCount"),
@Result(property = "payType", column = "payType"),
@Result(property = "orderDesc", column = "orderDesc"),
@Result(property = "product",column = "productId",javaType = Product.class,one = @One(select="yh.dao.IProductDao.findById"))
})
List<Orders> findAll() throws Exception;
//one中select的值为包名+类名+方法名
package yh.dao;
public interface IProductDao {
//根据id查询单个产品
@Select("select * from product where id=#{id}")
public Product findById(String id);
日志
日志切面
有些方法失败未找到原因
org.springframework.web.util.NestedServletException: Request processing failed; nested exception is java.lang.NullPointerException
org.springframework.web.util.NestedServletException: Request processing failed; nested exception is java.lang.NoSuchMethodException: yh.controller.IRoleController.findAll()
日志信息都查询的到
问题解决
Controller层方法未添加public修饰符
没有public修饰符时使用getDeclaredMethod方法来获取方法对象
- 获取ip时需要使用request对象:
package yh.controller;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.context.SecurityContext;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.User;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.RequestMapping;
import yh.domain.SysLog;
import yh.service.ISysLogService;
import javax.servlet.http.HttpServletRequest;
import java.lang.reflect.Method;
import java.util.Date;
//切面类处理日志
@Component//注册Bean
@Aspect
public class LogAop {
@Autowired
private HttpServletRequest request;
@Autowired
private ISysLogService sysLogService;
//访问时间
private Date startTime;
//访问的类
private Class cls;
//访问的方法
private Method method;
@Pointcut("execution(* yh.controller.*.*(..))")
private void myPointCut() {
}
// 主要获取访问时间、访问的类、访问的方法
@Before("myPointCut()")//在controller包下的方法执行前执行
public void doBefore(JoinPoint joinPoint) throws Exception {
System.out.println("前置");
startTime = new Date();//获取访问时间
//获取访问的类
cls = joinPoint.getTarget().getClass();
//获取访问的方法名
String methodName = joinPoint.getSignature().getName();
//考虑方法是否有参数
Object[] args = joinPoint.getArgs();//获取方法参数
//1.无参数
if (args == null || args.length == 0) {
method = cls.getMethod(methodName);
} else {
//2.有参数
//就将args中所有元素遍历,获取对应的Class,装入到Class[]数组
Class[] classArgs = new Class[args.length];
for (int i = 0; i < args.length; i++) {
classArgs[i] = args[i].getClass();
}
method = cls.getMethod(methodName, classArgs);
}
}
//主要获取日志中其它信息,时长、ip、url...
@After("myPointCut()")//在controller包下的方法执行后执行
public void doAfter(JoinPoint joinPoint) throws Exception {
System.out.println("后置");
//获取url,即RequestMapping注解的value值
//1.获取类上的@RequestMapping对象
if (cls != SysLogController.class) {
RequestMapping classAnnotation = (RequestMapping) cls.getAnnotation(RequestMapping.class);
if (classAnnotation != null) {
//2.获取方法上的@RequestMapping对象
RequestMapping methodAnnotation = (RequestMapping) method.getAnnotation(RequestMapping.class);
if (methodAnnotation != null) {
String url = "";//值为类注解value+方法注解的value
url = classAnnotation.value()[0] + methodAnnotation.value()[0];
//获取到信息后,创建日志对象
SysLog sysLog = new SysLog();
//获取访问时长--差值
long executionTime = new Date().getTime() - startTime.getTime();
//封装日志对象的属性
sysLog.setExecutionTime(executionTime);
sysLog.setUrl(url);
//获取ip
String ip=request.getRemoteAddr();
sysLog.setIp(ip);
//获取操作对象的用户名--通过SpringSecurity框架控制的
SecurityContext context = SecurityContextHolder.getContext();
//或者通过request对象获取
//SecurityContext context= request.getSession().getAttribute("SPRING_SECURITY_CONTEXT");
String username = ((User) (context.getAuthentication().getPrincipal())).getUsername();
sysLog.setUsername(username);
String methodN = "[类名]" + cls.getName() + "[方法名]" + method.getName();
sysLog.setMethod(methodN);
sysLog.setVisitTime(startTime);
//调用service层
sysLogService.save(sysLog);
}
}
}
}
}
package yh.controller;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.context.SecurityContext;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.User;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.RequestMapping;
import yh.domain.SysLog;
import yh.service.ISysLogService;
import javax.servlet.http.HttpServletRequest;
import java.lang.reflect.Method;
import java.util.Date;
//切面类处理日志
/*未找到原因先不适用AOP功能*/
@Component//注册Bean
@Aspect
public class LogAop {
@Autowired
private HttpServletRequest request;
@Autowired
private ISysLogService sysLogService;
//访问时间
private Date startTime;
//访问的类
private Class cls;
//访问的方法
private Method method;
@Pointcut("execution(* yh.controller.*.*(..))")
private void myPointCut() {
}
// 主要获取访问时间、访问的类、访问的方法
@Before("myPointCut()")//在controller包下的方法执行前执行
public void doBefore(JoinPoint joinPoint) throws NoSuchMethodException,NullPointerException {
System.out.println("前置");
startTime = new Date();//获取访问时间
//获取访问的类
cls = joinPoint.getTarget().getClass();
//获取访问的方法名
String methodName = joinPoint.getSignature().getName();
//考虑方法是否有参数
Object[] args = joinPoint.getArgs();//获取方法参数
//1.无参数
if (args == null || args.length == 0) {
//method = cls.getDeclaredMethod(methodName);//此方法用于控制层没有public修饰符
method = cls.getMethod(methodName);
} else {
//2.有参数
//就将args中所有元素遍历,获取对应的Class,装入到Class[]数组
Class[] classArgs = new Class[args.length];
for (int i = 0; i < args.length; i++) {
classArgs[i] = args[i].getClass();
}
method = cls.getMethod(methodName, classArgs);
}
}
//主要获取日志中其它信息,时长、ip、url...
@After("myPointCut()")//在controller包下的方法执行后执行
public void doAfter(JoinPoint joinPoint) throws NoSuchMethodException,NullPointerException {
System.out.println("后置");
//获取url,即RequestMapping注解的value值
//1.获取类上的@RequestMapping对象
if (cls != SysLogController.class) {
RequestMapping classAnnotation = (RequestMapping) cls.getAnnotation(RequestMapping.class);
if (classAnnotation != null) {
//2.获取方法上的@RequestMapping对象
RequestMapping methodAnnotation = method.getAnnotation(RequestMapping.class);
if (methodAnnotation != null) {
String url = "";//值为类注解value+方法注解的value
url = classAnnotation.value()[0] + methodAnnotation.value()[0];
//获取到信息后,创建日志对象
SysLog sysLog = new SysLog();
//获取访问时长--差值
long executionTime = new Date().getTime() - startTime.getTime();
//封装日志对象的属性
sysLog.setExecutionTime(executionTime);
sysLog.setUrl(url);
//获取ip
String ip=request.getRemoteAddr();
sysLog.setIp(ip);
//获取操作对象的用户名--通过SpringSecurity框架控制的
SecurityContext context = SecurityContextHolder.getContext();
//或者通过request对象获取
//SecurityContext context= request.getSession().getAttribute("SPRING_SECURITY_CONTEXT");
String username = ((User) (context.getAuthentication().getPrincipal())).getUsername();
sysLog.setUsername(username);
String methodN = "[类名]" + cls.getName() + "[方法名]" + method.getName();
sysLog.setMethod(methodN);
sysLog.setVisitTime(startTime);
//调用service层
try {
sysLogService.save(sysLog);
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
}
}
<listener>
<listener-class>org.springframework.web.context.request.RequestContextListener</listener-class>
</listener>