Strusts2+spring+hibernate Although is the mainstream web development framework, but SPRINGMVC more and more people use, it is also very useful, very cool!
A small example of springmvc+spring+hibernate is implemented here. The comments are all in the code.
The structure of each package of the project is as follows:
1, first of all, 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.miquan</groupid><artifactid>springmvc_spring_ Hibernate</artifactid><packaging>war</packaging><version>0.0.1-snapshot</version ><name>springmvc_spring_hibernate Maven Webapp</name><url>http://maven.apache.org</url ><dependencies><!--EXPECTJ--><dependency><groupid>org.springframework</groupid ><artifactid>spring-aspects</artifactid><version>4.0.4.release</version></ dependency><!--JUnit--><dependency><groupid>junit</groupid><artifactid>junit </artifactid><version>4.11</version><scope>test</scope></dependency><!- -Spring--><dEpendency><groupid>org.springframework</groupid><artifactid>spring-context</artifactid ><version>4.1.0.RELEASE</version></dependency><dependency><groupId> org.springframework.ws</groupid><artifactid>spring-ws-core</artifactid><version>2.2.0. Release</version></dependency><dependency><groupid>org.springframework</groupid> <artifactId>spring-orm</artifactId><version>4.1.1.RELEASE</version></dependency> <!--hibernate--><dependency><groupid>org.hibernate</groupid><artifactid> hibernate-core</artifactid><version>4.3.6.final</version></dependency><!--DBCP-- ><dependency><groupid>commons-dbcp</groupid><artifactid>commons-dbcp</artifactid ><version>1.4</version></dependency><!--MYSQL--><dependency><groupid> Mysql</groupid><arTifactid>mysql-connector-java</artifactid><version>5.1.18</version></dependency> </dependencies><build><finalName>springmvc_spring_hibernate</finalName></build> </project>
2, Web. XML configuration Good SPRINGMVC and spring
<?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 "><!--Spring configuration. (must match, to not find the bean)-<listener> <listener-class>org.springframework.web.context.contextloader listener</listener-class> </listener><!--Spring MVC (The system will find all the controllers and initialize them at boot time according to the configuration in Springmvc-servlet)--><servlet><servlet-name>springmvc</ Servlet-name><servlet-class>org.springframework.web.servlet.dispatcherservlet</servlet-class> <load-on-startup>1</load-on-startup></servlet><servlet-mapping><servlet-name> springmvc</servlet-name><url-pattern>/</url-pattern><!--can't add *--></servlet-mapping here ></web-app>
3, SPRINGMVC configuration--springmvc-servlet.xml (note naming, corresponding to SPRINGMVC in servlet)
<?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/SPR Ing-beans.xsd Http://www.springframework.org/schema/context Http://www.springframework.org/schema/context/sp Ring-context.xsd Http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring- Mvc.xsd "> <!--the controller's package exists. Note that the package name is written here, not the class name---<context:component-scan base-package= "Com.miquan.controller"/><MVC: Annotation-driven/><!--static file--><mvc:resources location= "/res/" mapping= "/res/**" ></MVC: resources><!--Jump FilePrefix--><bean class= "org.springframework.web.servlet.view.InternalResourceViewResolver" ><property name = "prefix" value= "/web-inf/view/" ></property><property name= "suffix" value= ". JSP" ></property> </bean></beans>
4, spring configuration--applicationcontext.xml default is this name, need to customize the Baidu again.
<?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" xmlns:aop= "HTTP://WWW.SPRINGFRAMEWORK.ORG/SCHEMA/AOP" 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/c Ontext/spring-context-3.0.xsd HTTP://WWW.SPRINGFRAMEWORK.ORG/SCHEMA/AOP http://www.springframework.org/s Chema/aop/spring-aop-3.0.xsd Http://www.springframework.org/schema/tx http://www.springframework.org/s Chema/tx/spring-tx.xsd "><!--auto-sweep annotation Package--><context:annotation-config/><context:component-scan Base-package= "com.miquan.*"/><!--data source--><bean id= "DataSource" class= "Org.apache.commons.dbcp.BasicDataSource" destroy-method= "Close" ><property name= "Driverclassname" value= " Com.mysql.jdbc.Driver "/><property name=" url "value=" Jdbc:mysql://127.0.0.1:3306/ssh "/><property name= "Username" value= "root"/><property name= "password" value= "root"/></bean><!--configuration Sessionfactory-- ><bean id= "Sessionfactory" class= "Org.springframework.orm.hibernate4.LocalSessionFactoryBean" >< Property Name= "DataSource" ref= "DataSource"/><property name= "Packagestoscan" value= "Com.miquan.model"/> <!--entity class package--><property name= "hibernateproperties" ><props><prop key= "Hibernate.dialect" > Org.hibernate.dialect.mysqldialect</prop><prop key= "Hibernate.show_sql" >true</prop><!-- Automatically creates a table, but does not automatically create the database, so you create the database first. --><prop key= "Hibernate.hbm2ddl.auto" >update</prop><prop key= "Hibernate.format_sql" >true </prop></props></property></bean><!--for injection into Generaldao Medium--><bean id= "hibernatetemplate" class= "Org.springframework.orm.hibernate4.HibernateTemplate" >< Property Name= "Sessionfactory" ref= "sessionfactory"/></bean><!--configuration Transaction Management--><bean id= " TransactionManager "class=" Org.springframework.orm.hibernate4.HibernateTransactionManager "><property name= "Sessionfactory" ref= "Sessionfactory"/></bean><!--opening transaction management annotations--><aop:aspectj-autoproxy/>< Tx:annotation-driven/></beans>
5, Entity class.
Package Com.miquan.model;import Javax.persistence.entity;import Javax.persistence.generatedvalue;import Javax.persistence.id;import javax.persistence.Table; @Entity @tablepublic class User {private int id;private String Username;private String password;public User () {super ();} public User (int ID, string username, string password) {super (); this.id = Id;this.username = Username;this.password = PASSW Ord;} @Id @generatedvaluepublic int getId () {return Id;} public void setId (int id) {this.id = ID;} Public String GetUserName () {return username;} public void Setusername (String username) {this.username = username;} Public String GetPassword () {return password;} public void SetPassword (String password) {this.password = password;} @Overridepublic String toString () {return "User [id=" + ID + ", username=" + Username + ", password=" + password + "]";}}
6, controller layer.
Package Com.miquan.controller;import Org.springframework.beans.factory.annotation.autowired;import Org.springframework.stereotype.controller;import Org.springframework.web.bind.annotation.requestmapping;import Org.springframework.web.bind.annotation.requestmethod;import Com.miquan.model.user;import Com.miquan.service.IUserService, @Controller @requestmapping (value= "/user") public class Usercontroller {/** * Inject UserService. * If UserService inherits an interface, * Here the type must be interface Iuserservice, * cannot be class UserService, do not know why. */@Autowiredprivate Iuserservice userservice; @RequestMapping (value= "/registe", method=requestmethod.get) public String Registe () {User user = new User (0, "Liang Qiao", "999"); userservice.registe (user); return "Index";}}
7, service layer.
Package Com.miquan.service;import Com.miquan.model.user;public Interface Iuserservice {public boolean registe (User user);}
Package Com.miquan.service;import Org.springframework.beans.factory.annotation.autowired;import Org.springframework.stereotype.service;import Org.springframework.transaction.annotation.transactional;import Com.miquan.dao.igeneraldao;import Com.miquan.model.User; @Servicepublic class UserService implements Iuserservice {@ Autowiredprivate Igeneraldao generaldao;/* * Here to have business annotations, default Readonly=true, do not set the words will be error. * Both insert and update operations are needed. */@Transactional (Readonly=false) Public boolean registe (user user) {generaldao.save (user); return false;}}
8, DAO layer.
Package Com.miquan.dao;import Java.io.serializable;import Java.util.list;public interface Igeneraldao {public <T > T FindByID (class<t> type, Serializable ID); Public <T> list<t> findAll (class<t> type); public void Save (Object ... entities); public void Update (Object ... entities); public void Saveorupdate (Object entity); public void Delete (Object ... entities); public void Deletebyid (class<?> type, Serializable ID); public void Refresh (Object ... entities); public void Flush ();}
Package Com.miquan.dao;import Java.io.serializable;import Java.util.list;import Org.springframework.beans.factory.annotation.autowired;import Org.springframework.orm.hibernate4.hibernatetemplate;import org.springframework.stereotype.repository;/** * This class encapsulates the DAO into an operation class that is copied from the Web. */@Repositorypublic class Generaldao implements Igeneraldao {/** * This bean needs to be injected into the sessionfactory, so the Bean is written in the configuration. */@Autowiredprivate hibernatetemplate hibernatetemplate;public <T> T FindByID (class<t> type, Serializable ID) {return hibernatetemplate.get (type, id);} Public <T> list<t> findAll (class<t> type) {return hibernatetemplate.loadall (type);} public void save (object ... entities) {for (object entity:entities) {Hibernatetemplate.save (entity);}} public void Saveorupdate (Object entity) {hibernatetemplate.saveorupdate (entity);} public void update (object ... entities) {for (object entity:entities) {hibernatetemplate.update (entity);}} public void Delete (Object ... entities) {for (object EntiTy:entities) {if (entity! = null) {Hibernatetemplate.delete (entity);}}} public void Deletebyid (class<?> type, Serializable ID) {if (id = = null) {return;} Object entity = FindByID (type, id); if (entity = = null) {return;} Delete (entity);} public void Refresh (Object ... entities) {for (object entity:entities) {Hibernatetemplate.refresh (entity);}} public void Flush () {Hibernatetemplate.flush ();}}
9, Test.
Package Com.miquan.service;import Org.junit.test;import Org.springframework.context.applicationcontext;import Org.springframework.context.support.filesystemxmlapplicationcontext;import Com.miquan.controller.UserController ;p Ublic class Userservicetest {@Testpublic void Test () {ApplicationContext ctx = new Filesystemxmlapplicationcontext (" Src/main/webapp/web-inf/applicationcontext.xml "); Usercontroller controller = Ctx.getbean (usercontroller.class); Controller.registe ();}}
Or deploy on Tomcat, access the HTTP://LOCALHOST:8080/XX project/user/registe in the browser.
A small example of springmvc+spring+hibernate