将项目中的对象和对象之间的管理,纳入spring容器,由spring管理
1 实现spring+hibernate集成
1.1 新建web项目
建立项目的包结构(package)
1.2加入jar包
1.3 建立pojo类
package org.guangsoft.pojo;
/***
* 定部门的pojo类
* **/
public class Dept
{
private Integer did;
private String dname;
private String ddesc;
public Integer getDid()
{
return did;
}
public void setDid(Integer did)
{
this.did = did;
}
public String getDname()
{
return dname;
}
public void setDname(String dname)
{
this.dname = dname;
}
public String getDdesc()
{
return ddesc;
}
public void setDdesc(String ddesc)
{
this.ddesc = ddesc;
}
}
1.4 建立pojo的映射文件
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<hibernate-mapping package="com.bjsxt.pojo">
<!-- 类 到 表 -->
<class name="Dept" table="t_dept">
<id name="did" column="did" type="java.lang.Integer">
<generator class="native"></generator>
</id>
<!-- 其他简单属性 -->
<property name="dname" column="dname" type="java.lang.String"></property>
<property name="ddesc" column="ddesc" type="java.lang.String"></property>
</class>
</hibernate-mapping>
1.5建立Dao接口
package org.guangsoft.dao;
import org.guangsoft.pojo.Dept;
/**
* 部门数据访问接口
* ***/
public interface DeptDao
{
public void addDept(Dept dept);
}
1.6建立Dao接口的实现类
package org.guangsoft.dao.impl;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
import org.springframework.stereotype.Repository;
import org.guangsoft.dao.DeptDao;
import org.guangsoft.pojo.Dept;
/***
* 建立dao接口实现类:
* extends HibernateDaoSupport :
* 完成在dao类中获得session对象,hibernateTempldate对象
* ***/
@Repository
public class DeptDaoImpl extends HibernateDaoSupport
implements DeptDao
{
/***
* 给父类注入sessionFactory通过自动装配
* ***/
// private SessionFactory sessionFactory;
@Autowired
public void setSessionFactory01(SessionFactory sessionFactory)
{
super.setSessionFactory(sessionFactory);
}
@Override
public void addDept(Dept dept)
{
super.getHibernateTemplate().save(dept);
}
}
1.7建立业务service接口
package org.guangsoft.service;
import org.guangsoft.pojo.Dept;
/**
* 部门的业务接口
* ***/
public interface DeptService
{
public void saveDeptService(Dept dept);
}
1.8 建立service接口实现类
package org.guangsoft.service.impl;
import org.guangsoft.dao.DeptDao;
import org.guangsoft.pojo.Dept;
import org.guangsoft.service.DeptService;
/***
* 部门业务接口实现类
* ***/
public class DeptServiceImpl implements DeptService
{
// 声明dao对象
private DeptDao deptDao;
@Override
public void saveDeptService(Dept dept)
{
deptDao.addDept(dept);
}
}
1.9配置spring容器
applicationContext.xml
<?xml version="1.0" encoding="UTF-8"?>
<!-- 到入xml文件的约束 -->
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:aop="http://www.springframework.org/schema/aop" xmlns:p="http://www.springframework.org/schema/p"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.1.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-4.1.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.1.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-4.1.xsd
">
<!-- 加载db.xml -->
<import resource="db.xml" />
<!-- 加载transaction.xml -->
<import resource="transaction.xml" />
<!-- 开启注解扫描 -->
<context:component-scan
base-package="org.guangsoft.action,org.guangsoft.service.impl,
org.guangsoft.dao.impl"></context:component-scan>
</beans>
db.xml
<?xml version="1.0" encoding="UTF-8"?>
<!-- 到入xml文件的约束 -->
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:aop="http://www.springframework.org/schema/aop" xmlns:p="http://www.springframework.org/schema/p"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.1.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-4.1.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.1.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-4.1.xsd
">
<!-- 配置数据库连接池(DataSource) -->
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<!-- 输入连接池的属性 -->
<property name="driverClass" value="com.mysql.jdbc.Driver"></property>
<property name="jdbcUrl" value="jdbc:mysql://localhost:3306/ssh"></property>
<property name="user" value="root"></property>
<property name="password" value="1111"></property>
</bean>
<!-- 实例化sessionFactory -->
<bean id="sessionFactory"
class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<!--注入数据库连接池 -->
<property name="dataSource" ref="dataSource"></property>
<!-- 配置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>
<!-- 加载hibernate的映射文件 -->
<property name="mappingResources">
<list>
<value>org/guangsoft/pojo/Dept.hbm.xml</value>
</list>
</property>
</bean>
</beans>
Transaction.xml
<?xml version="1.0" encoding="UTF-8"?>
<!-- 到入xml文件的约束 -->
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:aop="http://www.springframework.org/schema/aop" xmlns:p="http://www.springframework.org/schema/p"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.1.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-4.1.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.1.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-4.1.xsd
">
<!-- 实例化事务管理器对象 -->
<bean id="transactionManager"
class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<!-- 注入sessionFactory -->
<property name="sessionFactory" ref="sessionFactory">
</property>
</bean>
<!-- 声明事务特性 -->
<tx:advice id="txAdvice" transaction-manager="transactionManager">
<tx:attributes>
<tx:method name="add*" propagation="REQUIRED" isolation="DEFAULT" />
<tx:method name="delete*" propagation="REQUIRED" isolation="DEFAULT" />
<tx:method name="update*" propagation="REQUIRED" isolation="DEFAULT" />
<tx:method name="save*" propagation="REQUIRED" isolation="DEFAULT" />
<tx:method name="get*" propagation="REQUIRED" isolation="DEFAULT"
read-only="true" />
<tx:method name="select*" propagation="REQUIRED" isolation="DEFAULT"
read-only="true" />
<tx:method name="*" propagation="REQUIRED" isolation="DEFAULT"
read-only="true" />
</tx:attributes>
</tx:advice>
<!-- 进行aop的配置 -->
<aop:config>
<!-- 声明切入点 -->
<aop:pointcut expression="execution(* org.guangsoft.service.impl.*.*(..))"
id="pc" />
<!-- 织入 -->
<aop:advisor advice-ref="txAdvice" pointcut-ref="pc" />
</aop:config>
</beans>
1.10 建立测试类
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class TestApp
{
@Test
public void addDept()
{
// 加载spring的配置文件,获得bean容器
ApplicationContext ac = new
ClassPathXmlApplicationContext("applicationContext.xml");
// 获得bean对象
DeptService deptService = (DeptService) ac.getBean("deptServiceImpl");
// 添加部门
Dept dept = new Dept();
dept.setDname("安慰部");
dept.setDdesc("逗你玩");
deptService.saveDeptService(dept);
}
}
2 spring+struts2集成
2.1加入jar
Struts2-spring-plugin.jar
2.2建立Action
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import com.opensymphony.xwork2.Action;
import com.opensymphony.xwork2.ModelDriven;
@Controller
@Scope("prototype")
// DeptAction在spring容器中是非单例的
public class DeptAction implements ModelDriven<Dept>
{
// 自动装配
@Autowired
private DeptService deptService;
// 声明部门对象
private Dept dept = new Dept();
@Override
public Dept getModel()
{
return dept;
}
/***
* 处理部门的添加请求
* **/
public String addDept()
{
deptService.saveDeptService(dept);
return Action.SUCCESS;
}
}
2.3进行struts2的配置
<?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>
<package name="dept" namespace="/" extends="struts-default">
<!-- class:是Action在spring容器中对应的id -->
<action name="deptAction_*" class="deptAction" method="{1}">
<result>/index.jsp</result>
</action>
</package>
</struts>
3.4在web.xml中配置
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" 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_2_5.xsd">
<display-name></display-name>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<!-- 加载spring的配置文件 -->
<!-- 配置上下的初始化参数application -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml</param-value>
</context-param>
<!--通过监听器加载spring的配置iwenijan -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!-- 配置open Session in view -->
<filter>
<filter-name>osiv</filter-name>
<filter-class>org.springframework.orm.hibernate3.support.OpenSessionInViewFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>osiv</filter-name>
<url-pattern>*.action</url-pattern>
</filter-mapping>
<!-- 配置struts2的核心控制器 -->
<filter>
<filter-name>struts</filter-name>
<filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>struts</filter-name>
<url-pattern>*.action</url-pattern>
</filter-mapping>
<filter-mapping>
<filter-name>struts</filter-name>
<url-pattern>*.jsp</url-pattern>
</filter-mapping>
</web-app>
2.5建立UI页面
<%@ 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>
<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">
</head>
<body>
<s:form action="deptAction_addDept.action" method="post" theme="simple">
<div>
部门名称:
<s:textfield name="dname"></s:textfield>
</div>
<div>
部门描述:
<s:textfield name="ddesc"></s:textfield>
</div>
<div>
<s:submit value="提交"></s:submit>
</div>
</s:form>
</body>
</html>