Spring SSH

配置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" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
  <display-name>hibernate4_01</display-name>
  
  	<!-- 配置spring的监听器 -->
  	<listener>
  		<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  	</listener>
    <!-- 配置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>/*</url-pattern>
	</filter-mapping>
  <welcome-file-list>
    <welcome-file>index.html</welcome-file>
    <welcome-file>index.htm</welcome-file>
    <welcome-file>index.jsp</welcome-file>
    <welcome-file>default.html</welcome-file>
    <welcome-file>default.htm</welcome-file>
    <welcome-file>default.jsp</welcome-file>
  </welcome-file-list>
</web-app>

配置 WEB-INF/applicationContext.xml 配置

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<beans
	xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xmlns:tx="http://www.springframework.org/schema/tx"
	xsi:schemaLocation=
					"http://www.springframework.org/schema/beans 
					http://www.springframework.org/schema/beans/spring-beans.xsd
					http://www.springframework.org/schema/tx
					http://www.springframework.org/schema/tx/spring-tx.xsd">
<!-- 1.配置数据源 -->
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
	<property name="driverClassName" value="oracle.jdbc.driver.OracleDriver"/>
	<property name="url" value="jdbc:oracle:thin:@localhost:1521:orcl"/>
	<property name="username" value="scott"/>
	<property name="password" value="tiger"/>
</bean>
<!-- 2.配置sessionFactory -->
<bean id="sessionFactory" 
	  class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
	<!-- 配置数据源 -->
	<property name="dataSource" ref="dataSource"/>
	<!-- 配置hibernate的一些常见属性 -->
	<property name="hibernateProperties">
		<props>
			<prop key="hibernate.show_sql">true</prop>
			<prop key="hibernate.dialect">org.hibernate.dialect.Oracle10gDialect</prop>
			<prop key="hibernate.connection.autocommit">false</prop>
		</props>
	</property>
	<!-- 配置映射文件(hbm) -->
	<property name="mappingResources">
		<list>
			<value>com/minde/po/Dept.hbm.xml</value>
			<value>com/minde/po/Emp.hbm.xml</value>
		</list>
	</property>
</bean>
<!-- 配置dao -->
<bean id="bd" class="com.minde.dao.BaseDaoImpl" scope="prototype">
	<property name="sessionFactory" ref="sessionFactory"/>
</bean>
<!-- 配置事务管理器 -->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
	<property name="dataSource" ref="dataSource"/>
</bean>
</beans>

dao层BaseDaoImpl

package com.minde.dao;

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

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;


public class BaseDaoImpl implements IBaseDao {
	
	private SessionFactory sessionFactory;
	//设置获取注入的sessionFactory
public void setSessionFactory(SessionFactory sessionFactory) {this.sessionFactory = sessionFactory;}//列表@Overridepublic List findAll(String hql) {Session session = this.sessionFactory.openSession();Query q = session.createQuery(hql);List list = q.list();session.close();return list;}@Overridepublic void save(Object obj) {Session session = this.sessionFactory.openSession();session.beginTransaction();session.save(obj);session.getTransaction().commit();session.close();}@Overridepublic void update(Object obj) {Session session = this.sessionFactory.openSession();session.beginTransaction();session.merge(obj);session.getTransaction().commit();session.close();}@Overridepublic Object findObjectById(Class clazz, Serializable id) {Session session = this.sessionFactory.openSession();Object obj = session.get(clazz, id);session.close();return obj;}@Overridepublic void delete(Object obj) {Session session = this.sessionFactory.openSession();session.beginTransaction();session.delete(obj);session.getTransaction().commit();session.close();}}


在action类中的dao对象一定要加入JavaBean(get和set)不然无法获取到注入的sessionFactory

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值