springMVC框架整合--(Spring+SpringMVC+hibernate)

来源:互联网 发布:软件测试的简历 编辑:程序博客网 时间:2024/05/22 07:02

Spring MVC属于SpringFrameWork的后续产品,已经融合在Spring Web Flow里面。Spring 框架提供了构建 Web 应用程序的全功能 MVC 模块。使用 Spring 可插入的 MVC 架构,从而在使用Spring进行WEB开发时,可以选择使用Spring的SpringMVC框架或集成其他MVC开发框架,如hibernate,MyBatis等。

注解:
扫描指定的包中的类上的注解,常用的注解有:
@Controller 声明Action组件 @RequestMapping (“/user”) @RequestMapping (“/login”)
@Service 声明Service组件 @Service(“myMovieLister”)
@Repository 声明Dao组件
@Component 泛指组件, 当不好归类时.
@RequestMapping(“/menu”) 请求映射
@Resource 用于注入,( j2ee提供的 ) 默认按名称装配,@Resource(name=”beanName”)
@Autowired 用于注入,(srping提供的) 默认按类型装配
@Transactional( rollbackFor={Exception.class}) 事务管理
@ResponseBody 返回数据以json数据形式返回
@Scope(“prototype”) 设定bean的作用域

1.配置web.xml , 使用Spring MVC,配置DispatcherServlet

<servlet>         <servlet-name> springmvc</servlet-name >         <servlet-class> org.springframework.web.servlet.DispatcherServlet</servlet-class >         <init-param>              <param-name> contextConfigLocation</param-name >             <param-value> classpath:dispatcher-servlet.xml</param-value > //SpringMvc的分发配置文件        </init-param>     </servlet >     <servlet-mapping >         <servlet-name> springmvc</servlet-name >         <url-pattern> /</ url-pattern>//拦截/user/login     </servlet-mapping > <context-param >                    <param-name> contextConfigLocation</param-name >                   <param-value> classpath:application-context.xml</param-value >//数据库初始化,事务初始化,model层扫描     </context-param ><listener>      <listener-class> org.springframework.web.context.ContextLoaderListener</listener-class >    </listener >

2.dispatcher-servlet.xml或者springMVC.xml的配置

<!-- Enables the Spring MVC @Controller programming model -->     <mvc:annotation-driven >           <mvc:argument-resolvers>                    <bean class="org.springframework.mobile.device.DeviceWebArgumentResolver" />               <bean                    class= "org.springframework.mobile.device.site.SitePreferenceWebArgumentResolver" />           </mvc:argument-resolvers>     </mvc:annotation-driven ><!-- Handles HTTP GET requests for /resources/** by efficiently serving          up static resources in the ${webappRoot}/resources directory -->     <mvc:resources mapping="/resources/**" location="/resources/" />     <!-- spring mvc对静态资源的访问 -->        <mvc:resources location= "/image/" mapping="/image/**" />               <mvc:resources location= "/js/" mapping ="/js/**"/>                <mvc:resources location= "/css/" mapping="/css/**" />                         <mvc:resources location="/static/" mapping="/static/**"/>     <!-- Resolves views selected for rendering by @Controllers to .jsp resources          in the /WEB-INF/views directory -->           <bean class="org.springframework.mobile.device.view.LiteDeviceDelegatingViewResolver" >           <constructor-arg>               <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver" >                    <property name= "prefix" value="/WEB-INF/views/" />                    <property name= "suffix" value =".jsp" />               </bean>           </constructor-arg>           <property name= "enableFallback" value ="true" />           <property name= "mobilePrefix" value ="mobile/" />           <property name= "tabletPrefix" value ="tablet/" />     </bean ><!-- json -->   <bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter" >           <property name="messageConverters">                <list >                   <ref bean="mappingJacksonHttpMessageConverter" />               </list>            </property >         </bean > 

3.数据库链接,model层扫描,事务配置

<?xml version= "1.0" encoding ="UTF-8"?><beans xmlns="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context" 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:p="http://www.springframework.org/schema/p" xmlns:util="http://www.springframework.org/schema/util" xmlns:jdbc="http://www.springframework.org/schema/jdbc"    xmlns:cache="http://www.springframework.org/schema/cache"    xsi:schemaLocation= "    http://www.springframework.org/schema/context    http://www.springframework.org/schema/context/spring-context.xsd    http://www.springframework.org/schema/beans    http://www.springframework.org/schema/beans/spring-beans.xsd    http://www.springframework.org/schema/tx    http://www.springframework.org/schema/tx/spring-tx.xsd    http://www.springframework.org/schema/jdbc    http://www.springframework.org/schema/jdbc/spring-jdbc-3.1.xsd    http://www.springframework.org/schema/cache    http://www.springframework.org/schema/cache/spring-cache-3.1.xsd    http://www.springframework.org/schema/aop    http://www.springframework.org/schema/aop/spring-aop.xsd">    <!-- 自动扫描web包 ,将带有注解的类 纳入spring容器管理 -->    <context:component-scan base-package="com.first" ></context:component-scan>    <!-- 引入jdbc配置文件 -->    <bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer" >        <property name= "locations">            <list>                <value> classpath:jdbc.properties</value >            </list>        </property>    </bean >    <!-- data source -->    <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" >                    <property name= "driverClassName" value="${jdbc.driver}" > </property>               <property name= "url" value="${jdbc.url}" ></property>                    <property name= "username" value="${jdbc.user}" ></property>                    <property name= "password" value="${jdbc.pass}" ></property>                    <!--initialSize: 初始化连接-->                      <property name= "initialSize" value="5" />                     <!--maxIdle: 最大空闲连接-->                      <property name= "maxIdle" value ="10"/>                      <!--minIdle: 最小空闲连接-->                      <property name= "minIdle" value ="5"/>                     <!--maxActive: 最大连接数量-->                      <property name= "maxActive" value="15" />                     <!--removeAbandoned: 是否自动回收超时连接-->                      <property name= "removeAbandoned" value="true" />                     <!--removeAbandonedTimeout: 超时时间(以秒数为单位)-->                      <property name="removeAbandonedTimeout" value="180"/>                      <!--maxWait: 超时等待时间以毫秒为单位 6000毫秒/1000等于60秒-->                     <property name= "maxWait" value="3000" />                     <property name= "validationQuery">                     <value> SELECT 1</ value>                     </property>                      <property name= "testOnBorrow">                     <value> true</ value>                     </property>             </bean>           <bean id = "jdbcTemplate"            class = "org.springframework.jdbc.core.JdbcTemplate" >           <property name = "dataSource" ref="dataSource"/>       </bean ><!-- 配置sessionFactory,model层扫描 -->      <bean id="sessionFactory"       class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean" >         <property name= "dataSource" ref ="dataSource" />         <property name= "packagesToScan" value="com.first.model" />         <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= "hibernate.format_sql">true</prop >             </props>          </property>      </bean >     <!-- 配置事务管理器 -->     <bean id="transactionManager"         class="org.springframework.orm.hibernate3.HibernateTransactionManager" >         <property name= "sessionFactory">             <ref bean= "sessionFactory" />          </property>      </bean >     <!-- 配置事务的传播特性 -->      <tx:advice id="txAdvice" transaction-manager="transactionManager" >         <tx:attributes>          <tx:method name= "save*" propagation ="REQUIRED" />             <tx:method name= "add*" propagation ="REQUIRED" />             <tx:method name= "delete*" propagation ="REQUIRED" />             <tx:method name= "update*" propagation="REQUIRED" />             <tx:method name= "*" read-only ="true" />         </tx:attributes>      </tx:advice >     <!-- 那些类的哪些方法参与事务-->    <aop:config >         <aop:pointcut id= "allManagerMethod"             expression="bean(*Service)" />         <aop:advisor pointcut-ref="allManagerMethod" advice-ref="txAdvice" />     </aop:config >       <tx:annotation-driven transaction-manager="transactionManager" /></beans>

4.jdbc.properties配置

jdbc.url=jdbc\:mysql \://localhost\:3306/test?useUnicode\=true&characterEncoding\=UTF-8jdbc.user=rootjdbc.pass=jdbc.driver=com.mysql.jdbc.Driver
0 0
原创粉丝点击