3つの主要なSSMフレームワークの新しいバージョンを完了する前に、この記事では、その統合プロセスとプロジェクトのソースコードについて説明します。バージョン番号は、Spring 5.2.2.RELEASE、SpringMVC 5.2.2.RELEASE、MyBatis 3.5.2です。
背景紹介
ssm-demoは、GitHubに投稿した最初のオープンソースプロジェクトです。このプロジェクトは2016年末に開発する必要があり、このプロジェクトをGitHubオープンソースプラットフォームにオープンソースすることを選択しました。
2016年にJava Webプロジェクトを開発するときにSpring 4.Xバージョンを使用することは比較的一般的な現象であり、バージョンが遅れていると人々に感じさせることはありませんが、それはすでに2020年であるため、Springは元のコードに基づいて実行されました。関連するフレームワークのアップグレードなので、このSpring 5バージョンに関連する3つの主要なSSMフレームワークチュートリアルの統合を整理しました。
春5はここです
Spring 5は非常に重要なバージョン更新です。このバージョンは、オープンソースウェアハウスで頻繁に更新されていることもわかります。このバージョンは、エンタープライズ開発で徐々にシェアを拡大していくと誰もが信じているはずです。
このチュートリアルで選択したバージョン番号について簡単に説明します。
-
Springバージョンは比較的新しいバージョンを選択し、元の4.2.4.RELEASEから5.2.2.RELEASEにアップグレードされました。このバージョンは2019年10月3日に正式にリリースされ、バージョンアップデートログはv5.2.2.RELEASEでした。
-
Spring MVCのバージョンも、5.2.2.RELEASEであるSpringのバージョンで更新されます。
-
MyBatisのバージョンは3.5.2にアップグレードされ、MyBatis 3.5以降はSpring 5以降に対応し、Spring 4はMyBatis 3.5以降をサポートしていません。対応するバージョンのmybatis-springもバージョン2.0以降にアップグレードする必要があります。 2.0.1。
上記は、3つの主要なSSMフレームワークのバージョンの選択です。バージョン番号は、自分で選択して統合しています。微調整できますが、テストに注意を払い、エラーを報告しないでください。
メインコードの統合
ソースコードのディレクトリ構造は次のとおりです。
pom依存
Spring Spring MVC MyBatisの3つの主要なフレームワークのメインバージョン番号が導入されました。ここでは、pom.xmlファイルを直接指定します。
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.ssm.demo</groupId>
<artifactId>ssm-demo</artifactId>
<packaging>war</packaging>
<version>1.0-SNAPSHOT</version>
<name>ssm-demo</name>
<url>http://maven.apache.org</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<spring.version>5.2.2.RELEASE</spring.version>
<java.version>1.8</java.version>
<jdbc.driver.version>8.0.16</jdbc.driver.version>
<aspectj.version>1.9.5</aspectj.version>
<javax.servlet-api.version>3.1.0</javax.servlet-api.version>
<jsp-api.version>2.2</jsp-api.version>
<jstl.version>1.2</jstl.version>
<mybatis.version>3.5.2</mybatis.version>
<mybatis-spring.version>2.0.1</mybatis-spring.version>
<maven.test.skip>true</maven.test.skip>
</properties>
<dependencies>
<!-- Begin: spring依赖 -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context-support</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-tx</artifactId>
<version>${spring.version}</version>
</dependency>
<!-- End: spring依赖 -->
<!-- Begin: springmvc依赖 -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>${spring.version}</version>
</dependency>
<!-- End: springmvc依赖 -->
<!-- Begin: mybatis依赖 -->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>${mybatis.version}</version>
</dependency>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
<version>${mybatis-spring.version}</version>
</dependency>
<!-- End: mybatis依赖 -->
<!-- Begin: 数据库依赖包 -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>${jdbc.driver.version}</version>
<scope>runtime</scope>
</dependency>
<!-- End: 数据库依赖包 -->
<!-- Begin: aspectj相关jar包-->
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjrt</artifactId>
<version>${aspectj.version}</version>
</dependency>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>${aspectj.version}</version>
</dependency>
<!-- End: aspectj相关jar包-->
<!-- Begin: Servlet相关依赖包 -->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>${javax.servlet-api.version}</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>javax.servlet.jsp</groupId>
<artifactId>jsp-api</artifactId>
<version>${jsp-api.version}</version>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
<version>${jstl.version}</version>
</dependency>
<!-- End: Servlet相关依赖包 -->
</dependencies>
<build>
<finalName>ssm-demo</finalName>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.2</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
</plugins>
</build>
</project>
复制代码
jarパッケージがダウンロードされるまでしばらくお待ちください。
Spring構成ファイル
applicationContext.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:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd">
<!-- 自动扫描 -->
<context:component-scan base-package="com.ssm.demo.dao"/>
<context:component-scan base-package="com.ssm.demo.service"/>
<!-- 配置数据源 -->
<bean id="dataSource"
class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="com.mysql.cj.jdbc.Driver"/>
<property name="url"
value="jdbc:mysql://localhost:3306/ssm-demo"/>
<!-- 改为你的地址即可 -->
<property name="username" value="root"/>
<property name="password" value="131313"/>
</bean>
<!-- 配置mybatis的sqlSessionFactory -->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource"/>
<!-- 自动扫描mappers.xml文件 -->
<!--<property name="mapperLocations" value="classpath:mappers/*.xml"></property>-->
<!-- mybatis配置文件 -->
<property name="configLocation" value="classpath:mybatis-config.xml"></property>
</bean>
<!-- DAO接口所在包名,Spring会自动查找其下的类 -->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="com.ssm.demo.dao"/>
<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"></property>
</bean>
<!-- (事务管理)transaction manager, use JtaTransactionManager for global tx -->
<bean id="transactionManager"
class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"/>
</bean>
<!-- 配置事务通知属性 -->
<tx:advice id="txAdvice" transaction-manager="transactionManager">
<!-- 定义事务传播属性 -->
<tx:attributes>
<tx:method name="insert*" propagation="REQUIRED"/>
<tx:method name="update*" propagation="REQUIRED"/>
<tx:method name="upd*" propagation="REQUIRED"/>
<tx:method name="edit*" propagation="REQUIRED"/>
<tx:method name="save*" propagation="REQUIRED"/>
<tx:method name="add*" propagation="REQUIRED"/>
<tx:method name="new*" propagation="REQUIRED"/>
<tx:method name="set*" propagation="REQUIRED"/>
<tx:method name="remove*" propagation="REQUIRED"/>
<tx:method name="delete*" propagation="REQUIRED"/>
<tx:method name="del*" propagation="REQUIRED"/>
<tx:method name="change*" propagation="REQUIRED"/>
<tx:method name="check*" propagation="REQUIRED"/>
<tx:method name="get*" propagation="REQUIRED" read-only="true"/>
<tx:method name="search*" propagation="REQUIRED" read-only="true"/>
<tx:method name="find*" propagation="REQUIRED" read-only="true"/>
<tx:method name="load*" propagation="REQUIRED" read-only="true"/>
<tx:method name="*" propagation="REQUIRED" read-only="true"/>
</tx:attributes>
</tx:advice>
<!-- 配置事务切面 -->
<aop:config>
<aop:pointcut id="serviceOperation"
expression="(execution(* com.ssm.demo.service.*.*(..)))"/>
<aop:advisor advice-ref="txAdvice" pointcut-ref="serviceOperation"/>
</aop:config>
</beans>
复制代码
SpringMVC設定
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:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd">
<!-- 使用注解的包,包括子集 -->
<context:component-scan base-package="com.ssm.demo.controller"/>
<!-- 视图解析器 -->
<bean id="viewResolver"
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/"/>
<property name="suffix" value=".jsp"></property>
</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>
<typeAliases>
<package name="com.ssm.demo.entity"/>
</typeAliases>
</configuration>
复制代码
web.xmlの構成
最後に、Java Webプロジェクトで最も重要な構成ファイルであるweb.xmlを使用します。ここでは、Springの関連する構成を読み込み、Springコンテナを起動します。同時に、SpringMVCを構成し、すべてのリクエストをフロントコントローラのDispatcherServletに渡して処理します。
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_2_5.xsd"
id="WebApp_ID" version="2.5">
<display-name>ssm-demo</display-name>
<!--Start 欢迎页-->
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<!--End 欢迎页-->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml</param-value>
</context-param>
<!--Start 编码过滤器 解决乱码问题-->
<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>
<!--End 编码过滤器 解决乱码问题-->
<!--Start spring监听器 -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!--End Start spring监听器 -->
<!--Start spring mvc servlet-->
<servlet>
<servlet-name>springMVC</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring-mvc.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<!--End spring mvc servlet-->
<!--Start servlet-mapping -->
<servlet-mapping>
<servlet-name>springMVC</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>
<!--End servlet-mapping -->
</web-app>
复制代码
JSPページ
最後に、テスト用のJSPページがあります。webappディレクトリに新しいindex.jspを作成します。コードは次のとおりです:
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>ssm demo</title>
</head>
<body>
Hello SSM ( Spring 5.2 + MyBatis 3.5)!
</body>
</html>
复制代码
プロジェクトの立ち上げとプレビュー
ソースコードをテストしました。Tomcatにデプロイして開始すると、次のページが表示されます。
Spring 5の3つの主要なSSMフレームワークの統合が成功しました!
これは、Spring 5 + Spring MVC 5 + MyBatis 3.5の統合チュートリアルです。統合のためだけのものです。Spring5をアップグレードするときに誰もが参照できます。他の機能は自分で実装できます。以前に書いたオープンソースプロジェクトで拡張することもできます。
注意が必要な事項
- サフィックスをリクエスト
ここで設定されているのは、.doの最後で要求をインターセプトすることです。必要に応じて変更できます。
- マッパーファイル
プロジェクトに追加されたMapperファイルがないため、この構成の行をコメントアウトしました。Mapperファイルを追加した後、このコメントの行を削除する必要があります。
ソースコードのダウンロード
ダウンロードリンクは次のとおりです。
download.csdn.net/download/ZH…
アップロード中に、ポイントをダウンロードする必要がないように設定しました。後でポイントをダウンロードする必要がある場合は、CSDNプラットフォームで設定する必要があります。
最後に書く
小さなプロモーションとして、興味のある友人がご覧になれます。最近、ナゲッツプラットフォームに小さなブックレット「Spring Boot Large-scale Online Mall Project Practical Tutorial」を発行しました(リンクをクリックするか、下の写真をクリックして購入すると、割引が適用されます8折りたたみ):
この小冊子は、Spring Bootテクノロジースタックに焦点を当てており、使用される他の技術フレームワークは、最新のテクノロジートレンドを考慮し、知識を浅いものから深いものに段階的に拡張し、基本を学びながら、Springを学ぶだけでなく、特定の開発スキルを習得することもできますBootのファーは、ソースコードの設計と内部原則も認識しています。SpringBootの関連テクノロジースタックの統合を学習するだけでなく、Spring Bootテクノロジースタックを使用して大規模なショッピングモールシステムを構築できるため、高品質の学習を実現できます。レベル経験。Hello Worldプロジェクトから離れれば、完全な実践プロジェクトを取得できるだけでなく、現在のホットスプリングブートテクノロジスタックを埋めることができ、技術的な深さと給与の昇進を十分に保護できます。
これはショッピングモールの実用的なプロジェクトです。一部のページのプレビューは次のとおりです。
興味のある友達は注意を払うことができます。
転載/出典を示すことを除いて、著者はすべてオリジナルであり、転載を歓迎しますが、この声明は著者の同意なしに保持する必要があり、元のテキストリンクは記事ページの明確な位置に示されます。それ以外の場合は、法的責任を追う権利が留保されます。
ご覧いただきありがとうございます私は13歳ですこの記事は私の公開アカウント「プログラマーの小さな物語」で最初に公開されました。