1. applicationContext.xml文件
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans "
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-2.0.xsd ">
<!-- //用这个代替下面的sessionFactory会更好, dataSource不用定义了,因为hibernate.cfg.xml中已经定义了C3P0数据源
<bean id="sessionFactory"
class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="configLocation"
value="classpath:hibernate.cfg.xml">
</property>
</bean>
-->
<!-- 定义数据源Bean,使用C3P0数据源实现 -->
<bean id="dataSource"
class="com.mchange.v2.c3p0.ComboPooledDataSource"
destroy-method="close">
<!-- 指定连接数据库的驱动 -->
<property name="driverClass"
value="oracle.jdbc.driver.OracleDriver" />
<!-- 指定连接数据库的URL -->
<property name="jdbcUrl"
value="jdbc:oracle:thin:@192.168.36.11:1521:myproject" />
<!-- 指定连接数据库的用户名 -->
<property name="user" value="test" />
<!-- 指定连接数据库的密码 -->
<property name="password" value="test" />
<!-- 指定连接数据库连接池的最大连接数 -->
<property name="maxPoolSize" value="40" />
<!-- 指定连接数据库连接池的最小连接数 -->
<property name="minPoolSize" value="1" />
<!-- 指定连接数据库连接池的初始化连接数 -->
<property name="initialPoolSize" value="1" />
<!-- 指定连接数据库连接池的连接的最大空闲时间 -->
<property name="maxIdleTime" value="20" />
</bean>
<!-- 配置SessionFactory -->
<bean id="sessionFactory"
class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">
org.hibernate.dialect.Oracle9Dialect
</prop>
<prop key="hibernate.show_sql">true</prop>
<!-- 使用c3p0连接池 -->
<prop key="hibernate.connection.provider_class">
org.hibernate.connection.C3P0ConnectionProvider
</prop>
</props>
</property>
<property name="mappingResources">
<list>
<value>
com/happy/ssh/mapping/AdminMessage.hbm.xml
</value>
</list>
</property>
<property name="dataSource">
<ref bean="dataSource" />
</property>
</bean>
<!-- 配置TransactionManager -->
<bean id="transactionManager"
class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory" />
</bean>
<!-- 配置事务拦截器 -->
<bean id="transactionInterceptor"
class="org.springframework.transaction.interceptor.TransactionInterceptor">
<!-- 事务拦截器bean需要依赖注入一个事务管理器 -->
<property name="transactionManager" ref="transactionManager" />
<property name="transactionAttributes">
<!-- 下面定义事务传播属性-->
<props>
<prop key="insert*">PROPAGATION_REQUIRED</prop>
<prop key="find*">PROPAGATION_REQUIRED,readOnly</prop>
<prop key="*">PROPAGATION_REQUIRED</prop>
</props>
</property>
</bean>
<!-- Dao定义 -->
<bean id="adminMessageDao"
class="com.happy.ssh.dao.AdminMessageDao">
<property name="sessionFactory" ref="sessionFactory" />
</bean>
<!-- 定义BeanNameAutoProxyCreator创建代理( 注意: 可以定义多个BeanNameAutoProxyCreator)-->
<bean
class="org.springframework.aop.framework.autoproxy.BeanNameAutoProxyCreator">
<!-- 指定对满足哪些bean name的bean自动生成业务代理 -->
<property name="beanNames">
<!-- 下面是所有需要自动创建事务代理的bean,如*Dao,*Service等,-->
<list>
<value></value>
</list>
<!-- 此处可增加其他需要自动创建事务代理的bean-->
</property>
<!-- 下面定义BeanNameAutoProxyCreator所需的事务拦截器-->
<property name="interceptorNames">
<list>
<value>transactionInterceptor</value>
<!-- 此处可增加其他新的Interceptor -->
</list>
</property>
</bean>
</beans>
2、hibernate.cfg.xml文件
<?xml version='1.0' encoding='UTF-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd ">
<!-- Generated by MyEclipse Hibernate Tools. -->
<hibernate-configuration>
<session-factory>
<!-- 指定连接数据库的驱动 -->
<property name="connection.driver_class">
oracle.jdbc.driver.OracleDriver
</property>
<!-- 指定连接数据库的URL -->
<property name="connection.url">
jdbc:oracle:thin:@192.168.36.11:1521:myproject
</property>
<!-- 指定连接数据库的用户名 -->
<property name="connection.username">test</property>
<!-- 指定连接数据库的密码 -->
<property name="connection.password">test</property>
<!-- 指定连接数据库的方言 -->
<property name="dialect">
org.hibernate.dialect.Oracle9Dialect
</property>
<property name="format_sql">true</property>
<!-- 输出调试语句 -->
<property name="show_sql">true</property>
<!-- 指定连接数据库连接池的最大连接数 -->
<property name="c3p0.max_size">40</property>
<!-- 指定连接数据库连接池的最小连接数 -->
<property name="c3p0.min_size">1</property>
<mapping resource="com/happy/ssh/mapping/AdminMessage.hbm.xml"/>
</session-factory>
</hibernate-configuration>
3. AdminMessage.hbm.xml
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd ">
<hibernate-mapping package="com.hc360.model.domain">
<class name="Role" table="SS_ROLES" dynamic-insert="true" dynamic-update="true">
<id name="id" column="ID">
<generator class="native"/>
</id>
<property name="name" column="NAME" not-null="true"/>
<property name="descn" column="DESCN"/>
<!--
<set name="users" table="SS_USER_ROLE" lazy="true" inverse="true" cascade="save-update" batch-size="5">
<key>
<column name="ROLE_ID" not-null="true"/>
</key>
<many-to-many class="User" column="USER_ID" outer-join="auto"/>
</set>
-->
</class>
</hibernate-mapping>
Struts2,Spring,Hibernate整合例子一个
收藏
1.建立web项目
2.在web.xml 配置 struts2,和 spring ,
<!-- 配置Struts2 -->
<filter>
<filter-name>struts2</filter-name>
<filter-class>
org.apache.struts2.dispatcher.FilterDispatcher(struts的过滤器)
</filter-class>
</filter>
<filter-mapping>(加载struts2)
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<!-- 配置Spring -->
<listener>
<listener-class>
org.springframework.web.context.ContextLoaderListener(spring监听器)
</listener-class>
</listener>
<!-- 查找spring配置文件 -->
<context-param>(程序从此处开始加载.xml 文件,并执行)
<param-name>contextConfigLocation</param-name>
<param-value>classpath*:*.xml</param-value>
</context-param>
3.在src根目录下创建struts.xml文件
<include file="struts-default.xml"/>固定格式,用到此包
4.在src根目录 创建hibernate.cfg.xml文件
<?xml version='1.0' encoding='UTF-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd
">
<hibernate-configuration>
<session-factory>
<!-- 基本配置 -->
<property name="dialect">
org.hibernate.dialect.Oracle9Dialect
</property>
<property name="show_sql">false</property>
<property name="format_sql">true</property>
<property name="bytecode.use_reflection_optimizer">true</property>
<property name="max_fetch_depth">2</property>
<property name="default_batch_fetch_size">8</property>
<property name="jdbc.batch_size">20</property>
</session-factory>
</hibernate-configuration>
系统给的文件 只需要拷贝,都是一些数据库库的基本配置
5.在src根目录 拷贝struts.properties文件
struts.objectFactory = spring 该项是告诉系统 用spring来 控制hibernate和Action
struts.action.extension=action 告诉系统访问时以什么结尾。可以改为: do 例如:login.do ,此处为:login.action
6.在src根目录下 创建applicationContext.xml
配置数据源
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName" value ="oracle.jdbc.driver.OracleDriver"/> <!--jtds<property name="driverClassName" value ="net.sourceforge.jtds.jdbc.Driver"/>-->
<property name="url" value ="jdbc:oracle:thin:@192.168.0.95:1521:ZXDB2"/> <!--<property name="url" value="jdbc:jtds:sqlserver://localhost:1455/demo"></property>-->
<property name="username" value ="test"/>
<property name="password" value ="test"/>
</bean>
创建sessionFactory 工厂
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="dataSource">
<ref local="dataSource"/>
</property>
<property name="configLocation">
<value type="org.springframework.core.io.Resource">classpath:hibernate.cfg.xml</value>
</property>
</bean>
考来用
7.创建 实体类
就是写一个bean 含有get/set 方法
8.在实体类所在的目录下,写一个 类名.hbm.xml文件,在配置文件中添加属性
<?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
">
<hibernate-mapping>
<class name="com.zx.test.model.User" table="LB_USER">
<id name="id" type="java.lang.Long">主键的类型为Long
<column name="ID"/>
<generator class="sequence">
<param name="sequence">SEQ_LB_USER</param>指定主键生成方式,必须在pl/sql创建主键生成方式
</generator>
</id>
<property name="name" type="java.lang.String">
<column name="name" length="20"/>
</property>
<property name="age" type="java.lang.Integer">
<column name="age" length="2"/>
</property>
<property name="password" type="java.lang.String">
<column name="password" length="20"/>
</property>
所有属性的name 必须与 bean中的set后的名称一样
</class>
</hibernate-mapping>
9.创建一个 Dao 实现操作方法
10 创建 service 调用 Dao中的方法
11.创建 Action 将service的得到的user 加到Action中
12.在applicationContext.xml 中配置
将userDao注入到sessionFactory中
将userService 注入到UserDao中
将userAction 注入到 userService中
13.在struts.xml中 配置Action
<package name="user" extends="struts-default(固定与include对应)" namespace="/use(访问时路径)r">
<action name="userAdd(访问时路径)" class="userAction" method="userAdd">
<result name="success">/userShow.jsp</result>
</action>
</package>
此处配置了访问时的路径:/user/userAdd
14.将8中的配置文件 加入到 4 中
<mapping resource="com/zx/test/model/User.hbm.xml" />
引用配置文件
15.写jsp页面 表单提交到 Action
9.10.11:是 Action 调用 Service ,Service调用 Dao 也是分层的体现
Web.xml 是web工程的配置文件
ApplicationContext 是 spring的配置文件
Hibernate.hbm.xml 是hibernate的配置文件
建表是 注意创建主键时 name 为 表明
项目目录:
User.java
package com.zx.test.model;
public class User {
private Long id;
private String name;
private Integer age;
private String password;
// get/set方法
}
User.hbm.xml
<?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
">
<hibernate-mapping>
<class name="com.zx.test.model.User" table="LB_USER">
<id name="id" type="java.lang.Long">
<column name="ID"/>
<generator class="sequence">
<param name="sequence">SEQ_LB_USER</param>
</generator>
</id>
<property name="name" type="java.lang.String">
<column name="name" length="20"/>
</property>
<property name="age" type="java.lang.Integer">
<column name="age" length="2"/>
</property>
<property name="password" type="java.lang.String">
<column name="password" length="20"/>
</property>
</class>
</hibernate-mapping>
UserDao.java
package com.zx.test.dao;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
import com.zx.test.model.User;
public class UserDao extends HibernateDaoSupport {
/**
* 保存user
* @param user
*/
public void saveUser(User user){
// spring自带方法 保存
this.getHibernateTemplate().save(user);
}
//update, delete ,findAll 方法同上,省略!
}
UserService.java
package com.zx.test.service;
import com.zx.test.dao.UserDao;
import com.zx.test.model.User;
public class UserService {
private UserDao userDao;
public void setUserDao(UserDao userDao) {
this.userDao = userDao;
}
public void saveUser(User user){
userDao.saveUser(user);
}
// 在此处添加业务逻辑方法;
}
UserAction.java
package com.zx.test.action;
import com.opensymphony.xwork2.ActionSupport;
import com.zx.test.model.User;
import com.zx.test.service.UserService;
public class UserAction extends ActionSupport {
private User user;
public User getUser() {
return user;
}
public void setUser(User user) {
this.user = user;
}
private UserService userService ;
public void setUserService(UserService userService) {
this.userService = userService;
}
public String userAdd(){
this.userService.saveUser(user);
return SUCCESS;
}
//添加action方法
}
struts.xml:
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd
">
<struts>
<include file="struts-default.xml"/>
<package name="user" extends="struts-default" namespace="/user">
<action name="userAdd" class="userAction" method="userAdd">
<result name="success">/userShow.jsp</result>
</action>
</package>
</struts>
hibernate.cfg.xml
<?xml version='1.0' encoding='UTF-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd
">
<hibernate-configuration>
<session-factory>
<!-- 基本配置 -->
<property name="dialect">
org.hibernate.dialect.Oracle9Dialect
</property>
<property name="show_sql">false</property>
<property name="format_sql">true</property>
<property name="bytecode.use_reflection_optimizer">true</property>
<property name="max_fetch_depth">2</property>
<property name="default_batch_fetch_size">8</property>
<property name="jdbc.batch_size">20</property>
<mapping resource="com/zx/test/model/User.hbm.xml" />
</session-factory>
</hibernate-configuration>
applicationContext.xml
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName" value ="oracle.jdbc.driver.OracleDriver"/>
<property name="url" value ="jdbc:oracle:thin:@192.168.0.95:1521:ZXDB2"/>
<property name="username" value ="test"/>
<property name="password" value ="test"/>
</bean>
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="dataSource">
<ref local="dataSource"/>
</property>
<property name="configLocation">
<value type="org.springframework.core.io.Resource">classpath:hibernate.cfg.xml</value>
</property>
</bean>
<bean id="userDao" class="com.zx.test.dao.UserDao">
<property name="sessionFactory" ref="sessionFactory"></property>
</bean>
<bean id="userService" class="com.zx.test.service.UserService">
<property name="userDao" ref="userDao"></property>
</bean>
<bean id="userAction" class="com.zx.test.action.UserAction" scope="prototype">
<property name="userService" ref="userService"></property>
</bean>
web.xml:
<!-- 配置Struts2 -->
<filter>
<filter-name>struts2</filter-name>
<filter-class>
org.apache.struts2.dispatcher.FilterDispatcher
</filter-class>
<init-param>
<param-name>config</param-name>
<param-value>/WEB-INF/struts.xml </param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<!-- 配置Spring -->
<listener>
<listener-class>
org.springframework.web.context.ContextLoaderListener </listener-class>
</listener>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath*:*.xml</param-value>
</context-param>
index.jsp:
<form action="<%=request.getContextPath() %>/user/userAdd.action" name="form1" method="post">
userName: <input type ="text" name="user.name"><br>
password:<input type="password" name="user.password"><br>
age:<input type="text" name="user.age"><br>
<input type="submit" value="submit"> <input type="reset" value="reset">
</form>
userShow.jsp
在第一行添加 struts2 的标签
即:<%@ taglib prefix="s" uri="/struts-tags" %>
<body>
userName: <s:property value="user.name"/><br>
password:<s:property value="user.password"/><br>
age:<s:property value="user.age"/>
</body>
struts.properties 文件可要可无!(可以自己配置)