簡單的Springmvc+Mybatis+Druid項目搭建

簡單的web搭建,參考 >>>https://github.com/Eliteams/quick4j

得益于開源船惨,quick4j是一個非常適合學習的項目,可以拆開了一個個集上去

eclipse搭maven的項目非常難受缕陕,建議使用idea

這里依舊使用的eclipse粱锐,可以更好的了解框架手動滑稽

項目結(jié)構(gòu)

項目結(jié)構(gòu).png

項目入口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_3_0.xsd" id="WebApp_ID" version="3.0">
  <display-name>SpMVC</display-name>
    <!-- Spring -->  
    <!-- 配置Spring配置文件路徑 -->  
    <context-param>  
        <param-name>contextConfigLocation</param-name>  
        <param-value>  
            classpath*:config/applicationContext.xml   
        </param-value>  
    </context-param>  
    <!-- 配置Spring上下文監(jiān)聽器 -->  
    <listener>  
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>  
    </listener>  
    <!-- Spring -->  
  
    <!-- 配置Spring字符編碼過濾器 -->  
    <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>  
  
    <!-- 配置log4j配置文件路徑 -->  
    <context-param>  
        <param-name>log4jConfigLocation</param-name>  
        <param-value>classpath:log4j.properties</param-value>  
    </context-param>  
    <!-- 60s 檢測日志配置 文件變化 -->  
    <context-param>  
        <param-name>log4jRefreshInterval</param-name>  
        <param-value>60000</param-value>  
    </context-param>  
  
    <!-- 配置Log4j監(jiān)聽器 -->  
    <listener>  
        <listener-class>org.springframework.web.util.Log4jConfigListener</listener-class>  
    </listener>  
  
    <!-- Spring MVC 核心控制器 DispatcherServlet 配置 -->  
    <servlet>  
        <servlet-name>dispatcher</servlet-name>  
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>  
        <init-param>  
            <param-name>contextConfigLocation</param-name>  
            <param-value>classpath*:config/spring-mvc.xml</param-value>  
        </init-param>  
        <load-on-startup>1</load-on-startup>  
    </servlet>  
    <servlet-mapping>  
        <servlet-name>dispatcher</servlet-name>  
        <!-- 攔截所有/rest/* 的請求,交給DispatcherServlet處理,性能最好 -->  
        <url-pattern>/rest/*</url-pattern>  
    </servlet-mapping>  
  
    <!-- 首頁 -->  
    <welcome-file-list>  
        <welcome-file>rest/index</welcome-file>  
    </welcome-file-list>  
  
    <!-- 錯誤頁 -->  
    <error-page>  
        <error-code>404</error-code>  
        <location>/rest/page/404</location>  
    </error-page>  
    <error-page>  
        <error-code>500</error-code>  
        <location>/rest/page/500</location>  
    </error-page>  
    <error-page>  
        <exception-type>org.apache.shiro.authz.AuthorizationException</exception-type>  
        <location>/rest/page/401</location>  
    </error-page>  
</web-app>

spring 配置文件applicationContext.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: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:p="http://www.springframework.org/schema/p"  
       xmlns:util="http://www.springframework.org/schema/util" xmlns:jdbc="http://www.springframework.org/schema/jdbc"  
       xmlns:cache="http://www.springframework.org/schema/cache"  
       xsi:schemaLocation="  
    http://www.springframework.org/schema/context  
    http://www.springframework.org/schema/context/spring-context.xsd  
    http://www.springframework.org/schema/beans  
    http://www.springframework.org/schema/beans/spring-beans.xsd  
    http://www.springframework.org/schema/tx  
    http://www.springframework.org/schema/tx/spring-tx.xsd  
    http://www.springframework.org/schema/jdbc  
    http://www.springframework.org/schema/jdbc/spring-jdbc.xsd  
    http://www.springframework.org/schema/cache  
    http://www.springframework.org/schema/cache/spring-cache.xsd  
    http://www.springframework.org/schema/aop  
    http://www.springframework.org/schema/aop/spring-aop.xsd  
    http://www.springframework.org/schema/util  
    http://www.springframework.org/schema/util/spring-util.xsd">  
  
    <!-- 自動掃描quick4j包 ,將帶有注解的類 納入spring容器管理 -->  
    <context:component-scan base-package="com"></context:component-scan>  
  
    <!-- 引入配置文件 -->  
    <bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">  
        <property name="locations">  
            <list>  
                <value>classpath:jdbc.properties</value>  
            </list>  
        </property>  
    </bean>  
  
    <!-- dataSource 配置 -->  
    <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource" init-method="init" destroy-method="close">  
        <!-- 基本屬性 url、user扛邑、password -->  
        <property name="url" value="${jdbc.url}"/>  
        <property name="username" value="${jdbc.username}"/>  
        <property name="password" value="${jdbc.password}"/>  
  
        <!-- 配置初始化大小怜浅、最小、最大 -->  
        <property name="initialSize" value="${ds.initialSize}"/>  
        <property name="minIdle" value="${ds.minIdle}"/>  
        <property name="maxActive" value="${ds.maxActive}"/>  
  
        <!-- 配置獲取連接等待超時的時間 -->  
        <property name="maxWait" value="${ds.maxWait}"/>  
  
        <!-- 配置間隔多久才進行一次檢測蔬崩,檢測需要關閉的空閑連接恶座,單位是毫秒 -->  
        <property name="timeBetweenEvictionRunsMillis" value="${ds.timeBetweenEvictionRunsMillis}"/>  
  
        <!-- 配置一個連接在池中最小生存的時間,單位是毫秒 -->  
        <property name="minEvictableIdleTimeMillis" value="${ds.minEvictableIdleTimeMillis}"/>  
  
        <property name="validationQuery" value="SELECT 'x'"/>  
        <property name="testWhileIdle" value="true"/>  
        <property name="testOnBorrow" value="false"/>  
        <property name="testOnReturn" value="false"/>  
  
        <!-- 打開PSCache沥阳,并且指定每個連接上PSCache的大小 -->  
        <property name="poolPreparedStatements" value="false"/>  
        <property name="maxPoolPreparedStatementPerConnectionSize" value="20"/>  
  
        <!-- 配置監(jiān)控統(tǒng)計攔截的filters -->  
        <property name="filters" value="stat"/>  
    </bean>  
  
    <!-- mybatis文件配置跨琳,掃描所有mapper文件 -->  
    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean" p:dataSource-ref="dataSource"  
          p:configLocation="classpath:config/mybatis-config.xml"  
          p:mapperLocations="classpath:com/dao/*.xml"/>  
  
    <!-- spring與mybatis整合配置鹅经,掃描所有dao -->  
    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer" p:basePackage="com.dao"  
          p:sqlSessionFactoryBeanName="sqlSessionFactory"/>  
  
    <!-- 對dataSource 數(shù)據(jù)源進行事務管理 -->  
    <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"  
          p:dataSource-ref="dataSource"/>  
  
    <!-- 事務管理 通知 -->  
    <tx:advice id="txAdvice" transaction-manager="transactionManager">  
        <tx:attributes>  
            <!-- 對insert,update,delete 開頭的方法進行事務管理,只要有異常就回滾 -->  
            <tx:method name="insert*" propagation="REQUIRED" rollback-for="java.lang.Throwable"/>  
            <tx:method name="update*" propagation="REQUIRED" rollback-for="java.lang.Throwable"/>  
            <tx:method name="delete*" propagation="REQUIRED" rollback-for="java.lang.Throwable"/>  
            <!-- select,count開頭的方法,開啟只讀,提高數(shù)據(jù)庫訪問性能 -->  
            <tx:method name="select*" read-only="true"/>  
            <tx:method name="count*" read-only="true"/>  
            <!-- 對其他方法 使用默認的事務管理 -->  
            <tx:method name="*"/>  
        </tx:attributes>  
    </tx:advice>  
  
    <!-- 事務 aop 配置 -->  
    <aop:config>  
        <aop:pointcut id="serviceMethods" expression="execution(* com.service..*(..))"/>  
        <aop:advisor advice-ref="txAdvice" pointcut-ref="serviceMethods"/>  
    </aop:config>  
  
    <!-- 配置使Spring采用CGLIB代理 -->  
    <aop:aspectj-autoproxy proxy-target-class="true"/>  
  
    <!-- 啟用對事務注解的支持 -->  
    <tx:annotation-driven transaction-manager="transactionManager"/>  

</beans>  

springMVC 配置文件 spring-mvc.xml

<?xml version="1.0" encoding="UTF-8"?>  
<beans xmlns="http://www.springframework.org/schema/beans"  
       xmlns:aop="http://www.springframework.org/schema/aop"  
       xmlns:context="http://www.springframework.org/schema/context"  
       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"  
       xsi:schemaLocation="http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.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.xsd   
        http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd   
        http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd">  
  
    <!-- 掃描controller(controller層注入) -->  
    <context:component-scan base-package="com.controller"/>  
  
    <!-- 會自動注冊DefaultAnnotationHandlerMapping與AnnotationMethodHandlerAdapter 兩個bean,是spring MVC為@Controllers分發(fā)請求所必須的 -->  
    <!-- 指定自己定義的validator -->  
    <mvc:annotation-driven />  
  
    <!-- 對模型視圖添加前后綴 -->  
    <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver"  
          p:prefix="/WEB-INF/views/" p:suffix=".jsp"/>  
  
    <!-- 配置springMVC處理上傳文件的信息 -->  
    <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">  
        <property name="defaultEncoding" value="utf-8"/>  
        <property name="maxUploadSize" value="10485760000"/>  
        <property name="maxInMemorySize" value="40960"/>  
    </bean>  

</beans>  

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>  

    <!-- 配置mybatis的緩存夺蛇,延遲加載等等一系列屬性 -->  
    <settings>  
  
        <!-- 全局映射器啟用緩存 -->  
        <setting name="cacheEnabled" value="true"/>  
  
        <!-- 查詢時,關閉關聯(lián)對象即時加載以提高性能 -->  
        <setting name="lazyLoadingEnabled" value="true"/>  
  
        <!-- 對于未知的SQL查詢跷睦,允許返回不同的結(jié)果集以達到通用的效果 -->  
        <setting name="multipleResultSetsEnabled" value="true"/>  
  
        <!-- 允許使用列標簽代替列名 -->  
        <setting name="useColumnLabel" value="true"/>  
  
        <!-- 不允許使用自定義的主鍵值(比如由程序生成的UUID 32位編碼作為鍵值)功炮,數(shù)據(jù)表的PK生成策略將被覆蓋 -->  
        <setting name="useGeneratedKeys" value="false"/>  
  
        <!-- 給予被嵌套的resultMap以字段-屬性的映射支持 FULL,PARTIAL -->  
        <setting name="autoMappingBehavior" value="PARTIAL"/>  
  
        <!-- 對于批量更新操作緩存SQL以提高性能 BATCH,SIMPLE -->  
        <!-- <setting name="defaultExecutorType" value="BATCH" /> -->  
  
        <!-- 數(shù)據(jù)庫超過25000秒仍未響應則超時 -->  
        <!-- <setting name="defaultStatementTimeout" value="25000" /> -->  
  
        <!-- Allows using RowBounds on nested statements -->  
        <setting name="safeRowBoundsEnabled" value="false"/>  
  
        <!-- Enables automatic mapping from classic database column names A_COLUMN to camel case classic Java property names aColumn. -->  
        <setting name="mapUnderscoreToCamelCase" value="true"/>  
  
        <!-- MyBatis uses local cache to prevent circular references and speed up repeated nested queries. By default (SESSION) all queries executed during a session are cached. If localCacheScope=STATEMENT   
            local session will be used just for statement execution, no data will be shared between two different calls to the same SqlSession. -->  
        <setting name="localCacheScope" value="SESSION"/>  
  
        <!-- Specifies the JDBC type for null values when no specific JDBC type was provided for the parameter. Some drivers require specifying the column JDBC type but others work with generic values   
            like NULL, VARCHAR or OTHER. -->  
        <setting name="jdbcTypeForNull" value="OTHER"/>  
  
        <!-- Specifies which Object's methods trigger a lazy load -->  
        <setting name="lazyLoadTriggerMethods" value="equals,clone,hashCode,toString"/>  
  
        <!-- 設置關聯(lián)對象加載的形態(tài)溅潜,此處為按需加載字段(加載字段由SQL指 定),不會加載關聯(lián)表的所有字段薪伏,以提高性能 -->  
        <setting name="aggressiveLazyLoading" value="false"/>  
  
    </settings>  
  
    <typeAliases>  
        <package name="com.bean"/>  
    </typeAliases>  
  
</configuration>  

數(shù)據(jù)源文件 jdbc.properties

##JDBC Global Setting  
jdbc.driver=com.mysql.jdbc.Driver  
jdbc.url=jdbc:mysql://localhost:3306/boot?useUnicode=true&characterEncoding=utf-8  
jdbc.username=root
jdbc.password=
  
##DataSource Global Setting  
  
#配置初始化大小滚澜、最小、最大  
ds.initialSize=1  
ds.minIdle=1  
ds.maxActive=20  
  
#配置獲取連接等待超時的時間   
ds.maxWait=60000  
  
#配置間隔多久才進行一次檢測嫁怀,檢測需要關閉的空閑連接设捐,單位是毫秒  
ds.timeBetweenEvictionRunsMillis=60000  
  
#配置一個連接在池中最小生存的時間潦牛,單位是毫秒  
ds.minEvictableIdleTimeMillis=300000  

日志監(jiān)控 log4j.properties

# DEBUG,INFO,WARN,ERROR,FATAL  
LOG_LEVEL=INFO  
  
log4j.rootLogger=${LOG_LEVEL},CONSOLE,FILE  
  
log4j.appender.CONSOLE=org.apache.log4j.ConsoleAppender  
log4j.appender.CONSOLE.Encoding=utf-8  
log4j.appender.CONSOLE.layout=org.apache.log4j.PatternLayout  
#log4j.appender.CONSOLE.layout.ConversionPattern=[%-5p] %d{yyyy-MM-dd HH:mm:ss} %C{8}@(%F:%L):%m%n   
log4j.appender.CONSOLE.layout.ConversionPattern=[%-5p] %d{yyyy-MM-dd HH:mm:ss} %C{1}@(%F:%L):%m%n  
  
log4j.appender.FILE=org.apache.log4j.DailyRollingFileAppender  
log4j.appender.FILE.File=${catalina.base}/logs/quick4j.log  
log4j.appender.FILE.Encoding=utf-8  
log4j.appender.FILE.DatePattern='.'yyyy-MM-dd  
log4j.appender.FILE.layout=org.apache.log4j.PatternLayout  
#log4j.appender.FILE.layout=org.apache.log4j.HTMLLayout  
log4j.appender.FILE.layout.ConversionPattern=[%-5p] %d{yyyy-MM-dd HH\:mm\:ss} %C{8}@(%F\:%L)\:%m%n   

數(shù)據(jù)庫


數(shù)據(jù)庫文件.png

瀏覽器返回結(jié)果

瀏覽器返回結(jié)果.png

本項目地址 >>>https://github.com/Chaimouren/Web_Spring

end

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個濱河市挡育,隨后出現(xiàn)的幾起案子巴碗,更是在濱河造成了極大的恐慌,老刑警劉巖即寒,帶你破解...
    沈念sama閱讀 219,490評論 6 508
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件橡淆,死亡現(xiàn)場離奇詭異,居然都是意外死亡母赵,警方通過查閱死者的電腦和手機逸爵,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 93,581評論 3 395
  • 文/潘曉璐 我一進店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來凹嘲,“玉大人师倔,你說我怎么就攤上這事≈懿洌” “怎么了趋艘?”我有些...
    開封第一講書人閱讀 165,830評論 0 356
  • 文/不壞的土叔 我叫張陵,是天一觀的道長凶朗。 經(jīng)常有香客問我瓷胧,道長,這世上最難降的妖魔是什么棚愤? 我笑而不...
    開封第一講書人閱讀 58,957評論 1 295
  • 正文 為了忘掉前任搓萧,我火速辦了婚禮,結(jié)果婚禮上宛畦,老公的妹妹穿的比我還像新娘瘸洛。我一直安慰自己,他們只是感情好次和,可當我...
    茶點故事閱讀 67,974評論 6 393
  • 文/花漫 我一把揭開白布反肋。 她就那樣靜靜地躺著,像睡著了一般斯够。 火紅的嫁衣襯著肌膚如雪囚玫。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 51,754評論 1 307
  • 那天读规,我揣著相機與錄音抓督,去河邊找鬼。 笑死束亏,一個胖子當著我的面吹牛铃在,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播,決...
    沈念sama閱讀 40,464評論 3 420
  • 文/蒼蘭香墨 我猛地睜開眼定铜,長吁一口氣:“原來是場噩夢啊……” “哼阳液!你這毒婦竟也來了?” 一聲冷哼從身側(cè)響起揣炕,我...
    開封第一講書人閱讀 39,357評論 0 276
  • 序言:老撾萬榮一對情侶失蹤帘皿,失蹤者是張志新(化名)和其女友劉穎,沒想到半個月后畸陡,有當?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體鹰溜,經(jīng)...
    沈念sama閱讀 45,847評論 1 317
  • 正文 獨居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 37,995評論 3 338
  • 正文 我和宋清朗相戀三年丁恭,在試婚紗的時候發(fā)現(xiàn)自己被綠了曹动。 大學時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點故事閱讀 40,137評論 1 351
  • 序言:一個原本活蹦亂跳的男人離奇死亡牲览,死狀恐怖墓陈,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情第献,我是刑警寧澤贡必,帶...
    沈念sama閱讀 35,819評論 5 346
  • 正文 年R本政府宣布,位于F島的核電站痊硕,受9級特大地震影響赊级,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜岔绸,卻給世界環(huán)境...
    茶點故事閱讀 41,482評論 3 331
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望橡伞。 院中可真熱鬧盒揉,春花似錦、人聲如沸兑徘。這莊子的主人今日做“春日...
    開封第一講書人閱讀 32,023評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽挂脑。三九已至藕漱,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間崭闲,已是汗流浹背肋联。 一陣腳步聲響...
    開封第一講書人閱讀 33,149評論 1 272
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留刁俭,地道東北人橄仍。 一個月前我還...
    沈念sama閱讀 48,409評論 3 373
  • 正文 我出身青樓,卻偏偏與公主長得像,于是被迫代替她去往敵國和親侮繁。 傳聞我的和親對象是個殘疾皇子虑粥,可洞房花燭夜當晚...
    茶點故事閱讀 45,086評論 2 355

推薦閱讀更多精彩內(nèi)容