老样子先数据库中反射Cat.java
1 package com.hpe.bean; 2 // Generated 2016-11-18 11:07:54 by Hibernate Tools 3.5.0.Final 3 4 import javax.persistence.Column; 5 import javax.persistence.Entity; 6 import javax.persistence.GeneratedValue; 7 import static javax.persistence.GenerationType.IDENTITY; 8 import javax.persistence.Id; 9 import javax.persistence.Table; 10 11 /** 12 * Cat generated by hbm2java 13 */ 14 @Entity 15 @Table(name = "cat", catalog = "java4") 16 public class Cat implements java.io.Serializable { 17 18 private Integer id; 19 private String sex; 20 private String name; 21 private String pinzhong; 22 private Double price; 23 private Integer age; 24 25 public Cat() { 26 } 27 28 public Cat(String sex, String name, String pinzhong, Double price, Integer age) { 29 this.sex = sex; 30 this.name = name; 31 this.pinzhong = pinzhong; 32 this.price = price; 33 this.age = age; 34 } 35 36 @Id 37 @GeneratedValue(strategy = IDENTITY) 38 39 @Column(name = "ID", unique = true, nullable = false) 40 public Integer getId() { 41 return this.id; 42 } 43 44 public void setId(Integer id) { 45 this.id = id; 46 } 47 48 @Column(name = "SEX") 49 public String getSex() { 50 return this.sex; 51 } 52 53 public void setSex(String sex) { 54 this.sex = sex; 55 } 56 57 @Column(name = "NAME") 58 public String getName() { 59 return this.name; 60 } 61 62 public void setName(String name) { 63 this.name = name; 64 } 65 66 @Column(name = "PINZHONG") 67 public String getPinzhong() { 68 return this.pinzhong; 69 } 70 71 public void setPinzhong(String pinzhong) { 72 this.pinzhong = pinzhong; 73 } 74 75 @Column(name = "PRICE", precision = 22, scale = 0) 76 public Double getPrice() { 77 return this.price; 78 } 79 80 public void setPrice(Double price) { 81 this.price = price; 82 } 83 84 @Column(name = "AGE") 85 public Integer getAge() { 86 return this.age; 87 } 88 89 public void setAge(Integer age) { 90 this.age = age; 91 } 92 93 }
dao层CatDao.java
1 package com.hpe.dao; 2 3 import org.hibernate.Session; 4 import org.hibernate.SessionFactory; 5 import org.springframework.beans.factory.annotation.Autowired; 6 import org.springframework.stereotype.Repository; 7 8 import com.hpe.bean.Cat; 9 10 @Repository 11 public class CatDao { 12 13 @Autowired 14 private SessionFactory sf; 15 16 public int savaCat(Cat cat){ 17 Session session = sf.openSession(); 18 int i = (int) session.save(cat); 19 return i; 20 } 21 22 }
service层CatService.java
1 package com.hpe.service; 2 3 import org.springframework.beans.factory.annotation.Autowired; 4 import org.springframework.stereotype.Service; 5 6 import com.hpe.bean.Cat; 7 import com.hpe.dao.CatDao; 8 9 @Service 10 public class CatService { 11 12 @Autowired 13 private CatDao cd; 14 15 public boolean Cservice(Cat cat){ 16 int i = cd.savaCat(cat); 17 if(i>0){ 18 return true; 19 } 20 return false; 21 } 22 }
Controller层CatController.java
1 package com.hpe.controller; 2 3 import org.springframework.beans.factory.annotation.Autowired; 4 import org.springframework.stereotype.Controller; 5 import org.springframework.web.bind.annotation.RequestMapping; 6 import org.springframework.web.bind.annotation.RequestMethod; 7 8 import com.hpe.bean.Cat; 9 import com.hpe.service.CatService; 10 11 @Controller 12 public class CatController { 13 14 @Autowired 15 private CatService cs; 16 17 @RequestMapping(value="/cat",method=RequestMethod.POST) 18 public String getCat(Cat cat){ 19 boolean b = cs.Cservice(cat); 20 if(b){ 21 return "success"; 22 } 23 return "cat"; 24 } 25 }
在前面的配置文件加了springmvc切面所以这边来测试下TestAdvice.java
1 package com.hpe.advice; 2 3 import org.aspectj.lang.JoinPoint; 4 import org.aspectj.lang.ProceedingJoinPoint; 5 6 public class TestAdvice { 7 8 // 前置通知 9 public void before(JoinPoint point) { 10 11 System.out.println(point.getTarget() + "开始进行log记录---"); 12 System.out.println(point.getSignature().getName() + ":方法开始执行"); 13 Object obj[] = point.getArgs(); 14 if (obj != null) { 15 for (Object objs : obj) { 16 System.out.println(objs); 17 } 18 } 19 } 20 21 // 后通知 22 public void after(JoinPoint point) { 23 System.out.println(point.getTarget() + "方法执行结束----"); 24 25 } 26 27 // 返回后通知 28 public void afterReturn(JoinPoint point, Object ret) { 29 System.out.println(point.getTarget() + "方法执行后的结果为" + ret); 30 } 31 32 // 抛出异常后通知 33 public void afterThrow(JoinPoint point, Object ex) { 34 System.out.println("异常为" + ex); 35 36 } 37 38 // 环绕通知必须又一个参数ProceedingJoinpoint和一个返回值类型 39 public Object around(ProceedingJoinPoint pjp) { 40 Object obj = null; 41 42 try { 43 // 前置通知 44 System.out.println(pjp.getTarget() + "前置通知开始执行"); 45 obj = pjp.proceed(); 46 // 返回通知 47 System.out.println(pjp.getSignature().getName() + "方法返回执行结果为:" + obj); 48 } catch (Throwable e) { 49 50 e.printStackTrace(); 51 System.out.println("异常通知开始执行"); 52 } 53 // 后置通知 54 System.out.println(pjp.getTarget() + "后置通知-----"); 55 56 return obj; 57 58 } 59 60 }
前台页面cat.jsp
1 <%@ page language="java" contentType="text/html; charset=utf-8" 2 pageEncoding="utf-8"%> 3 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> 4 <html> 5 <head> 6 <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> 7 <head> 8 <title>Insert title here</title> 9 </head> 10 <body> 11 <FORM action="../cat" method="post"> 12 性别:<INPUT type="text" name="sex" /><BR /> 13 名字:<INPUT type="text" name="name" /><BR /> 14 品种:<INPUT type="text" name="pinzhong" /><BR /> 15 价格:<INPUT type="text" name="price" /><BR /> 16 年龄:<INPUT type="text" name="age" /><BR /> 17 <INPUT type="submit" value="注册"/> 18 19 </FORM> 20 21 </body> 22 </html>
success.jsp
<?xml version="1.0" encoding="UTF-8" ?> <jsp:root xmlns:jsp="http://java.sun.com/JSP/Page" version="2.0"> <jsp:directive.page contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" session="false"/> <jsp:output doctype-root-element="html" doctype-public="-//W3C//DTD XHTML 1.0 Transitional//EN" doctype-system="http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd" omit-xml-declaration="true" /> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <title>Insert title here</title> </head> <body> 注册成功 </body> </html> </jsp:root>
页面展示
控制台打印输出
时间: 2024-11-03 22:34:34