ssm整合(Spring+SpringMVC+Mybatis)

1、Spring

Spring致力於提供一種方法管理你的業務對象。IOC容器,它能夠裝載bean(也就是咱們java中的類,固然也包括service dao裏面的),有了這個機制,咱們就不用在每次使用這個類的時候爲它初始化,不多看到關鍵字new。另外spring的aop,事務管理等等都是咱們常常用到的。css

2、Mybatis

第一,它能自由控制sql,這會讓有數據庫經驗的人編寫的代碼能搞提高數據庫訪問的效率。html

第二,它可使用xml的方式來組織管理咱們的sql,由於通常程序出錯不少狀況下是sql出錯,別人接手代碼後能快速找到出錯地方,甚至能夠優化原來寫的sql。java

3、SpringMVC

它用於web層,至關於controller(等價於傳統的servlet和struts的action),用來處理用戶請求。mysql

舉個例子,用戶在地址欄輸入http://網站域名/login,那麼springmvc就會攔截到這個請求,而且調用controller層中相應的方法,(中間可能包含驗證用戶名和密碼的業務邏輯,以及查詢數據庫操做,但這些都不是springmvc的職責),最終把結果返回給用戶,而且返回相應的頁面(固然也能夠只反饋josn/xml等格式數據)。git

springmvc就是作前面和後面過程的活,與用戶打交道!!github

 

 

4、簡單的整合過程

先將Spring和mybatis配置文件完成,緊接着完成SpringMVC配置文件,最後將三大框架寫進web.xmlweb

第一步:導入jar包(此處沒有使用Maven,沒有寫pom.xml文件,採用的是從本地直接引入jar包)spring

 

第二步:添加配置文件(持久層、事務、業務層、SpringMVC)sql

總體配置文件結構圖:數據庫

 

spring+mybatis整合配置文件(通常命名爲spring-mybatis.xml或applicationContext-dao.xml

 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <beans xmlns="http://www.springframework.org/schema/beans"
 3     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
 4     xmlns:context="http://www.springframework.org/schema/context"
 5     xmlns:mvc="http://www.springframework.org/schema/mvc"
 6     xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
 7         http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd
 8         http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
 9           <!-- 經過命名空間配置解析配置文件的工具類 -->
10        <context:property-placeholder location="classpath:res-*.properties"/>
11      <!-- 配置數據源。dbcp  c3p0 druid。。。 -->
12     <!-- 配置數據源 dataSource -->
13     <!-- <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource" destroy-method="close">
14         <property name="url" value="${jdbc.url}" />
15         <property name="username" value="${jdbc.username}" />
16         <property name="password" value="${jdbc.password}" />
17         <property name="driverClassName" value="${jdbc.driver}" />
18         <property name="maxActive" value="10" />
19         <property name="minIdle" value="5" />
20     </bean> -->
21     <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
22         <property name="driverClassName" value="com.mysql.jdbc.Driver"></property>
23         <property name="url" value="jdbc:mysql://localhost:3306/test001"></property>
24         <property name="username" value="root"></property>
25         <property name="password" value="root"></property>
26     </bean>
27     <!-- 配置建立mybatis上下文對象的工具類 -->
28     <bean  class="org.mybatis.spring.SqlSessionFactoryBean" >
29         <!-- 配置datasource -->
30         <property name="dataSource" ref="dataSource"/>
31         <!-- 配置mybatis的配置文件 -->
32         <property name="configLocation" value="classpath:SqlMapperClient.xml"/>
33     </bean>
34     <!-- 配置掃描接口與映射配置文件對象  該對象會去建立接口的代理對象。而且根據接口的名稱做爲該對象的默認名稱-->
35     <bean  class="org.mybatis.spring.mapper.MapperScannerConfigurer">
36         <property name="basePackage" value="com.chenk.mapper"/>
37     </bean>
38 </beans>

數據庫配置文件res-db.properties

jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/test001?characterEncoding=utf-8
jdbc.username=root
jdbc.password=root

 

applicationContext-service.xml或者稱爲spring.xml

 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <beans xmlns="http://www.springframework.org/schema/beans"
 3     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
 4     xmlns:context="http://www.springframework.org/schema/context"
 5     xmlns:mvc="http://www.springframework.org/schema/mvc"
 6     xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
 7         http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd
 8         http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
 9           <!-- 經過命名空間配置解析配置文件的工具類 -->
10    <context:component-scan base-package="com.chenk.service"/>
11 </beans>

applicationContext-trans.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.xsd
        http://www.springframework.org/schema/aop
        http://www.springframework.org/schema/aop/spring-aop.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context.xsd
        http://www.springframework.org/schema/tx
        http://www.springframework.org/schema/tx/spring-tx.xsd">
     <!-- 須要在配置文件的頭中開啓tx的命名空間 -->
     <!-- 先配置基於dataSource的事務管理器的切面(AOP)-->
     <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
         <!-- 該切面須要一個datasource -->
         <property name="dataSource">
             <ref bean="dataSource"/>
         </property>
     </bean>
    <!-- 配置事物傳播行爲以及事物隔離級別 -->
    <tx:advice id="txAdvice" transaction-manager="transactionManager">
        <tx:attributes>
            <!-- 
            name:須要參與到事物控制中的方法名。可使用*來通配
            propagation:事物的傳播行爲。REQUIRED 必需要運行在一個事務中
            isolation:事物的隔離級別 。DEFAULT以數據默認的隔離級別爲主。
            read-only:只讀事物。只讀並不會作事物提交與回滾。他會優化查詢執行的效率。因此若是不是DML操做。建議都須要擁有隻讀事物
             -->
            <tx:method name="add*" propagation="REQUIRED" isolation="DEFAULT"/>
            <tx:method name="drop*" propagation="REQUIRED"/>
            <tx:method name="modify*" propagation="REQUIRED"/>
            <tx:method name="update*" propagation="REQUIRED"/>
            <tx:method name="*" read-only="true"/>
        </tx:attributes>
    </tx:advice>
    <!-- 配置切點 -->
    <aop:config>
        <aop:pointcut expression="execution(* com.chenk.service.impl.*.*(..))" id="mypointcut"/>
        <aop:advisor advice-ref="txAdvice" pointcut-ref="mypointcut"/>
    </aop:config>
</beans>

SqlMapperClient.xml(mybaits的整體配置文件,都是爲了方便後期的維護擴展)

<?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>
    <!-- 之因此咱們須要添加一個mybaits的整體配置文件,目的是爲了之後配置一些mybaits的插件 好比說分頁插件 -->
  <plugins>
          <plugin interceptor="com.github.pagehelper.PageHelper">
            <!-- 設置數據庫類型 Oracle,Mysql,MariaDB,SQLite,Hsqldb,PostgreSQL六種數據庫-->        
            <property name="dialect" value="mysql"/>
        </plugin>
  </plugins>
  </configuration>

 

springmvc.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:mvc="http://www.springframework.org/schema/mvc"
    xmlns:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans.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.xsd">
    <!-- 掃描組件 -->
    <context:component-scan base-package="com.chenk.web.contoller"/>
    <mvc:annotation-driven/>
    
    
    <!-- 對靜態資源方式 -->
    <mvc:resources location="/WEB-INF/js/" mapping="/js/**"/>
    <mvc:resources location="/WEB-INF/css/" mapping="/css/**"/>
    <mvc:resources location="/WEB-INF/img/" mapping="/img/**"/>
    <mvc:resources location="/WEB-INF/upload/" mapping="/upload/**"/>
    <!-- 配置視圖解析器 -->
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/WEB-INF/jsp/"/>
        <property name="suffix" value=".jsp"/>
    </bean>
    <!-- 文件上傳組件 -->
    <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
        <!-- 設置表單中字符的編碼格式 -->
        <property name="defaultEncoding" value="utf-8"></property>
        <!-- 上傳文件的字節大小 單位是字節 -->
        <property name="maxUploadSize" value="100000000"></property>
        <!-- 設置上傳圖片的字節緩衝區的大小 -->
        <property name="maxInMemorySize" value="1024"></property>
    </bean>
    
    <!-- 配置異常處理器 -->
    <bean id="exceptionResolver" class="org.springframework.web.servlet.handler.SimpleMappingExceptionResolver">
    <property name="exceptionMappings">
        <props>
            <prop key="org.springframework.web.multipart.MaxUploadSizeExceededException">error</prop>
            <prop key="java.lang.RuntimeException">redirect:/error2.jsp</prop>
        </props>
    </property>    
    </bean>
</beans>

 

配置log4j日誌打印文件log4j.properties

log4j.rootLogger=debug,R,stdout

log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=<%d> %p (%F:%L) [%t] (%c) - %m%n

log4j.appender.R=org.apache.log4j.RollingFileAppender
log4j.appender.R.File=d:/SysLog.log
log4j.appender.R.MaxFileSize=500KB
log4j.appender.R.MaxBackupIndex=7
log4j.appender.R.layout=org.apache.log4j.PatternLayout
log4j.appender.R.layout.ConversionPattern=<%d> %p (%F:%L) [%t] %c - %m%n

 

web.xml(將三大框架的啓動寫進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">
  <!-- 啓動spring -->
  <context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>classpath:applicationContext-*.xml</param-value>
  </context-param>
  <listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  </listener>
  <!-- 啓動springmvc -->
  <servlet>
      <servlet-name>DispatcherServlet</servlet-name>
      <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
      <init-param>
          <param-name>contextConfigLocation</param-name>
          <param-value>classpath:springmvc.xml</param-value>
      </init-param>
     <load-on-startup>1</load-on-startup>
  </servlet>
  <servlet-mapping>
      <servlet-name>DispatcherServlet</servlet-name>
      <url-pattern>/</url-pattern>
  </servlet-mapping>
</web-app>

 到此爲止,配置文件所有完成,測試

 

推薦文章:

《SSM框架搭建》二.mybatis3,spring4整合                                      http://www.javashuo.com/article/p-zkxbfdnu-e.html

手把手教你整合最優雅SSM框架:SpringMVC + Spring + MyBatis      https://github.com/liyifeng1994/ssm

相關文章
相關標籤/搜索