Spring2+Strutrs2+Hibernate3整合过程总结
来源:互联网 发布:html标签保存到数据库 编辑:程序博客网 时间:2024/05/22 00:37
第一步:关于web.xml
1.用于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>
2.用于spring的配置,
<context-param> <param-name>contextConfigLocation</param-name> // 这个不能随意取名字,必须是这个 <param-value>classpath:applicationContext.xml</param-value> </context-param>
<listener>
<span style="white-space:pre"></span>//启动Web容器时,自动装配ApplicationContext的配置信息 <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener>
第二步:关于applicationContext.xml
1.用于hibernate连接数据库的配置,
<bean id="datasource" class="org.apache.commons.dbcp.BasicDataSource"> //这里的class是hibernate的<property name="driverClassName" value="com.mysql.jdbc.Driver"></property><property name="url" value="jdbc:mysql://127.0.0.1:3306/bookshop"></property><property name="username" value="root"></property><property name="password" value="mysql"></property></bean>
<span style="white-space:pre"></span><bean id="sessionFactory"class="org.springframework.orm.hibernate3.LocalSessionFactoryBean"><property name="dataSource" ref="datasource"></property> //利用上边定义好的bean<property name="hibernateProperties"><props><prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop></props></property><property name="configLocation" value="classpath:hibernate.cfg.xml"></property></bean>当DAO层用到sessionFactory时,
<pre name="code" class="java">public class UserDAOImp implements UserDAO {SessionFactory sessionFactory; // 没有使用继承接口的方式public List search(Users user) {Session session = sessionFactory.openSession();Transaction ts = null;/* * Users user1 = new Users(); user1.setPassword("123"); * user1.setUsername("andrew"); */ts = session.beginTransaction();String hql = "from Users where username=? and password=?";Query query = session.createQuery(hql);query.setParameter(0, user.getUsername(),Hibernate.STRING);query.setParameter(1, user.getPassword(),Hibernate.STRING);List<Users> list = query.list();ts.commit();return list;}public SessionFactory getSessionFactory() {return sessionFactory;}public void setSessionFactory(SessionFactory sessionFactory) {this.sessionFactory = sessionFactory;}}
<bean id="userDAO" class="com.andrew.dao.UserDAOImp"><property name="sessionFactory" ref="sessionFactory"></property></bean>
第三步:关于struts.xml
<package name="default" extends="struts-default"><action name="login" class="userAction" method="login"> //这里的class的value是使用spring配置文件里的bean设置的id<result name="success">main.jsp</result><result name="error">error.jsp</result></action></package>
0 0
- Spring2+Strutrs2+Hibernate3整合过程总结
- Struts2+Spring2.5+Hibernate3.3 整合总结
- Struts2+Spring2.5+Hibernate3.3 整合总结
- Struts2+Spring2+Hibernate3整合
- Struts1.2+Spring2.0+Hibernate3.1整合总结 MyEclipse5.5
- Struts2.1.6 + Spring2.5.6 + Hibernate3.3.2 整合总结
- 关于整合Struts2+Spring2+ Hibernate3
- Struts2+Spring2+Hibernate3 整合开发
- struts2+spring2+hibernate3整合SiteMesh
- Spring2整合Hibernate3.1问题
- 整合struts2+spring2+hibernate3.1
- Struts2+spring2+hibernate3整合方案
- Struts2+spring2+hibernate3整合方案
- 整合struts2+hibernate3+spring2.5
- spring2.5整合hibernate3.0
- spring2.5整合hibernate3.0整合Struts
- struts2.0+spring2.0+hibernate3.1 多层模型整合 完整过程 常见错误
- struts2.0+spring2.0+hibernate3.1多层模型整合完整过程 常见错误(转)
- C++ 前置操作符与后置操作符
- xml解析和序列化
- 剑指offer--二叉树的下一个结点
- 完整Debian7配置LAMP(Apache/MySQL/PHP)环境及搭建建站
- Linux中fork()函数详解
- Spring2+Strutrs2+Hibernate3整合过程总结
- 信号
- 音效的播放
- static_cast, dynamic_cast, const_cast学习和探讨
- Hdoj 1213 How Many Tables 【并查集】
- 调用函数的冒泡排序法
- Pktgen-DPDK使用说明
- 图解正向代理、反向代理、透明代理
- C++面试宝典2011版