自定义注解MyBatisRepositors.java
package com.jikexueyuan.annotation;
/**
* MyBatis使用该注解标识DAO
*/
public @interface MyBatisRepository {
}
记录日志的方面组件OperateLogger.java
package com.jikexueyuan.aspect;
import java.text.SimpleDateFormat;
import java.util.Date;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.AfterThrowing;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.springframework.stereotype.Component;
/**
* 用于记录日志的方面组件,演示Spring AOP的各种通知类型。
*/
@Component
@Aspect
public class OperateLogger {
/**
* 前置通知、后置通知、最终通知使用的方法
*/
@Before("within(com.jikexueyuan.controller..*)")
public void log1() {
// 记录日志
System.out.println("-->记录用户操作信息");
}
/**
* 环绕通知使用的方法
*/
@Around("within(com.jikexueyuan.controller..*)")
public Object log2(ProceedingJoinPoint p) throws Throwable {
// 目标组件的类名
String className = p.getTarget().getClass().getName();
// 调用的方法名
String method = p.getSignature().getName();
// 当前系统时间
String date = new SimpleDateFormat(
"yyyy-MM-dd hh:mm:ss").format(new Date());
// 拼日志信息
String msg = "-->用户在" + date + ",执行了"
+ className + "." + method + "()";
// 记录日志
System.out.println(msg);
// 执行目标组件的方法
Object obj = p.proceed();
// 在调用目标组件业务方法后也可以做一些业务处理
System.out.println("-->调用目标组件业务方法后...");
return obj;
}
/**
* 异常通知使用的方法
*/
@AfterThrowing(pointcut="within(com.jikexueyuan.controller..*)",throwing="e")
public void log3(Exception e) {
StackTraceElement[] elems = e.getStackTrace();
// 将异常信息记录
System.out.println("-->" + elems[0].toString());
}
}
控制层
package com.jikexueyuan.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
@Controller
public class IndexController {
/**
* 页面跳转
* @return
*/
@RequestMapping(value="toPoetriesSearch.do")
public String toPoetriesSearch(){
return "poetriesSearch";
}
@RequestMapping(value="toPoetsSearch.do")
public String toPoetsSearch(){
return "poetsSearch";
}
}
package com.jikexueyuan.controller;
import javax.annotation.Resource;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.alibaba.fastjson.JSONObject;
import com.jikexueyuan.entity.PageModel;
import com.jikexueyuan.entity.Poetries;
import com.jikexueyuan.service.PoetriesService;
@Controller
public class PoeController{
@Resource
private PoetriesService poetriesService;
/**
* 用题目或名句搜索
* @param pageModel
* @param poetries
* @return
*/
@RequestMapping(value ="listPoeWithPageForJson.do")
@ResponseBody
public Object listPoeWithPageForJson(PageModel<Poetries> pageModel,Poetries poetries) {
Object jsonObject = null;
try {
pageModel.setQueryObj(poetries);
poetriesService.listPoeWithPage(pageModel);
jsonObject = JSONObject.toJSON(pageModel);
} catch (Exception e) {
e.printStackTrace();
}
return jsonObject;
}
}
package com.jikexueyuan.controller;
import javax.annotation.Resource;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.alibaba.fastjson.JSONObject;
import com.jikexueyuan.entity.PageModel;
import com.jikexueyuan.entity.Poets;
import com.jikexueyuan.service.PoetsService;
@Controller
public class PoetsController {
@Resource
private PoetsService poetsService;
/**
* 诗人名搜索诗句
* @param pageModel
* @param poets
* @return
*/
@RequestMapping(value ="listByNameWithPageForJson.do")
@ResponseBody
public Object listByNameWithPageForJson(PageModel<Poets> pageModel,Poets poets) {
Object jsonObject = null;
try {
pageModel.setQueryObj(poets);
poetsService.listByName(pageModel);
jsonObject = JSONObject.toJSON(pageModel);
} catch (Exception e) {
e.printStackTrace();
}
return jsonObject;
}
}