整合Struts2+Hibernate5.0+Spring4.0
本整合采用struts利用文件配置
Hibernate用xml配置
Spring配置文件和注解
引入jar包
hibernate-release-5.2.7
struts-2.3.31
spring-framework-4.3.6
mysql 的数据驱动包
注意:引入log4j之后不要忘了去引入commons-logging-1.1.3.jar
配置文件
- struts2框架的配置文件
web.xml空文件
index.jsp
配置struts2.3版本的过滤器
struts2
org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter
struts2
*.action
struts2
*.jsp
- Hibernate框架的配置文件
可以集成在Spring中。
- Spring框架的配置文件
web.xml中配置spring的监听器
org.springframework.web.context.ContextLoaderListener
contextConfigLocation
classpath:applicationContext.xml
applicationContext.xml 基本内容
- log4j.properties
### direct log messages to stdout ###
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.Target=System.out
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%d{ABSOLUTE} %5p %c:%L - %m%n
### direct messages to file hibernate.log ###
#log4j.appender.file=org.apache.log4j.FileAppender
#log4j.appender.file.File=hibernate.log
#log4j.appender.file.layout=org.apache.log4j.PatternLayout
#log4j.appender.file.layout.ConversionPattern=%d{ABSOLUTE} %5p %c{1}:%L - %m%n
### set log levels - for more verbose logging change 'info' to 'debug' ###
log4j.rootLogger=warn, stdout
#log4j.logger.org.hibernate=info
#log4j.logger.org.hibernate=debug
### log HQL query parser activity
#log4j.logger.org.hibernate.hql.ast.AST=debug
### log just the SQL
#log4j.logger.org.hibernate.SQL=debug
### log JDBC bind parameters ###
#log4j.logger.org.hibernate.type=info
#log4j.logger.org.hibernate.type=debug
### log schema export/update ###
log4j.logger.org.hibernate.tool.hbm2ddl=debug
### log HQL parse trees
#log4j.logger.org.hibernate.hql=debug
### log cache activity ###
#log4j.logger.org.hibernate.cache=debug
### log transaction activity
#log4j.logger.org.hibernate.transaction=debug
### log JDBC resource acquisition
#log4j.logger.org.hibernate.jdbc=debug
### enable the following line if you want to track down connection ###
### leakages when using DriverManagerConnectionProvider ###
#log4j.logger.org.hibernate.connection.DriverManagerConnectionProvider=trace
包的命名
struts2和spring的两种整合方式
- 1、Action的类由Struts2自身去创建
在struts.xml中配置
applicationContext.xml中只配置service和dao
- 2、Action的类交给Spring框架去创建
struts.xml改为
applicationContext.xml改为
hibernate与spring整合
- 创建pojo的映射文件
- 在applicationContext.xml中配置数据库
${jdbc.driverClass}
${jdbc.url}
${jdbc.username}
${jdbc.password}
org.hibernate.dialect.MySQLDialect
true
true
update
com/ben/pojo/Product.hbm.xml
- jdbc.properties
jdbc.driverClass=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/sshdemo
jdbc.username=root
jdbc.password=123456
- dao层做适当的修改
- 事务配置
最终文件
pojo层文件
Product.java
package com.ben.pojo;
/**
* 产品的实体类
* @author zzq
*
*/
public class Product {
private Integer pid;
private String pname;
private Double price;
public Integer getPid() {
return pid;
}
public void setPid(Integer pid) {
this.pid = pid;
}
public String getPname() {
return pname;
}
public void setPname(String pname) {
this.pname = pname;
}
public Double getPrice() {
return price;
}
public void setPrice(Double price) {
this.price = price;
}
public Product() {
super();
}
public Product(Integer pid, String pname, Double price) {
super();
this.pid = pid;
this.pname = pname;
this.price = price;
}
@Override
public String toString() {
return "Product [pid=" + pid + ", pname=" + pname + ", price=" + price + "]";
}
}
Product.hbm.xml
service层文件
ProductServiceImpl.java
package com.ben.service.impl;
import javax.annotation.Resource;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;
import com.ben.dao.IProductDAO;
import com.ben.pojo.Product;
import com.ben.service.IProdoctService;
@Service("productService")
public class ProductServiceImpl implements IProdoctService {
private IProductDAO productDAO;
@Override
public void save(Product product) {
System.out.println("ProductServiceImpl");
productDAO.save(product);
}
public IProductDAO getProductDAO() {
return productDAO;
}
@Resource(name="productDAO")
public void setProductDAO(IProductDAO productDAO) {
this.productDAO = productDAO;
}
}
dao层文件
package com.ben.dao.impl;
import javax.annotation.Resource;
import org.hibernate.FlushMode;
import org.hibernate.SessionFactory;
import org.springframework.orm.hibernate5.support.HibernateDaoSupport;
import org.springframework.stereotype.Repository;
import com.ben.dao.IProductDAO;
import com.ben.pojo.Product;
@Repository("productDAO")
public class ProductDAOImpl extends HibernateDaoSupport implements IProductDAO{
@Override
@Transactional(propagation=Propagation.REQUIRED,rollbackFor=Exception.class,timeout=10000,isolation=Isolation.DEFAULT)
public void save(Product product) {
System.out.println(product);
System.out.println("ProductDAOImpl");
getHibernateTemplate().save(product);
}
@Resource(name="sessionFactory")
public void setSessionFactory0(SessionFactory sessionFactory){
super.setSessionFactory(sessionFactory);
}
}
applicationContext.xml
${jdbc.driverClass}
${jdbc.url}
${jdbc.username}
${jdbc.password}
org.hibernate.dialect.MySQLDialect
true
true
update
true
com/ben/pojo/Product.hbm.xml
web.xml
struts2
org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter
struts2
*.action
org.springframework.web.context.ContextLoaderListener
contextConfigLocation
classpath:applicationContext.xml
index.jsp
注意
1、在发现无法注入sessionFactory的时候,记得在dao层添加
@Resource(name="sessionFactory")
public void setSessionFactory0(SessionFactory sessionFactory){
super.setSessionFactory(sessionFactory);
}
2、在出现事务不能为“readonly”的时候,如果用的是hibernate3。可以在web.xml中添加解决问题,但是在hibernate5的配置中没有效果,点击源码发现,flushMode这个变量已经不见了,在方法中的调用中也显示已经过时
openSessionInViewFilter
org.springframework.orm.hibernate3.support.OpenSessionInViewFilter
flushMode
AUTO
openSessionInViewFilter
*.action
3、要用事务的配置,在applicationContext.xml 中配置(注解版)
在applicationContext.xml中添加
dao层中的方法上添加
@Transactional(propagation=Propagation.REQUIRED,rollbackFor=Exception.class,timeout=10000,isolation=Isolation.DEFAULT)
4、事务配置,非注解版