您的位置:首页 > 编程语言 > Java开发

spring,hibernate,struts2三大框架注解方式整合案例

2013-07-20 20:45 579 查看
本次整合所用各个框架的版本分别为hibernate-3.2.7,struts-2.3.14.3,spring-framework-2.5.6。

下面是所用到的jar包的截图





下面正式进行代码部分:

先是javabean部分:

package com.yc.bean;

import java.io.Serializable;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.EnumType;
import javax.persistence.Enumerated;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;

@Entity    //ejb3.0
@Table(name="t_emp")
public class Emp implements Serializable {
@Id
@GeneratedValue(strategy=GenerationType.AUTO)
private int eid;
@Column(length=20,nullable=false)
private String name;
@Column(length=18,nullable=false)
private String pwd;

//配置枚举
//@Enumerated(EnumType.STRING)   //以字符串存数据库
@Enumerated(EnumType.ORDINAL)    //以0,1,存
private Gender gender = Gender.MAN; // 枚举类型

public String getName() {
return name;
}

public String getDoPwd(){

return "....";
}

public int getEid() {
return eid;
}

public void setEid(int eid) {
this.eid = eid;
}

public void setName(String name) {
this.name = name;
}

public String getPwd() {
return pwd;
}

public void setPwd(String pwd) {
this.pwd = pwd;
}

public Gender getGender() {
return gender;
}

public void setGender(Gender gender) {
this.gender = gender;
}

}


package com.yc.bean;

public enum Gender {
MAN, WOMUN   //字表值

//0,1  数据值
}


接下来是dao层的构建:

package com.yc.dao;

import java.io.Serializable;
import java.util.List;

import org.hibernate.Session;
import org.hibernate.criterion.DetachedCriteria;
import org.springframework.orm.hibernate3.HibernateTemplate;

public interface IBaseDao {

public <T> boolean add(T entity) throws Exception;

public <T> Integer addAndGetId4Integer(T entity) throws Exception;

public <T> String addAndGetId4String(T entity) throws Exception;

public int executeByHql(String hql) throws Exception;

public <T> List<T> findByHql(String hql) throws Exception;

public int executeBySql(String sql) throws Exception;

public <T> List<T> findBySql(String sql) throws Exception;

public <T> boolean edit(T entity) throws Exception;

public boolean edit(String hql) throws Exception;

public int editByHql(String hql) throws Exception;

public <T> boolean remove(T entity) throws Exception;

public <T> T getById(Class<T> c, String id) throws Exception;

public <T> T getById(Class<T> c, Integer id) throws Exception;

public <T> T get(Class<T> c, Serializable id) throws Exception;

public <T> T get(String hql) throws Exception;

public <T> List<T> getList(String hql) throws Exception;

public boolean remove(String hql) throws Exception;

public <T> List<T> getList(Class<T> c) throws Exception;

public <T> List<T> getList(String hql, Object[] obj) throws Exception;

public List<?> showPage(String queryHql, String queryCountHql,
int firstResult, int maxResult) throws Exception;

// public <T> void showPage(String queryHql,String queryCountHql,Page<T>
// page) throws Exception;

public List<?> showPage(String queryCountHql, DetachedCriteria cResult,
int firstResult, int maxResult) throws Exception ;

public <T> List<T> find(DetachedCriteria sql) throws Exception;

public Session session();

public HibernateTemplate getTemplate();

}


package com.yc.dao.impl;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.criterion.DetachedCriteria;
import org.springframework.orm.hibernate3.HibernateTemplate;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
import org.springframework.stereotype.Repository;
import org.springframework.stereotype.Service;

import com.yc.dao.IBaseDao;

@Service("iBaseDao")
public class BaseDaoImpl extends HibernateDaoSupport implements IBaseDao {

/**
* 注意:HibernateDaoSupport中原有的setSessionFactory方法是一个最终方法,不可被重写
* 所以添加了一个setSessionFactory1方法来注入sessionFactory
*
* @param sessionFactory
*/

@Resource(name="sessionFactory")
public void setSessionFactory1(SessionFactory sessionFactory) {
this.setSessionFactory(sessionFactory);
}

public <T> boolean add(T entity) throws Exception {
boolean bo = false;

try {
Serializable io = this.getHibernateTemplate().save(entity);
if (io != null) {
bo = true;
}
} catch (Exception e) {
bo = false;
throw new RuntimeException(e);
}

return bo;
}

public <T> Integer addAndGetId4Integer(T entity) throws Exception {
Integer id = null;

try {
id = (Integer) this.getHibernateTemplate().save(entity);
} catch (Exception e) {
throw new RuntimeException(e);
}

return id;
}

public <T> String addAndGetId4String(T entity) throws Exception {
String id = null;

try {

id = (String) this.getHibernateTemplate().save(entity);

} catch (Exception e) {
throw new RuntimeException(e);
}
return id;
}

public int executeByHql(String hql) throws Exception {

try {
return this.getHibernateTemplate().bulkUpdate(hql);
} catch (Exception e) {
// TODO Auto-generated catch block
throw new RuntimeException(e);
}

}

public <T> List<T> findByHql(String hql) throws Exception {
List list = null;
try {
list = (List<T>) this.getHibernateTemplate().find(hql);
} catch (Exception e) {
// TODO Auto-generated catch block
throw new RuntimeException(e);
}
return list;
}

public int executeBySql(String sql) throws Exception {
try {
return this.getSession().createSQLQuery(sql).executeUpdate();
} catch (Exception e) {
// TODO Auto-generated catch block
throw new RuntimeException(e);
}

}

public <T> List<T> findBySql(String sql) throws Exception {
List list = null;
try {
list = (List<T>) this.getSession().createSQLQuery(sql).list();
} catch (Exception e) {
// TODO Auto-generated catch block
throw new RuntimeException(e);
}
return list;
}

public <T> boolean edit(T entity) throws Exception {
boolean bo = false;

try {
this.getHibernateTemplate().update(entity);
bo = true;
} catch (Exception e) {
bo = false;
throw new RuntimeException(e);
}

return bo;
}

public boolean edit(String hql) throws Exception {
boolean bo = false;
try {
int count = this.getHibernateTemplate().bulkUpdate(hql);
bo = count > 0 ? true : false;
} catch (Exception e) {
bo = false;
throw new RuntimeException(e);
}
return bo;
}

public int editByHql(String hql) throws Exception {
int count = 0;
try {
count = this.getHibernateTemplate().bulkUpdate(hql);
} catch (Exception e) {
throw new RuntimeException(e);
}
return count;
}

public <T> boolean remove(T entity) throws Exception {
boolean bo = false;

try {
this.getHibernateTemplate().delete(entity);
bo = true;
} catch (Exception e) {
bo = false;
throw new RuntimeException(e);
}
return bo;
}

public <T> T getById(Class<T> c, String id) throws Exception {
T ety = null;
try {
ety = (T) this.getHibernateTemplate().get(c, id);
} catch (Exception e) {
throw new RuntimeException(e);
}

return ety;
}

public <T> T getById(Class<T> c, Integer id) throws Exception {
T ety = null;
try {
ety = (T) this.getHibernateTemplate().get(c, id);
} catch (Exception e) {
throw new RuntimeException(e);
}

return ety;
}

public <T> T get(Class<T> c, Serializable id) throws Exception {
T ety = null;
try {
ety = (T) this.getHibernateTemplate().get(c, id);
} catch (Exception e) {
throw new RuntimeException(e);
}
return ety;
}

public <T> T get(String hql) throws Exception {
T ety = null;
try {
ety = (T) this.getSession().createSQLQuery(hql).setMaxResults(1);
} catch (Exception e) {
throw new RuntimeException(e);
}
return ety;
}

public <T> List<T> getList(String hql) throws Exception {
List<T> list = null;
try {
list = this.getHibernateTemplate().find(hql);
} catch (Exception e) {
throw new RuntimeException(e);
}
return list;
}

public boolean remove(String hql) throws Exception {
try {
return this.executeByHql(hql) > 0 ? true : false;
} catch (Exception e) {
throw new RuntimeException(e);
}
}

public <T> List<T> getList(Class<T> c) throws Exception {
List<T> list = null;
try {
list = (List<T>) this.getHibernateTemplate().findByCriteria(
DetachedCriteria.forClass(c));
} catch (Exception e) {
throw new RuntimeException(e);
}
return list;
}

public <T> List<T> getList(String hql, Object[] obj) throws Exception {
List<T> list = null;
try {
list = (List<T>) this.getHibernateTemplate().find(hql, obj);
} catch (Exception e) {
throw new RuntimeException(e);
}
return list;
}

public List<?> showPage(String queryHql, String queryCountHql,
int firstResult, int maxResult) throws Exception {
List<Object> list = new ArrayList<Object>();
try {
Session session = this.session();
list.add(session.createQuery(queryHql).setFirstResult(firstResult)
.setMaxResults(maxResult).list());
list.add(session.createQuery(queryCountHql).setMaxResults(1)
.uniqueResult());

} catch (Exception e) {
throw new RuntimeException(e);
}

return list;
}

public List<?> showPage(String queryCountHql, DetachedCriteria cResult,
int firstResult, int maxResult) throws Exception {
List<Object> list = new ArrayList<Object>();
try {
Session session = this.getSession();
list.add(this.getHibernateTemplate().findByCriteria(cResult,
firstResult, maxResult));

list.add(session.createQuery(queryCountHql).setMaxResults(1)
.uniqueResult());
} catch (Exception e) {
throw new RuntimeException(e);
}
return list;
}

public <T> List<T> find(DetachedCriteria dc) throws Exception {
List<T> list = new ArrayList<T>();
try {
list = this.getHibernateTemplate().findByCriteria(dc);

} catch (Exception e) {
throw new RuntimeException(e);
}

return list;
}

public Session session() {
// TODO Auto-generated method stub
return this.getSession();
}

public HibernateTemplate getTemplate() {
// TODO Auto-generated method stub
return this.getHibernateTemplate();
}

}


再接下来是biz层:

package com.yc.service;

import java.util.List;

import com.yc.bean.Emp;

public interface EmpService {

public List<Emp> findAll() throws Exception;

}


package com.yc.service.impl;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Repository;

import com.yc.bean.Emp;
import com.yc.dao.IBaseDao;
import com.yc.service.EmpService;

@Repository("empService")
public class EmpServiceImpl implements EmpService {

@Resource
private IBaseDao iBaseDao;

public IBaseDao getiBaseDao() {
return iBaseDao;
}

public void setiBaseDao(IBaseDao iBaseDao) {
this.iBaseDao = iBaseDao;
}

public List<Emp> findAll() throws Exception {
// TODO Auto-generated method stub
return iBaseDao.getList(Emp.class);
}

}


下面是actions:

package com.yc.actions;

import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.interceptor.SessionAware;
import org.springframework.stereotype.Controller;

import com.opensymphony.xwork2.ActionSupport;
import com.yc.bean.Emp;
import com.yc.service.EmpService;

@Controller("empAction")
public class EmpAction extends ActionSupport implements SessionAware {

@Resource(name = "empService")
private EmpService empService;

Map<String, Object> session;

public void setSession(Map<String, Object> arg0) {
// TODO Auto-generated method stub
this.session = arg0;
}

@Action(value = "emp_findAll", results = { @Result(name = "success", location = "/show.jsp", type = "redirect") })
public String findAll() {
List<Emp> list = null;

try {
list = this.empService.findAll();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
session.put("list", list);

return "success";

}

public EmpService getEmpService() {
return empService;
}

public void setEmpService(EmpService empService) {
this.empService = empService;
}

}


再下面是struts2.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>

<!-- 禁用动态方法调用  XXX!XXX -->
<constant name="struts.enable.DynamicMethodInvocation" value="false" />
<!-- 禁用开发模式 -->
<constant name="struts.devMode" value="false" />

<!-- struts2使用自己的对象工厂来创建和管理action对象,但与spring整合,我们要转spring来创建和管理action对象 -->
<constant name="struts.objectFactory" value="spring"></constant>
<constant name="struts.configuration.xml.reload" value="true"></constant>
<package name="default" namespace="/" extends="struts-default">

</package>

</struts>


下面是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" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" version="2.5">
<display-name></display-name>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>

<!-- spring提供的字符过滤器 -->
<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>
<init-param>
<param-name>forceEncoding</param-name>
<param-value>true</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>encodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>

<!-- 配置session的过期时间为10分钟 -->
<session-config>
<session-timeout>10</session-timeout>
</session-config>

<!-- 配置全层的错误页面 -->
<error-page>
<exception-type>java.lang.Throwable</exception-type>
<location>/common/error.jsp</location>
</error-page>

<!--  配置spring提供的用来解决lazy加载时出现session closed错误  ,注意,一定要在struts2过滤器之前使用 -->
<filter>
<filter-name>openSessionInView</filter-name>
<filter-class>org.springframework.orm.hibernate3.support.OpenSessionInViewFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>openSessionInView</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<filter>

<!-- struts2的过滤器 -->
<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>

<!-- spring的监听器,用来初始化spring的容器 -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>

<!-- 启动spring刷新  ,防止程序内存溢出问题 -->
<listener>
<listener-class>org.springframework.web.util.IntrospectorCleanupListener</listener-class>
</listener>
</web-app>


下面是重点applocationContext.xml的配置,我们这里用c3p0来配置数据库,还的一种是用dbcp来配置:

<?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:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd"> 
<bean id="myDataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"
destroy-method="close">
<property name="driverClass" value="com.microsoft.sqlserver.jdbc.SQLServerDriver" />
<property name="jdbcUrl"
value="jdbc:sqlserver://localhost:1433;databaseName=s2sh" />
<property name="user" value="sa" />
<property name="password" value="XXX" />

<!-- 初始化时获取三个连接,取值应在minPoolSize与maxPoolSize之间。Default: 3 -->
<property name="initialPoolSize" value="10"></property>

<!--连接池中保留的最小连接数。Default: 15 -->
<property name="minPoolSize" value="5"></property>

<!--连接池中保留的最大连接数。Default: 15 -->
<property name="maxPoolSize" value="100"></property>
</bean>

<bean id="sessionFactory"
class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
<property name="dataSource" ref="myDataSource" />
<!-- <property name="annotatedClasses"> 应配置用来扫描一个路径下所有配置文件 <list> <value>com.yc.bean.Emp</value>
</list> </property> -->
<property name="packagesToScan" value="com.yc.bean"></property><!-- 通过配置用来扫描一个路径下所有配置文件 -->
<property name="hibernateProperties">
<value>
hibernate.dialect=org.hibernate.dialect.SQLServerDialect
hibernate.show_sql =true
hibernate.format_sql =true
hibernate.jdbc.batch_size=30
hibernate.jdbc.fetch_size=100
hibernate.max_fetch_depth=2
<!--hibernate.hbm2ddl.auto=create hibernate.hbm2ddl.auto=create 因为现在是通过
对象来创建数据表,所以请一定要配置 hbm2dd,但只能用一次, 用完就要注释掉 hibernate.jdbc.batch_size 一次更新30条语句,更新完则清空hibernate缓存.
hibernate.jdbc.fetch_size 一次查询多少条数据. hibernate.hbm2ddl.auto =create 因为现在是通过对象来创数据库表,一定要配置hibernate.hbm2ddl.auto,但只能用一次,用完就要删除掉 -->
</value>
</property>
</bean>

<!-- 自动扫描 -->
<context:component-scan base-package="com.yc" />

<!-- 启用切面技术 -->
<aop:aspectj-autoproxy />

<!-- 启用事务注解支持 事务管理器的默认名为 transactionManager -->
<tx:annotation-driven transaction-manager="transactionManager" />

<!-- 事务处理 -->
<bean id="transactionManager"
class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory" />
</bean>

<!-- 配置切入点 -->
<aop:config>
<aop:pointcut id="projectServiceMethods" expression="execution(* com.yc.biz.impl.*.*(..))" />
<aop:advisor advice-ref="txAdvice" pointcut-ref="projectServiceMethods" />
</aop:config>
<!-- 配置增强 -->
<tx:advice id="txAdvice" transaction-manager="transactionManager">
<tx:attributes>
<tx:method name="find*" propagation="SUPPORTS" read-only="true" /><!-- 以find开头的方法不作事务处理,但如果程序内部有代码实现的事务会执行,但spring的事务不会加入其中 -->
<tx:method name="load*" propagation="SUPPORTS" read-only="true" />
<tx:method name="get*" propagation="SUPPORTS" read-only="true" />
<tx:method name="*" propagation="REQUIRED" /><!-- 一定加入事务处理 -->

</tx:attributes>
</tx:advice>

</beans>


下面是jsp页面代码:

index.jsp

<%@ page language="java" import="java.util.*" pageEncoding="ISO-8859-1"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">

<title>My JSP 'index.jsp' starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
</head>

<body>
<a href="emp_findAll">select all</a>
</body>
</html>


show.jsp

<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>

<%@ taglib prefix="s" uri="/struts-tags"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme() + "://"
+ request.getServerName() + ":" + request.getServerPort()
+ path + "/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">

<title>My JSP 'index.jsp' starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
</head>

<body>

<s:iterator value="#session.list" var="item">
<s:property value="#item.eid" />-----<s:property value="#item.name" />-----<s:property
value="#item.doPwd" />-----<s:property value="#item.gender"/>
</s:iterator>

</body>
</html>


至此,三大框架整合完毕,本人亲测成功

最后,贴点错误:

包异常: www.findjar.net

nested exception is java.lang.NoClassDefFoundError: org/slf4j/LoggerFactory

解决方案: 使用 log4j.jar, slf4j-api-1.5.6.jar, slf4j-log4j12-1.4.3.jar 包, 这个三包是hibernate annotation要用的包, 并请注意将原来hibernate 中导入的log4j-1.2.jar去掉.

装配异常:

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'org.springframework.transaction.interceptor.TransactionInterceptor#0': Cannot resolve reference to bean 'transactionManager' while setting bean property 'transactionManager'; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'transactionManager' is defined

这表示找不到id名为transactionManager 事务管理器对象. 因为我们开启的事务的注解配置

<!-- 启用事务注解支持 @Transaction -->

<tx:annotation-driven /> ,而这个配置mo认使用了名为 transactionManager的事务管理器对象,但现程序中配置的transactionManager对象名却不叫transactionManager, 所以报错.

解决方案: 将事务管理器的声明部分的名字改为transactionManager即可.

<bean id="transactionManager"

class="org.springframework.orm.hibernate3.HibernateTransactionManager">

<property name="sessionFactory" ref="sessionFactory" />

</bean>

下面贴下jar下载地址:http://download.csdn.net/detail/surehao/5789087
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: