Example of struts2 + spring + hibernte integration, struts2hibernte
Simple implementation of the Add User Function, for the reference of beginners only, you can expand the program function (add, delete, modify, query ).
Paste the code here. You can download it as needed (because it is relatively lazy ).
ApplicationContext. 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: aop = "http://www.springframework.org/schema/aop" xmlns: tx = "http://www.springframework.org/schema/tx" xmlns: p = "http://www.springframework.org/schema/p" xmlns: util = "http://www.springframework.org/schema/util" xmlns: jdbc = "h Ttp: // www.springframework.org/schema/jdbc "xmlns: cache =" http://www.springframework.org/schema/cache "xsi: schemaLocation =" http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/tx http://www.springfram Ework.org/schema/tx/spring-tx.xsd http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-3.1.xsd http://www.springframework.org/schema/cache http://www.springframework.org/schema/cache/spring-cache-3.1.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd http: // ww Export springframework.org/schema/util/spring-util.xsd "> <! -- Introduce the external configuration file --> <context: property-placeholder location = "classpath: jdbc. properties"/> <! -- Configure the connection pool --> <bean id = "dataSource" class = "com. mchange. v2.c3p0. comboPooledDataSource "> <property name =" driverClass "value =" $ {jdbc. driverClass} "/> <property name =" jdbcUrl "value =" $ {jdbc. url} "/> <property name =" user "value =" $ {jdbc. username} "/> <property name =" password "value =" $ {jdbc. password} "/> </bean> <! -- Configure Hibernate Attributes --> <bean id = "sessionFactory" class = "org. springframework. orm. hibernate4.LocalSessionFactoryBean"> <! -- Inject connection pool --> <property name = "dataSource" ref = "dataSource"/> <! -- Configure the attributes of Hibernate --> <property name = "hibernateProperties"> <props> <prop key = "hibernate. dialect "> org. hibernate. dialect. mySQLDialect </prop> <prop key = "hibernate. show_ SQL "> true </prop> <prop key =" hibernate. format_ SQL "> true </prop> <prop key =" hibernate. hbm2ddl. auto "> update </prop> </props> </property> <! -- Load the Hibernate ing file in Hibernate --> <property name = "mappingResources"> <list> <value> cn/bj/ssh/entity/User. hbm. xml </value> </list> </property> </bean> <! -- Configure Action class --> <bean id = "userAction" class = "cn. bj. ssh. action. UserAction" scope = "prototype"> <! -- Manually inject service --> <property name = "userService" ref = "userService"/> </bean> <! -- Configure the business class --> <bean id = "userService" class = "cn. bj. ssh. service. userService "> <property name =" userDao "ref =" userDao "/> </bean> <! -- Configure the DAO class --> <bean id = "userDao" class = "cn. bj. ssh. dao. userDao "> <property name =" sessionFactory "ref =" sessionFactory "/> </bean> <! -- Configure the Transaction Manager --> <bean id = "transactionManager" class = "org. springframework. orm. hibernate4.HibernateTransactionManager "> <property name =" sessionFactory "ref =" sessionFactory "/> </bean> <! -- Enable annotation --> <tx: annotation-driven transaction-manager = "transactionManager"/> </beans>View Code
Database Connection configuration: jdbc. properties
# JDBC Configuration jdbc.driverClass=com.mysql.jdbc.Driverjdbc.url=jdbc:mysql://127.0.0.1:3306/sshjdbc.username=rootjdbc.password=root
View Code
Struts. xml
<? Xml version = "1.0" encoding = "UTF-8"?> <! DOCTYPE struts PUBLIC "-// Apache Software Foundation // DTD Struts Configuration 2.3 // EN" "http://struts.apache.org/dtds/struts-2.3.dtd"> <struts> <! -- Directly write the id to the spring Management --> <package name = "ssh" extends = "struts-default" namespace = "/"> <action name = "user _ *" class = "userAction" method = "{1}"> <result name = "loginSuccess">/index. jsp </result> <! -- <Result name = "success_save">/index. jsp </result> --> </action> </package> </struts>View Code
UserAction. java
Package cn. bj. ssh. action; import com. opensymphony. xwork2.ActionSupport; import com. opensymphony. xwork2.ModelDriven; import cn. bj. ssh. entity. user; import cn. bj. ssh. service. userService; public class UserAction extends ActionSupport implements ModelDriven <User> {private static final long serialVersionUID = 1L; // model-driven class private User user = new User (); // automatic private UserService userService injection; public void setUserService (UserService userService) {this. userService = userService ;}@ Override public User getModel () {return user ;}// save User public String save () {userService. save (user); return "loginSuccess ";}}View Code
UserService. java (because it is relatively simple and looks more intuitive, there is no write interface between service and dao)
package cn.bj.ssh.service;import org.springframework.transaction.annotation.Transactional;import cn.bj.ssh.dao.UserDao;import cn.bj.ssh.entity.User;@Transactionalpublic class UserService{ private UserDao userDao; public void setUserDao(UserDao userDao) { this.userDao = userDao; } public void save(User user){ userDao.save(user); } }View Code
UserDao. java
package cn.bj.ssh.dao;import org.hibernate.SessionFactory;import cn.bj.ssh.entity.User;public class UserDao { private SessionFactory sessionFactory; public void setSessionFactory(SessionFactory sessionFactory) { this.sessionFactory = sessionFactory; } public String save(User user) { this.sessionFactory.getCurrentSession().save(user); return "success_save"; } }View Code
Entity class User. jsp
package cn.bj.ssh.entity;public class User { private Integer pid; private String name; private String password; private Double height; public User(){} public User(Integer pid, String name, String password,Double height) { super(); this.pid = pid; this.name = name; this.password = password; this.height = height; } public Integer getPid() { return pid; } public void setPid(Integer pid) { this.pid = pid; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } public Double getHeight() { return height; } public void setHeight(Double height) { this.height = height; } }View Code
Ing file: User. hbm. xml
<% @ Page language = "java" contentType = "text/html; charset = UTF-8 "pageEncoding =" UTF-8 "%> <% @ taglib uri ="/struts-tags "prefix =" s "%> <! DOCTYPE html PUBLIC "-// W3C // dtd html 4.01 Transitional // EN" "http://www.w3.org/TR/html4/loose.dtd"> View Code
Add User page: addUser. jsp
<?xml version="1.0" encoding="UTF-8"?><!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">View Code
Demo link. If you are interested, download it.
Http://share.weiyun.com/b0a8c4fb51feaed92c69af29c5232d81