1. 程式人生 > >SSM框架整合篇

SSM框架整合篇

SSM整合

Author:SimpleWu

github(已上傳SSMrest風格簡單增刪該查例項):https://gitlab.com/450255266/code/

目前Spring+SpringMVC+Mybatis是一套非常流行的配套開發框架。

  1. spring核心ioc、aop技術,ioc解耦,使得程式碼複用,可維護性大幅度提升,aop提供切面程式設計,同樣的增強了生產力。提供了對其他優秀開源框架的整合支援
  2. spring mvc是對比struts2等mvc框架來說的,不說struts2爆出的那麼多安全漏洞,而且是類攔截,所有Action變數共享,同時是filter入口的,而spring mvc是方法攔截,controller獨享request response資料,採用的serlvet入口,與spring無縫對接。開發而言,spring mvc更加輕量和低入門。
  3. mybatis輕量級半自動化框架,sql由開發者編寫可對語句進行調優,並且mybatis使用XML方式JAVA程式碼與SQL可以解耦並且支援動態SQL語句,學習成本低。

框架搭建步驟

導包

  1. 匯入Spring+SpringMVC(如果不會選全倒進去就行了)
  2. 匯入mybatis包(如果需要用到日誌可將mybatis依賴包匯入)
  3. 匯入mybatis-spring-1.3.1.jar(整合必須又這個包)
  4. 匯入c3p0(當然你也可以使用其他連線池)
  5. 匯入資料庫驅動

配置log4j.properties

由於MyBatis依賴與log4j輸出sql語句資訊,所以需要配置log4j配置檔案。

#設定輸出級別和輸出位置
log4j.rootLogger=debug,Console
#設定控制檯相關的引數
log4j.appender.Console=org.apache.log4j.ConsoleAppender  
log4j.appender.Console.layout=org.apache.log4j.PatternLayout  
log4j.appender.Console.layout.ConversionPattern=%d [%t] %-5p [%c] - %m%n  
#設定MyBatis的輸出內容
log4j.logger.java.sql.ResultSet=INFO  
log4j.logger.org.apache=INFO  
log4j.logger.java.sql.Connection=DEBUG  
log4j.logger.java.sql.Statement=DEBUG  
log4j.logger.java.sql.PreparedStatement=DEBUG

配置WEB.xml

1.設定編碼過濾器

<filter>
        <description>字符集過濾器</description>
        <filter-name>encodingFilter</filter-name>
        <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
        <init-param>
            <description>字符集編碼</description>
            <param-name>encoding</param-name>
            <param-value>UTF-8</param-value>
        </init-param>
</filter>
<filter-mapping>
    <filter-name>encodingFilter</filter-name>
    <url-pattern>/*</url-pattern>
</filter-mapping>

2.新增Spring配置檔案位置(等下我們建立spring-context.xml)

<!-- 配置載入Spring-context檔案 -->
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>classpath:spring-context.xml</param-value>
    </context-param>
<!--新增Spring的監聽器-->
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>

3.DispatcherServlet配置

<!-- SprigMVC配置 -->
<servlet>
    <servlet-name>dispatcher</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <init-param>
        <description>springmvc 配置檔案</description>
        <param-name>contextConfigLocation</param-name>
        <param-value>classpath:spring-mvc.xml</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
    <servlet-name>dispatcher</servlet-name>
    <url-pattern>/</url-pattern>
</servlet-mapping>

4.新增PUT DELETE支援

<!-- 新增PUT DELETE支援 -->
    <filter>
        <filter-name>HiddenHttpMethodFilter</filter-name>
        <filter-class>org.springframework.web.filter.HiddenHttpMethodFilter</filter-class>
    </filter>
    <filter-mapping>
        <filter-name>HiddenHttpMethodFilter</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>

5.配置Sessin過期時間

<!-- 配置session超時時間,單位分鐘 -->
<session-config>
    <session-timeout>15</session-timeout>
</session-config>

spring-mvc.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/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.2.xsd
        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-4.2.xsd">
    <!-- 讓掃描spring掃描這個包下所有的類,讓標註spring註解的類生效 -->
    <context:component-scan base-package="com.simple.ssm.controller">
        <!-- 只掃描@Controller與@ControllerAdvice修飾的類 -->
        <context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
        <context:include-filter type="annotation" expression="org.springframework.web.bind.annotation.ControllerAdvice"/>
    </context:component-scan>


    <!-- 加入靜態資源與動態資源支援 -->
    <mvc:default-servlet-handler/>
    <mvc:annotation-driven/>
    
    <!-- 檢視解析器 -->
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/WEB-INF/views/" />
        <property name="suffix" value=".jsp"></property>
    </bean>

</beans>

spring-context.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:tx="http://www.springframework.org/schema/tx"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:aop="http://www.springframework.org/schema/aop"
    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-4.2.xsd
        http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.2.xsd
        http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.2.xsd">
    
    <!--引入外部資料庫連線資訊檔案-->
    <context:property-placeholder location="classpath:db.properties" />

    <!-- 掃描所有除@Controller ,@ControllerAdvice修飾的bean -->
    <context:component-scan base-package="com.simple.ssm">
        <context:exclude-filter type="annotation"
            expression="org.springframework.stereotype.Controller" />
        <context:exclude-filter type="annotation"
            expression="org.springframework.web.bind.annotation.ControllerAdvice" />
    </context:component-scan>

    <!-- 配置c3p0連線池 -->
    <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
        <property name="driverClass" value="${mysql.driverClass}" />
        <!-- jdbc:mysql://localhost/mybatis?characterEncoding=utf8&amp;serverTimezone=UTC -->
        <property name="jdbcUrl" value="${mysql.jdbcUrl}" />
        <!-- 連線使用者名稱 -->
        <property name="user" value="${mysql.user}" />
        <property name="password" value="${mysql.password}" />
        <!-- 連線密碼 -->
        <!-- 佇列中的最小連線數 -->
        <property name="minPoolSize" value="15" />
        <!-- 佇列中的最大連線數 -->
        <property name="maxPoolSize" value="25" />
        <!-- 當連線耗盡時建立的連線數 -->
        <property name="acquireIncrement" value="15" />
        <!-- 等待時間 -->
        <property name="checkoutTimeout" value="10000" />
        <!-- 初始化連線數 -->
        <property name="initialPoolSize" value="20" />
        <!-- 最大空閒時間,超出時間連線將被丟棄 -->
        <property name="maxIdleTime" value="20" />
        <!-- 每隔60秒檢測空閒連線 -->
        <property name="idleConnectionTestPeriod" value="60000" />
    </bean>

    <!-- 配置事務管理器 -->
    <bean id="transactionManager"
        class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource" ref="dataSource" />
    </bean>

    <!-- 攔截器方式配置事物 -->
    <!-- 
        <tx:advice id="txAdvice" transaction-manager="transactionManager">
            <tx:attributes>
                <tx:method name="add*" propagation="REQUIRED" />
                <tx:method name="insert*" propagation="REQUIRED" />
                <tx:method name="save*" propagation="REQUIRED" />
                <tx:method name="update*" propagation="REQUIRED" />
                <tx:method name="delete*" propagation="REQUIRED" />
                <tx:method name="remove*" propagation="REQUIRED" />
                <tx:method name="find*" propagation="SUPPORTS" />
                <tx:method name="load*" propagation="SUPPORTS" />
                <tx:method name="search*" propagation="SUPPORTS" />
                <tx:method name="*" propagation="SUPPORTS" />
            </tx:attributes>
        </tx:advice>
     -->

    <!-- 配置切面 -->
    <!-- <aop:config> 事務入口(Service的包路徑) <aop:pointcut id="transactionPointcut" 
        expression="execution(* com.simple.ssm.service.*.*(..))" /> 將事務通知與切入點組合 <aop:advisor 
        pointcut-ref="transactionPointcut" advice-ref="txAdvice" /> </aop:config> -->

    <!-- 使用註解來控制事務 -->
    <tx:annotation-driven transaction-manager="transactionManager" />

    <!-- 配置mybatis, 繫結c3p0 -->
    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <property name="dataSource" ref="dataSource" />
        <!-- 配置mybatis配置檔案所在位置 -->
        <property name="configLocation" value="classpath:mybatis-config.xml" />
        <!-- 配置實體類XML對映所在位置 -->
        <property name="mapperLocations" value="classpath:com/simple/ssm/dao/mapper/*.xml" />
    </bean>

    <!-- 掃描生成所有dao層 -->
    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        <!-- 指定持久化介面包位置 -->
        <property name="basePackage" value="com.simple.ssm.dao"></property>
        <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"></property>
    </bean>
</beans>

mybaits-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="mapUnderscoreToCamelCase" value="true"/>
        <!-- 開啟二級快取 -->
        <setting name="cacheEnabled" value="true"/>
        <!-- 開啟懶載入 -->
        <setting name="lazyLoadingEnabled" value="true"/>
    </settings>
    
    <typeAliases>
        <package name="com.simple.ssm.entitys"/>
    </typeAliases>
</configuration>

db.properties(可內建)

mysql.driverClass=com.mysql.jdbc.Driver
mysql.jdbcUrl=jdbc:mysql://localhost/mybatis?characterEncoding=utf8&serverTimezone=UTC
mysql.user=root
mysql.password=root

到這裡其實我們的SSM已經整合完成,如果我們需要其他功能可以在加,不要忘記匯入包。

學習是永無止境的。

來源:https://www.cnblogs.com/SimpleWu/p/9792466.html

 

鄭州女性不孕不育醫院

鄭州不孕不育醫院

鄭州哪個醫院治不孕不育好

鄭州不孕不育