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>
項目整體路徑: