已經充分封裝DAO層和Service層
Spring4.0.3.RELEASE、Hibernate4.3.5.Final、Mysql
用Maven創建項目,pom.xml如下:
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.lei.demo</groupId> <artifactId>mvc-hibernate4</artifactId> <packaging>war</packaging> <version>0.0.1-SNAPSHOT</version> <name>mvc-hibernate4 Maven Webapp</name> <url>http://maven.apache.org</url> <dependencies> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>3.8.1</version> <scope>test</scope> </dependency> <dependency> <groupId>javax.servlet</groupId> <artifactId>servlet-api</artifactId> <version>2.5</version> </dependency> <dependency> <groupId>jstl</groupId> <artifactId>jstl</artifactId> <version>1.2</version> </dependency> <!-- Spring --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-core</artifactId> <version>4.0.3.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>4.0.3.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-web</artifactId> <version>4.0.3.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-webmvc</artifactId> <version>4.0.3.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-orm</artifactId> <version>4.0.3.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-tx</artifactId> <version>4.0.3.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-test</artifactId> <version>4.0.3.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-jdbc</artifactId> <version>4.0.3.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-expression</artifactId> <version>4.0.3.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-aop</artifactId> <version>4.0.3.RELEASE</version> </dependency> <!-- AspectJ --> <dependency> <groupId>org.aspectj</groupId> <artifactId>aspectjrt</artifactId> <version>1.7.4</version> </dependency> <dependency> <groupId>org.aspectj</groupId> <artifactId>aspectjweaver</artifactId> <version>1.7.4</version> </dependency> <!-- Mysql --> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>5.1.29</version> </dependency> <!-- Hibernate4 --> <dependency> <groupId>org.hibernate</groupId> <artifactId>hibernate-core</artifactId> <version>4.3.5.Final</version> </dependency> <!-- for JPA, use hibernate-entitymanager instead of hibernate-core --> <dependency> <groupId>org.hibernate</groupId> <artifactId>hibernate-entitymanager</artifactId> <version>4.3.5.Final</version> </dependency> <!-- 以下可選 --> <dependency> <groupId>org.hibernate</groupId> <artifactId>hibernate-envers</artifactId> <version>4.3.5.Final</version> </dependency> <dependency> <groupId>org.hibernate</groupId> <artifactId>hibernate-c3p0</artifactId> <version>4.3.5.Final</version> </dependency> <dependency> <groupId>org.hibernate</groupId> <artifactId>hibernate-proxool</artifactId> <version>4.3.5.Final</version> </dependency> <dependency> <groupId>org.hibernate</groupId> <artifactId>hibernate-infinispan</artifactId> <version>4.3.5.Final</version> </dependency> <dependency> <groupId>org.hibernate</groupId> <artifactId>hibernate-ehcache</artifactId> <version>4.3.5.Final</version> </dependency> <dependency> <groupId>org.hibernate</groupId> <artifactId>hibernate-commons-annotations</artifactId> <version>4.0.4.Final</version> </dependency> <dependency> <groupId>org.hibernate.javax.persistence</groupId> <artifactId>hibernate-jpa-2.1-api</artifactId> <version>1.0.0.Final</version> </dependency> <dependency> <groupId>org.jboss</groupId> <artifactId>jandex</artifactId> <version>1.1.0.Final</version> </dependency> <!-- 為了讓Hibernate使用代理模式,需要javassist --> <dependency> <groupId>org.javassist</groupId> <artifactId>javassist</artifactId> <version>3.18.1-GA</version> </dependency> <dependency> <groupId>org.jboss.logging</groupId> <artifactId>jboss-logging</artifactId> <version>3.1.3.GA</version> </dependency> <dependency> <groupId>org.jboss.spec.javax.annotation</groupId> <artifactId>jboss-annotations-api_1.2_spec</artifactId> <version>1.0.0.Final</version> </dependency> <dependency> <groupId>antlr</groupId> <artifactId>antlr</artifactId> <version>2.7.7</version> </dependency> <dependency> <groupId>dom4j</groupId> <artifactId>dom4j</artifactId> <version>1.6.1</version> </dependency> <dependency> <groupId>commons-collections</groupId> <artifactId>commons-collections</artifactId> <version>3.1</version> </dependency> <dependency> <groupId>commons-dbcp</groupId> <artifactId>commons-dbcp</artifactId> <version>1.4</version> </dependency> <dependency> <groupId>commons-pool</groupId> <artifactId>commons-pool</artifactId> <version>1.4</version> </dependency> <dependency> <groupId>javax.transaction</groupId> <artifactId>jta</artifactId> <version>1.1</version> </dependency> <dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-api</artifactId> <version>1.6.1</version> </dependency> <dependency> <groupId>org.hibernate.javax.persistence</groupId> <artifactId>hibernate-jpa-2.0-api</artifactId> <version>1.0.0.Final</version> </dependency> <!-- tomcat7.0.35 數據庫連接池 --> <dependency> <groupId>org.apache.tomcat</groupId> <artifactId>tomcat-dbcp</artifactId> <version>7.0.35</version> </dependency> <dependency> <groupId>com.google.guava</groupId> <artifactId>guava</artifactId> <version>16.0.1</version> </dependency> </dependencies> <build> <finalName>mvc-hibernate4</finalName> </build> </project>View Code
數據庫采用Mysql,新建user表,我們演示操作此表對user進行增刪改查
DROP TABLE IF EXISTS `user`; CREATE TABLE `user` ( `id` int(11) NOT NULL AUTO_INCREMENT, `age` int(11) DEFAULT NULL, `nice_name` varchar(32) DEFAULT NULL, `name` varchar(32) DEFAULT NULL, PRIMARY KEY (`id`) ) ENGINE=InnoDB AUTO_INCREMENT=29 DEFAULT CHARSET=utf8;
在model層下,創建Entity類Users.java見下
package com.lei.demo.model; import java.io.Serializable; import javax.persistence.Column; import javax.persistence.Entity; import javax.persistence.GeneratedValue; import javax.persistence.GenerationType; import javax.persistence.Id; @Entity(name="User") public class User implements Serializable { private static final long serialVersionUID = 1L; public User(){ super(); } @Id @GeneratedValue(strategy=GenerationType.AUTO) @Column(name="id") private Long id; @Column(name="name",length=32) private String name; @Column(name="age") private Integer age; @Column(name="nice_name",length=32) private String nice_name; @Override public String toString() { return "User [id=" + id + ", user_name=" + name + ", age=" + age + ", nice_name=" + nice_name + "]"; } public final Long getId() { return id; } public final void setId(Long id) { this.id = id; } public final String getName() { return name; } public final void setName(String name) { this.name = name; } public final Integer getAge() { return age; } public final void setAge(Integer age) { this.age = age; } public final String getNice_name() { return nice_name; } public final void setNice_name(String nice_name) { this.nice_name = nice_name; } }
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0"> <display-name>Archetype Created Web Application</display-name> <context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:/spring-*.xml</param-value> </context-param> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <!-- 定義DispatcherServlet --> <servlet> <servlet-name>mvc-dispatcher</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <init-param> <!-- 默認/WEB-INF/[servlet名字]-servlet.xml加載上下文, 如果配置了contextConfigLocation參數, 將使用classpath:/mvc-dispatcher-servlet.xml加載上下文 --> <param-name>contextConfigLocation</param-name> <param-value>classpath:/mvc-dispatcher-servlet.xml</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet> <!-- 攔截匹配的請求,這裡所有請求采用名字為mvc-dispatcher的DispatcherServlet處理 --> <servlet-mapping> <servlet-name>mvc-dispatcher</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping> </web-app>
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context" xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:p="http://www.springframework.org/schema/p" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:tx="http://www.springframework.org/schema/tx" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd "> <!-- 啟動自動掃描該包下所有的Bean(例如@Controller) --> <context:component-scan base-package="com.lei.demo" /> <!-- 基於注釋的事務,當注釋中發現@Transactional時,使用id為“transactionManager”的事務管理器 --> <!-- 如果沒有設置transaction-manager的值,則spring以缺省默認的事務管理器來處理事務,默認事務管理器為第一個加載的事務管理器 --> <tx:annotation-driven transaction-manager="transactionManager"/> <!-- 定義視圖解析器 --> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="prefix"> <value>/WEB-INF/jsp/</value> </property> <property name="suffix"> <value>.jsp</value> </property> </bean> </beans>
spring-hibernate.xml文件
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:aop="http://www.springframework.org/schema/aop" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd "> <!-- Hibernate4 --> <!-- 加載資源文件 其中包含變量信息,必須在Spring配置文件的最前面加載,即第一個加載--> <context:property-placeholder location="classpath:persistence-mysql.properties" /> <bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean"> <property name="dataSource" ref="dataSource" /> <property name="packagesToScan"> <list> <!-- 可以加多個包 --> <value>com.lei.demo.model</value> </list> </property> <property name="hibernateProperties"> <props> <prop key="hibernate.dialect">${hibernate.dialect}</prop> <prop key="hibernate.show_sql">${hibernate.show_sql}</prop> </props> </property> </bean> <!-- 數據庫映射 --> <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource"> <property name="driverClassName" value="${jdbc.driverClassName}" /> <property name="url" value="${jdbc.url}" /> <property name="username" value="${jdbc.user}" /> <property name="password" value="${jdbc.pass}" /> </bean> <!-- 配置Hibernate事務管理器 --> <bean id="transactionManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager"> <property name="sessionFactory" ref="sessionFactory" /> </bean> <!-- 配置事務異常封裝 --> <bean id="persistenceExceptionTranslationPostProcessor" class="org.springframework.dao.annotation.PersistenceExceptionTranslationPostProcessor" /> </beans>
資源文件persistence-mysql.properties如下:
# jdbc.X jdbc.driverClassName=com.mysql.jdbc.Driver jdbc.url=jdbc:mysql://192.168.150.63:3306/test?createDatabaseIfNotExist=true jdbc.user=root jdbc.pass=root111111 # hibernate.X hibernate.connection.driverClass=org.gjt.mm.mysql.Driver hibernate.connection.url=jdbc:mysql://192.168.150.63:3306/test hibernate.dialect=org.hibernate.dialect.MySQL5Dialect hibernate.connection.username=root hibernate.connection.password=root111111 hibernate.show_sql=true
此處model層只有一個User類,見前文user.java。
DAO層包括3個包,各個包中類如下:
com.lei.demo.dao.common: IOperations, AbstractHibernateDao
com.lei.demo.dao: IUserDao
com.lei.demo.dao.impl: UserDao
代碼如下:
IOperations.java,通用的操作接口
package com.lei.demo.dao.common; import java.io.Serializable; import java.util.List; /* * 通用的操作接口 */ public interface IOperations<T extends Serializable> { T findOne(final long id); List<T> findAll(); void create(final T entity); T update(final T entity); void delete(final T entity); void deleteById(final long entityId); }
AbstractHibernateDao.java實現了IOperations接口,所有DAO都需要繼承此類,這樣就避免重復的寫一些CURD。
package com.lei.demo.dao.common; import java.io.Serializable; import java.util.List; import javax.annotation.Resource; import org.hibernate.Session; import org.hibernate.SessionFactory; import com.google.common.base.Preconditions; @SuppressWarnings("unchecked") public abstract class AbstractHibernateDao<T extends Serializable> implements IOperations<T> { private Class<T> clazz; @Resource(name="sessionFactory") private SessionFactory sessionFactory; protected final void setClazz(final Class<T> clazzToSet) { this.clazz = Preconditions.checkNotNull(clazzToSet); } protected final Session getCurrentSession() { return sessionFactory.getCurrentSession(); } @Override public final T findOne(final long id) { return (T)getCurrentSession().get(clazz, id); } @Override public final List<T> findAll() { return getCurrentSession().createQuery("from " + clazz.getName()).list(); } @Override public final void create(final T entity) { Preconditions.checkNotNull(entity); // getCurrentSession().persist(entity); getCurrentSession().saveOrUpdate(entity); } @Override public final T update(final T entity) { Preconditions.checkNotNull(entity); getCurrentSession().update(entity); return entity; //return (T)getCurrentSession().merge(entity); } @Override public final void delete(final T entity) { Preconditions.checkNotNull(entity); getCurrentSession().delete(entity); } @Override public final void deleteById(final long entityId) { final T entity = findOne(entityId); Preconditions.checkState(entity != null); delete(entity); } }
IUserDao.java具體的DAO的接口,繼承了IOperations
package com.lei.demo.dao; import com.lei.demo.dao.common.IOperations; import com.lei.demo.model.User; public interface IUserDao extends IOperations<User> { //讓所有的DAO都實現基本的操作接口IOperations //除了實現IOperations中的基本操作之外,特定的DAO要實現其他操作可以在對應的接口DAO中定義方法, //此處UserDao的接口IUserDao不需要實現其他方法 }
UserDao.java具體的DAO實現
package com.lei.demo.dao.impl; import org.springframework.stereotype.Repository; import com.lei.demo.dao.IUserDao; import com.lei.demo.dao.common.AbstractHibernateDao; import com.lei.demo.model.User; @Repository("usersDao") public class UserDao extends AbstractHibernateDao<User> implements IUserDao { public UserDao() { super(); setClazz(User.class); } }
至此,DAO層已經完成。
Service層包括3個包,各個包中類如下:
com.lei.demo.service.common: AbstractService
com.lei.demo.service: IUserService
com.lei.demo.service.impl: UserService
AbstractService.java,實現了IOperations,所有的Service都要繼承此類,這樣就避免了重復實現通用的方法,注意類前的注釋“@Transactional
”,表示其中所有的方法都需要引入事務,代碼如下:
package com.lei.demo.service.common; import java.io.Serializable; import java.util.List; import org.springframework.transaction.annotation.Transactional; import com.lei.demo.dao.common.IOperations; @Transactional public abstract class AbstractService<T extends Serializable> implements IOperations<T> { protected abstract IOperations<T> getDao(); @Override public T findOne(final long id) { return getDao().findOne(id); } @Override public List<T> findAll() { return getDao().findAll(); } @Override public void create(final T entity) { getDao().create(entity); } @Override public T update(final T entity) { return getDao().update(entity); } @Override public void delete(final T entity) { getDao().delete(entity); } @Override public void deleteById(long entityId) { getDao().deleteById(entityId); } }
IUserService.java接口
package com.lei.demo.service;UserService.java,具體的Service實現類就很簡單了,因為大部分通用的方法都在其繼承的虛類AbstractService中實現了,這裡只要注入具體的dao即可:
package com.lei.demo.service.impl; import javax.annotation.Resource; import org.springframework.stereotype.Service; import com.lei.demo.dao.IUserDao; import com.lei.demo.dao.common.IOperations; import com.lei.demo.model.User; import com.lei.demo.service.IUserService; import com.lei.demo.service.common.AbstractService; @Service("userService") public class UserService extends AbstractService<User> implements IUserService { @Resource(name="usersDao") private IUserDao dao; public UserService() { super(); } @Override protected IOperations<User> getDao() { return this.dao; } }
Jsp頁面放在WEB-INF/jsp/user/…下,分別是user的增刪改查頁面,
WEB-INF/jsp/user/add.jsp
WEB-INF/jsp/user/edit.jsp
WEB-INF/jsp/user/list.jsp
WEB-INF/jsp/user/detail.jsp
以下是具體頁面代碼
WEB-INF/jsp/user/add.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>Add User</title> </head> <body> <form action="add" method="post"> Name:<input id="name" name="name" type="text" /> <br> Nice Name:<input id="nice_name" name="nice_name" type="text" /> <br> Age:<input id="age" name="age" type="text" /> <br> <input type="submit" value="提交"> </form> </body> </html>
WEB-INF/jsp/user/edit.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> <%@ taglib prefix="form" uri="http://www.springframework.org/tags/form"%> <!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>Edit user</title> </head> <body> <c:url var="saveUrl" value="/user/save/${userAttribute.id }" /> <form:form modelAttribute="userAttribute" action="${saveUrl }"> <table> <tr> <td>ID:</td> <td><form:input path="id" readonly="true"/></td> </tr> <tr> <td>Name:</td> <td><form:input path="name"/></td> </tr> <tr> <td>Nice name:</td> <td><form:input path="nice_name"/></td> </tr> <tr> <td>Age:</td> <td><form:input path="age"/></td> </tr> </table> <input type="submit" value="Save"> </form:form> </body> </html>
WEB-INF/jsp/user/list.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>User List</title> </head> <body> <a href="add">Add</a> <table> <tr> <td>ID</td> <td>Name</td> <td>NiceName</td> <td>Age</td> </tr> <c:forEach var="user" items="${userList }" > <tr> <td>${user.id }</td> <td>${user.name }</td> <td>${user.nice_name }</td> <td>${user.age }</td> <td><a href="show/${user.id }">詳細</a></td> <td><a href="edit/${user.id }">編輯</a></td> <td><a href="del/${user.id }">刪除</a></td> </tr> </c:forEach> </table> </body> </html>
WEB-INF/jsp/user/detail.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>Show user</title> </head> <body> <c:out value="${user.id }"></c:out> <br> <c:out value="${user.name }"></c:out> <br> <c:out value="${user.nice_name }"></c:out> <br> <c:out value="${user.age }"></c:out> </body> </html>
寫Controller訪問上邊的jsp,UserController.java
package com.lei.demo.controller; import java.util.List; import javax.annotation.Resource; import org.springframework.stereotype.Controller; import org.springframework.ui.Model; import org.springframework.web.bind.annotation.ModelAttribute; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.servlet.ModelAndView; import com.lei.demo.model.User; import com.lei.demo.service.IUserService; @Controller @RequestMapping("/user") public class UserController { @Resource(name="userService") private IUserService userService; @RequestMapping(value="/count",method=RequestMethod.GET) public ModelAndView userCount() { int count = userService.findAll().size(); ModelAndView mv = new ModelAndView(); mv.addObject("userCount", count); mv.setViewName("user/userCount"); return mv; } @RequestMapping(value="/list",method=RequestMethod.GET) public ModelAndView getUserlist(Model model){ ModelAndView mv = new ModelAndView(); List<User> userList = userService.findAll(); System.out.println("log======table 'user' all records:"+userList.size()); mv.addObject("userList", userList); mv.setViewName("user/list"); return mv; } @RequestMapping(value="/add",method=RequestMethod.GET) public ModelAndView getAdd(){ ModelAndView mv = new ModelAndView(); mv.setViewName("user/add"); return mv; } @RequestMapping(value="/add",method=RequestMethod.POST) public String add(@ModelAttribute("user") User user){ userService.create(user); return "redirect:/user/list"; } @RequestMapping(value="/show/{userid}",method=RequestMethod.GET) public ModelAndView show(@PathVariable Long userid){ User user = userService.findOne(userid); ModelAndView mv = new ModelAndView(); mv.addObject("user", user); mv.setViewName("user/detail"); return mv; } @RequestMapping(value="/del/{userid}",method=RequestMethod.GET) public String del(@PathVariable Long userid){ userService.deleteById(userid); return "redirect:/user/list"; } @RequestMapping(value="/edit/{userid}",method=RequestMethod.GET) public ModelAndView getEdit(@PathVariable Long userid,Model model){ User user = userService.findOne(userid); model.addAttribute("userAttribute", user); ModelAndView mv = new ModelAndView(); mv.setViewName("user/edit"); return mv; } @RequestMapping(value="/save/{userid}",method=RequestMethod.POST) public String saveEdit(@ModelAttribute("userAttribute") User user,@PathVariable Long userid){ userService.update(user); return "redirect:/user/list"; } }
Spring中如何配置Hibernate事務 http://www.linuxidc.com/Linux/2013-12/93681.htm
Struts2整合Spring方法及原理 http://www.linuxidc.com/Linux/2013-12/93692.htm
基於 Spring 設計並實現 RESTful Web Services http://www.linuxidc.com/Linux/2013-10/91974.htm
Spring-3.2.4 + Quartz-2.2.0集成實例 http://www.linuxidc.com/Linux/2013-10/91524.htm
使用 Spring 進行單元測試 http://www.linuxidc.com/Linux/2013-09/89913.htm
運用Spring注解實現Netty服務器端UDP應用程序 http://www.linuxidc.com/Linux/2013-09/89780.htm
Spring 3.x 企業應用開發實戰 PDF完整高清掃描版+源代碼 http://www.linuxidc.com/Linux/2013-10/91357.htm
Spring 的詳細介紹:請點這裡
Spring 的下載地址:請點這裡