SpringMvc+Spring+Mybatis 环境集成



1、基本概念


1.1、Spring


        Spring是一个开源框架,Spring是于2003 年兴起的一个轻量级的Java 开发框架,由Rod Johnson 在其著作Expert One-On-One J2EE Development and Design中阐述的部分理念和原型衍生而来。它是为了解决企业应用开发的复杂性而创建的。Spring使用基本的JavaBean来完成以前只可能由EJB完成的事情。然而,Spring的用途不仅限于服务器端的开发。从简单性、可测试性和松耦合的角度而言,任何Java应用都可以从Spring中受益。 简单来说,Spring是一个轻量级的控制反转(IoC)和面向切面(AOP)的容器框架。


1.2、SpringMVC
     
        Spring MVC属于SpringFrameWork的后续产品,已经融合在Spring Web Flow里面。Spring MVC 分离了控制器、模型对象、分派器以及处理程序对象的角色,这种分离让它们更容易进行定制。




2.环境搭建详解

1.web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
 <servlet>
  <servlet-name>hello</servlet-name>
  <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
  <init-param>
  <param-name>contextConfigLocation</param-name>
  <param-value>classpath*:config/hello-servlet.xml</param-value>
  </init-param>
  <load-on-startup>1</load-on-startup>
 </servlet>
   <servlet-mapping>
  <servlet-name>hello</servlet-name>
  <url-pattern>/</url-pattern>
  </servlet-mapping>
  
   <context-param>  
        <param-name>contextConfigLocation</param-name>  
        <param-value>classpath*:config/springAnnotation-core.xml;classpath:config/spring-myBatis.xml</param-value>  
        <!-- <param-value>classpath*:config/springAnnotation-servlet.xml</param-value> -->  
  </context-param>  
    
<!--   配置spring启动listener入口 -->  
  <listener>  
     <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>  
  </listener>  
    <!--SpringMvc提供的编码配置  -->
  <filter>
        <filter-name>encodingFilter</filter-name>
        <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
        <init-param>
            <param-name>encoding</param-name>
            <param-value>UTF-8</param-value>
        </init-param>
        <init-param>
            <param-name>forceEncoding</param-name>
            <param-value>true</param-value>
        </init-param>
    </filter>
    <filter-mapping>
        <filter-name>encodingFilter</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>
</web-app>

2.SpringMvc配置文件(hello-servlet.xml)

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"  
 xmlns:context="http://www.springframework.org/schema/context"  
 xmlns:p="http://www.springframework.org/schema/p"  
 xmlns:mvc="http://www.springframework.org/schema/mvc"  
 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
 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.xsd  
      http://www.springframework.org/schema/mvc  
      http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd"> 
      <!-- 注解扫描包 -->
<context:component-scan base-package="com.gaorui.Controller.Annotation"/>
<!-- <context:component-scan base-package="com.gaorui.Service"/> -->
<mvc:annotation-driven/>
<bean  id="ViewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/jsp/"></property>
<property name="suffix" value=".jsp"></property>
</bean>
</beans> 

3.Spring和SpringMvc集成的配置文件(springAnnotation-core.xml)

通过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: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-2.5.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context-2.5.xsd
        http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
        http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd" >


<bean  id="springManager" class="com.gaorui.Service.SpringManager">
</bean>




    
</beans>

4.Spring和Mybatis的集成(springAnnotation-core.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:aop="http://www.springframework.org/schema/aop"   
xmlns:context="http://www.springframework.org/schema/context"  
xmlns:jee="http://www.springframework.org/schema/jee"  
xmlns:tx="http://www.springframework.org/schema/tx"  
xsi:schemaLocation="    
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd  
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd  
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd  
http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-4.0.xsd  
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd">


<context:property-placeholder location="classpath:resource/db.properties" />

<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
<property name="username" value="${db.username}"></property>
<property name="password" value="${db.password}"></property>
<property name="driverClassName" value="${db.driverClassName}"></property>  
<property name="url" value="${db.url}"></property>
<property name="maxActive" value="${db.maxActive}"></property>
        <property name="maxIdle" value="${db.maxIdle}"></property>
        <property name="maxWait" value="${db.maxWait}"></property>
        <property name="defaultAutoCommit" value="${db.defaultAutoCommit}"></property>
</bean>


<!-- spring会话管理 -->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="configLocation" value="classpath:resource/mybatis-config.xml" />
<property name="dataSource" ref="dataSource" />
<property name="mapperLocations">
<list>
<!-- 在com.kanq.app.archives.mapper包或以下所有目录中所有以.xml结尾的文件 -->
<value>classpath:com/gaorui/mapper/*.xml</value>
</list>
</property>
<!-- 指定了这个属性之后,mybatis配置文件中的sql语句的返回实体可以不用嘉宝了 -->
<property name="typeAliasesPackage" value="com.gaorui.entity" /> 
</bean>


//这里是Spring整合Mybatis重要核心

主要亮点在于 UserMapper 居然不用实现类,而且在调用 getUser 的时候,也是使用直接调用了UserMapper实现类

以上的MapperScannerConfigurer class的注释中描述道: 
   从base 包中搜索所有下面所有 interface,并将其注册到 Spring Bean容器中,其注册的class bean是MapperFactoryBean。 
<!-- 自动扫描com.kanq.app.springmvc.mapper包下的Mapper接口,并实现其功能  -->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="com.gaorui.dao" />
</bean>
    
    <!-- spring事务管理 -->
    <bean name="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource" ref="dataSource"></property>
    </bean>
    
    <!-- 使用注解的方式来管理声明式事务 -->
    <tx:annotation-driven transaction-manager="transactionManager"/>
    
    <!-- 事务操作 
    <tx:advice id="txAdvice" transaction-manager="transactionManager">
<tx:attributes>  
<tx:method name="insert*" propagation="REQUIRED" />  
<tx:method name="update*" propagation="REQUIRED" />  
<tx:method name="edit*" propagation="REQUIRED" />  
<tx:method name="save*" propagation="REQUIRED" />  
<tx:method name="add*" propagation="REQUIRED" />  
<tx:method name="new*" propagation="REQUIRED" />  
<tx:method name="set*" propagation="REQUIRED" />  
<tx:method name="remove*" propagation="REQUIRED" />  
<tx:method name="delete*" propagation="REQUIRED" />  
<tx:method name="change*" propagation="REQUIRED" />  
<tx:method name="get*" propagation="REQUIRED" read-only="true" />  
<tx:method name="find*" propagation="REQUIRED" read-only="true" />  
<tx:method name="load*" propagation="REQUIRED" read-only="true" />  
<tx:method name="*" propagation="REQUIRED" read-only="true" />  
</tx:attributes>  
</tx:advice>

<aop:config>  
<aop:pointcut id="serviceOperation" expression="execution(* comcompany.service..*.*(..))" />  
<aop:advisor advice-ref="txAdvice" pointcut-ref="serviceOperation" />  
</aop:config>
-->
</beans>

4.Mybatis的本地配置(mybatis-config.xml)

<?xml version="1.0" encoding="UTF-8" ?> 
<!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN" "http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
<settings>
<!-- 全局映射器启用缓存 -->
<setting name="cacheEnabled" value="true" />
<!-- 查询时,关闭关联对象即时加载以提高性能 -->
<setting name="lazyLoadingEnabled" value="true" />
<!-- 设置关联对象加载的形态,此处为按需加载字段(加载字段由SQL指 定),不会加载关联表的所有字段,以提高性能 -->
<setting name="aggressiveLazyLoading" value="false" />
<!-- 对于未知的SQL查询,允许返回不同的结果集以达到通用的效果 -->
<setting name="multipleResultSetsEnabled" value="true" />
<!-- 允许使用列标签代替列名 -->
<setting name="useColumnLabel" value="true" />
<!-- 允许使用自定义的主键值(比如由程序生成的UUID 32位编码作为键值),数据表的PK生成策略将被覆盖 -->
<setting name="useGeneratedKeys" value="true" />
<!-- 给予被嵌套的resultMap以字段-属性的映射支持 -->
<setting name="autoMappingBehavior" value="FULL" />
<!-- 对于批量更新操作缓存SQL以提高性能 -->
<setting name="defaultExecutorType" value="BATCH" />
<!-- 数据库超过25000秒仍未响应则超时 -->
<setting name="defaultStatementTimeout" value="25000" />
</settings>
</configuration>

项目整体路径:



發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章