一区二区三区在线-一区二区三区亚洲视频-一区二区三区亚洲-一区二区三区午夜-一区二区三区四区在线视频-一区二区三区四区在线免费观看

服務器之家:專注于服務器技術及軟件下載分享
分類導航

PHP教程|ASP.NET教程|Java教程|ASP教程|編程技術|正則表達式|C/C++|IOS|C#|Swift|Android|JavaScript|易語言|

服務器之家 - 編程語言 - Java教程 - Spring整合SpringMVC + Mybatis基礎框架的配置文件詳解

Spring整合SpringMVC + Mybatis基礎框架的配置文件詳解

2021-08-02 10:54jiawei3998 Java教程

這篇文章主要介紹了Spring整合SpringMVC + Mybatis基礎框架的配置文件,本文給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下

前言

新建一個普通的Maven項目

基本目錄結構

?
1
2
3
4
5
6
7
├── src     #
│ ├── main  #
│ │ └── java    # java代碼目錄
│ │ └── resources # 配置文件目錄, 存放下面Spring配置文件
│ ├── test      # 單元測試目錄
├── web     # web目錄
│ └── WEB-INF   # web.xml 配置文件目錄

1. Mybatis層編寫

1、在 resources 目錄下新建數據庫配置文件 database.properties

?
1
2
3
4
5
jdbc.driver=com.mysql.jdbc.Driver
# 如果是使用 MySQL8.0+ 那么還需要增加一個時區的配置; serverTimezone=Asia/Shanghai
jdbc.url=jdbc:mysql://localhost:3306/ssmbuild?useSSL=true&useUnicode=true&characterEncoding=utf8
jdbc.username=root
jdbc.password=123456

2、在 resources 目錄下創建Mybatis配置文件 mybatis-config.xml

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
<?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>
 <!--配置數據源, 交給Spring-->
 
 <!--配置log-->
 <settings>
 <!--STDOUT_LOGGING: 標準的日志工廠實現-->
 <setting name="logImpl" value="STDOUT_LOGGING"/>
 </settings>
 
 <!--配置別名-->
 <typeAliases>
 <package name="com.pro.pojo"/>
 </typeAliases>
 
 <!--綁定Mapper-->
 <mappers>
 <mapper class="com.pro.dao.BooksMapper"/>
 </mappers>
 
</configuration>

2. Spring層編寫

1. Spring整合Mybatis

配置Spring整合MyBatis,這里數據源使用c3p0連接池;

編寫Spring整合Mybatis的相關的配置文件;在 resources 目錄下創建 spring-dao.xml

注意:這里要引入上面Mybatis層的兩個配置文件,配置文件的名稱不要寫錯

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
<?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"
 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">
 
 <!--1. 關聯數據庫配置文件-->
 <context:property-placeholder location="classpath:database.properties"/>
 
 <!--2. 連接池
 dbcp: 半自動化操作, 不能自動連接
 c3p0: 自動化操作 (自動加載配置文件并設置到對象中)
 druid, hikari
 -->
 <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
 <property name="driverClass" value="${jdbc.driver}"/>
 <property name="jdbcUrl" value="${jdbc.url}"/>
 <property name="user" value="${jdbc.username}"/>
 <property name="password" value="${jdbc.password}"/>
 
 <!--c3p0連接池的私有屬性, 最大最小連接池大小-->
 <property name="maxPoolSize" value="30"/>
 <property name="minPoolSize" value="10"/>
 <!--關閉連接后不自動commit-->
 <property name="autoCommitOnClose" value="false"/>
 <!--連接超時-->
 <property name="checkoutTimeout" value="10000"/>
 <!--獲取連接失敗重試次數-->
 <property name="acquireRetryAttempts" value="2"/>
 </bean>
 
 <!--3. sqlSessionFactory-->
 <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
 <property name="dataSource" ref="dataSource"/>
 <!--綁定Mybatis配置文件-->
 <property name="configLocation" value="classpath:mybatis-config.xml"/>
 </bean>
 
 <!--4. 配置Dao掃描包, 動態實現Dao接口注入到Spring容器中-->
 <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
 <!--注入 sqlSessionFactory-->
 <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"/>
 <!--配置要掃描的dao包-->
 <property name="basePackage" value="com.pro.dao"/>
 </bean>
</beans>

2. Spring整合service

將業務層的類注入到Spring中,在 resources 目錄下創建 spring-service.xml

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
<?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:aop="http://www.springframework.org/schema/aop"
 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">
 
 <!--1. 掃描service下的包-->
 <context:component-scan base-package="com.pro.service"/>
 
 <!--2. 將業務層的類注入到Spring中-->
 <bean id="BooksServiceImpl" class="com.pro.service.BooksServiceImpl">
 <property name="booksMapper" ref="booksMapper"/>
 </bean>
 
 <!--3. 配置聲明式事務-->
 <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
 <!--注入數據源-->
 <property name="dataSource" ref="dataSource"/>
 </bean>
 
 <!--4. 配置aop實現事務織入-->
 <!--配置事務通知-->
 <tx:advice id="txAdvice" transaction-manager="transactionManager">
 <!--1. 給那些方法配置事務-->
 <!--2. 配置事務的傳播特性: propagation-->
 <tx:attributes>
 <tx:method name="*" propagation="REQUIRED"/>
 </tx:attributes>
 </tx:advice>
 
 <!--配置事務切入-->
 <aop:config>
 <!--mapper包下的所有類的所有方法-->
 <aop:pointcut id="txPointCut" expression="execution(* com.pro.dao.*.*(..))"/>
 <aop:advisor advice-ref="txAdvice" pointcut-ref="txPointCut"/>
 </aop:config>
</beans>

3. SpringMVC層編寫

1. 編寫web.xml

修改 WEB-INF 下的 web.xml 文件

這里引入Spring整合的配置文件 applicationContext.xml

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="https://jakarta.ee/xml/ns/jakartaee"
 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 xsi:schemaLocation="https://jakarta.ee/xml/ns/jakartaee https://jakarta.ee/xml/ns/jakartaee/web-app_5_0.xsd"
 version="5.0">
 
 <!--DispatchServlet-->
 <servlet>
 <servlet-name>springmvc</servlet-name>
 <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
 <!--加載Spring配置文件-->
 <init-param>
 <param-name>contextConfigLocation</param-name>
 <param-value>classpath:applicationContext.xml</param-value>
 </init-param>
 <!--啟動級別-->
 <load-on-startup>1</load-on-startup>
 </servlet>
 <servlet-mapping>
 <servlet-name>springmvc</servlet-name>
 <url-pattern>/</url-pattern>
 </servlet-mapping>
 
 <!--亂碼過濾-->
 <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>
 </filter>
 <filter-mapping>
 <filter-name>encodingFilter</filter-name>
 <url-pattern>/*</url-pattern>
 </filter-mapping>
 
 <!--Session過期時間-->
 <session-config>
 <session-timeout>15</session-timeout>
 </session-config>
</web-app>

2. 編寫spring-mvc.xml

resources 目錄下創建 spring-mvc.xml

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
<?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/beans
 http://www.springframework.org/schema/beans/spring-beans.xsd
 http://www.springframework.org/schema/mvc
 http://www.springframework.org/schema/mvc/spring-mvc.xsd
 http://www.springframework.org/schema/context
 https://www.springframework.org/schema/context/spring-context.xsd">
 
 
 <!--1. 注解驅動-->
 <mvc:annotation-driven/>
 <!--2. 靜態資源過濾-->
 <mvc:default-servlet-handler/>
 <!--3. 掃描包: controller-->
 <context:component-scan base-package="com.pro.controller"/>
 <!--4. 視圖解析器-->
 <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
 <property name="prefix" value="/WEB-INF/jsp/"/>
 <property name="suffix" value=".jsp"/>
 </bean>
 
</beans>

4. Spring配置整合文件,applicationContext.xml

resources 目錄下創建 applicationContext.xml

這里引入上面三個配置文件 spring-dao.xmlspring-service.xmlspring-mvc.xml 整合成一個總的配置文件

?
1
2
3
4
5
6
7
8
9
10
11
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 xsi:schemaLocation="http://www.springframework.org/schema/beans
 http://www.springframework.org/schema/beans/spring-beans.xsd">
 
 <import resource="classpath:spring-dao.xml"/>
 <import resource="classpath:spring-service.xml"/>
 <import resource="classpath:spring-mvc.xml"/>
 
</beans>

依賴

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
<!--依賴-->
<dependencies>
 <dependency>
 <groupId>org.projectlombok</groupId>
 <artifactId>lombok</artifactId>
 <version>1.18.10</version>
 </dependency>
 <!--Junit-->
 <dependency>
 <groupId>junit</groupId>
 <artifactId>junit</artifactId>
 <version>4.13</version>
 </dependency>
 <!--數據庫驅動-->
 <dependency>
 <groupId>mysql</groupId>
 <artifactId>mysql-connector-java</artifactId>
 <version>5.1.47</version>
 </dependency>
 <!--數據庫連接池-->
 <dependency>
 <groupId>com.mchange</groupId>
 <artifactId>c3p0</artifactId>
 <version>0.9.5.2</version>
 </dependency>
 
 <!--Servlet - JSP -->
 <dependency>
 <groupId>javax.servlet</groupId>
 <artifactId>servlet-api</artifactId>
 <version>2.5</version>
 </dependency>
 <dependency>
 <groupId>javax.servlet.jsp</groupId>
 <artifactId>jsp-api</artifactId>
 <version>2.2</version>
 </dependency>
 <dependency>
 <groupId>javax.servlet</groupId>
 <artifactId>jstl</artifactId>
 <version>1.2</version>
 </dependency>
 
 <!--Mybatis-->
 <dependency>
 <groupId>org.mybatis</groupId>
 <artifactId>mybatis</artifactId>
 <version>3.5.2</version>
 </dependency>
 <dependency>
 <groupId>org.mybatis</groupId>
 <artifactId>mybatis-spring</artifactId>
 <version>2.0.2</version>
 </dependency>
 
 <!--Spring-->
 <dependency>
 <groupId>org.springframework</groupId>
 <artifactId>spring-webmvc</artifactId>
 <version>5.1.9.RELEASE</version>
 </dependency>
 <dependency>
 <groupId>org.springframework</groupId>
 <artifactId>spring-jdbc</artifactId>
 <version>5.1.9.RELEASE</version>
 </dependency>
 <dependency>
 <groupId>org.aspectj</groupId>
 <artifactId>aspectjweaver</artifactId>
 <version>1.9.4</version>
 </dependency>
</dependencies>
 
<!--靜態資源導出問題-->
<build>
 <resources>
 <resource>
 <directory>src/main/java</directory>
 <includes>
 <include>**/*.properties</include>
 <include>**/*.xml</include>
 </includes>
 <filtering>false</filtering>
 </resource>
 <resource>
 <directory>src/main/resources</directory>
 <includes>
 <include>**/*.properties</include>
 <include>**/*.xml</include>
 </includes>
 <filtering>false</filtering>
 </resource>
 </resources>
</build>

到此這篇關于Spring整合SpringMVC + Mybatis基礎框架的配置文件的文章就介紹到這了,更多相關Spring整合SpringMVC Mybatis內容請搜索服務器之家以前的文章或繼續瀏覽下面的相關文章希望大家以后多多支持服務器之家!

原文鏈接:https://www.cnblogs.com/pojo/archive/2021/01/31/14353557.html

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 天天快乐高清在线观看 | 午夜毛片在线观看 | 久青草国产在视频在线观看 | 国产成人精品一区二区仙踪林 | 热辣小秘书办公室 | 国产精品视频久 | 天美传媒传媒免费观看 | 国产传媒在线播放 | 美女露奶奶 | 欧美精品久久一区二区三区 | 好大用力深一点女公交车 | 欧美一级在线 | 亚洲欧美日韩精品高清 | brazzersxxx欧美| 四虎最新网址在线观看 | 久草在线草a免费线看 | 人与动人物aaaa | 天堂伊人 | 日本色淫 | 五月天精品视频播放在线观看 | 思思久久精品在热线热 | 三级午夜宅宅伦不卡在线 | 国产欧美一区二区三区免费 | 91热国产 | 国产亚洲视频网站 | 国产成人久久 | 日韩日b视频 | 国产我不卡 | 草逼吧 | 精品日本一区二区 | 亚洲精品乱码蜜桃久久久 | 久久精品成人免费看 | 男人天堂bt | 日韩视频在线免费观看 | 亚洲乱码尤物193yw在线播放 | 美女扒开肌肌让男人桶 | 好大好硬抽搐好爽想要 | 亚洲激情在线 | 国内9lporm自拍视频区 | 小浪妇奶真大水多 | 无限观看社区在线视频 |