Spring MVC Hibernate MySQL集成CRUD示例教程
在上一个教程中,我们学习了如何集成Spring和Hibernate。
今天,我们将继续前进,并将Spring MVC和Hibernate框架集成到一个Web应用程序CRUD示例中。
注意,在我们的示例中,我使用的是Spring4.0.3.Release
和Hibernate4.3.5.Final
版本,同一程序也与Spring 4和Hibernate 3兼容,但是您需要在Spring bean中进行一些小的更改上一教程中讨论的配置文件。
Maven依赖
让我们看一下Hibernate和Spring MVC框架集成所需的所有maven依赖关系。
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="https://maven.apache.org/POM/4.0.0" xmlns:xsi="https://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="https://maven.apache.org/POM/4.0.0 https://maven.apache.org/maven-v4_0_0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.theitroad.spring</groupId> <artifactId>SpringMVCHibernate</artifactId> <name>SpringMVCHibernate</name> <packaging>war</packaging> <version>1.0.0-BUILD-SNAPSHOT</version> <properties> <java-version>1.6</java-version> <org.springframework-version>4.0.3.RELEASE</org.springframework-version> <org.aspectj-version>1.7.4</org.aspectj-version> <org.slf4j-version>1.7.5</org.slf4j-version> <hibernate.version>4.3.5.Final</hibernate.version> </properties> <dependencies> <!-- Spring --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>${org.springframework-version}</version> <exclusions> <!-- Exclude Commons Logging in favor of SLF4j --> <exclusion> <groupId>commons-logging</groupId> <artifactId>commons-logging</artifactId> </exclusion> </exclusions> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-webmvc</artifactId> <version>${org.springframework-version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-tx</artifactId> <version>${org.springframework-version}</version> </dependency> <!-- Hibernate --> <dependency> <groupId>org.hibernate</groupId> <artifactId>hibernate-core</artifactId> <version>${hibernate.version}</version> </dependency> <dependency> <groupId>org.hibernate</groupId> <artifactId>hibernate-entitymanager</artifactId> <version>${hibernate.version}</version> </dependency> <!-- Apache Commons DBCP --> <dependency> <groupId>commons-dbcp</groupId> <artifactId>commons-dbcp</artifactId> <version>1.4</version> </dependency> <!-- Spring ORM --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-orm</artifactId> <version>${org.springframework-version}</version> </dependency> <!-- AspectJ --> <dependency> <groupId>org.aspectj</groupId> <artifactId>aspectjrt</artifactId> <version>${org.aspectj-version}</version> </dependency> <!-- Logging --> <dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-api</artifactId> <version>${org.slf4j-version}</version> </dependency> <dependency> <groupId>org.slf4j</groupId> <artifactId>jcl-over-slf4j</artifactId> <version>${org.slf4j-version}</version> <scope>runtime</scope> </dependency> <dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-log4j12</artifactId> <version>${org.slf4j-version}</version> <scope>runtime</scope> </dependency> <dependency> <groupId>log4j</groupId> <artifactId>log4j</artifactId> <version>1.2.15</version> <exclusions> <exclusion> <groupId>javax.mail</groupId> <artifactId>mail</artifactId> </exclusion> <exclusion> <groupId>javax.jms</groupId> <artifactId>jms</artifactId> </exclusion> <exclusion> <groupId>com.sun.jdmk</groupId> <artifactId>jmxtools</artifactId> </exclusion> <exclusion> <groupId>com.sun.jmx</groupId> <artifactId>jmxri</artifactId> </exclusion> </exclusions> <scope>runtime</scope> </dependency> <!-- @Inject --> <dependency> <groupId>javax.inject</groupId> <artifactId>javax.inject</artifactId> <version>1</version> </dependency> <!-- Servlet --> <dependency> <groupId>javax.servlet</groupId> <artifactId>servlet-api</artifactId> <version>2.5</version> <scope>provided</scope> </dependency> <dependency> <groupId>javax.servlet.jsp</groupId> <artifactId>jsp-api</artifactId> <version>2.1</version> <scope>provided</scope> </dependency> <dependency> <groupId>javax.servlet</groupId> <artifactId>jstl</artifactId> <version>1.2</version> </dependency> <!-- Test --> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.7</version> <scope>test</scope> </dependency> </dependencies> <build> <plugins> <plugin> <artifactId>maven-eclipse-plugin</artifactId> <version>2.9</version> <configuration> <additionalProjectnatures> <projectnature>org.springframework.ide.eclipse.core.springnature</projectnature> </additionalProjectnatures> <additionalBuildcommands> <buildcommand>org.springframework.ide.eclipse.core.springbuilder</buildcommand> </additionalBuildcommands> <downloadSources>true</downloadSources> <downloadJavadocs>true</downloadJavadocs> </configuration> </plugin> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <version>2.5.1</version> <configuration> <source>1.6</source> <target>1.6</target> <compilerArgument>-Xlint:all</compilerArgument> <showWarnings>true</showWarnings> <showDeprecation>true</showDeprecation> </configuration> </plugin> <plugin> <groupId>org.codehaus.mojo</groupId> <artifactId>exec-maven-plugin</artifactId> <version>1.2.1</version> <configuration> <mainClass>org.test.int1.Main</mainClass> </configuration> </plugin> </plugins> <finalName>${project.artifactId}</finalName> </build> </project>
当我创建Spring MVC项目时,STS(Spring工具套件)包括上述某些依赖项。
上面的重要依赖项是spring-context,spring-webmvc,spring-tx,hibernate-core,hibernate-entitymanager和spring-orm。
我正在使用Apache Commons DBCP进行连接池,但是在现实生活中,很可能您已通过容器完成了连接池,而我们所需要做的只是提供要使用的JNDI参考详细信息。
注意:我注意到某些读者遇到数据库连接问题。
请注意,在我的pom.xml中,没有数据库驱动程序。
这对我有用,因为我在tomcat lib目录中有MySQL驱动程序,并为其配置了一些数据源连接。
对于任何与数据库连接有关的问题,请将数据库驱动程序放在容器lib中或者将其包含在pom.xml依赖项中。
部署描述符
我们需要在我们的Web应用程序中插入spring框架,这是通过将Spring框架DispatcherServlet
配置为前端控制器来完成的。
我们的web.xml文件如下所示。
<?xml version="1.0" encoding="UTF-8"?> <web-app version="2.5" xmlns="https://java.sun.com/xml/ns/javaee" xmlns:xsi="https://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="https://java.sun.com/xml/ns/javaee https://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"> <!-- The definition of the Root Spring Container shared by all Servlets and Filters --> <context-param> <param-name>contextConfigLocation</param-name> <param-value>/WEB-INF/spring/root-context.xml</param-value> </context-param> <!-- Creates the Spring Container shared by all Servlets and Filters --> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <!-- Processes application requests --> <servlet> <servlet-name>appServlet</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <init-param> <param-name>contextConfigLocation</param-name> <param-value>/WEB-INF/spring/appServlet/servlet-context.xml</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>appServlet</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping> </web-app>
大部分是样板代码,最重要的部分是spring上下文文件位置,我们将其中配置spring bean和服务。
如果需要,可以根据项目要求进行更改。
休眠实体Bean
我们在实体bean类中使用JPA批注,但是,我们也可以在XML文件中使用简单的Java bean和映射细节。
在这种情况下,我们需要在Spring bean配置中配置Hibernate SessionFactory时提供映射文件详细信息。
package com.theitroad.spring.model; import javax.persistence.Column; import javax.persistence.Entity; import javax.persistence.GeneratedValue; import javax.persistence.GenerationType; import javax.persistence.Id; import javax.persistence.Table; /** * Entity bean with JPA annotations * Hibernate provides JPA implementation * @author hyman * */ @Entity @Table(name="PERSON") public class Person { @Id @Column(name="id") @GeneratedValue(strategy=GenerationType.IDENTITY) private int id; private String name; private String country; public int getId() { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getCountry() { return country; } public void setCountry(String country) { this.country = country; } @Override public String toString(){ return "id="+id+", name="+name+", country="+country; } }
我们的实体bean映射到MySQL数据库中的PERSON表,请注意,我没有使用" @Column"注解对" name"和" country"域进行注解,因为它们具有相同的名称。
SQL脚本下面显示了表的详细信息。
CREATE TABLE `Person` ( `id` int(11) unsigned NOT NULL AUTO_INCREMENT, `name` varchar(20) NOT NULL DEFAULT '', `country` varchar(20) DEFAULT NULL, PRIMARY KEY (`id`) ) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8;
Hibernate DAO实施
我们将创建" PersonDAO"接口来声明将在项目中使用的方法。
接下来,我们将为其提供休眠特定的实现。
package com.theitroad.spring.dao; import java.util.List; import com.theitroad.spring.model.Person; public interface PersonDAO { public void addPerson(Person p); public void updatePerson(Person p); public List<Person> listPersons(); public Person getPersonById(int id); public void removePerson(int id); }
特定于Hibernate的DAO实现如下所示。
package com.theitroad.spring.dao; import java.util.List; import org.hibernate.Session; import org.hibernate.SessionFactory; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.stereotype.Repository; import com.theitroad.spring.model.Person; @Repository public class PersonDAOImpl implements PersonDAO { private static final Logger logger = LoggerFactory.getLogger(PersonDAOImpl.class); private SessionFactory sessionFactory; public void setSessionFactory(SessionFactory sf){ this.sessionFactory = sf; } @Override public void addPerson(Person p) { Session session = this.sessionFactory.getCurrentSession(); session.persist(p); logger.info("Person saved successfully, Person Details="+p); } @Override public void updatePerson(Person p) { Session session = this.sessionFactory.getCurrentSession(); session.update(p); logger.info("Person updated successfully, Person Details="+p); } @SuppressWarnings("unchecked") @Override public List<Person> listPersons() { Session session = this.sessionFactory.getCurrentSession(); List<Person> personsList = session.createQuery("from Person").list(); for(Person p : personsList){ logger.info("Person List::"+p); } return personsList; } @Override public Person getPersonById(int id) { Session session = this.sessionFactory.getCurrentSession(); Person p = (Person) session.load(Person.class, new Integer(id)); logger.info("Person loaded successfully, Person details="+p); return p; } @Override public void removePerson(int id) { Session session = this.sessionFactory.getCurrentSession(); Person p = (Person) session.load(Person.class, new Integer(id)); if(null != p){ session.delete(p); } logger.info("Person deleted successfully, person details="+p); } }
注意,我没有使用Hibernate Transaction,这是因为Spring框架会注意它。
Spring服务类
这是我们的使用Hibernate DAO类与Person对象一起工作的服务类。
package com.theitroad.spring.service; import java.util.List; import com.theitroad.spring.model.Person; public interface PersonService { public void addPerson(Person p); public void updatePerson(Person p); public List<Person> listPersons(); public Person getPersonById(int id); public void removePerson(int id); }
package com.theitroad.spring.service; import java.util.List; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; import com.theitroad.spring.dao.PersonDAO; import com.theitroad.spring.model.Person; @Service public class PersonServiceImpl implements PersonService { private PersonDAO personDAO; public void setPersonDAO(PersonDAO personDAO) { this.personDAO = personDAO; } @Override @Transactional public void addPerson(Person p) { this.personDAO.addPerson(p); } @Override @Transactional public void updatePerson(Person p) { this.personDAO.updatePerson(p); } @Override @Transactional public List<Person> listPersons() { return this.personDAO.listPersons(); } @Override @Transactional public Person getPersonById(int id) { return this.personDAO.getPersonById(id); } @Override @Transactional public void removePerson(int id) { this.personDAO.removePerson(id); } }
注意,Spring 声明式事务管理是通过使用@Transactional注释来应用的。
Spring 控制器类
我们的DAO和Service类已经准备就绪,是时候编写控制器类了,该类将处理客户端请求,并使用服务类执行特定于数据库的操作,然后返回视图页面。
package com.theitroad.spring; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Qualifier; 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 com.theitroad.spring.model.Person; import com.theitroad.spring.service.PersonService; @Controller public class PersonController { private PersonService personService; @Autowired(required=true) @Qualifier(value="personService") public void setPersonService(PersonService ps){ this.personService = ps; } @RequestMapping(value = "/persons", method = RequestMethod.GET) public String listPersons(Model model) { model.addAttribute("person", new Person()); model.addAttribute("listPersons", this.personService.listPersons()); return "person"; } //For add and update person both @RequestMapping(value= "/person/add", method = RequestMethod.POST) public String addPerson(@ModelAttribute("person") Person p){ if(p.getId() == 0){ //new person, add it this.personService.addPerson(p); }else{ //existing person, call update this.personService.updatePerson(p); } return "redirect:/persons"; } @RequestMapping("/remove/{id}") public String removePerson(@PathVariable("id") int id){ this.personService.removePerson(id); return "redirect:/persons"; } @RequestMapping("/edit/{id}") public String editPerson(@PathVariable("id") int id, Model model){ model.addAttribute("person", this.personService.getPersonById(id)); model.addAttribute("listPersons", this.personService.listPersons()); return "person"; } }
注意,我使用的是@Controller注释,因此Spring框架会将其视为处理客户端请求的Controller类。
我也使用了@Autowired和@Qualifier注解来注入PersonService,我们也可以在spring上下文xml文件中完成它。
推荐阅读:Spring Bean自动装配
Spring Bean配置
我们的服务已经准备就绪,我们所需要的只是通过Spring bean配置进行连接。
我们的root-context.xml文件为空,因此我们仅查看servlet-context.xml文件。
<?xml version="1.0" encoding="UTF-8"?> <beans:beans xmlns="https://www.springframework.org/schema/mvc" xmlns:xsi="https://www.w3.org/2001/XMLSchema-instance" xmlns:beans="https://www.springframework.org/schema/beans" xmlns:context="https://www.springframework.org/schema/context" xmlns:tx="https://www.springframework.org/schema/tx" xsi:schemaLocation="https://www.springframework.org/schema/mvc https://www.springframework.org/schema/mvc/spring-mvc.xsd https://www.springframework.org/schema/beans https://www.springframework.org/schema/beans/spring-beans.xsd https://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd https://www.springframework.org/schema/tx https://www.springframework.org/schema/tx/spring-tx-4.0.xsd"> <!-- DispatcherServlet Context: defines this servlet's request-processing infrastructure --> <!-- Enables the Spring MVC @Controller programming model --> <annotation-driven <!-- Handles HTTP GET requests for /resources/** by efficiently serving up static resources in the ${webappRoot}/resources directory --> <resources mapping="/resources/**" location="/resources/" <!-- Resolves views selected for rendering by @Controllers to .jsp resources in the /WEB-INF/views directory --> <beans:bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <beans:property name="prefix" value="/WEB-INF/views/" <beans:property name="suffix" value=".jsp" </beans:bean> <beans:bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close"> <beans:property name="driverClassName" value="com.mysql.jdbc.Driver" <beans:property name="url" value="jdbc:mysql://localhost:3306/TestDB" <beans:property name="username" value="hyman" <beans:property name="password" value="hyman123" </beans:bean> <!-- Hibernate 4 SessionFactory Bean definition --> <beans:bean id="hibernate4AnnotatedSessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean"> <beans:property name="dataSource" ref="dataSource" <beans:property name="annotatedClasses"> <beans:list> <beans:value>com.theitroad.spring.model.Person</beans:value> </beans:list> </beans:property> <beans:property name="hibernateProperties"> <beans:props> <beans:prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect </beans:prop> <beans:prop key="hibernate.show_sql">true</beans:prop> </beans:props> </beans:property> </beans:bean> <beans:bean id="personDAO" class="com.theitroad.spring.dao.PersonDAOImpl"> <beans:property name="sessionFactory" ref="hibernate4AnnotatedSessionFactory" </beans:bean> <beans:bean id="personService" class="com.theitroad.spring.service.PersonServiceImpl"> <beans:property name="personDAO" ref="personDAO"></beans:property> </beans:bean> <context:component-scan base-package="com.theitroad.spring" <tx:annotation-driven transaction-manager="transactionManager" <beans:bean id="transactionManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager"> <beans:property name="sessionFactory" ref="hibernate4AnnotatedSessionFactory" </beans:bean> </beans:beans>
dataorg bean是为org.apache.commons.dbcp.BasicDataSource`类定义的,用于基本连接池。
org.springframework.orm.hibernate4.LocalSessionFactoryBean` bean用于Hibernate 4 SessionFactory。
对于Hibernate 3,您将找到与" org.springframework.orm.hibernate3.LocalSessionFactoryBean"和" org.springframework.orm.hibernate3.AnnotationSessionFactoryBean"类似的类。
重要的一点是,当我们依赖Spring框架进行Hibernate Session管理时,我们不应该定义hibernate.current_session_context_class
,否则,会遇到很多与会话事务相关的问题。
personDAO和personService Bean是自我理解的。
Spring ORM需要org.springframework.orm.hibernate4.HibernateTransactionManager
的transactionManager bean定义来支持休眠会话事务管理。
对于Hibernate 3,您会发现与org.springframework.orm.hibernate3.HibernateTransactionManager
类似的类。
Spring使用AOP进行事务管理,您现在可以将其与@Transactional批注关联。
查看页面
该应用程序的最后一部分是视图页面,注意在Controller处理程序方法中添加到Model的属性,我们将使用它们来创建视图页面。
我们还将使用JSTL标签,spring core和spring form标签。
<%@ taglib uri="https://java.sun.com/jsp/jstl/core" prefix="c" %> <%@ taglib uri="https://www.springframework.org/tags" prefix="spring" %> <%@ taglib uri="https://www.springframework.org/tags/form" prefix="form" %> <%@ page session="false" %> <html> <head> <title>Person Page</title> <style type="text/css"> .tg {border-collapse:collapse;border-spacing:0;border-color:#ccc;} .tg td{font-family:Arial, sans-serif;font-size:14px;padding:10px 5px;border-style:solid;border-width:1px;overflow:hidden;word-break:normal;border-color:#ccc;color:#333;background-color:#fff;} .tg th{font-family:Arial, sans-serif;font-size:14px;font-weight:normal;padding:10px 5px;border-style:solid;border-width:1px;overflow:hidden;word-break:normal;border-color:#ccc;color:#333;background-color:#f0f0f0;} .tg .tg-4eph{background-color:#f9f9f9} </style> </head> <body> <h1> Add a Person </h1> <c:url var="addAction" value="/person/add" ></c:url> <form:form action="${addAction}" commandName="person"> <table> <c:if test="${!empty person.name}"> <tr> <td> <form:label path="id"> <spring:message text="ID" </form:label> </td> <td> <form:input path="id" readonly="true" size="8" disabled="true" <form:hidden path="id" </td> </tr> </c:if> <tr> <td> <form:label path="name"> <spring:message text="Name" </form:label> </td> <td> <form:input path="name" </td> </tr> <tr> <td> <form:label path="country"> <spring:message text="Country" </form:label> </td> <td> <form:input path="country" </td> </tr> <tr> <td colspan="2"> <c:if test="${!empty person.name}"> <input type="submit" value="<spring:message text="Edit Person"" </c:if> <c:if test="${empty person.name}"> <input type="submit" value="<spring:message text="Add Person"" </c:if> </td> </tr> </table> </form:form> <br> <h3>Persons List</h3> <c:if test="${!empty listPersons}"> <table class="tg"> <tr> <th width="80">Person ID</th> <th width="120">Person Name</th> <th width="120">Person Country</th> <th width="60">Edit</th> <th width="60">Delete</th> </tr> <c:forEach items="${listPersons}" var="person"> <tr> <td>${person.id}</td> <td>${person.name}</td> <td>${person.country}</td> <td><a href="<c:url value='/edit/${person.id}' " >Edit</a></td> <td><a href="<c:url value='/remove/${person.id}' " >Delete</a></td> </tr> </c:forEach> </table> </c:if> </body> </html>
Spring MVC Hibernate应用测试
只需将项目构建并部署到您选择的任何servlet容器中,例如Tomcat。
下面的屏幕截图显示了我们应用程序的视图页面。
您还将在服务器日志文件中找到类似的日志。
Hibernate: insert into PERSON (country, name) values (?, ?) INFO : com.theitroad.spring.dao.PersonDAOImpl - Person saved successfully, Person Details=id=15, name=hyman, country=USA Hibernate: select person0_.id as id1_0_, person0_.country as country2_0_, person0_.name as name3_0_ from PERSON person0_ INFO : com.theitroad.spring.dao.PersonDAOImpl - Person List::id=10, name=Raman, country=UK2 INFO : com.theitroad.spring.dao.PersonDAOImpl - Person List::id=11, name=Lisa, country=France INFO : com.theitroad.spring.dao.PersonDAOImpl - Person List::id=15, name=hyman, country=USA