目錄
- 前言
- 1. Mybatis層撰寫
- 2. Spring層撰寫
- 1. Spring整合Mybatis
- 2. Spring整合service
- 3. SpringMVC層撰寫
- 1. 撰寫web.xml
- 2. 撰寫spring-mvc.xml
- 4. Spring配置整合檔案,applicationContext.xml
- 依賴
前言
新建一個普通的Maven專案
基本目錄結構
├── src #
│ ├── main #
│ │ └── java # java代碼目錄
│ │ └── resources # 組態檔目錄, 存放下面Spring組態檔
│ ├── test # 單元測驗目錄
├── web # web目錄
│ └── WEB-INF # web.xml 組態檔目錄
1. Mybatis層撰寫
1、在 resources 目錄下新建資料庫組態檔 database.properties
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
<?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="https://www.cnblogs.com/pojo/p/STDOUT_LOGGING"/>
</settings>
<!--配置別名-->
<typeAliases>
<package name="com.pro.pojo"/>
</typeAliases>
<!--系結Mapper-->
<mappers>
<mapper />
</mappers>
</configuration>
2. Spring層撰寫
1. Spring整合Mybatis
-
配置Spring整合MyBatis,這里資料源使用c3p0連接池;
-
撰寫Spring整合Mybatis的相關的組態檔;在
resources目錄下創建spring-dao.xml
注意:這里要引入上面Mybatis層的兩個組態檔,組態檔的名稱不要寫錯
<?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" >
<property name="driverClass" value="https://www.cnblogs.com/pojo/p/${jdbc.driver}"/>
<property name="jdbcUrl" value="https://www.cnblogs.com/pojo/p/${jdbc.url}"/>
<property name="user" value="https://www.cnblogs.com/pojo/p/${jdbc.username}"/>
<property name="password" value="https://www.cnblogs.com/pojo/p/${jdbc.password}"/>
<!--c3p0連接池的私有屬性, 最大最小連接池大小-->
<property name="maxPoolSize" value="https://www.cnblogs.com/pojo/p/30"/>
<property name="minPoolSize" value="https://www.cnblogs.com/pojo/p/10"/>
<!--關閉連接后不自動commit-->
<property name="autoCommitOnClose" value="https://www.cnblogs.com/pojo/p/false"/>
<!--連接超時-->
<property name="checkoutTimeout" value="https://www.cnblogs.com/pojo/p/10000"/>
<!--獲取連接失敗重試次數-->
<property name="acquireRetryAttempts" value="https://www.cnblogs.com/pojo/p/2"/>
</bean>
<!--3. sqlSessionFactory-->
<bean id="sqlSessionFactory" >
<property name="dataSource" ref="dataSource"/>
<!--系結Mybatis組態檔-->
<property name="configLocation" value="https://www.cnblogs.com/pojo/p/classpath:mybatis-config.xml"/>
</bean>
<!--4. 配置Dao掃描包, 動態實作Dao介面注入到Spring容器中-->
<bean >
<!--注入 sqlSessionFactory-->
<property name="sqlSessionFactoryBeanName" value="https://www.cnblogs.com/pojo/p/sqlSessionFactory"/>
<!--配置要掃描的dao包-->
<property name="basePackage" value="https://www.cnblogs.com/pojo/p/com.pro.dao"/>
</bean>
</beans>
2. Spring整合service
將業務層的類注入到Spring中,在 resources 目錄下創建 spring-service.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: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" >
<property name="booksMapper" ref="booksMapper"/>
</bean>
<!--3. 配置宣告式事務-->
<bean id="transactionManager" >
<!--注入資料源-->
<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
<?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
<?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 >
<property name="prefix" value="https://www.cnblogs.com/WEB-INF/jsp/"/>
<property name="suffix" value="https://www.cnblogs.com/pojo/p/.jsp"/>
</bean>
</beans>
4. Spring配置整合檔案,applicationContext.xml
在 resources 目錄下創建 applicationContext.xml
這里引入上面三個組態檔 spring-dao.xml、spring-service.xml、spring-mvc.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"
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>
依賴
<!--依賴-->
<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>
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/255055.html
標籤:Java
