Spring SSH
来源:互联网 发布:java cors 跨域 编辑:程序博客网 时间:2024/06/05 16:28
配置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"?><beansxmlns="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.xsdhttp://www.springframework.org/schema/txhttp://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;//设置获取注入的sessionFactorypublic 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
阅读全文
0 0
- Spring SSH
- SSH-【struts+spring+hibernate】
- SSH中的spring配置文件
- SSH(Structs Spring Hibernate)
- SSH中的spring配置文件
- ssh (spring struts hibernate)
- SSH(struts+spring+hibernate)
- SSH 框架之Spring
- SSH(struts+spring+hibernate)
- SSH spring bens.xml
- ssh整合 spring getSession()
- ssh事务处理spring配置文件
- ssh 整合struts+spring
- SSH框架-Spring
- SSH(struts+spring+hibernate)
- SSH整合Spring事务管理
- spring and ssh 整合
- SSH-----Spring入门必读
- java对论坛排序操作
- 微信服务器配置(nginx+python+supervisord, etc:java)
- PHP实现多服务器SESSION共享
- 【动画】上下滑动
- HTML中的行级标签和块级标签
- Spring SSH
- MySQL中文取首字母实现
- MySQL存储过程实现更新UUID
- VirtualPlanetBuilder入门简介
- 添加对私有数据成员的支持
- 《Metasploit 魔鬼训练营》博客目录
- 客观题 编程综合
- java文件上传
- 题目1041:Simple Sorting