This article is based on this blog post, the original text here http://blog.csdn.net/bjyfb/article/details/8998267 1. Description
Build Springmvc+spring+hibernate framework, the project structure as shown in Figure 1
The referenced jar package is Spring3.2.6 and Hibernate4, as shown in Figure 2
2. configuration file 2.1 spring-mvc.xml
Pay attention to the contents of the annotation, write a more detailed
<?xml version= "1.0" encoding= "UTF-8"?> <beans "xmlns=" xmlns:
Xsi= "Http://www.w3.org/2001/XMLSchema-instance" xmlns:context= "Http://www.springframework.org/schema/context" Xmlns:mvc= "Http://www.springframework.org/schema/mvc" xsi:schemalocation= "Http://www.springframework.org/schema /beans http://www.springframework.org/schema/beans/spring-beans.xsd Http://www.springframework.org/schema/con Text Http://www.springframework.org/schema/context/spring-context-3.2.xsd Http://www.springframework.org/schem A/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd "default-autowire=" ByName "> <!--Open Kai Annotation, @--> <mvc:annotation-driven/> <!--annotation Scan package in Java file, note to replace it with your own path--> <context:component-sca n base-package= "Com.lq.controller" > <!--scan only @controller part--> <context:include-filter type= "a Nnotation "expression=" Org.springframework.sTereotype. Controller "/> </context:component-scan> <!--Complete the mapping of request and annotation Pojo--> <bean class=" Org.spr Ingframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor "/> <!--static resource (js/image) Access, Can add multiple--> <mvc:resources location= "/js/" mapping= "/js/**"/> <!--define View parser--> <bean id= "view Resolver "class=" Org.springframework.web.servlet.view.InternalResourceViewResolver "> <property name= "prefix" value= "/" ></property> <property name= "suffix" value= ". jsp" ></property> </bean > </beans>
2.2 Spring-common.xml
<?xml version= "1.0" encoding= "UTF-8"?> <!--note The version number of the spring-*.xsd in the following configuration is to match the jar package used--> <beans xmlns= "http ://www.springframework.org/schema/beans "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 "xmlns:context=" http:// Www.springframework.org/schema/context "xsi:schemalocation=" Http://www.springframework.org/schema/beans http:/ /www.springframework.org/schema/beans/spring-beans-3.2.xsd Http://www.springframework.org/schema/tx http:/ /www.springframework.org/schema/tx/spring-tx-3.2.xsd HTTP://WWW.SPRINGFRAMEWORK.ORG/SCHEMA/AOP http://www. Springframework.org/schema/aop/spring-aop.xsd Http://www.springframework.org/schema/context HTTP://WWW.SPR Ingframework.org/schema/context/spring-context.xsd > <!--only parse annotations except controller (that is, resolve service, DAO), avoid duplicate loading and cause transaction invalidation --> <context:component-scan BASE-package= "COM.LQ" > <!--annotation the content that is resolved in Spting-mvc--> <context:exclude-filter type= "Annotation" expression= "Org.springframework.stereotype.Controller"/> </context:component-scan> < !--Configure the data source--> <bean id= "DataSource" class= "Org.springframework.jdbc.datasource.DriverManagerDataSource" &
Gt
<property name= "Driverclassname" value= "Com.mysql.jdbc.Driver" ></property> <!--replaced with its own database path--> <property name= "url" value= "jdbc:mysql://localhost/mvctest" ></property> <property name= "User" Name "value=" "root" ></property> <property name= "password" value= "root" ></property> </be an> <!--configuration sessionfactory--> <bean id= "Sessionfactory" class= Te4. Localsessionfactorybean "> <property name=" dataSource "ref=" DataSource "/> <!--Packagestoscan The path where the package is scanned (The content in name is actually a keyword that can scan an entire package--> <property name= "Packagestoscan" > <list> & lt;! --Multiple entity--> <value>com.lq.entity</value> </list> </PR can be added here operty> <property name= "hibernateproperties" > <props> <prop key= "Hib Ernate.dialect ">org.hibernate.dialect.MySQLDialect</prop> <!--Create the database for the first time, then change to update, no The content clears the--> <prop key= "Hibernate.hbm2ddl.auto" >create</prop> <prop key= "h
Ibernate.show_sql ">true</prop> <prop key=" Hibernate.format_sql ">true</prop> <prop key= "Hibernate.current_session_context_class" > Org.springframework.orm.hibernate4.SpringSessionContext </prop> </props> & Lt;/property> </bean> <!--Configure a transaction manager--> <Bean id= "TransactionManager" class= "Org.springframework.orm.hibernate4.HibernateTransactionManager" > &L T;property name= "Sessionfactory" ref= "sessionfactory"/> </bean> <tx:advice id= "Txadvice" transaction- Manager= "TransactionManager" > <tx:attributes> <!--intercept the path set in the next section of configuration AOP, the path configured in Txpointcut--&
Gt <!--specific propagation meaning please self-examination--> <tx:method name= "save*" propagation= "REQUIRED"/> <tx:
Method Name= "add*" propagation= "REQUIRED"/> <tx:method name= "create*" propagation= "REQUIRED"/> <tx:method name= "insert*" propagation= "REQUIRED"/> <tx:method name= "update*" propagation= "RE quired "/> <tx:method name= merge*" propagation= "REQUIRED"/> <tx:method name= "del*" p ropagation= "REQUIRED"/> <tx:method name= "remove*" propagation= "REQUIRED"/> D name= "put* "propagation=" REQUIRED "/> <tx:method name=" use* "propagation=" REQUIRED "/> <!--H Ibernate4 must be configured to open a transaction getcurrentsession () not get--> <tx:method name= "get*" propagation= "REQUIRED" read-onl Y= "true"/> <tx:method name= "count*" propagation= "REQUIRED" read-only= "true"/> Thod name= "find*" propagation= "REQUIRED" read-only= "true"/> <tx:method name= "list*" propagation= "REQUIR" ED "read-only=" true "/> <tx:method name=" * "read-only=" true "/> </tx:attributes> &l T;/tx:advice> <aop:config expose-proxy= "true" > <!--only implement transactions for the business Logic Layer (service layer)--> <aop:p Ointcut id= "txpointcut" expression= "Execution (* com.lq.service.*.* (..))"/> <aop:advisor advice- ref= "Txadvice" pointcut-ref= "Txpointcut"/> </aop:config> </beans>
2.3 Web.xml
<?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" 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>spring-mvc-model </display-name> <welcome-file-list> <welcome-file>index.jsp</welcome-file> </we Lcome-file-list> <!--load all configuration files--> <context-param> <param-name>contextconfiglocation </param-name> <param-value>classpath*:config/spring/spring-*.xml</param-value> </context- Param> <!--Configure Spring listening--> <listener> <listener-class>org.springframework.web.context .
Contextloaderlistener</listener-class> </listener> <!--configuration Springmvc--> <servlet> <servlet-name>springmvc</servlet-Name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> < Init-param> <param-name>contextConfigLocation</param-name> <param-value>class Path*:config/spring/spring-mvc.xml</param-value> </init-param> <load-on-startup>1</lo ad-on-startup> </servlet> <servlet-mapping> <servlet-name>springmvc</servlet-name&
Gt
<url-pattern>/</url-pattern> </servlet-mapping> <!--configuration Character Set--> <filter> <filter-name>encodingFilter</filter-name> <filter-class> Org.springframework.web.filter.characterencodingfilter</filter-class> <init-param> <par Am-name>encoding</param-name> <param-value>UTF-8</param-value> </init-param>
; <init-param> <param-nAme>forceencoding</param-name> <param-value>true</param-value> </init-param>
; </filter> <filter-mapping> <filter-name>encodingFilter</filter-name> <url-p Attern>/*</url-pattern> </filter-mapping> <!--configuration session--> <filter> <f Ilter-name>opensession</filter-name> <filter-class> Org.springframework.orm.hibernate4.support.opensessioninviewfilter</filter-class> </filter> < Filter-mapping> <filter-name>openSession</filter-name> <url-pattern>/*</url-patter N> </filter-mapping> </web-app>
3. Pojo class
3.1 User
Package com.lq.entity;
Import javax.persistence.*;
Import Org.hibernate.annotations.GenericGenerator;
@Entity
//Created database table name
@Table (name = "T_user") public
class USER {
@Id
//keyword generation mode
@ Generatedvalue (generator = "System-uuid")
@GenericGenerator (name = "System-uuid", strategy = "uuid")
@ Column (length =)
private String ID;
@Column (length =)
private String userName;
@Column (length =)
private String age;
Get and set methods are omitted
3.2 Customer
Package com.lq.entity;
Import javax.persistence.*;
Import Org.hibernate.annotations.GenericGenerator;
@Entity
@Table (name = "T_customer") public
class Customer {
@Id
@GeneratedValue (generator = " System-uuid ")
@GenericGenerator (name =" System-uuid ", strategy =" uuid ")
@Column (length =)
private String ID;
@Column (length =)
private String name;
@Column (length =)
private String age;
Get and set methods are omitted
4. DAO Layer
4.1 Userdao
Package Com.lq.dao;
Import java.util.List;
Import Com.lq.entity.User;
Public interface Userdao {public
User GetUser (String ID);
Public list<user> Getalluser ();
public void AddUser (user user);
public Boolean Delu