プロジェクトとSSMフレームワークの統合のアイデアを作成する
1つは、プロジェクトを作成する
springMVCは後で構成するため、IDEAのWebスケルトンを使用してMavenプロジェクトを作成します。
画像
次のようにプロジェクトディレクトリを作成します。同時に、プロジェクトに必要なパッケージとファイルが手動で作成されています。
画像
上の図で、application.xmlはSpring構成ファイル、log4jはログ構成ファイル、springMVC.xmlはspringMVC構成ファイルです
pom.xmlファイルで必要な依存関係を構成しますプロジェクト全体の依存関係は次のとおりです。
<pre style = "box-sizing:border-box; outline:none; margin:0px; padding:0px; border:0px; font-style:normal; font-variant-ligatures:normal; font-variant-caps:normal ; font-variant-numeric:inherit; font-variant-east-asian:inherit; font-weight:400; font-stretch:inherit; font-size:18px; line-height:inherit; font-family:couriernew、courier 、等幅;垂直配置:ベースライン;色:rgb(93、93、93);文字間隔:通常;孤立:2;テキスト配置:開始;テキストインデント:0px;テキスト変換:なし;ウィンドウ: 2;ワード間隔:0px; -webkit-text-stroke-width:0px; background-color:rgb(255、255、255); text-decoration-style:initial; text-decoration-color:initial; ">
<?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.hust.demo-ssm</groupId>
<artifactId>demo</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>war</packaging>
<name>demo Maven Webapp</name>
<!-- FIXME change it to the project's website -->
<url>http://www.example.com</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
<spring.version>5.0.2.RELEASE</spring.version>
<slf4j.version>1.6.6</slf4j.version>
<log4j.version>1.2.12</log4j.version>
<mysql.version>5.1.6</mysql.version>
<mybatis.version>3.4.5</mybatis.version>
</properties>
<dependencies>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>1.6.8</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aop</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-tx</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>${mysql.version}</version>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>servlet-api</artifactId>
<version>2.5</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>javax.servlet.jsp</groupId>
<artifactId>jsp-api</artifactId>
<version>2.0</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>jstl</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
</dependency>
<!-- log start -->
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>${log4j.version}</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>${slf4j.version}</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<version>${slf4j.version}</version>
</dependency>
<!-- log end -->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>${mybatis.version}</version>
</dependency>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
<version>1.3.0</version>
</dependency>
<dependency>
<groupId>c3p0</groupId>
<artifactId>c3p0</artifactId>
<version>0.9.1.2</version>
<type>jar</type>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.9.0</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-core</artifactId>
<version>2.9.0</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-annotations</artifactId>
<version>2.9.0</version>
</dependency>
</dependencies>
<build>
<finalName>demo</finalName>
<pluginManagement><!-- lock down plugins versions to avoid using Maven defaults (may be moved to parent pom) -->
<plugins>
<plugin>
<artifactId>maven-clean-plugin</artifactId>
<version>3.1.0</version>
</plugin>
<!-- see http://maven.apache.org/ref/current/maven-core/default-bindings.html#Plugin_bindings_for_war_packaging -->
<plugin>
<artifactId>maven-resources-plugin</artifactId>
<version>3.0.2</version>
</plugin>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.0</version>
</plugin>
<plugin>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.22.1</version>
</plugin>
<plugin>
<artifactId>maven-war-plugin</artifactId>
<version>3.2.2</version>
</plugin>
<plugin>
<artifactId>maven-install-plugin</artifactId>
<version>2.5.2</version>
</plugin>
<plugin>
<artifactId>maven-deploy-plugin</artifactId>
<version>2.8.2</version>
</plugin>
</plugins>
</pluginManagement>
</build>
</project>
</ pre>
そのため、現時点ではプロジェクトのスケルトンが構築され、プロジェクトの依存関係がインポートされています。次に、統合のアイデアを紹介します。
2. SSM統合のアイデア
目的に関しては、統合後、サービス層は依存関係注入を通じてdao層のメソッドを呼び出すことができなければならず、コントローラー層は依存関係注入を通じてサービス層のメソッドを呼び出す必要があります。したがって、統合の基本的な考え方は、春を使用してmybatisとspringMVCを統合することです。
画像
Mybatisの設定
まず、mybatisフレームワークを構成します。
1つは、データベースdemo_ssmを作成し、アカウントテーブルを作成する
sqlステートメントは次のとおりです。
<pre style = "box-sizing:border-box; outline:none; margin:0px; padding:0px; border:0px; font-style:normal; font-variant-ligatures:normal; font-variant-caps:normal ; font-variant-numeric:inherit; font-variant-east-asian:inherit; font-weight:400; font-stretch:inherit; font-size:18px; line-height:inherit; font-family:couriernew、courier 、等幅;垂直配置:ベースライン;色:rgb(93、93、93);文字間隔:通常;孤立:2;テキスト配置:開始;テキストインデント:0px;テキスト変換:なし;ウィンドウ: 2;ワード間隔:0px; -webkit-text-stroke-width:0px; background-color:rgb(255、255、255); text-decoration-style:initial; text-decoration-color:initial; ">
CREATE DATABASE `demo_ssm`;
USE `demo_ssm`;
DROP TABLE IF EXISTS `account`;
CREATE TABLE `account`(
`id` INT(11) NOT NULL AUTO_INCREMENT,
`name` VARCHAR(32),
`money` DOUBLE,
PRIMARY KEY(`id`)
)ENGINE=INNODB DEFAULT CHARSET=utf8;
INSERT INTO `account`(`name`,`money`) VALUES('赵敏',99999.99),('张无忌',11111.1),('木婉清',22222.22);
</ pre>
2.アカウントエンティティクラスを作成します。
<pre style = "box-sizing:border-box; outline:none; margin:0px; padding:0px; border:0px; font-style:normal; font-variant-ligatures:normal; font-variant-caps:normal ; font-variant-numeric:inherit; font-variant-east-asian:inherit; font-weight:400; font-stretch:inherit; font-size:18px; line-height:inherit; font-family:couriernew、courier 、等幅;垂直配置:ベースライン;色:rgb(93、93、93);文字間隔:通常;孤立:2;テキスト配置:開始;テキストインデント:0px;テキスト変換:なし;ウィンドウ: 2;ワード間隔:0px; -webkit-text-stroke-width:0px; background-color:rgb(255、255、255); text-decoration-style:initial; text-decoration-color:initial; ">
public class Account implements Serializable {
private Integer id;
private String name;
private Double money;
//getter and setter
//toString
}
</ pre>
3番目に、アノテーションを使用して永続化レイヤーインターフェイスを開発します
<pre style = "box-sizing:border-box; outline:none; margin:0px; padding:0px; border:0px; font-style:normal; font-variant-ligatures:normal; font-variant-caps:normal ; font-variant-numeric:inherit; font-variant-east-asian:inherit; font-weight:400; font-stretch:inherit; font-size:18px; line-height:inherit; font-family:couriernew、courier 、等幅;垂直配置:ベースライン;色:rgb(93、93、93);文字間隔:通常;孤立:2;テキスト配置:開始;テキストインデント:0px;テキスト変換:なし;ウィンドウ: 2;ワード間隔:0px; -webkit-text-stroke-width:0px; background-color:rgb(255、255、255); text-decoration-style:initial; text-decoration-color:initial; ">
package com.hust.dao;
public interface IAccountDao {
@Select("select * from account")
public List<Account> findAllAccount();
}
</ pre>
この時点で、Mybatisが構成されています。現時点では、データソースは構成されていません。Springを統合するときは、構成とテストを待機します。
MybatisのSpring構成と統合
1つは、Spring構成
この時点で、applicationContext.xmlファイルの構成:
<pre style = "box-sizing:border-box; outline:none; margin:0px; padding:0px; border:0px; font-style:normal; font-variant-ligatures:normal; font-variant-caps:normal ; font-variant-numeric:inherit; font-variant-east-asian:inherit; font-weight:400; font-stretch:inherit; font-size:18px; line-height:inherit; font-family:couriernew、courier 、等幅;垂直配置:ベースライン;色:rgb(93、93、93);文字間隔:通常;孤立:2;テキスト配置:開始;テキストインデント:0px;テキスト変換:なし;ウィンドウ: 2;ワード間隔:0px; -webkit-text-stroke-width:0px; background-color:rgb(255、255、255); text-decoration-style:initial; text-decoration-color:initial; ">
<?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"
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">
<!-- 配置扫描-->
<context:component-scan base-package="com.hust">
<context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
</context:component-scan>
</beans>
</ pre>
注意点:
自動アセンブリはパッケージ内の注釈をスキャンする必要があるため、注釈のスキャンを有効にするように構成する必要があります。アノテーションスキャンを有効にするには、<context:annotation-config />と<context:component-scan>の2つの方法があります。2つの間の類似点と相違点は次のとおりです。
<context:annotation-config>:アノテーションスキャンは、Springコンテナーに登録されているBean用です
<context:component-scan>:<context:annotation-config>のすべての機能だけでなく、指定されたパッケージの下の対応するBeanもスキャンします
2、SpringはMybatisを統合
思考分析:
Mybatisフレームワークはプロキシオブジェクトを自動的に生成できます。そのため、永続化レイヤーについては、インターフェースと注釈のみを記述しました。特定の実装クラスは、フレームワークによって完成されます。データベースのCRUDも、このプロキシオブジェクトを通じて実装されます。したがって、生成されたプロキシオブジェクトをコンテナに格納し、Springフレームワークで制御できる限り、統合は成功します。
もう1つの重要な点は、MybatisフレームワークがSqlSessionFactoryファクトリインスタンスを使用してSqlSessionインスタンスを作成することです。SqlSessionインスタンスを通じて、データベースを操作できます。
要するに、MybatisとSpringの統合の鍵は、コンテナーがSqlSessionFactoryBeanクラスを制御できるようにすることです。
具体配置
特定の構成は、3つの部分に分かれています。
1.接続の設定****プール**** 2. sqlSessionファクトリクラスの設定3.スキャンパッケージ用のスキャナーの設定
したがって、構成は次のとおりです。
<pre style = "box-sizing:border-box; outline:none; margin:0px; padding:0px; border:0px; font-style:normal; font-variant-ligatures:normal; font-variant-caps:normal ; font-variant-numeric:inherit; font-variant-east-asian:inherit; font-weight:400; font-stretch:inherit; font-size:18px; line-height:inherit; font-family:couriernew、courier 、等幅;垂直配置:ベースライン;色:rgb(93、93、93);文字間隔:通常;孤立:2;テキスト配置:開始;テキストインデント:0px;テキスト変換:なし;ウィンドウ: 2;ワード間隔:0px; -webkit-text-stroke-width:0px; background-color:rgb(255、255、255); text-decoration-style:initial; text-decoration-color:initial; ">
<?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"
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">
<!-- 配置扫描-->
<context:component-scan base-package="com.hust">
</context:component-scan>
<!-- 配置数据源-->
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="driverClass" value="com.mysql.jdbc.Driver"/>
<property name="jdbcUrl" value="jdbc:mysql:///demo_ssm"/>
<property name="user" value="root"/>
<property name="password" value="root"/>
</bean>
<!-- 配置SqlSession的工厂-->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource"/>
</bean>
<!-- 配置扫描的包 -->
<bean id="mapperScanner" class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="com.hust.dao"/>
</bean>
</beans>
</ pre>
この構成の後、SpringコンテナはsqlSessionファクトリクラスを作成できます。次に@RepositoryアノテーションをIAccountDaoに追加すると、AccountDaoインターフェイスのプロキシオブジェクトをコンテナで管理して統合できます。テストクラスでテストしてみましょう:
<pre style = "box-sizing:border-box; outline:none; margin:0px; padding:0px; border:0px; font-style:normal; font-variant-ligatures:normal; font-variant-caps:normal ; font-variant-numeric:inherit; font-variant-east-asian:inherit; font-weight:400; font-stretch:inherit; font-size:18px; line-height:inherit; font-family:couriernew、courier 、等幅;垂直配置:ベースライン;色:rgb(93、93、93);文字間隔:通常;孤立:2;テキスト配置:開始;テキストインデント:0px;テキスト変換:なし;ウィンドウ: 2;ワード間隔:0px; -webkit-text-stroke-width:0px; background-color:rgb(255、255、255); text-decoration-style:initial; text-decoration-color:initial; ">
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = {"classpath:applicationContext.xml"})
public class SpringTest {
@Autowired
private IAccountDao accountDao;
@Test
public void testSpring(){
List<Account> accounts = accountDao.findAllAccount();
for (Account account:accounts
) {
System.out.println(account);
}
}
}
</ pre>
結果は次のとおりです。
<pre style = "box-sizing:border-box; outline:none; margin:0px; padding:0px; border:0px; font-style:normal; font-variant-ligatures:normal; font-variant-caps:normal ; font-variant-numeric:inherit; font-variant-east-asian:inherit; font-weight:400; font-stretch:inherit; font-size:18px; line-height:inherit; font-family:couriernew、courier 、等幅;垂直配置:ベースライン;色:rgb(93、93、93);文字間隔:通常;孤立:2;テキスト配置:開始;テキストインデント:0px;テキスト変換:なし;ウィンドウ: 2;ワード間隔:0px; -webkit-text-stroke-width:0px; background-color:rgb(255、255、255); text-decoration-style:initial; text-decoration-color:initial; ">
Account{id=1, name='赵敏', money=99999.99}
Account{id=2, name='张无忌', money=11111.1}
Account{id=3, name='木婉清', money=22222.22}
</ pre>
すべてのアカウントが正常にクエリされ、依存関係の注入がコンテナを介して実行できることを示しています。これは、Springとmybatisの両方が正常に構成され、統合されたことを示しています。
PS:ここで、Springは@RunWithと@ContextConfigurationを使用してjunitを統合します
springMVC構成
1. web.xmlを構成する
SpringMVCフレームワークは、すべてのHTTP要求と応答を処理するために使用されるDispatcherServletを中心に設計されています。したがって、DispatcherServletで処理するリクエストをマップするようにweb.xmlファイルを構成する必要があります。
<pre style = "box-sizing:border-box; outline:none; margin:0px; padding:0px; border:0px; font-style:normal; font-variant-ligatures:normal; font-variant-caps:normal ; font-variant-numeric:inherit; font-variant-east-asian:inherit; font-weight:400; font-stretch:inherit; font-size:18px; line-height:inherit; font-family:couriernew、courier 、等幅;垂直配置:ベースライン;色:rgb(93、93、93);文字間隔:通常;孤立:2;テキスト配置:開始;テキストインデント:0px;テキスト変換:なし;ウィンドウ: 2;ワード間隔:0px; -webkit-text-stroke-width:0px; background-color:rgb(255、255、255); text-decoration-style:initial; text-decoration-color:initial; ">
<servlet>
<servlet-name>demo</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:springMVC.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>demo</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</ pre>
明らかに、サーブレットとサーブレットマッピングを構成する必要があります。
<init-param>構成により、初期化中にサーブレット構成ファイルをロードできます。
2.配置springMVC.xml
<pre style = "box-sizing:border-box; outline:none; margin:0px; padding:0px; border:0px; font-style:normal; font-variant-ligatures:normal; font-variant-caps:normal ; font-variant-numeric:inherit; font-variant-east-asian:inherit; font-weight:400; font-stretch:inherit; font-size:18px; line-height:inherit; font-family:couriernew、courier 、等幅;垂直配置:ベースライン;色:rgb(93、93、93);文字間隔:通常;孤立:2;テキスト配置:開始;テキストインデント:0px;テキスト変換:なし;ウィンドウ: 2;ワード間隔:0px; -webkit-text-stroke-width:0px; background-color:rgb(255、255、255); text-decoration-style:initial; text-decoration-color:initial; ">
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:context="http://www.springframework.org/schema/context"
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
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd">
<!-- 配置创建 springMVC要扫描的包 -->
<context:component-scan base-package="com.hust">
<context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
</context:component-scan>
<bean id="internalResourceViewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/pages/"/>
<property name="suffix" value=".jsp"/>
</bean>
<!-- 开启SpringMVC框架注解的支持 -->
<mvc:annotation-driven/>
</beans>
</ pre>
3. AccountControllerクラスを記述します
<pre style = "box-sizing:border-box; outline:none; margin:0px; padding:0px; border:0px; font-style:normal; font-variant-ligatures:normal; font-variant-caps:normal ; font-variant-numeric:inherit; font-variant-east-asian:inherit; font-weight:400; font-stretch:inherit; font-size:18px; line-height:inherit; font-family:couriernew、courier 、等幅;垂直配置:ベースライン;色:rgb(93、93、93);文字間隔:通常;孤立:2;テキスト配置:開始;テキストインデント:0px;テキスト変換:なし;ウィンドウ: 2;ワード間隔:0px; -webkit-text-stroke-width:0px; background-color:rgb(255、255、255); text-decoration-style:initial; text-decoration-color:initial; ">
@Controller
@RequestMapping("/account")
public class AccountController {
@Autowired
private IAccountService accountService;
@RequestMapping("/all")
public String findAllAccounts(){
return "success";
}
}
</ pre>
この時点で、springMVCが構成され、サーバーを起動します。/account/allパスにアクセスした後、success.jspページにジャンプすると、次の図が表示されます。
画像
これは、springMVC構成が成功したことを示しています
SpringはspringMVCを統合します
思考分析
この時点でサーバーが起動している場合、applicationContext.xmlファイルは読み込まれず、Springフレームワークは役割を果たさないため、@ AutoWireアノテーションを介して依存性注入を実装し、サービスレイヤーのメソッドを呼び出すことはできません。したがって、私たちのアイデアは、構成ファイルをロードし、サーバーの起動時にコンテナを作成することです。このようにして、プレゼンテーション層はビジネス層メソッドを呼び出すことができます。
実装
サーバーの起動後に構成ファイルが読み込まれ、コンテナが初期化されるようにリスナーを設定します。
最終的なweb.xmlファイルは次のとおりです。
<pre style = "box-sizing:border-box; outline:none; margin:0px; padding:0px; border:0px; font-style:normal; font-variant-ligatures:normal; font-variant-caps:normal ; font-variant-numeric:inherit; font-variant-east-asian:inherit; font-weight:400; font-stretch:inherit; font-size:18px; line-height:inherit; font-family:couriernew、courier 、等幅;垂直配置:ベースライン;色:rgb(93、93、93);文字間隔:通常;孤立:2;テキスト配置:開始;テキストインデント:0px;テキスト変換:なし;ウィンドウ: 2;ワード間隔:0px; -webkit-text-stroke-width:0px; background-color:rgb(255、255、255); text-decoration-style:initial; text-decoration-color:initial; ">
<!DOCTYPE web-app PUBLIC
"-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
"http://java.sun.com/dtd/web-app_2_3.dtd" >
<web-app>
<display-name>Archetype Created Web Application</display-name>
<!-- 配置spring提供的监听器,用于启动服务时加载容器。
该监听器会加载WEB-INF目录中名称为applicationContext.xml的配置文件-->
<listener>
<listener-class>
org.springframework.web.context.ContextLoaderListener
</listener-class>
</listener>
<!-- 指定spring配置文件的位置 -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml</param-value>
</context-param>
<servlet>
<servlet-name>demo</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:springMVC.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>demo</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>
</ pre>
同時に、AccountControllerクラスを変更します。
<pre style = "box-sizing:border-box; outline:none; margin:0px; padding:0px; border:0px; font-style:normal; font-variant-ligatures:normal; font-variant-caps:normal ; font-variant-numeric:inherit; font-variant-east-asian:inherit; font-weight:400; font-stretch:inherit; font-size:18px; line-height:inherit; font-family:couriernew、courier 、等幅;垂直配置:ベースライン;色:rgb(93、93、93);文字間隔:通常;孤立:2;テキスト配置:開始;テキストインデント:0px;テキスト変換:なし;ウィンドウ: 2;ワード間隔:0px; -webkit-text-stroke-width:0px; background-color:rgb(255、255、255); text-decoration-style:initial; text-decoration-color:initial; ">
@Controller
@RequestMapping("/account")
public class AccountController {
@Autowired
private IAccountService accountService;
@RequestMapping("/all")
public String findAllAccounts(){
List<Account> all = accountService.findAllAccount();
for (Account a:all
) {
System.out.println(a);
}
return "success";
}
}
</ pre>
success.jspページにアクセスすると、バックエンドは出力します
<pre style = "box-sizing:border-box; outline:none; margin:0px; padding:0px; border:0px; font-style:normal; font-variant-ligatures:normal; font-variant-caps:normal ; font-variant-numeric:inherit; font-variant-east-asian:inherit; font-weight:400; font-stretch:inherit; font-size:18px; line-height:inherit; font-family:couriernew、courier 、等幅;垂直配置:ベースライン;色:rgb(93、93、93);文字間隔:通常;孤立:2;テキスト配置:開始;テキストインデント:0px;テキスト変換:なし;ウィンドウ: 2;ワード間隔:0px; -webkit-text-stroke-width:0px; background-color:rgb(255、255、255); text-decoration-style:initial; text-decoration-color:initial; ">
Account{id=1, name='赵敏', money=99999.99}
Account{id=2, name='张无忌', money=11111.1}
Account{id=3, name='木婉清', money=22222.22}
</ pre>
コンテナーを介してaccountServiceオブジェクトを挿入でき、統合が成功したことを示しています。
PS:@RestControllerアノテーションを使用してAPIを実現し、フロントエンドとバックエンドを分離する効果を実現します。
概要
ポイントは主に、Springフレームワークを使用して他のフレームワークを統合する方法ですが、その中で、Springフレームワークの役割を理解することは非常に重要です。
質問がある場合や何か間違ったことを書いた場合は、メッセージを残して議論してください!