struts2+hibernate+spring配置详解
来源:互联网 发布:知乎 出书 编辑:程序博客网 时间:2024/04/28 22:48
struts2+hibernate+spring配置详解
struts2+hibernate+spring配置详解
哎 ,当初一个人做好难,现在终于弄好了,希望自学这个的能少走些弯路。
以下是自己配置的案例
注意,要想明白的比较好,请下载这个配套的资料,献给初学者(这是获取资料的超链接,点击下载)
配置完成后
1,建立web项目ssh51
2,导入所有的jar包,包括Struts2,spring,hibernate
3.在项目下建立3个资源文件
4,建立持久化类和文件
package com.biluo.domain;public class Person { private Integer id; private String name; private String password; public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; }}
<?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 package="com.biluo.domain"> <class name="Person" table="person"> <id name="id" type="java.lang.Integer" column="id"> <!--generator用于指定主键的生成策略, hilo native increment sequence uuid --> <generator class="native" /> </id> <property name="name" type="java.lang.String" > <column name="name" /> </property> <property name="password" type="java.lang.String" > <column name="password" /> </property> </class></hibernate-mapping>
5,在config目录下建立3个配置包 分别是 struts2 ,hibernate ,spring
6,建立一个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> <!-- 一个sessionFactory代表数据库的一个连接 --><session-factory></session-factory></hibernate-configuration>
7.配置Spring,模块化
/ssh51/config/spring/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" xmlns:context="http://www.springframework.org/schema/context" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:ehcache="http://www.springmodules.org/schema/ehcache" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd http://www.springmodules.org/schema/ehcache http://www.springmodules.org/schema/cache/springmodules-ehcache.xsd"> <import resource="applicationContext-db.xml"/><!-- 引入数据库配置文件 --> <import resource="applicationContext-people.xml"/><!-- 引入项模块 --></beans>
/ssh51/config/spring/applicationContext-db.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" xmlns:context="http://www.springframework.org/schema/context" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:ehcache="http://www.springmodules.org/schema/ehcache" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd http://www.springmodules.org/schema/ehcache http://www.springmodules.org/schema/cache/springmodules-ehcache.xsd"> <!-- 按照指定的路径加载配置文件 --> <bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> <property name="locations"> <value>classpath:jdbc.properties</value> </property> </bean> <bean id="dataSource" destroy-method="close" class="org.apache.commons.dbcp.BasicDataSource"> <property name="driverClassName" value="${jdbc.driverClassName}" /> <property name="url" value="${jdbc.url}" /> <property name="username" value="${jdbc.username}" /> <property name="password" value="${jdbc.password}" /> </bean></beans>
/ssh51/config/spring/applicationContext-people.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" xmlns:context="http://www.springframework.org/schema/context" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:ehcache="http://www.springmodules.org/schema/ehcache" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd http://www.springmodules.org/schema/ehcache http://www.springmodules.org/schema/cache/springmodules-ehcache.xsd"></beans>
8,配置数据库连接参数,写在一个文件中比较科学
/ssh51/config/jdbc.properties
jdbc.driverClassName=com.mysql.jdbc.Driverjdbc.url=jdbc\:mysql\://localhost\:3306/spring_hibernatejdbc.username=rootjdbc.password=541711153
9,配置sessionFactory这个很重要
/ssh51/config/spring/applicationContext-db.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" xmlns:context="http://www.springframework.org/schema/context" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:ehcache="http://www.springmodules.org/schema/ehcache" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd http://www.springmodules.org/schema/ehcache http://www.springmodules.org/schema/cache/springmodules-ehcache.xsd"> <!-- 按照指定的路径加载配置文件 --> <bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> <property name="locations"> <value>classpath:jdbc.properties</value> </property> </bean> <bean id="dataSource" destroy-method="close" class="org.apache.commons.dbcp.BasicDataSource"> <property name="driverClassName" value="${jdbc.driverClassName}" /> <property name="url" value="${jdbc.url}" /> <property name="username" value="${jdbc.username}" /> <property name="password" value="${jdbc.password}" /> </bean> <bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean"> <property name="dataSource"><ref bean="dataSource" /></property> <property name="mappingDirectoryLocations"> <list> <value>classpath:com/biluo/domain</value><!-- 配置映射文件的路径 --> </list> </property> <property name="hibernateProperties"> <props> <prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop> <!--控制台显示sql语句 --> <prop key="hibernate.show_sql">true</prop> <prop key="hibernate.format_sql">false</prop> <prop key="hibernate.hbm2ddl.auto">update</prop> </props> </property> </bean></beans>
10..好了,这里进入很重要的一步,必须测试,否者下面的不用看了,测试sessionfactory
package com.biluo.test;import org.junit.Test;import org.springframework.context.ApplicationContext;import org.springframework.context.support.ClassPathXmlApplicationContext;public class SessionFactoryTest { @Test public void testSessionFactory(){ ApplicationContext context = new ClassPathXmlApplicationContext("spring/applicationContext.xml"); context.getBean("sessionFactory"); }}
注意:这里要引入测试类,否者会报莫名其妙的错误
这一步,没做对的,就不要往下看了,一定要对了,才能继续
11.可以编写dao层,和service层了,这里要用接口编程,否者spring就没意义了,接口在 下面理解为代理对象
package com.biluo.dao;import com.biluo.domain.Person;public interface PersonDao { public void savePerson(Person person);}
package com.biluo.dao.impl;import org.springframework.orm.hibernate3.support.HibernateDaoSupport;import com.biluo.dao.PersonDao;import com.biluo.domain.Person;public class PersonDaoImpl extends HibernateDaoSupport implements PersonDao{ public void savePerson(Person person) { // TODO Auto-generated method stub this.getHibernateTemplate().save(person); }}
package com.biluo.service;import com.biluo.domain.Person;public interface PersonService { public void savePerson(Person person);}
package com.biluo.service.impl;import com.biluo.dao.PersonDao;import com.biluo.domain.Person;import com.biluo.service.PersonService;public class PersonServiceImpl implements PersonService{ private PersonDao personDao; public void setPersonDao(PersonDao personDao) { this.personDao = personDao; } public void savePerson(Person person) { // TODO Auto-generated method stub personDao.savePerson(person); }}
12.下面要配置事务了
<?xml version="1.0" encoding="UTF-8"?><beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:ehcache="http://www.springmodules.org/schema/ehcache" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd http://www.springmodules.org/schema/ehcache http://www.springmodules.org/schema/cache/springmodules-ehcache.xsd"> <!-- 按照指定的路径加载配置文件 --> <bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> <property name="locations"> <value>classpath:jdbc.properties</value> </property> </bean> <bean id="dataSource" destroy-method="close" class="org.apache.commons.dbcp.BasicDataSource"> <property name="driverClassName" value="${jdbc.driverClassName}" /> <property name="url" value="${jdbc.url}" /> <property name="username" value="${jdbc.username}" /> <property name="password" value="${jdbc.password}" /> </bean> <bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean"> <property name="dataSource"><ref bean="dataSource" /></property> <property name="mappingDirectoryLocations"> <list> <value>classpath:com/biluo/domain</value><!-- 配置映射文件的路径 --> </list> </property> <property name="hibernateProperties"> <props> <prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop> <!--控制台显示sql语句 --> <prop key="hibernate.show_sql">true</prop> <prop key="hibernate.format_sql">false</prop> <prop key="hibernate.hbm2ddl.auto">update</prop> </props> </property> </bean> <!-- 这个不配置会默认提交事务的,而且不是经过代理对象,结果会被写到数据库的 --><bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager"> <property name="sessionFactory"> <ref bean="sessionFactory"/> </property> </bean> <tx:advice id="tx" transaction-manager="transactionManager"> <tx:attributes> <tx:method name="save*" propagation="NOT_SUPPORTED" read-only="false"/> </tx:attributes> </tx:advice> <aop:config> <aop:pointcut expression="execution(* com.biluo.service.impl.PersonServiceImpl.*.*(..))" id="perform"/> <aop:advisor advice-ref="tx" pointcut-ref="perform"/> </aop:config></beans>
13.配置applicationContext-person.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" xmlns:context="http://www.springframework.org/schema/context" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:ehcache="http://www.springmodules.org/schema/ehcache" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd http://www.springmodules.org/schema/ehcache http://www.springmodules.org/schema/cache/springmodules-ehcache.xsd"> <bean id="personDao" class="com.biluo.dao.impl.PersonDaoImpl"> <property name="sessionFactory"> <ref bean="sessionFactory"/> </property> </bean> <bean id="personService" class="com.biluo.service.impl.PersonServiceImpl"> <property name="personDao"> <ref bean="personDao"/> </property> </bean></beans>
14.可以测试service和dao层了
package com.biluo.test;import org.junit.Test;import org.springframework.context.ApplicationContext;import org.springframework.context.support.ClassPathXmlApplicationContext;import com.biluo.domain.Person;import com.biluo.service.PersonService;public class PersonServiceTest { @Test public void testPersonService(){ ApplicationContext context = new ClassPathXmlApplicationContext("spring/applicationContext.xml"); PersonService personService = (PersonService)context.getBean("personService"); Person person = new Person(); person.setName("aaa"); personService.savePerson(person); }}
15,建立action
package com.biluo.action;import com.biluo.domain.Person;import com.biluo.service.PersonService;import com.opensymphony.xwork2.ActionSupport;public class PersonAction extends ActionSupport { private PersonService personService; private Person person; public Person getPerson() { return person; } public void setPerson(Person person) { this.person = person; } public void setPersonService(PersonService personService) { this.personService = personService; } public String savePerson(){ this.personService.savePerson(person); return "success"; }}
16,配置Action
<?xml version="1.0" encoding="UTF-8"?><beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:ehcache="http://www.springmodules.org/schema/ehcache" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd http://www.springmodules.org/schema/ehcache http://www.springmodules.org/schema/cache/springmodules-ehcache.xsd"> <bean id="personDao" class="com.biluo.dao.impl.PersonDaoImpl"> <property name="sessionFactory"> <ref bean="sessionFactory"/> </property> </bean> <bean id="personService" class="com.biluo.service.impl.PersonServiceImpl"> <property name="personDao"> <ref bean="personDao"/> </property> </bean> <bean id="personAction" class="com.biluo.action.PersonAction" scope="prototype"><!-- scope="prototype"这一点要是,因为action是多个实例的 --> <property name="personService"> <ref bean="personService"/> </property> </bean></beans>
17.添加struts配置文件,也要模块化
/ssh51/config/struts.xml
<?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> <constant name="struts.il8n.encoding" value="UTF-8" /> <constant name="struts.ui.theme" value="simple"/> <!-- <constant name="struts.devMode" value="true"/> --> <include file="struts2/struts-person.xml"></include></struts>
/ssh51/config/struts2/struts-person.xml
<?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="default" namespace="/" extends="struts-default"> <!--在Struts配置文件中配置action,但是他的class属性不再指向action的实现类 ,而是指向spring容器的action实例的id --> <action name="personAction_*" class="personAction" method="{1}"> <result name="input">/login.jsp</result> <result name="success">/success.jsp</result> </action> </package> </struts>
18.配置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>login.jsp</welcome-file> </welcome-file-list> <!-- 配置spring资源 --> <context-param> <param-name>contextConfigLocation</param-name> <!-- 这一点配错路径 会报这个错误No bean named 'sessionFactory ' is defined --> <param-value>classpath:/spring/applicationContext.xml</param-value> </context-param> <filter> <filter-name>OpenSessionInViewFilter</filter-name> <filter-class> org.springframework.orm.hibernate3.support.OpenSessionInViewFilter </filter-class> <init-param> <param-name>sessionFactoryBeanName</param-name> <param-value>sessionFactory</param-value> </init-param> <init-param> <param-name>singleSession</param-name> <param-value>true</param-value> </init-param> <init-param> <param-name>flushMode</param-name> <param-value>AUTO</param-value> </init-param> </filter> <filter-mapping> <filter-name>OpenSessionInViewFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> <!-- 配置spring --> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <!--struts2的过滤器 --> <filter> <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></web-app>
19,编写jsp注册页面
/ssh51/WebRoot/login.jsp
<%@ 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 PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"><html> <head> <base href="<%=basePath%>"> <title>My JSP 'login.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"> <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"> <meta http-equiv="description" content="This is my page"> <!-- <link rel="stylesheet" type="text/css" href="styles.css"> --> </head> <body><form action="personAction_savePerson.action" method="post"> <input type="text" name="person.name" value="${person.name}"/> <input type="password" name="person.password" value="${person.password}"/> <input type="submit" value="注册"/></form> </body></html>
/ssh51/WebRoot/success.jsp
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%><%String path = request.getContextPath();String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";%><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"><html> <head> <base href="<%=basePath%>"> <title>My JSP 'success.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"> <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"> <meta http-equiv="description" content="This is my page"> <!-- <link rel="stylesheet" type="text/css" href="styles.css"> --> </head> <body> 恭喜你 配置成功 <br> </body></html>
运行如果没有报错,而且jsp页面的数据被写入到数据库,就说明配置成功了
- struts2+hibernate+spring配置详解
- Struts2+Hibernate+Spring配置
- struts2 - spring -hibernate -proxool配置
- Struts2+Spring+Hibernate 配置步骤
- Struts2,Hibernate,Spring 环境配置
- Spring+Struts2+Hibernate整合配置
- hibernate+spring配置详解
- hibernate+spring 配置详解
- Struts2 + Spring + Hibernate的jar包详解
- Struts2+Spring+Hibernate整合过程详解
- Spring+Struts2+hibernate框架整合详解
- Maven搭建Spring+Struts2+Hibernate项目详解
- Struts2+Spring+Hibernate整合过程详解
- Maven搭建Spring+Struts2+Hibernate项目详解
- Struts2+Spring+Hibernate整合过程详解
- Maven搭建Spring+Struts2+Hibernate项目详解
- Maven搭建Spring+Struts2+Hibernate项目详解
- Maven搭建Spring+Struts2+Hibernate项目详解
- Dedecms channel 标签 指定typeid时 currentstyle无效的修复办法
- vim 插件 通过vundle 安装 tagbar
- object-c 宏定义
- 交换两个变量的值(多种方法)
- JavaScript验证手机号是否正确(正则表达式)
- struts2+hibernate+spring配置详解
- Python Beginner
- 跟着韩老师学HTML5的tank大战一些问题和代码
- 简单了解文档碎片DocumentFragment的使用
- 对象适配器模式
- javascript JSON.stringify 语法实例讲解
- 初当技术面试官感悟:给面试者的忠告
- onTouchEvent方法介绍
- bootstrap导航条、分页导航学习笔记6-3(为导航条添加标题、二级菜单及状态)