现在说下整合
struts代表的是控制层,hibernate代表的是服务层,spring暂且说是代表业务层
spring整合struts 只需要一个jar包,好像是,struts-spring.jar什么的,这个东西 可以在网上找到
spring整合hibernate 有两种方式
第一种是在spring配置文件中配置数据源
第二种是在spring中加载hibernate.cfg.xml
下面,我们开始讲解ssh整合的步骤
1.添加所要的jar包
2.web.xml
<!-- 配置struts的过滤器,struts的本质就是过滤器-->
<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>
<!--配置spring监听器,在启动时就加载spring-->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath*:applicationContext*.xml</param-value>
</context-param>
<listener>
<listener-class>
org.springframework.web.context.ContextLoaderListener
</listener-class>
</listener>
struts.xml 这个文件就不说了
spring整合hibernate
第一种方法
先写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>
<session-factory>
<property name="connection.url">
jdbc:mysql://localhost:3306/food
</property>
<property name="connection.driver_class">
com.mysql.jdbc.Driver
</property>
<property name="connection.username">root</property>
<property name="connection.password">123</property>
<property name="dialect">
org.hibernate.dialect.MySQLDialect
</property>
<property name="show_sql">false</property>
<property name="format_sql">true</property>
<property name="myeclipse.connection.profile">
AirLineDB
</property>
</session-factory>
</hibernate-configuration>
再在spring中加载hibernate配置文件 红色部分为重要部分
<bean id="sessionFactory"
class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="configLocation">
<value>classpath:hibernate.cfg.xml</value>
</property>
<property name="mappingResources">
<list>
<value>config/hibernate/hbm/Admin.hbm.xml</value>
<value>
config/hibernate/hbm/Cafe.hbm.xml
</value>
<value>config/hibernate/hbm/Menu.hbm.xml</value>
</list>
</property>
</bean>
第二种
直接在spring中配置数据源
spring在第三方依赖包中包含了两个数据源的实现类包,其一是Apache的DBCP,其二是C3p0,可以在spring配置文件中利用这两者中任何一个配置数据源
1.用dbcp数据源
<!--配数据源 -->
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
destroy-method="close">
<property name="driverClassName" value="com.mysql.jdbc.Driver" />
<property name="url" value="jdbc:mysql://localhost:3306/cuitxtw" />
<property name="username" value="root" />
<property name="password" value="123" />
</bean>
<context:annotation-config></context:annotation-config>
<context:component-scan base-package="dwj.test.*"></context:component-scan>
<context:property-placeholder location="classpath:jdbc.properties"/>
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<!-- 基本信息 -->
<property name="jdbcUrl" value="${jdbcUrl}"></property>
<property name="driverClass" value="${driverClass}"></property>
<property name="user" value="${username}"></property>
<property name="password" value="${password}"></property>
<!-- 其他配置 -->
<!--初始化时获取三个连接,取值应在minPoolSize与maxPoolSize之间。Default: 3 -->
<property name="initialPoolSize" value="3"></property>
<!--连接池中保留的最小连接数。Default: 3 -->
<property name="minPoolSize" value="3"></property>
<!--连接池中保留的最大连接数。Default: 15 -->
<property name="maxPoolSize" value="5"></property>
<!--当连接池中的连接耗尽的时候c3p0一次同时获取的连接数。Default: 3 -->
<property name="acquireIncrement" value="3"></property>
<!-- 控制数据源内加载的PreparedStatements数量。如果maxStatements与maxStatementsPerConnection均为0,则缓存被关闭。Default: 0 -->
<property name="maxStatements" value="8"></property>
<!-- maxStatementsPerConnection定义了连接池内单个连接所拥有的最大缓存statements数。Default: 0 -->
<property name="maxStatementsPerConnection" value="5"></property>
<!--最大空闲时间,1800秒内未使用则连接被丢弃。若为0则永不丢弃。Default: 0 -->
<property name="maxIdleTime" value="1800"></property>
</bean>
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource"></property>
<property name="configLocation" value="classpath:hibernate.cfg.xml"></property>
</bean>
第三种 使用jpa注解代替hibernate配置文件 jpa注解的知识 请到我的博客看jpa注解详解
然后在application.xml配置文件中 整合jpa配置
<!--配数据源 -->
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
destroy-method="close">
<property name="driverClassName" value="com.mysql.jdbc.Driver" />
<property name="url" value="jdbc:mysql://localhost:3306/cuitxtw" />
<property name="username" value="root" />
<property name="password" value="123" />
</bean>
<bean id="sessionFactory"
class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
<prop key="hibernate.show_sql">true</prop>
<prop key="hibernate.hbm2ddl.auto">update</prop>
<prop key="default_schema">cuitxtw</prop>
</props>
</property>
<property name="annotatedClasses">
<list>
<value>com.xtw.entity.User</value>
<value>com.xtw.entity.Article</value>
<value>com.xtw.entity.Ftype</value>
<value>com.xtw.entity.Stype</value>
<value>com.xtw.entity.DownLoad</value>
<value>com.xtw.entity.Mail</value>
<value>com.xtw.entity.MailReply</value>
</list>
</property>
</bean>