One of the previous articles, http://blog.csdn.net/wangdianyong/article/details/38842693, had also written about the integration between the three, but did not use Maven
Led to the project contains a large number of packages, looking very unattractive!
Use MAVEN to re-integrate it below! Novice also look at the liberal enlighten!
The establishment process of MAEVN project is omitted
See Project structure
Individual profile code I'll post it, please change it in a different place.
Database.properties
hibernate.dialect=org.hibernate.dialect.mysqldialectdriverclassname=com.mysql.jdbc.drivervalidationquery= SELECT 1url=jdbc:mysql://localhost:3306/test?useunicode=true&characterencoding=utf-8username=rootpassword= Roothibernate.hbm2ddl.auto=updatehibernate.show_sql=truehibernate.format_sql=true
Spring-hibernate.xml
<?xml version= "1.0" encoding= "UTF-8"? ><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 "xsi:schemalocation=" Http://www.springframework.org/schema/beans http ://www.springframework.org/schema/beans/spring-beans-3.0.xsd HTTP://WWW.SPRINGFRAMEWORK.ORG/SCHEMA/TX/http WWW.SPRINGFRAMEWORK.ORG/SCHEMA/TX/SPRING-TX-3.0.XSDHTTP://WWW.SPRINGFRAMEWORK.ORG/SCHEMA/AOP/HTTP Www.springframework.org/schema/aop/spring-aop-3.0.xsd "><!--using c3p0 data source, MySQL database--><bean id=" DataSource "class=" Com.mchange.v2.c3p0.ComboPooledDataSource "destroy-method=" Close "><!--MySQL5-->< Property Name= "Driverclass" value= "${driverclassname}" ></property><property name= "JdbcUrl" value= "${ URL} "></property><property name=" user "value=" ${username} "></property><property name=" Password "value=" ${pasSword} "></property><property name=" maxpoolsize "value=" "></property><property name=" Minpoolsize "value=" 1 "></property><property name=" Initialpoolsize "value=" 1 "></property>< Property Name= "MaxIdleTime" value= "></property></bean><!--session factory--><bean Id=" Sessionfactory "class=" Org.springframework.orm.hibernate4.LocalSessionFactoryBean "><property name=" DataSource "ref=" DataSource "/><property name=" hibernateproperties "><props><prop key=" Hibernate.hbm2ddl.auto ">${hibernate.hbm2ddl.auto}</prop><prop key=" Hibernate.dialect ">${ Hibernate.dialect}</prop><prop key= "Hibernate.show_sql" >${hibernate.show_sql}</prop><prop key= "Hibernate.format_sql" >${hibernate.format_sql}</prop></props></property><!--annotation Mode configuration --><property name= "Packagestoscan" ><list><value>com.iss.model</value></list> </property></bean><!--Configuring Transaction--><bean name= "Txmanager" class= " Org.springframework.orm.hibernate4.HibernateTransactionManager "><property name=" sessionfactory "ref=" Sessionfactory "></property></bean><tx:annotation-driven transaction-manager=" TxManager "/> </beans>
Spring-mvc.xml
<?xml version= "1.0" encoding= "UTF-8"? ><beans xmlns= "Http://www.springframework.org/schema/beans" xmlns: Mvc= "Http://www.springframework.org/schema/mvc" xmlns:xsi= "Http://www.w3.org/2001/XMLSchema-instance" xmlns:p= " http://www.springframework.org/schema/p "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.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 "><!--automatically scans all classes under the controller package so that it thinks spring MVC's controllers--><context:component-scan Base-package= "Com.iss.controller"/><!--avoid IE when performing Ajax, return json appears download file--><!--<bean id= " Mappingjacksonhttpmessageconverter "--><!--class=" Org.springframework.http.converter.json.MappingJacksonHttpMessageConverter ">--><! --<property name= "Supportedmediatypes" >--><!--<list>--><!--<value>text/html; charset=utf-8</value>--><!--</list>--><!--</property>--><!--</bean>- -><!--Start the annotation feature of spring MVC, complete the mapping of request and annotation Pojo--><!--<bean class= " Org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter "> <property name=" Messageconverters "> <list> <ref bean=" Mappingjacksonhttpmessageconverter "/>json converter </list> </property> </bean>--><mvc:annotation-driven/><!--The resolution of the model view name by adding a prefix to the Model view name-->< beanclass= "Org.springframework.web.servlet.view.InternalResourceViewResolver" ><property name= "prefix" Value= "/"/><property name= "suffix" value= ". JSP"/></bean></beans>
Spring.xml
<?xml Version= "1.0" encoding= "UTF-8"? ><beans xmlns= "Http://www.springframework.org/schema/beans" xmlns:xsi= "http ://www.w3.org/2001/XMLSchema-instance "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.0.xsd Http://www.springframework.org/schema/context Http://www.springframework.org/schema/context /spring-context-3.0.xsd "><!--enable annotations--><context:annotation-config/><context: Component-scanbase-package= "Com.iss.action,com.iss.dao.impl,com.iss.service" ></context:component-scan ><!--Introducing the Properties file Hibernate property configuration--><context:property-placeholder location= "Classpath:database.properties"/ ><!--automatically scan DAO and service Pack (auto injection)--><!--<context:component-scan base-package= "com.iss*"/>-->< /beans>
Xml
<?xml version= "1.0" encoding= "UTF-8"? ><web-app version= "3.0" xmlns= "Http://java.sun.com/xml/ns/javaee" Xmlns:xsi= "Http://www.w3.org/2001/XMLSchema-instance" xsi:schemalocation= "Http://java.sun.com/xml/ns/javaee http ://java.sun.com/xml/ns/javaee/web-app_3_0.xsd "><display-name></display-name><context-param ><param-name>contextconfiglocation</param-name><param-value>/web-inf/classes/spring.xml,/ web-inf/classes/spring-hibernate.xml,</param-value></context-param><listener>< Listener-class>org.springframework.web.context.contextloaderlistener</listener-class></listener ><filter><filter-name>encodingFilter</filter-name><filter-class> Org.springframework.web.filter.characterencodingfilter</filter-class><init-param><param-name >encoding</param-name><param-value>UTF-8</param-value></init-param></filter> <filter-mapping><filter-name>encodingfilter</filter-name><url-pattern>/*</url-pattern></filter-mapping><!-- Hibernate session lost Solution--><filter><filter-name>opensessioninview</filter-name>< filter-class>org.springframework.orm.hibernate4.support.opensessioninviewfilter</filter-class></ filter><filter-mapping><filter-name>opensessioninview</filter-name><url-pattern>/* </url-pattern></filter-mapping><!--struts2 configuration--><!--<filter> <filter-name> Struts2</filter-name> <filter-class> Org.apache.struts2.dispatcher.ng.filter.strutsprepareandexecutefilter</filter-class> </filter> < filter-mapping> <filter-name>struts2</filter-name> <url-pattern>/*</url-pattern> </ filter-mapping>--><!--SPRINGMVC configuration--><servlet><description>spring MVC servlet</ Description><servlet-name>springmvc</servlet-name><servlet-class>org.Springframework.web.servlet.dispatcherservlet</servlet-class><init-param><description>spring MVC configuration File </description><param-name>contextconfiglocation</param-name><param-value>/ web-inf/classes/spring-mvc.xml</param-value></init-param><load-on-startup>1</ Load-on-startup></servlet><servlet-mapping><servlet-name>springmvc</servlet-name> <url-pattern>/</url-pattern></servlet-mapping><welcome-file-list><welcome-file> Index.jsp</welcome-file></welcome-file-list></web-app>
Contents of each package in Src/main/java
Com.iss.controller.UserController
Package Com.iss.controller;import Org.springframework.beans.factory.annotation.autowired;import Org.springframework.stereotype.controller;import Org.springframework.web.bind.annotation.requestmapping;import Com.iss.model.user;import Com.iss.service.UserService; @Controller ("Usercontroller") @RequestMapping ("User") public class Usercontroller {@Autowiredprivate userservice userservice, @RequestMapping ("/reg") Public String Saveuser ( User user) {System.out.println (userservice); userservice.save (user); return "Success";}}
Com.iss.dao.UserDao
Package Com.iss.dao;import Com.iss.model.user;public Interface userdao<t> {public User save (T o);}
Com.iss.dao.impl.UserDaoImpl
Package Com.iss.dao.impl;import Org.hibernate.session;import Org.hibernate.sessionfactory;import Org.springframework.beans.factory.annotation.autowired;import Org.springframework.stereotype.repository;import Com.iss.dao.userdao;import Com.iss.model.User; @Repository ("Userdao") public class Userdaoimpl implements userdao< user> {@Autowiredprivate sessionfactory sessionfactory;////public void Setsessionfactory (sessionfactory Sessionfactory) {//this.sessionfactory = sessionfactory;//}////public sessionfactory getsessionfactory () {//return SE ssionfactory;//} @Overridepublic User save (User O) {Session session = Sessionfactory.getcurrentsession (); Session.save ( o); return o;}}
Com.iss.model.User
Package Com.iss.model;import Java.io.serializable;import Javax.persistence.entity;import Javax.persistence.generatedvalue;import Javax.persistence.generationtype;import Javax.persistence.Id;import javax.persistence.Table; @Entity @table (name = "User") public class user implements Serializable {private int id;private String name; @Id @generatedvalue (strategy = generationtype.auto) 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;}}
Com.iss.service.UserService
Package Com.iss.service;import Com.iss.model.user;public Interface UserService {Public user save (user user);
Com.iss.service.impl.UserServiceImpl
Package Com.iss.service.impl;import Org.springframework.beans.factory.annotation.autowired;import Org.springframework.stereotype.service;import Org.springframework.transaction.annotation.transactional;import Com.iss.dao.userdao;import com.iss.model.user;import Com.iss.service.UserService; @Service ("UserService") @ Transactionalpublic class Userserviceimpl implements UserService {@Autowiredprivate userdao<user> userdao;@ Overridepublic user Save (user user) {//TODO auto-generated method Stubreturn userdao.save (user);}}
index,jsp
<body><form action= "Useraction" method= "POST" ><table width= "207" border= "0" align= "center" ><tr ><TD colspan= "2" align= "center" nowrap= "nowrap" > User registration </TD></TR><TR><TD width= "68" nowrap= "nowrap" > User name </TD><TD width= "127" nowrap= "nowrap" ><label> <inputname= "name" Type= " Text "id=" username "size="/> </label></td></tr><tr><td colspan= "2" align= "center" nowrap= "nowrap" ><label><input type= "submit" value= "register"/> <input type= "reset" value= "refill"/></ Label></td></tr></table></form></body>
success.jsp
<body>${user.name}</body>
When you're done, mvn install to the Tomcat server!
Spring-springmvc-hibernate maven Integration