SpringMVC+Spring+MyBatis 整合與圖片上傳簡單示例

来源:https://www.cnblogs.com/gdwkong/archive/2018/05/01/8784780.html
-Advertisement-
Play Games

本文主要介紹如何對Spring、MyBatis、springmvc進行整合,給出一系列的配置文件,併進行了圖片上傳簡單示例。 ...


一、思路:

(一) Dao層:

1、 SqlMapConfig.xml,空文件即可。需要文件頭。
2、 applicationContext_dao.xml。

a) 資料庫連接池
b) SqlSessionFactory對象,需要spring和mybatis整合包下的。
c) 配置mapper文件掃描器。

(二)Service層:

1、applicationContext_service.xml包掃描器,掃描@service註解的類。
2、applicationContext_trans.xml配置事務。

(三)Controller層:

Springmvc.xml
1、包掃描器,掃描@Controller註解的類。
2、配置註解驅動。
3、視圖解析器。

(四)web.xml

1. 配置spring容量監聽器
2. 配置前端控制器

二、構建項目

(一)構建maven項目,添加依賴

  1 <?xml version="1.0" encoding="UTF-8"?>
  2 
  3 <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4          xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  5   <modelVersion>4.0.0</modelVersion>
  6 
  7   <groupId>com.cenobitor</groupId>
  8   <artifactId>ssm_template</artifactId>
  9   <version>1.0-SNAPSHOT</version>
 10   <packaging>war</packaging>
 11 
 12   <name>ssm_template Maven Webapp</name>
 13   <!-- FIXME change it to the project's website -->
 14   <url>http://www.example.com</url>
 15 
 16   <properties>
 17     <!--設置編譯的版本使用1.8版本-->
 18     <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
 19     <!-- 和設置編譯插件是一樣的效果-->
 20     <maven.compiler.source>1.8</maven.compiler.source>
 21     <maven.compiler.target>1.8</maven.compiler.target>
 22 
 23 
 24     <junit.version>4.12</junit.version>
 25     <spring.version>4.3.10.RELEASE</spring.version>
 26     <servlet-api.version>2.5</servlet-api.version>
 27     <mybatis.version>3.2.8</mybatis.version>
 28     <mybatis.spring.version>1.2.2</mybatis.spring.version>
 29     <pagehelper.version>4.0.0</pagehelper.version>
 30     <javassist.version>3.11.0.GA</javassist.version>
 31     <mysql.version>5.1.32</mysql.version>
 32     <druid.version>1.0.9</druid.version>
 33     <commons-fileupload.version>1.3.1</commons-fileupload.version>
 34     <commons-lang3.version>3.3.2</commons-lang3.version>
 35     <commons-io.version>1.3.2</commons-io.version>
 36     <jackson.version>2.8.9</jackson.version>
 37     <jstl.version>1.2</jstl.version>
 38     <jsp-api.version>2.0</jsp-api.version>
 39     <fastjson.version>1.2.28</fastjson.version>
 40     <slf4j.version>1.6.4</slf4j.version>
 41     <lombok.version>1.16.20</lombok.version>
 42   </properties>
 43 
 44   <dependencies>
 45     <!-- Apache工具組件 -->
 46     <dependency>
 47       <groupId>org.apache.commons</groupId>
 48       <artifactId>commons-lang3</artifactId>
 49       <version>${commons-lang3.version}</version>
 50     </dependency>
 51     <dependency>
 52       <groupId>org.apache.commons</groupId>
 53       <artifactId>commons-io</artifactId>
 54       <version>${commons-io.version}</version>
 55     </dependency>
 56     <!-- Jackson Json處理工具包 -->
 57     <dependency>
 58       <groupId>com.fasterxml.jackson.core</groupId>
 59       <artifactId>jackson-databind</artifactId>
 60       <version>${jackson.version}</version>
 61     </dependency>
 62 
 63     <!--alibaba json處理工具包-->
 64     <dependency>
 65       <groupId>com.alibaba</groupId>
 66       <artifactId>fastjson</artifactId>
 67       <version>${fastjson.version}</version>
 68     </dependency>
 69     <!-- 日誌處理 -->
 70     <dependency>
 71       <groupId>org.slf4j</groupId>
 72       <artifactId>slf4j-log4j12</artifactId>
 73       <version>${slf4j.version}</version>
 74     </dependency>
 75     <!--實體類get/set註解-->
 76     <dependency>
 77       <groupId>org.projectlombok</groupId>
 78       <artifactId>lombok</artifactId>
 79       <version>${lombok.version}</version>
 80       <scope>provided</scope>
 81     </dependency>
 82     <!--單元測試-->
 83     <dependency>
 84       <groupId>junit</groupId>
 85       <artifactId>junit</artifactId>
 86       <version>${junit.version}</version>
 87     </dependency>
 88 
 89     <!-- Spring -->
 90     <dependency>
 91       <groupId>org.springframework</groupId>
 92       <artifactId>spring-context</artifactId>
 93       <version>${spring.version}</version>
 94     </dependency>
 95     <dependency>
 96       <groupId>org.springframework</groupId>
 97       <artifactId>spring-beans</artifactId>
 98       <version>${spring.version}</version>
 99     </dependency>
100     <dependency>
101       <groupId>org.springframework</groupId>
102       <artifactId>spring-webmvc</artifactId>
103       <version>${spring.version}</version>
104     </dependency>
105     <dependency>
106       <groupId>org.springframework</groupId>
107       <artifactId>spring-jdbc</artifactId>
108       <version>${spring.version}</version>
109     </dependency>
110     <dependency>
111       <groupId>org.springframework</groupId>
112       <artifactId>spring-aspects</artifactId>
113       <version>${spring.version}</version>
114     </dependency>
115     <dependency>
116       <groupId>org.springframework</groupId>
117       <artifactId>spring-jms</artifactId>
118       <version>${spring.version}</version>
119     </dependency>
120     <dependency>
121       <groupId>org.springframework</groupId>
122       <artifactId>spring-context-support</artifactId>
123       <version>${spring.version}</version>
124     </dependency>
125     <dependency>
126       <groupId>org.springframework</groupId>
127       <artifactId>spring-test</artifactId>
128       <version>${spring.version}</version>
129     </dependency>
130     <!--jsp相關-->
131     <dependency>
132       <groupId>javax.servlet</groupId>
133       <artifactId>servlet-api</artifactId>
134       <version>${servlet-api.version}</version>
135       <scope>provided</scope>
136     </dependency>
137     <!-- 文件上傳組件 -->
138     <dependency>
139       <groupId>commons-fileupload</groupId>
140       <artifactId>commons-fileupload</artifactId>
141       <version>${commons-fileupload.version}</version>
142     </dependency>
143     <!-- Mybatis -->
144     <dependency>
145       <groupId>org.mybatis</groupId>
146       <artifactId>mybatis</artifactId>
147       <version>${mybatis.version}</version>
148     </dependency>
149     <dependency>
150       <groupId>org.mybatis</groupId>
151       <artifactId>mybatis-spring</artifactId>
152       <version>${mybatis.spring.version}</version>
153     </dependency>
154     <!--MyBatis分頁插件-->
155     <dependency>
156       <groupId>com.github.pagehelper</groupId>
157       <artifactId>pagehelper</artifactId>
158       <version>${pagehelper.version}</version>
159     </dependency>
160     <!-- MySql -->
161     <dependency>
162       <groupId>mysql</groupId>
163       <artifactId>mysql-connector-java</artifactId>
164       <version>${mysql.version}</version>
165     </dependency>
166     <!-- 連接池 -->
167     <dependency>
168       <groupId>com.alibaba</groupId>
169       <artifactId>druid</artifactId>
170       <version>${druid.version}</version>
171     </dependency>
172 
173   </dependencies>
174 
175   <build>
176     <finalName>ssm_template</finalName>
177     <!--設置mapper.xml配置與mapper類處於同個包下,否則編譯時候無法生成到同一目錄下,會報BindingException-->
178     <resources>
179       <resource>
180         <directory>src/main/java</directory>
181         <includes>
182           <include>**/*.xml</include>
183         </includes>
184       </resource>
185       <resource>
186         <directory>src/main/resources</directory>
187       </resource>
188     </resources>
189 
190     <plugins>
191       <!-- 配置Tomcat7插件 -->
192       <plugin>
193         <groupId>org.apache.tomcat.maven</groupId>
194         <artifactId>tomcat7-maven-plugin</artifactId>
195         <version>2.2</version>
196         <configuration>
197           <uriEncoding>UTF-8</uriEncoding>
198         </configuration>
199       </plugin>
200       <!-- 配置打包時跳過測試  -->
201       <plugin>
202         <groupId>org.apache.maven.plugins</groupId>
203         <artifactId>maven-surefire-plugin</artifactId>
204         <version>2.12.4</version>
205       </plugin>
206     </plugins>
207   </build>
208 
209 </project>

(二)applicationContext.xml的配置

1、applicationContext_dao.xml(資料庫)的配置

 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <beans xmlns="http://www.springframework.org/schema/beans"
 3        xmlns:context="http://www.springframework.org/schema/context"
 4        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 5        xsi:schemaLocation="http://www.springframework.org/schema/beans
 6             http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
 7             http://www.springframework.org/schema/context
 8             http://www.springframework.org/schema/context/spring-context-4.0.xsd">
 9 
10 
11     <!-- dao層 資料庫連接部分-->
12     <!-- 載入配置文件 -->
13     <context:property-placeholder location="classpath:properties/db.properties" />
14 
15     <!-- 資料庫連接池 -->
16     <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource" destroy-method="close">
17         <property name="driverClassName" value="${jdbc.driver}" />
18         <property name="url" value="${jdbc.url}" />
19         <property name="username" value="${jdbc.username}" />
20         <property name="password" value="${jdbc.password}"/>
21         <!-- 連接池的最大資料庫連接數 -->
22         <property name="maxActive" value="10" />
23         <!-- 最大空閑數 -->
24         <property name="maxIdle" value="5" />
25     </bean>
26     <!-- 配置SqlSessionFactory單例 -->
27     <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
28         <!-- 配置數據源 -->
29         <property name="dataSource" ref="dataSource" />
30         <!-- 載入mybatis核心配置文件 -->
31         <property name="configLocation" value="classpath:mybatis/SqlMapConfig.xml" />
32         <!--別名包掃描-->
33         <property name="typeAliasesPackage" value="com.cenobitor.pojo"/>
34 
35     </bean>
36     <!-- 動態代理dao配置,包掃描(推薦方式)-->
37     <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
38         <!-- 支持多包配置,多包時用,或者;分隔 -->
39         <property name="basePackage" value="com.cenobitor.mapper" />
40     </bean>
41 </beans>
1 jdbc.driver=com.mysql.jdbc.Driver
2 jdbc.url=jdbc:mysql://localhost:3306/ssm?characterEncoding=utf-8
3 jdbc.username=root
4 jdbc.password=

2、applicationContext_service.xml(service掃描)的配置

 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <beans xmlns="http://www.springframework.org/schema/beans"
 3        xmlns:context="http://www.springframework.org/schema/context"
 4        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 5        xsi:schemaLocation="http://www.springframework.org/schema/beans
 6             http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
 7             http://www.springframework.org/schema/context
 8             http://www.springframework.org/schema/context/spring-context-4.0.xsd">
 9 
10     <!-- 配置service掃描 -->
11     <context:component-scan base-package="com.cenobitor.service"/>
12 
13 </beans>

3、applicationContext_trans.xml(事務)的配置

 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <beans xmlns="http://www.springframework.org/schema/beans"
 3        xmlns:aop="http://www.springframework.org/schema/aop"
 4        xmlns:tx="http://www.springframework.org/schema/tx"
 5        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 6        xsi:schemaLocation="http://www.springframework.org/schema/beans
 7             http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
 8             http://www.springframework.org/schema/aop
 9             http://www.springframework.org/schema/aop/spring-aop-4.0.xsd
10             http://www.springframework.org/schema/tx
11             http://www.springframework.org/schema/tx/spring-tx-4.0.xsd">
12 
13     <!-- 事務管理器 -->
14     <bean id

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

-Advertisement-
Play Games
更多相關文章
  • 2018-3 問題描述: 近來,跳一跳這款小游戲風靡全國,受到不少玩家的喜愛。 簡化後的跳一跳規則如下:玩家每次從當前方塊跳到下一個方塊,如果沒有跳到下一個方塊上則游戲結束。 如果跳到了方塊上,但沒有跳到方塊的中心則獲得1分;跳到方塊中心時,若上一次的得分為1分或這是本局游戲的第一次跳躍則此次得分為 ...
  • 1.Spring-eureka(註冊中心) Eureka基礎架構包括 註冊中心,服務提供者,服務消費者 1.創建服務註冊中心 import org.springframework.boot.SpringApplication; import org.springframework.boot.auto ...
  • Description There is an old country and the king fell in love with a devil. The devil always asks the king to do some crazy things. Although the king ...
  • lesson Twelve 2018-05-02 01:24:19 foreach: JAVA5的新特征之一,在遍曆數組、集合方面 語法:foreach( part1:part2 ) {part3} 的語法: part1:定義一個局部變數,類型與part2中的對象元素的類型一致. part2:被遍歷 ...
  • Infi-chu: http://www.cnblogs.com/Infi-chu/ Beautiful Soup 藉助網頁的結構和屬性等特性來解析網頁,這樣就可以省去複雜的正則表達式的編寫。 Beautiful Soup是Python的一個HTML或XML的解析庫。 1.解析器 解析器 使用方法 ...
  • 1、 購買功能變數名稱 2、 購買雲伺服器ecs 3、 遠程訪問雲伺服器並裝上Java環境和必備軟體 3.1安裝遠程訪問工具 3.2 jdk環境配置 3.3 Mysql依賴關係 重新配置MySQL的遠程訪問許可權。需要配置MySQL內部的訪問許可權,同時也需要配置防火牆的訪問許可權。阿裡雲比較簡單 ...
  • 預設使用的就是gbk編碼,這裡的例子改成了utf8編碼寫入—編碼 讀取—解碼 字元流 = 位元組流 + 編碼表#####################快捷操作的類FileWriter and FileReader ...
  • 設置項目的根路徑: 設置指定文件的在Tomcat中的虛擬路徑: 代碼: ...
一周排行
    -Advertisement-
    Play Games
  • 示例項目結構 在 Visual Studio 中創建一個 WinForms 應用程式後,項目結構如下所示: MyWinFormsApp/ │ ├───Properties/ │ └───Settings.settings │ ├───bin/ │ ├───Debug/ │ └───Release/ ...
  • [STAThread] 特性用於需要與 COM 組件交互的應用程式,尤其是依賴單線程模型(如 Windows Forms 應用程式)的組件。在 STA 模式下,線程擁有自己的消息迴圈,這對於處理用戶界面和某些 COM 組件是必要的。 [STAThread] static void Main(stri ...
  • 在WinForm中使用全局異常捕獲處理 在WinForm應用程式中,全局異常捕獲是確保程式穩定性的關鍵。通過在Program類的Main方法中設置全局異常處理,可以有效地捕獲並處理未預見的異常,從而避免程式崩潰。 註冊全局異常事件 [STAThread] static void Main() { / ...
  • 前言 給大家推薦一款開源的 Winform 控制項庫,可以幫助我們開發更加美觀、漂亮的 WinForm 界面。 項目介紹 SunnyUI.NET 是一個基於 .NET Framework 4.0+、.NET 6、.NET 7 和 .NET 8 的 WinForm 開源控制項庫,同時也提供了工具類庫、擴展 ...
  • 說明 該文章是屬於OverallAuth2.0系列文章,每周更新一篇該系列文章(從0到1完成系統開發)。 該系統文章,我會儘量說的非常詳細,做到不管新手、老手都能看懂。 說明:OverallAuth2.0 是一個簡單、易懂、功能強大的許可權+可視化流程管理系統。 有興趣的朋友,請關註我吧(*^▽^*) ...
  • 一、下載安裝 1.下載git 必須先下載並安裝git,再TortoiseGit下載安裝 git安裝參考教程:https://blog.csdn.net/mukes/article/details/115693833 2.TortoiseGit下載與安裝 TortoiseGit,Git客戶端,32/6 ...
  • 前言 在項目開發過程中,理解數據結構和演算法如同掌握蓋房子的秘訣。演算法不僅能幫助我們編寫高效、優質的代碼,還能解決項目中遇到的各種難題。 給大家推薦一個支持C#的開源免費、新手友好的數據結構與演算法入門教程:Hello演算法。 項目介紹 《Hello Algo》是一本開源免費、新手友好的數據結構與演算法入門 ...
  • 1.生成單個Proto.bat內容 @rem Copyright 2016, Google Inc. @rem All rights reserved. @rem @rem Redistribution and use in source and binary forms, with or with ...
  • 一:背景 1. 講故事 前段時間有位朋友找到我,說他的窗體程式在客戶這邊出現了卡死,讓我幫忙看下怎麼回事?dump也生成了,既然有dump了那就上 windbg 分析吧。 二:WinDbg 分析 1. 為什麼會卡死 窗體程式的卡死,入口門檻很低,後續往下分析就不一定了,不管怎麼說先用 !clrsta ...
  • 前言 人工智慧時代,人臉識別技術已成為安全驗證、身份識別和用戶交互的關鍵工具。 給大家推薦一款.NET 開源提供了強大的人臉識別 API,工具不僅易於集成,還具備高效處理能力。 本文將介紹一款如何利用這些API,為我們的項目添加智能識別的亮點。 項目介紹 GitHub 上擁有 1.2k 星標的 C# ...