SSM框架基礎配置文件

来源:https://www.cnblogs.com/Czar1996/archive/2019/01/19/10293788.html
-Advertisement-
Play Games

SSM框架基礎配置文件包括:applicationContext.xml(Spring框架核心配置文件)、dispatcher-servlet.xml(SpringMVC框架核心配置文件)、mybatis-config.xml(Mybatis配置文件)、database.properties(數據源 ...


SSM框架基礎配置文件包括:applicationContext.xml(Spring框架核心配置文件)、dispatcher-servlet.xml(SpringMVC框架核心配置文件)、mybatis-config.xml(Mybatis配置文件)、database.properties(數據源配置文件)、log4j.properties(日誌列印文件)、web.xml文件。以下為每個文件具體的配置及說明:

applicationContext.xml文件:

<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:p="http://www.springframework.org/schema/p"
    xmlns:tx="http://www.springframework.org/schema/tx"
    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/aop
                        http://www.springframework.org/schema/aop/spring-aop.xsd
                        http://www.springframework.org/schema/tx
                        http://www.springframework.org/schema/tx/spring-tx.xsd">
    <!-- 包瀏覽 -->
    <context:component-scan base-package="cn.xianqu"></context:component-scan>
    <!-- 讀取資料庫配置文件 -->
    <context:property-placeholder location="classpath:database.properties" />
    <!-- 創建數據源 -->
    <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
        destroy-method="close">
        <property name="driverClassName" value="${jdbc.driver}"></property>
        <property name="url" value="${jdbc.url}"></property>
        <property name="username" value="${jdbc.username}"></property>
        <property name="password" value="${jdbc.password}"></property>
    </bean>
    <!-- 創建sqlSessionFactory -->
    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <property name="dataSource" ref="dataSource"></property>
    <!-- 載入mybatis的核心配置文件 -->
        <property name="configLocation" value="classpath:mybatis-config.xml"></property>
    </bean>
    <!-- 重要的方式MapperScannerConfiger:讓spring自動掃描指定包下的mapper,生成mapper實例 -->
    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        <property name="basePackage" value="cn.xianqu.mapper"></property>
    </bean>
    <!-- 創建事務管理器 -->
    <bean id="transactionManager"
        class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource" ref="dataSource"></property>
    </bean>
    <!-- 讓事務的註解生效 -->
    <tx:annotation-driven proxy-target-class="true" />
    <!-- 聲明事務管理 -->
    <tx:advice id="myAdvice" transaction-manager="transactionManager">
        <tx:attributes>
            <tx:method name="query*" rollback-for="Exception"></tx:method>
            <tx:method name="find*" rollback-for="Exception"></tx:method>
            <tx:method name="get*" rollback-for="Exception"></tx:method>
            <tx:method name="add*" rollback-for="Exception"></tx:method>
            <tx:method name="insert*" rollback-for="Exception"></tx:method>
            <tx:method name="del*" rollback-for="Exception"></tx:method>
            <tx:method name="remove*" rollback-for="Exception"></tx:method>
            <tx:method name="update*" rollback-for="Exception"></tx:method>
        </tx:attributes>
    </tx:advice>
    <!-- 織入增強,形成切麵 -->
    <aop:config>
        <aop:pointcut expression="execution(public * cn.xianqu.service..*.*(..))"
            id="pointcut" />
        <aop:advisor advice-ref="myAdvice" pointcut-ref="pointcut" />
    </aop:config>
</beans>

 

 database.properties文件:

jdbc.driver=oracle.jdbc.driver.OracleDriver
jdbc.url=jdbc:oracle:thin:@localhost:1521:XE
jdbc.username=
jdbc.password=

 

 dispatcher-servlet.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:context="http://www.springframework.org/schema/context"
    xmlns:p="http://www.springframework.org/schema/p" xmlns:mvc="http://www.springframework.org/schema/mvc"
    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="cn.xianqu.controller"></context:component-scan>
    <!-- 讓SpringMVC的註解生效(設置消息解析器,解決轉換成JSON的中文字元亂碼問題) -->
    <mvc:annotation-driven>
        <mvc:message-converters>
            <bean class="org.springframework.http.converter.StringHttpMessageConverter">
                <property name="supportedMediaTypes">
                    <list>
                        <value>text/html;charset=UTF-8</value>
                    <value>application/json;charset=UTF-8</value>
                    </list>
                </property>
            </bean>
        </mvc:message-converters>
    </mvc:annotation-driven>
    <!-- 靜態資源映射 -->
    <mvc:resources location="/statics/" mapping="/statics/**"></mvc:resources>
    <!-- 文件上傳解析器 -->
    <bean id="multipartResolver"    class="org.springframework.web.multipart.commons.CommonsMultipartResolver"></bean>
    <!-- 視圖解析器 -->
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/views/"></property>
        <property name="suffix" value=".jsp"></property>
    </bean>
    <!-- 全局異常 -->
    <bean class="org.springframework.web.servlet.handler.SimpleMappingExceptionResolver">
        <property name="exceptionMappings">
            <props>
                <prop key="java.lang.Exception">err</prop>
            </props>
        </property>
    </bean>
</beans>

 

 mybatis-config.xml文件:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN" "mybatis-3-config.dtd" >
<configuration>
<!-- 設置別名 -->
    <typeAliases>
        <package name="cn.xianqu.entity" />
    </typeAliases>
</configuration>

 

 log4j.properties文件:

log4j.rootLogger=debug,CONSOLE,file
#log4j.rootLogger=ERROR,ROLLING_FILE

log4j.logger.org.bill=error
log4j.logger.org.apache.ibatis=debug
log4j.logger.org.mybatis.spring=debug
log4j.logger.java.sql.Connection=debug
log4j.logger.java.sql.Statement=debug
log4j.logger.java.sql.PreparedStatement=debug
log4j.logger.java.sql.ResultSet=debug

######################################################################################
# Console Appender  \u65e5\u5fd7\u5728\u63a7\u5236\u8f93\u51fa\u914d\u7f6e
######################################################################################
log4j.appender.CONSOLE=org.apache.log4j.ConsoleAppender
log4j.appender.Threshold=debug
log4j.appender.CONSOLE.Target=System.err
log4j.appender.CONSOLE.layout=org.apache.log4j.PatternLayout
log4j.appender.CONSOLE.layout.ConversionPattern= - (%r ms) - [%p] %d %c - %m%n

######################################################################################
# Rolling File  \u6587\u4ef6\u5927\u5c0f\u5230\u8fbe\u6307\u5b9a\u5c3a\u5bf8\u7684\u65f6\u5019\u4ea7\u751f\u4e00\u4e2a\u65b0\u7684\u6587\u4ef6
######################################################################################
#log4j.appender.ROLLING_FILE=org.apache.log4j.RollingFileAppender
#log4j.appender.ROLLING_FILE.Threshold=INFO
#log4j.appender.ROLLING_FILE.File=${baojia.root}/logs/log.log
#log4j.appender.ROLLING_FILE.Append=true
#log4j.appender.ROLLING_FILE.MaxFileSize=5000KB
#log4j.appender.ROLLING_FILE.MaxBackupIndex=100
#log4j.appender.ROLLING_FILE.layout=org.apache.log4j.PatternLayout
#log4j.appender.ROLLING_FILE.layout.ConversionPattern=%d{yyyy-M-d HH:mm:ss}%x[%5p](%F:%L) %m%n

######################################################################################
# DailyRolling File  \u6bcf\u5929\u4ea7\u751f\u4e00\u4e2a\u65e5\u5fd7\u6587\u4ef6\uff0c\u6587\u4ef6\u540d\u683c\u5f0f:log2009-09-11
######################################################################################
log4j.appender.file=org.apache.log4j.DailyRollingFileAppender
log4j.appender.file.DatePattern=yyyy-MM-dd
log4j.appender.file.File=log.log
log4j.appender.file.Append=true
log4j.appender.file.Threshold=debug
log4j.appender.file.layout=org.apache.log4j.PatternLayout
log4j.appender.file.layout.ConversionPattern= - (%r ms) - %d{yyyy-M-d HH:mm:ss}%x[%5p](%F:%L) %m%n

#DWR \u65e5\u5fd7
#log4j.logger.org.directwebremoting = ERROR

#\u663e\u793aHibernate\u5360\u4f4d\u7b26\u7ed1\u5b9a\u503c\u53ca\u8fd4\u56de\u503c
#log4j.logger.org.hibernate.type=DEBUG,CONSOLE 

#log4j.logger.org.springframework.transaction=DEBUG
#log4j.logger.org.hibernate=DEBUG
#log4j.logger.org.acegisecurity=DEBUG
#log4j.logger.org.apache.myfaces=TRACE
#log4j.logger.org.quartz=DEBUG

#log4j.logger.com.opensymphony=INFO  
#log4j.logger.org.apache.struts2=DEBUG  
log4j.logger.com.opensymphony.xwork2=debug

 

 web.xml文件:

<!DOCTYPE web-app PUBLIC
 "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
 "http://java.sun.com/dtd/web-app_2_3.dtd" >
<web-app>
    <display-name>Archetype Created Web Application</display-name>
    <!-- 載入spring的配置文件 -->
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>classpath:applicationContext*.xml</param-value>
    </context-param>
    <!--解決中文字元集亂碼的問題 -->
    <filter>
        <filter-name>characterEncodingFilter</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>characterEncodingFilter</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>
    <!-- 添加監聽器,自動讀取spring的配置文件 -->
    <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:dispatcher-servlet.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>

 

 


您的分享是我們最大的動力!

-Advertisement-
Play Games
更多相關文章
  • 最近在陸續做機房升級相關工作,配合DBA對產線資料庫鏈接方式做個調整,將原來直接鏈接讀庫的地址切換到統一的讀負載均衡的代理 haproxy 上,方便機櫃和伺服器的搬遷。 切換之後線上時不時的會發生 discard connection 錯誤,導致程式報 500 錯誤,但不是每次都必現的。 開發框... ...
  • 對 .NET 自帶的 BinaryReader、BinaryWriter 進行擴展. NuGet 上安裝 Syroot.IO.BinaryData 即可使用. ...
  • 一.資料庫概述 1.什麼是資料庫?先來看看百度怎麼說的. 資料庫,簡而言之可視為電子化的文件櫃——存儲電子文件的處所,用戶可以對文件中的數據運行新增、截取、更新、刪除等操作。 所謂“資料庫”系以一定方式儲存在一起、能予多個用戶共用、具有儘可能小的冗餘度、與應用程式彼此獨立的數據集合。 資料庫,簡而言 ...
  • 一、Akka簡介 Akka時spark的底層通信框架,Hadoop的底層通信框架時rpc。 併發的程式編寫很難,但是Akka解決了spark的這個問題。 Akka構建在JVM平臺上,是一種高併發、分散式、並且容錯的應用工具包; Akka使用Scala語言編寫,同時它提供了Scala和Java的開發接 ...
  • 為什麼突然在此提到這個梳理問題呢? 因為在自己實踐綜合練習學過的知識時,突然覺得有些知識點的運用總是不成功,於是翻過課本進行回顧,總是覺得是對的,可是當再進一步思考“既然是對的,為什麼在程式中總是不成功呢?”,後來發現,自己理所當然的理解(忽略了細節知識),導致程式通不過,現在結合同一個類中的不同方 ...
  • # 介面類:python 原生不支持# 抽象類:python 原生支持的 介面類 首先我們來看一個支付介面的簡單例子 介面類的多繼承 這是三種動物tiger 走路 游泳swan 走路 游泳 飛oldying 走路 飛 為了避免代碼重覆,我們寫以下三個類下麵就是實現了 介面類的規範 不需要有功能實現的 ...
  • 多線程 基本實現: 第一種,函數方式 # -*- coding:utf-8 -*- import thread import time def print_time(threadName, delay): count = 0 while count < 5: time.sleep(delay) co ...
  • 最近開髮網關服務的過程當中,需要用到kafka轉發消息與保存日誌,在進行壓測的過程中由於是多線程併發操作kafka producer 進行非同步send,發現send耗時有時會達到幾十毫秒的阻塞,很大程度上上影響了併發的性能,而在後續的測試中發現單線程發送反而比多線程發送效率高出幾倍。所以就對kafk ...
一周排行
    -Advertisement-
    Play Games
  • 移動開發(一):使用.NET MAUI開發第一個安卓APP 對於工作多年的C#程式員來說,近來想嘗試開發一款安卓APP,考慮了很久最終選擇使用.NET MAUI這個微軟官方的框架來嘗試體驗開發安卓APP,畢竟是使用Visual Studio開發工具,使用起來也比較的順手,結合微軟官方的教程進行了安卓 ...
  • 前言 QuestPDF 是一個開源 .NET 庫,用於生成 PDF 文檔。使用了C# Fluent API方式可簡化開發、減少錯誤並提高工作效率。利用它可以輕鬆生成 PDF 報告、發票、導出文件等。 項目介紹 QuestPDF 是一個革命性的開源 .NET 庫,它徹底改變了我們生成 PDF 文檔的方 ...
  • 項目地址 項目後端地址: https://github.com/ZyPLJ/ZYTteeHole 項目前端頁面地址: ZyPLJ/TreeHoleVue (github.com) https://github.com/ZyPLJ/TreeHoleVue 目前項目測試訪問地址: http://tree ...
  • 話不多說,直接開乾 一.下載 1.官方鏈接下載: https://www.microsoft.com/zh-cn/sql-server/sql-server-downloads 2.在下載目錄中找到下麵這個小的安裝包 SQL2022-SSEI-Dev.exe,運行開始下載SQL server; 二. ...
  • 前言 隨著物聯網(IoT)技術的迅猛發展,MQTT(消息隊列遙測傳輸)協議憑藉其輕量級和高效性,已成為眾多物聯網應用的首選通信標準。 MQTTnet 作為一個高性能的 .NET 開源庫,為 .NET 平臺上的 MQTT 客戶端與伺服器開發提供了強大的支持。 本文將全面介紹 MQTTnet 的核心功能 ...
  • Serilog支持多種接收器用於日誌存儲,增強器用於添加屬性,LogContext管理動態屬性,支持多種輸出格式包括純文本、JSON及ExpressionTemplate。還提供了自定義格式化選項,適用於不同需求。 ...
  • 目錄簡介獲取 HTML 文檔解析 HTML 文檔測試參考文章 簡介 動態內容網站使用 JavaScript 腳本動態檢索和渲染數據,爬取信息時需要模擬瀏覽器行為,否則獲取到的源碼基本是空的。 本文使用的爬取步驟如下: 使用 Selenium 獲取渲染後的 HTML 文檔 使用 HtmlAgility ...
  • 1.前言 什麼是熱更新 游戲或者軟體更新時,無需重新下載客戶端進行安裝,而是在應用程式啟動的情況下,在內部進行資源或者代碼更新 Unity目前常用熱更新解決方案 HybridCLR,Xlua,ILRuntime等 Unity目前常用資源管理解決方案 AssetBundles,Addressable, ...
  • 本文章主要是在C# ASP.NET Core Web API框架實現向手機發送驗證碼簡訊功能。這裡我選擇是一個互億無線簡訊驗證碼平臺,其實像阿裡雲,騰訊雲上面也可以。 首先我們先去 互億無線 https://www.ihuyi.com/api/sms.html 去註冊一個賬號 註冊完成賬號後,它會送 ...
  • 通過以下方式可以高效,並保證數據同步的可靠性 1.API設計 使用RESTful設計,確保API端點明確,並使用適當的HTTP方法(如POST用於創建,PUT用於更新)。 設計清晰的請求和響應模型,以確保客戶端能夠理解預期格式。 2.數據驗證 在伺服器端進行嚴格的數據驗證,確保接收到的數據符合預期格 ...