标签:
Spring步骤
1 创建项目
2 导入jar包 三大框架的jar包 数据库驱动jar
3 给项目分层
4 写DTO:一张表 对应的就是一个DTO 表中字段的个数及其类型 要和对应的DTO一致
5 写DTO的映射文件:取名规范 pojo.hbm.xml
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
6 写DAO接口 当前这个类 是离数据库最近的类
这个类中 是方法的集合 没有方法的具体实现
7 接口的实现类 实现接口 重写接口里面的所有方法 (操作数据库表的底层代码)
8 Service接口
9 Service实现类
10 jdbc.properties
jdbc.driver=oracle.jdbc.driver.OracleDriver
jdbc.url=jdbc\:oracle\:thin\:@localhost\:1521\:orcl
jdbc.username=scott
jdbc.password=Sa123456
jdbc.initSize=20
jdbc.maxSize=2000
jdbc.maxIdleTime=10000
11 GoodsAction :action调用service action是用来接收页面传递过来的值
12
struts.xml
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.1//EN"
"http://struts.apache.org/dtds/struts-2.1.dtd">
13 web.xml
<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内配置
1-- 事务管理 -->
路径: spring.jar-> org.springframework.orm.hibernate3->org.springframework.orm.hibernate3.HibernateTransactionManager.class
2 添加hibernate的核心配置(无hibernate配置文件)
路径: spring.jar->org.springframework.orm.hibernate3.annotationorg.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean.class
3把Hibernate的核心文件交给Spring管理
路径:spring.jar->org.springframework.orm.hibernate3->org.springframework.orm.hibernate3.LocalSessionFactoryBean.class
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="configLocation">
<value>classpath:hibernate.cfg.xml</value>
</property>
</bean>
完整的配置如下
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p"
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-2.5.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-2.5.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-2.5.xsd"
default-autowire="byName">
<!-- jdbc:properties 交给spring去管理 -->
<bean class="org.springframework.beans.factory.config.PreferencesPlaceholderConfigurer">
<property name="locations">
<value>classpath:jdbc.properties</value>
</property>
</bean>
<!-- 配置数据源 -->
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="driverClass" value="${jdbc.driver}"></property>
<property name="jdbcUrl" value="${jdbc.url}"></property>
<property name="user" value="${jdbc.username}"></property>
<property name="password" value="${jdbc.password}"></property>
<property name="initialPoolSize" value="${jdbc.initSize}"></property>
<property name="maxPoolSize" value="${jdbc.maxSize}"></property>
<property name="maxIdleTime" value="${jdbc.maxIdleTime}"></property>
</bean>
<!-- sessionFactory -->
<bean id="sessionFactory"
class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource"></property>
<!-- 将实体类的映射文件交给spring去管理 -->
<property name="mappingResources">
<value>com/softeem/dto/Goods.hbm.xml</value>
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.show_sql">true</prop>
<prop key="hibernate.format_sql">true</prop>
<prop key="hibernate.hbm2ddl.auto">update</prop>
<prop key="hibernate.current_session_context_class">thread</prop>
<prop key="hibernate.dialect">org.hibernate.dialect.OracleDialect</prop>
</props>
</property>
</bean>
<!-- 事务管理 -->
<bean id="txManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager"></bean>
<!-- 切入事务的配置 -->
<aop:config>
<aop:pointcut expression="execution (* com.softeem.service..*.*(..))" id="service"/>
<aop:advisor advice-ref="txAdvice" pointcut-ref="service"/>
</aop:config>
<!-- 事务的配置 -->
<tx:advice id="txAdvice" transaction-manager="txManager">
<tx:attributes>
<!-- 配置事务的传播特性 add* 对于以add 为前缀的所有方法 切入事务 -->
<tx:method name="add*" propagation="REQUIRED"/>
<tx:method name="update*" propagation="REQUIRED"/>
<tx:method name="delete*" propagation="REQUIRED" />
<tx:method name="search*" propagation="REQUIRED" read-only="true"/>
</tx:attributes>
</tx:advice>
<!-- 把DAO交给spring 去管理 -->
<bean id="goodsDAO" class="com.softeem.daoImpl.GoodsDAO"></bean>
<bean id="goodsService" class="com.softeem.serviceImpl.GoodsService"></bean>
</beans>
版权声明:本文为博主原创文章,未经博主允许不得转载。
标签:
原文地址:http://blog.csdn.net/yzjyhp/article/details/47045811