1、在web中配置创建数据库连接
首先创建用于建立数据库连接的DbLib.java:
然后,配置server中的context.xml文件,注意mytest
普通的创建数据库连接方式:
2、struts1的配置
首先在web.xml文件中:/Struts1Pro/WebContent/WEB-INF/
然后struts-config.xml文件中配置:/Struts1Pro/WebContent/WEB-INF/
3、struts2的配置
web.xml文件的配置,这个文件放在/Struts2Pro/WebContent/WEB-INF/目录下
struts.xml文件的配置,这个文件放在/Struts2Pro/src/目录下
user.xml文件配置:这个文件放在/Struts2Pro/src/目录下
picture.xml文件配置:这个文件放在/Struts2Pro/src/目录下
4、hibernate配置(注意版本5.30版)
xml配置:hibernate.cfg.xml (放到src目录下)和实体配置类:xxx.hbm.xml(与实体为同一目录中)
以下为注解配置方法:
hibernate.config.xml文件配置:这个文件放在/Struts2Pro/src/目录下
在类中做注解(仅拿user举例):
通过/Curricula/src/common/HibernateUtil.java来创建session工厂
通过注解来确定一对一的关系:user和contact
通过注解来确定一对多的关系:course和room\teacher
通过注解来确定多对多的关系:user和course
5、整合spring和struts及spring配置:
首先在web.xml中的变化,划红线为针对spring的新增内容:(整合spring和struts:添加jar包,增加红线部分)
A、IOC的XML配置方式
B、IOC的注解配置方式
C、注解方式实现AOP
D、xml配置方式实现AOP
6、spring和hibernate整合
<?xml version="1.0" encoding="UTF-8"?>
<!--xmlns:context引入命名空间,支持spring注解IOC-->
<!--xmlns:aop引入支持aop注解的xml命名空间-->
<!--xmlns:tx引入申明式事务支持的XML命名空间-->
<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"
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-4.1.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-4.1.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-4.1.xsd
">
<context:annotation-config></context:annotation-config>
<context:component-scan base-package="net.xinqushi.dao.impl,net.xinqushi.service.impl,net.xinqushi.aop"></context:component-scan>
<aop:aspectj-autoproxy></aop:aspectj-autoproxy>
<context:property-placeholder location="classpath:dataSource.properties"/>
<!-- 配置数据源 -->
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
<!-- 初始化连接数量; -->
<property name="initialSize" value="0" />
<!-- 最大并发连接数 -->
<property name="maxActive" value="20" />
<!-- 最大空闲连接数 -->
<property name="maxIdle" value="20" />
<!-- 最小空闲连接数 -->
<property name="minIdle" value="0" />
<!-- 最大等待时长 -->
<property name="maxWait" value="60000" />
<!-- 超过时间限制是否回收 -->
<property name="removeAbandoned" value="true" />
<!-- 超过时间限制多长; -->
<property name="removeAbandonedTimeout" value="180"/>
<!-- 数据源连接参数配置; -->
<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>
<!-- 配置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>
<!-- 初始化hibernateTemplate -->
<bean id="hibernateTemplate" class="org.springframework.orm.hibernate4.HibernateTemplate">
<property name="sessionFactory" ref="sessionFactory"></property>
</bean>
<!-- 配置事务管理器 -->
<bean id="transactionManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory"/>
</bean>
<!-- 定义切面 -->
<aop:config>
<aop:pointcut expression="execution(* net.xinqushi.service.impl.*.* (..))" id="txPointCut"/>
<aop:advisor advice-ref="txAdvice" pointcut-ref="txPointCut"/>
</aop:config>
<!-- 声明式事务 -->
<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>
</beans>