以是从struts2-------->>>hibernate--------------->>>>spring的整合过程。
环境:tomcat7+MyEclipse10.0+struts2+hibernate3+spring
说明:
1)、基本全部是使用注解方式。
2)、适合初学者
3)、文章最后有项目的源码
------------------struts2-------------------------7、根据struts建立对应jsp和action
关键配置:
/////////////////web.xml配置(只针对于struts2,没有涉及spring)//////////////////////////
<?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"> <welcome-file-list> <welcome-file>index.jsp</welcome-file> </welcome-file-list> <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>
<?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> <!-- 开发者模式 --> <constant name="struts.devMode" value="true"/> <package name="user" extends="struts-default" namespace="/user"> <action name="register" class="com.zzia.action.UserAction" method="register"> <result name="success">/registerOk.jsp</result> <result name="fail">/registerFail.jsp</result> </action> <action name="login" class="com.zzia.action.UserAction" method="login"> <result name="success">/loginOk.jsp</result> <result name="fail">/loginFail.jsp</result> </action> <action name="list" class="com.zzia.action.UserAction" method="list"> <result name="success">/listOk.jsp</result> <result name="fail">/listFail.jsp</result> </action> </package> </struts>
注意两种配置文件的配置(一个注解方式,一个xml方式)
<!-- 该语句是才用xml编写的配置文件(不推荐) --> <!-- <mapping resource="com/test/entry/<span style="font-family: Arial, Helvetica, sans-serif;">User</span><span style="font-family: Arial, Helvetica, sans-serif;">.hbm.xml"/> --></span> <!-- 才用注解方式编写 --> <mapping class="com.test.entry.User" />
@Entity public class User.... @GeneratedValue @Id public int getId() { return id; }
<pre name="code" class="java">public class HibernateAnnotationUtils { private static SessionFactory sf = null; static { sf = new AnnotationConfiguration().configure ().buildSessionFactory(); } public static SessionFactory getSessionFactory() { return sf; } public static Session getCurrentSession() { return sf.getCurrentSession(); } public static Session openSession() { return sf.openSession(); } }
<?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="connection.driver_class">com.mysql.jdbc.Driver</property> <property name="connection.url">jdbc:mysql://localhost/ssh</property> <property name="connection.username">root</property> <property name="connection.password">xxx</property> <!-- 一些方便调试的配置 --> <property name="dialect">org.hibernate.dialect.MySQLDialect</property> <property name="show_sql">true</property> <property name="format_sql">true</property> <!-- 一些优化以及连接池的配置 --> <!-- JDBC connection pool (use the built-in) --> <property name="connection.pool_size">1</property> <!-- Enable Hibernate's automatic session context management --> <property name="current_session_context_class">thread</property> <!-- Disable the second-level cache --> <property name="cache.provider_class">org.hibernate.cache.NoCacheProvider</propert y> <!-- 该语句是才用xml编写的配置文件(过时) --> <!-- <mapping resource="com/test/entry/User.hbm.xml"/> --> <!-- 才用注解方式编写 --> <mapping class="com.test.entry.User" /> </session-factory> </hibernate-configuration>
@Component public class... @Resource public void setUserService...
@Scope("prototype")//多例(不加上设计多线程错误,非常debug) public class UserAction....
<pre name="code" class="html"><listener> <listener- class>org.springframework.web.context.ContextLoaderListener</listener- class> <!-- default: /WEB-INF/applicationContext.xml --> </listener> <context-param> <param-name>contextConfigLocation</param-name> <!-- <param-value>/WEB-INF/applicationContext- *.xml,classpath*:applicationContext-*.xml</param-value> --> <param-value>classpath:applicationContext.xml</param- value> </context-param> <!-- 解决中文乱码问题 --> <filter> <filter-name>encodingFilter</filter-name> <filter- class>org.springframework.web.filter.CharacterEncodingFilter</filter- class> <init-param> <param-name>encoding</param-name> <param-value>UTF-8</param-value> </init-param> </filter> <filter-mapping> <filter-name>encodingFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping>
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context" xmlns:p="http://www.springframework.org/schema/p" xmlns:tx="http://www.springframework.org/schema/tx" 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-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd"> <!-- 配置bean的扫描路径 --> <context:annotation-config /> <context:component-scan base-package="com.zzia" /> <!-- 整合hibernate数据库 --> <!-- 配置jdbc --> <bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfi gurer"> <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.annotation.AnnotationSessionFa ctoryBean"> <property name="dataSource" ref="dataSource" /> <property name="packagesToScan"> <list> <value>com.zzia.model</value> </list> </property> <property name="hibernateProperties"> <props> <prop key="hibernate.dialect"> org.hibernate.dialect.MySQLDialect </prop> <prop key="hibernate.show_sql">true</prop> </props> </property> </bean> <!-- new code --> <!-- <bean id="hibernateTemplate" class="org.springframework.orm.hibernate3.HibernateTemplate"> --> <!-- <property name="sessionFactory" ref="sessionFactory"></property> --> <!-- </bean> --> </beans>
jdbc.driverClassName=com.mysql.jdbc.Driver jdbc.url=jdbc:mysql://localhost:3306/ssh jdbc.username=root jdbc.password=xxx
------------------------------------------------------------------------------------------------------------------------------