SpringMVC经典系列-04基于Spring3.1注解的方式进行项目开发---【LinusZhu】

      注意:此文章是个人原创,希望有转载需要的朋友们标明文章出处,如果各位朋友们觉得写的还好,就给个赞哈,你的鼓励是我创作的最大动力,LinusZhu在此表示十分感谢,当然文章中如有纰漏,请联系linuszhu@163.com,敬请朋友们斧正,谢谢。

      注意了:下面的项目使用的是Spring3.1进行开发的,是在上篇博文Spring2.5注解项目的基础上进行修改的,同时对hibernate进行了集成操作。项目框架采用的是:Spring MVC3.1+spring3.1+hibernate3,数据库使用的是Oracle,需要下载Oracle驱动包。 

      由于Spring3.1完全兼容Spring2.5,所以,我只需要在Spring2.5注解项目的基础上进行简单的修改,主要是对上面Spring2.5注解项目的类库和配置文件进行修改,项目中类的代码保持不变。

      项目修改步骤如下:

1. 导入Spring3.1相关jar包,如下:

2. 需要对spring配置文件springmvc-servlet.xml进行修改,修改后如下:

<?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:mvc="http://www.springframework.org/schema/mvc" xmlns:context="http://www.springframework.org/schema/context"

xmlns:util="http://www.springframework.org/schema/util"

xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd    

            http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd    

            http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd    

            http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.0.xsd">

 

<!-- 对web包中的所有类进行扫描,以完成Bean创建和自动依赖注入的功能 -->

<context:component-scan base-package="com.spring" />

 

<mvc:annotation-driven />  <!-- 支持spring3.0新的mvc注解 -->

    <!-- 启动Spring MVC的注解功能,完成请求和注解POJO的映射 -->

    <bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter"/>

 

   <!--对模型视图名称的解析,即在模型视图名称添加前后缀 p:prefix="/WEB-INF/jsp/" -->

<bean

class="org.springframework.web.servlet.view.InternalResourceViewResolver"

p:suffix=".jsp">

<!-- 如果使用jstl的话,配置下面的属性 -->

<property name="viewClass"

value="org.springframework.web.servlet.view.JstlView" />

</bean>

</beans>

3. spring配置文件hib-config.xml内容修改后,如下:

<?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:aop="http://www.springframework.org/schema/aop"

xmlns:tx="http://www.springframework.org/schema/tx" xmlns:context="http://www.springframework.org/schema/context"

xsi:schemaLocation="

http://www.springframework.org/schema/beans 

http://www.springframework.org/schema/beans/spring-beans-3.0.xsd

http://www.springframework.org/schema/tx

http://www.springframework.org/schema/tx/spring-tx-3.0.xsd

http://www.springframework.org/schema/aop 

http://www.springframework.org/schema/aop/spring-aop-3.0.xsd

  http://www.springframework.org/schema/context   

   http://www.springframework.org/schema/context/spring-context-3.0.xsd

">

<!-- 5 集成的hibernate的配置 -->

<context:component-scan base-package="com.spring" />

<!-- 支持aop注解 -->

<aop:aspectj-autoproxy />

<!-- jdbctemplate/hibernateTemplate使用的 -->

<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"

destroy-method="close">

<property name="driverClass" value="oracle.jdbc.driver.OracleDriver"></property>

<property name="jdbcUrl" value="jdbc:oracle:thin:@10.1.130.33:1521:orcl"></property>

<property name="user" value="huro"></property>

<property name="password" value="123456"></property>

<!--1连接池中保留的最小连接数。 -->

<property name="minPoolSize">

<value>10</value>

</property>

<!--1连接池中保留的最大连接数。Default: 15 -->

<property name="maxPoolSize">

<value>100</value>

</property>

<!--1最大空闲时间,100秒内未使用则连接被丢弃。若为0则永不丢弃。Default: 0 -->

<property name="maxIdleTime">

<value>100</value>

</property>

<!--当连接池中的连接耗尽的时候c3p0一次同时获取的连接数。Default: 3 -->

<property name="acquireIncrement">

<value>10</value>

</property>

<!--1初始化时获取的连接数,取值应在minPoolSize与maxPoolSize之间。Default: 3 -->

<property name="initialPoolSize">

<value>20</value>

</property>

<!--每60秒检查所有连接池中的空闲连接。Default: 0 -->

<property name="idleConnectionTestPeriod">

<value>60</value>

</property>

</bean>

<bean id="sessionFactory"

class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">

<property name="dataSource">

<ref bean="dataSource" />

</property>

<property name="hibernateProperties">

<props>

<prop key="hibernate.dialect">org.hibernate.dialect.Oracle10gDialect</prop>

<prop key="hibernate.show_sql">true</prop>

<prop key="hibernate.format_sql">true</prop>

<!-- 程序运行时自动生成表 生成表之后,程序就不需要,可以注释掉 -->

<prop key="hibernate.hbm2ddl.auto">update</prop>

<!-- 设定jdbc的statement读取数据的时候每次从数据库中取出的记录条数 30、50、100性能好,内存消耗小 提高数据库性能 -->

<prop key="hibernate.jdbc.fetch_size">100</prop>

<!-- 批量更新操作时候的批次大小 提高数据库性能 -->

<prop key="hibernate.jdbc.batch_size">50</prop>

</props>

</property>

<property name="packagesToScan">

<value>com.spring.bean</value>

</property>

</bean>

<bean id="hibernateTemplate" class="org.springframework.orm.hibernate3.HibernateTemplate">

<property name="sessionFactory" ref="sessionFactory"></property>

</bean>

<!--配置一个JdbcTemplate实例 -->

<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">

<property name="dataSource" ref="dataSource" />

</bean>

<!-- 配置事务管理 -->

<bean id="txManager"

class="org.springframework.orm.hibernate3.HibernateTransactionManager">

<property name="sessionFactory" ref="sessionFactory"></property>

</bean>

<tx:annotation-driven transaction-manager="txManager" />

<aop:config>

<aop:pointcut expression="execution(public * com.spring.service.impl.*.*(..))"

id="businessService" />

<aop:advisor advice-ref="txAdvice" pointcut-ref="businessService" />

</aop:config>

<tx:advice id="txAdvice" transaction-manager="txManager">

<tx:attributes>

<tx:method name="find*" read-only="true" propagation="NOT_SUPPORTED" />

<!-- get开头的方法不需要在事务中运行 。 有些情况是没有必要使用事务的,比如获取数据。开启事务本身对性能是有一定的影响的 -->

<tx:method name="*" />    <!-- 其他方法在实务中运行 -->

</tx:attributes>

</tx:advice>

</beans>

4. 项目的Web.xml配置不变(参照2.5注解方式的一样

 

5. 项目类的代码不变(参照2.5注解方式的一样

 

6. 项目运行,测试(参照2.5注解方式的一样

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 3
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值