|
一、Hibernate[附struts2] 1.添加hibernate的jar包 lib/required 2.添加struts2的jar包 apps/解壓一個(gè)項(xiàng)目blank/lib 3.添加mysql數(shù)據(jù)庫(kù)連接驅(qū)動(dòng) 4.添加struts2的配置文件struts.xml 5.添加hibernate的配置文件hibernate.cfg.xml <hibernate-configuration> <session-factory > <!-- Database connection settings --> <property name="connection.driver_class">com.mysql.jdbc.Driver</property> <property name="connection.url">jdbc:mysql://localhost:3306/databaseName?useSSL=false</property> <property name="connection.username">root</property> <property name="connection.password">123456</property> <property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property> <property name="show_sql">true</property> <!-- Drop and re-create the database schema on startup --> <property name="hbm2ddl.auto">update</property> <!-- mapping配置 --> <!-- 1、xml方式 <mapping resource="model/Student.hbm.xml"/> --> <!-- 2、注解方式 --> <mapping class="model.Student"/> <mapping class="model.Contact"/> </session-factory> </hibernate-configuration> 6.編寫初始化SessionFactory和獲取Session的代碼。 SessionFactory:連接池 Session:連接 public class HibernateUtil { private static SessionFactory sessionFactory; private static SessionFactory buildSessionFactory() { try { Configuration configuration = new Configuration(); configuration.configure("hibernate.cfg.xml"); ServiceRegistry serviceRegistry = new StandardServiceRegistryBuilder() .applySettings(configuration.getProperties()).build(); SessionFactory sessionFactory = configuration .buildSessionFactory(serviceRegistry); return sessionFactory; } catch (Throwable ex) { throw new ExceptionInInitializerError(ex); } } public static SessionFactory getSessionFactory() { if (sessionFactory == null) sessionFactory = buildSessionFactory(); return sessionFactory; } public static Session openSession() { return getSessionFactory().openSession(); } } 7、在JSP頁(yè)面測(cè)試配置 <body> <h3>測(cè)試session連接</h3> <% out.print(HibernateUtil.openSession()); %> </body> 若類似下圖所示,則配置成功 ![]() 二、Spring環(huán)境搭建 1、jar包配置通用包(7個(gè)): commons-dbcp2-2.1.jar commons-logging-1.1.3.jar commons-pool2-2.3.jar commons-collections4-4.0.jar aopalliance-1.0-20100517.210215-13.jar mysql-connector-java-5.1.33-bin.jar aspectjweaver-1.6.9.jar struts包(13+1) asm-3.3.jar
asm-commons-3.3.jar asm-tree-3.3.jar commons-fileupload-1.3.1.jar commons-io-2.2.jar commons-lang3-3.2.jar freemarker-2.3.22.jar javassist-3.11.0.GA.jar log4j-api-2.2.jar log4j-core-2.2.jar ognl-3.0.6.jar struts2-core-2.3.24.jar xwork-core-2.3.24.jar struts2-spring-plugin-2.3.24.jar hibernate包(10個(gè)) antlr-2.7.7.jar dom4j-1.6.1.jar hibernate-commons-annotations-4.0.5.Final.jar hibernate-core-4.3.10.Final.jar hibernate-jpa-2.1-api-1.0.0.Final.jar jandex-1.1.0.Final.jar
javassist-3.18.1-GA.jar jboss-logging-3.1.3.GA.jar jboss-logging-annotations-1.2.0.Beta1.jar jboss-transaction-api_1.2_spec-1.0.0.Final.jar spring包(10個(gè)) spring-beans-4.1.6.RELEASE.jar spring-context-4.1.6.RELEASE.jar spring-core-4.1.6.RELEASE.jar spring-expression-4.1.6.RELEASE.jar spring-aop-4.1.6.RELEASE.jar spring-web-4.1.6.RELEASE.jar spring-orm-4.1.6.RELEASE.jar spring-tx-4.1.6.RELEASE.jar spring-jdbc-4.1.6.RELEASE.jar spring-aspects-4.1.6.RELEASE.jar 2、配置xml文件 (1) web.xml <?xml version="1.0" encoding="UTF-8"?> <web-app id="WebApp_9" version="2.4" xmlns="http://java./xml/ns/j2ee" xmlns:xsi="http://www./2001/XMLSchema-instance" xsi:schemaLocation="http://java./xml/ns/j2ee http://java./xml/ns/j2ee/web-app_2_4.xsd"> <display-name>Struts Blank</display-name> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <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> <welcome-file-list> <welcome-file>index.html</welcome-file> </welcome-file-list> </web-app> (2) struts.xml <?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN" "http://struts./dtds/struts-2.3.dtd"> <struts> <package name="all" namespace="/" extends="struts-default"> </package> </struts> (3) hibernate-cfg.xml <!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN" "http://www./dtd/hibernate-configuration-3.0.dtd"> <hibernate-configuration> <session-factory> <!-- Database connection settings --> <property name="connection.driver_class">com.mysql.jdbc.Driver</property> <property name="connection.url">jdbc:mysql://localhost:3306/album</property> <property name="connection.username">root</property> <property name="connection.password">12345678</property> <property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property> <property name="show_sql">true</property> <!-- Drop and re-create the database schema on startup --> <property name="hbm2ddl.auto">update</property> <mapping class="net.xinqushi.model.User"/> </session-factory> </hibernate-configuration> (4)applicationContext.xml <?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www./schema/beans" xmlns:xsi="http://www./2001/XMLSchema-instance" xsi:schemaLocation="http://www./schema/beans http://www./schema/beans/spring-beans.xsd"> </beans> struts.xml和 hibernate-cfg.xml放在src目錄下 applicationContext.xml放在 WEB-INF目錄下 三、Spring與Hibernate整合 1、加入struts2-spring-plugin-2.3.24.jar插件包,配置文件不需要整合,整合過(guò)程比較簡(jiǎn)單。 web.xml中配置listener: <listener> <!-- 啟動(dòng)時(shí)會(huì)加載,關(guān)閉時(shí)卸載 --> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> ContextLoaderListener實(shí)現(xiàn)ServletContextListener接口,在服務(wù)器啟動(dòng)時(shí)加載, 默認(rèn)自動(dòng)載入置于WEB-INF目錄下的Spring配置文件applicationContext.xml 2、引入申明式事務(wù)支持的XML命名空間 xmlns:tx="http://www./schema/tx" http://www./schema/tx http://www./schema/tx/spring-tx-4.1.xsd 3、dataSource配置 占位符$ <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy- method="close"> <!-- 初始化連接數(shù)量; --> <property name="initialSize" value="0" /> <!-- 最大并發(fā)連接數(shù) --> <property name="maxActive" value="20" /> <!-- 最大空閑連接數(shù) --> <property name="maxIdle" value="20" /> <!-- 最小空閑連接數(shù) --> <property name="minIdle" value="0" /> <!-- 最大等待時(shí)長(zhǎng) --> <property name="maxWait" value="60000" /> <!-- 超過(guò)時(shí)間限制是否回收 --> <property name="removeAbandoned" value="true" /> <!-- 超過(guò)時(shí)間限制多長(zhǎng); --> <property name="removeAbandonedTimeout" value="180"/> <!-- 數(shù)據(jù)源連接參數(shù)配置; --> <property name="username" value="${db.username}"/> <property name="url" value="${db.url}"/> <property name="password" value="${db.password}"/> <property name="driverClassName" value="${db.driverClassName}"/> </bean> 4、讀取外部屬性文件,獲取數(shù)據(jù)源參數(shù) <context:property-placeholder location="classpath:dataSource.properties/> 在項(xiàng)目src目錄下創(chuàng)建dataSource.properties文件,設(shè)置占位符后參數(shù): db.username=root db.password=123456 db.url=jdbc:mysql://localhost:3306/album?useSSL=false db.driverClassName=com.mysql.jdbc.Driver hibernate.dialect=org.hibernate.dialect.MySQLDialect hibernate.show_sql=true hibernate.hbm2ddl.auto=update hibernate.format_sql=true 5、sessionFactory配置 <bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean"> <property name="dataSource" ref="dataSource"/> <property name="packagesToScan" value="net.xinqushi.model"/> <property name="hibernateProperties"> <props> <prop key="hibernate.dialect">${hibernate.dialect}</prop> <prop key="hibernate.show_sql">${hibernate.show_sql}</prop> <prop key="hibernate.hbm2ddl.auto">${hibernate.hbm2ddl.auto}</prop> <prop key="hibernate.format_sql">${hibernate.format_sql}</prop> </props> </property> </bean> 6、配置事務(wù)管理器 <bean id="transactionManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager"> <property name="sessionFactory" ref="sessionFactory"/> </bean> 7、初始化hibernateTemplate <bean id="hibernateTemplate" class="org.springframework.orm.hibernate4.HibernateTemplate"> <property name="sessionFactory" ref="sessionFactory"></property> </bean> 8、配置事務(wù)管理 <!-- 定義切面 --> <aop:config> <aop:pointcut expression="execution(* net.xinqushi.service.impl.*.* (..))" id="txPointCut"/> <aop:advisor advice-ref="txAdvice" pointcut-ref="txPointCut"/> </aop:config> <!-- 聲明式事務(wù) --> <tx:advice id="txAdvice" transaction-manager="transactionManager"> <tx:attributes> <tx:method name="add*" propagation="REQUIRED"/> <tx:method name="get*" read-only="true" propagation="REQUIRED"/> </tx:attributes> </tx:advice> |
|
|