Spring:
IOC基本的开发
spring-beans-4.2.4.RELEASE.jar
spring-context-4.2.4.RELEASE.jar
spring-core-4.2.4.RELEASE.jar
spring-expression-4.2.4.RELEASE.jar
com.springsource.org.apache.log4j-1.2.15.jar
com.springsource.org.apache.commons.logging-1.1.1.jar
AOP的开发
com.springsource.org.aopalliance-1.0.0.jar
com.springsource.org.aspectj.weaver-1.6.8.RELEASE.jar
spring-aop-4.2.4.RELEASE.jar
spring-aspects-4.2.4.RELEASE.jar
使用JDBC模板和事务管理
spring-jdbc-4.2.4.RELEASE.jar
spring-tx-4.2.4.RELEASE.jar
整合web项目(在web容器中添加监听器:当启动web.xml的时候,自动加载spring容器)
spring-web-4.2.4.RELEASE.jar
整合单元测试
spring-test-4.2.4.RELEASE.jar
整合ORM框架(spring整合hibernate)
spring-orm-4.2.4.RELEASE.jar
Struts:
struts2-convention-plugin-2.3.24.jar ---Struts2的注解开发包(后续注解)(@PackageParent,@Namespace,@Action)
struts2-spring-plugin-2.3.24.jar ---Struts2和Spring整合的jar包
struts2-json-plugin-2.3.24.jar ---整合AJAX(项目课讲)
Hibernate
基本的开发包:lib\required\*.jar
日志记录
数据库驱动
mysql-connector-java-5.1.7-bin.jar
连接池(可选的)
lib\optional\c3p0\*.jar
==配置struts的核心过滤器==
==设置一个监听器,在加载配置文件的时候就将spring的applicationCointext的配置文件加载==
----------------------------------------------------------------------------------------------
<filter>
<filter-name>struts2filter-name>
<filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilterfilter-class>
filter>
<filter-mapping>
<filter-name>struts2filter-name>
<url-pattern>/*url-pattern>
filter-mapping>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListenerlistener-class>
listener>
<context-param>
<param-name>contextConfigLocationparam-name>
<param-value>classpath:applicationContext.xmlparam-value>
context-param>
==引用约束==
==配置常量==
=设置后缀名,开发者模式...=
==配置action类==
-------------------------------------------------------------------------------------------------
<struts>
<constant name="struts.action.extension" value="action"/>
<package name="ssh" extends="struts-default" namespace="/">
<action name="customer_*" class="customerAction" method="{1}">action>
package>
struts>
==引入约束==
==配置sessionFactory==
=配置数据库=
=配置方言=
=配置可选属性=
=配置c3p0连接池=
=配置映射文件=
-------------------------------------------------------------------------------------------------
<hibernate-configuration>
<session-factory>
<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driverproperty>
<property name="hibernate.connection.url">jdbc:mysql:///lyric_sshproperty>
<property name="hibernate.connection.username">rootproperty>
<property name="hibernate.connection.password">123property>
<property name="hibernate.dialect">org.hibernate.dialect.MySQLDialectproperty>
<property name="hibernate.show_sql">trueproperty>
<property name="hibernate.format_sql">trueproperty>
<property name="hibernate.hbm2ddl.auto">updateproperty>
<property name="connection.provider_class">org.hibernate.c3p0.internal.C3P0ConnectionProviderproperty>
<property name="c3p0.min_size">5property>
<property name="c3p0.max_size">20property>
<property name="c3p0.timeout">120property>
<property name="c3p0.idle_test_period">3000property>
<mapping resource="com/lyric/domain/Customer.hbm.xml"/>
session-factory>
hibernate-configuration>
==引入约束==
==配置映射==
-------------------------------------------------------------------------------------------------
<hibernate-mapping>
<class name="com.lyric.domain.Customer" table="cst_customer">
<id name="cust_id" column="cust_id">
<generator class="native"/>
id>
<property name="cust_name" column="cust_name"/>
<property name="cust_source" column="cust_source"/>
<property name="cust_industry" column="cust_industry"/>
<property name="cust_level" column="cust_level"/>
<property name="cust_phone" column="cust_phone"/>
<property name="cust_mobile" column="cust_mobile"/>
class>
hibernate-mapping
==引入约束==
==注册hibernate的sessionFactory==
=将hibernate.cfg.xml文件引入进来=
==注册CustomerDao==
=将sessionFactory注入到CustomerDao中=
==注册CustomerService==
=将CustomerDao注入到CustomerService中=
==注册CustomerAction==
=将CustomerService注入到CustomerAction中=
===注册事务管理=
=并将sessionFactory注入进去(如果是JDBC模板的话就注入DataSource)=
==开启注解==
-------------------------------------------------------------------------------------------------
version="1.0" encoding="UTF-8"?>
"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"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd">
id="sessionFactory" class="org.springframework.orm.hibernate5.LocalSessionFactoryBean">
<property name="configLocation" value="classpath:hibernate.cfg.xml">property>
id="customerDao" class="com.lyric.dao.impl.CustomerDaoImpl">
<property name="sessionFactory" ref="sessionFactory">property>
id="customerService" class="com.lyric.Service.impl.CustomerServiceImpl">
<property name="customerDao" ref="customerDao">property>
id="customerAction" class="com.lyric.web.action.CustomerAction" scope="prototype">
<property name="customerService" ref="customerService">property>
id="transactionManager" class="org.springframework.orm.hibernate5.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory">property>
transaction-manager="transactionManager"/>
jdbc.properties
jdbc.driverClass=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql:///lyric_crm
jdbc.username=root
jdbc.password=123
区别:将hibernate.cfg.xml中的配置全部转移到applicationContext.xml中
提供一个关于jdbc的属性文件
在applicationContext.xml中配置c3p0连接池
在applicationContext.xml中配置属性和映射
配置数据源–hibernate.cfg.xml中的数据库的配置
配置方言
配置可选属性
加载映射文件
映射文件有三种添加方式(推荐第三种方式)
<property name="mappingResources">
<array>
<value>com/lyric/domain/Customer.hbm.xmlvalue>
array>
property>
===========================================================
<property name="mappingLocations">
<array>
<value>classpath:com/lyric/domain/*.hbm.xmlvalue>
array>
property>
===========================================================
<property name="mappingDirectoryLocations">
<array>
<value>classpath:com/lyric/domainvalue>
array>
property>
version="1.0" encoding="UTF-8"?>
"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"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd">
==注册hibernate中的sessionFactory,并且配置相关的配置==
id="sessionFactory" class="org.springframework.orm.hibernate5.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource">property>
<property name="hibernateProperties">
<prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialectprop>
<prop key="hibernate.show_sql">trueprop>
<prop key="hibernate.format_sql">trueprop>
<prop key="hibernate.hbm2ddl.auto">updateprop>
property>
<property name="mappingDirectoryLocations">
classpath:com/lyric/domain
property>
==配置dao==
id="customerDao" class="com.lyric.dao.impl.CustomerDaoImpl">
<property name="sessionFactory" ref="sessionFactory">property>
==配置service==
id="customerService" class="com.lyric.Service.impl.CustomerServiceImpl">
<property name="customerDao" ref="customerDao">property>
==配置action类==
id="customerAction" class="com.lyric.web.action.CustomerAction" scope="prototype">
<property name="customerService" ref="customerService">property>
==配置事务管理器==
id="transactionManager" class="org.springframework.orm.hibernate5.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory">property>
==开启事物的注解==
transaction-manager="transactionManager"/>
==引入外部的属性文件==
property-placeholder location="classpath:jdbc.properties"/>
==配置一个c3p0连接池作用域hibernate的数据源==
id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="driverClass" value="${jdbc.driverClass}"/>
<property name="jdbcUrl" value="${jdbc.url}"/>
<property name="user" value="${jdbc.username}"/>
<property name="password" value="${jdbc.password}"/>
<property name="minPoolSize" value="5">property>
<property name="maxPoolSize" value="20">property>
<property name="checkoutTimeout" value="120">property>
<property name="idleConnectionTestPeriod" value="3000">property>
命名查询,把使用的HQL语句从Dao中 提取出去,这样即可以实现通用,也可以提高程序的安全性
在hibernate.hbm.xml中添加
业务层查询数据,返回后,session关闭了, 表现层获取数据如果关联延迟数据,无法初始化 !
方案一: 在Xxx.hbm.xml中配置为立即加载 lazy=false (不推荐,因为只查询一个表的信息,其他表的信息都会立即检索出来)
方案二: Service方法返回前,即Session关闭之前,对延迟数据进行初始化 (缺点多,还需要手动写代码)
方案三: spring提供了OpenSessionInView 机制 (将Session开启到表现层 最前面 Filter )Spring 提供 OpenSessionInViewFilter 注意:需要配置在struts2 Filter前面,否则不起作用
<filter>
<filter-name>openSessionInViewFilterfilter-name>
<filter-class>org.springframework.orm.hibernate5.support.OpenSessionInViewFilterfilter-class>
filter>
<filter-mapping>
<filter-name>openSessionInViewFilterfilter-name>
<url-pattern>/*url-pattern>
filter-mapping>
OpenSessionInViewFilter:在request过程中维持hibernate的session。延迟session的关闭,直到request结束(即一次请求),再自动关闭session,也就是说,直到表现层的数据全部加载完毕,再关闭Session。