使用ssm框架的配置文件介紹

今天是我入職的第十三天,也是我從事java碼農開始,我就將我這一陣子的學習成果,做一個簡單的記錄,也是做一個前車之鑑,將自己學習過程中遇到的坑,寫在博客裏,也給有需要的朋友一點建議吧!下面開始我的筆記。

首先介紹ssm的配置文件,ssm分別是控制層的SpringMVC,業務層的Spring,持久層的Mybatis。

對於他們的詳細介紹,我會在以後的學習中,慢慢體會他們的原理,然後寫出記錄下來。

首先是SpringMVC的配置文件如下:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:p="http://www.springframework.org/schema/p" xmlns:context="http://www.springframework.org/schema/context"
    xmlns:aop="http://www.springframework.org/schema/aop" xmlns:websocket="http://www.springframework.org/schema/websocket"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans-4.2.xsd
    http://www.springframework.org/schema/context
    http://www.springframework.org/schema/context/spring-context-4.2.xsd
    http://www.springframework.org/schema/mvc
    http://www.springframework.org/schema/mvc/spring-mvc-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/websocket
    http://www.springframework.org/schema/websocket/spring-websocket.xsd">
    <mvc:resources mapping="/css/**" location="/css/"/>
    <mvc:resources mapping="/js/**" location="/js/"/>
    <mvc:resources mapping="/images/**" location="/images/"/>
    <!-- <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/WEB-INF/views/"/>
        <property name="suffix" value=".jsp"/>
    </bean> -->
</beans>

文件中的bean是用來簡寫返回視圖的路徑的,也就是ModelAndView,因爲路徑我現在也不是很清楚,所以我就把它屏蔽了,等真正明白了,我在記錄下。

接下來的是mybatis的配置文件:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE configuration
PUBLIC "-//ibatis.apache.org//DTD Config 3.0//EN"
"http://ibatis.apache.org/dtd/ibatis-3-config.dtd">
<configuration>
    <settings>
        <setting name="mapUnderscoreToCamelCase" value="true" />
    </settings>
</configuration>

僅僅設置了一個setting,這個setting的用處就是,在映射數據庫表和javabean時,數據庫中的字段一般都是以下劃線分割的比如:user_name,login_name等等,而javabean的字段一般都是以駝峯命名法命名的,比如userName,loginName等等,設置了setting,就可以完成映射。

下面主要介紹一下Spring的配置文件,也是SSM框架的核心,Spring說什麼一定要會,這個太重要了

SSM框架整合,基本上所有的配置文件都在Spring裏面了,因爲所有的對象都交給Spring容器管理了,大大降低了三層之間的耦合度,配置文件如下:

<?xml version="1.0" encoding="utf-8" ?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:tx="http://www.springframework.org/schema/tx"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:aop="http://www.springframework.org/schema/aop" xmlns:rabbit="http://www.springframework.org/schema/rabbit"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans-4.1.xsd
    http://www.springframework.org/schema/context
    http://www.springframework.org/schema/context/spring-context-4.1.xsd
    http://www.springframework.org/schema/mvc
    http://www.springframework.org/schema/mvc/spring-mvc-4.1.xsd
    http://www.springframework.org/schema/rabbit
    http://www.springframework.org/schema/rabbit/spring-rabbit-1.3.xsd
    http://www.springframework.org/schema/aop
    http://www.springframework.org/schema/aop/spring-aop-4.1.xsd
    http://www.springframework.org/schema/tx
    http://www.springframework.org/schema/tx/spring-tx-4.1.xsd">
    <!-- 開啓controller和service的包掃描 -->
    <context:component-scan base-package="com.doart.controller,com.doart.service"/>
    <mvc:annotation-driven>
        <mvc:message-converters>
            <bean class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter"/>
        </mvc:message-converters>
    </mvc:annotation-driven>
    <aop:aspectj-autoproxy expose-proxy="true"/>
    <!--數據源-->
    <bean id="baseDataSource" class="com.alibaba.druid.pool.DruidDataSource" abstract="true" >
        <!-- 配置初始化大小、最小、最大 -->
        <property name="initialSize" value="1" />
        <property name="minIdle" value="1" />
        <property name="maxActive" value="20" />
        <!-- 配置獲取連接等待超時的時間 -->
        <property name="maxWait" value="60000" />
        <!-- 配置間隔多久才進行一次檢測,檢測需要關閉的空閒連接,單位是毫秒 -->
        <property name="timeBetweenEvictionRunsMillis" value="60000" />
        <!-- 配置一個連接在池中最小生存的時間,單位是毫秒 -->
        <property name="minEvictableIdleTimeMillis" value="300000" />
        <!-- 用來檢測連接是否有效的sql,要求是一個查詢語句 -->
        <property name="validationQuery" value="SELECT now()" />
        <!-- 建議配置爲true,不影響性能,並且保證安全性。申請連接的時候檢測, 如果空閒時間大於timeBetweenEvictionRunsMillis,執行validationQuery檢測連接是否有效 -->
        <property name="testWhileIdle" value="true" />
        <property name="testOnBorrow" value="false" />
        <property name="testOnReturn" value="false" />
        <!-- 配置監控統計攔截的filters -->
        <property name="filters" value="stat" />
    </bean>
    <bean id="cjDataSource" class="com.alibaba.druid.pool.DruidDataSource" parent="baseDataSource" init-method="init" destroy-method="close">
        <!-- 基本屬性 url、user、password -->
        <property name="url" value="jdbc:mysql://192.168.0.104:3306/test?useUnicode=true&amp;characterEncoding=UTF-8&amp;allowMultiQueries=true&amp;zeroDateTimeBehavior=convertToNull&amp;generateSimpleParameterMetadata=true" />
        <property name="username" value="root" />
        <property name="password" value="1234" />
    </bean>
    <bean id="paginationInterceptor" class="com.github.pagehelper.PageInterceptor">
        <property name="properties">
            <value>
                helperDialect=mysql
                reasonable=true
                supportMethodsArguments=true
                params=count=countSql
                autoRuntimeDialect=true
            </value>
        </property>
    </bean>
    <!-- session工廠 mybatis的核心對象-->
    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <property name="plugins">
            <array>
                <ref bean="paginationInterceptor" />
            </array>
        </property>
        <property name="dataSource" ref="cjDataSource" />
        <property name="configLocation" value="classpath:mybatisConfig.xml"/>
        <property name="mapperLocations" value="classpath*:mapping/*.xml"/>
    </bean>
    <!-- 開啓dao層映射掃描-->
    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        <property name="basePackage" value="com.doart.dao" />
        <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"></property>
    </bean>
    <!-- 配置事務管理器 -->
    <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource" ref="cjDataSource" />
    </bean>
    <!--json格式數據轉換的配置  -->
    <bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter">
        <property name="messageConverters">
            <list>
                <bean class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter">
                </bean>
            </list>
        </property>
    </bean>
    <bean id="multipartResolver"
        class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
        <property name="defaultEncoding" value="utf-8"/><!-- 默認編碼ISO-8859-1 -->
        <property name="maxInMemorySize" value="10240"/><!-- 最大內存 10M -->
        <property name="uploadTempDir" value="/upload/"/><!-- 上傳的文件名字 -->
        <property name="maxUploadSize" value="-1" /><!-- 最大文件,-1不限制 -->
    </bean>
    <!-- 攔截器方式配置事務 -->
    <tx:advice id="transactionAdvice" transaction-manager="transactionManager">
        <tx:attributes>
            <tx:method name="import*" propagation="REQUIRED" rollback-for="java.lang.Exception"/>
            <tx:method name="add*" propagation="REQUIRED"      rollback-for="java.lang.Exception" />
            <tx:method name="insert*" propagation="REQUIRED" rollback-for="java.lang.Exception"/>
            <tx:method name="create*" propagation="REQUIRED" rollback-for="java.lang.Exception"/>
            <tx:method name="update*" propagation="REQUIRED" rollback-for="java.lang.Exception"/>
            <tx:method name="delete*" propagation="REQUIRED" rollback-for="java.lang.Exception"/>
            <tx:method name="batchInsert*" propagation="REQUIRED" rollback-for="java.lang.Exception"/>
            <tx:method name="batchUpdate*" propagation="REQUIRED" rollback-for="java.lang.Exception"/>
            <tx:method name="batchDelete*" propagation="REQUIRED" rollback-for="java.lang.Exception"/>
        </tx:attributes>
    </tx:advice>
    <aop:config>
        <aop:pointcut id="transactionPointcut" expression="execution(* com.doart.service..*.*(..))" />
        <aop:advisor pointcut-ref="transactionPointcut" advice-ref="transactionAdvice" />
    </aop:config>
</beans>

基本上配置文件裏都說明是做什麼用的了,有一些我還是不是很清楚,自己僅僅會去使用,日後一定深入理解,寫一篇關於ssm的文章。

配置文件說清楚了下面就是介紹一下我的項目的目錄結構,不然可能有小夥伴對路徑的一些問題頭疼。如下圖所示

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