シンプルでわかりやすい: SSM エントリーレベルのプロジェクト統合の例のチュートリアル + 添付のプロジェクト ソース コード


ようこそ ===フォロー===いいね=== コメントして、一緒に学び、一緒に進歩しましょう!

あなたのいいね、注目、コメントが私の創作の原動力です!

-------私の記事がお役に立てば幸いです-------

目次

I.はじめに

2. 推奨開発・動作環境

3. プロジェクトの基本構造

4. 通常の JAVAEE-WEB プロジェクトを作成する

 5. データベースを構築する

 6、pom.xml は依存関係を導入します

セブン、エンティティクラスを作成

 8. 3 層アーキテクチャーの対応するモジュールのクラスとインターフェースを作成する

9. Spring と Mybatis の統合

1.Spring 構成ファイル:

2.SpringとMybatisの統合構成

3. 統合テスト

4. 試験結果

Nine、Spring、SpringMVC の統合

1.スプリング構成

2.Spring MVC の構成

3. SSM フレームワーク統合テスト

10. 注意事項とバグフィードバック

1. コンソールの中国語文字化けソリューション:

 2. Mysql データベースの中国語文字化けソリューション:

 3. サーバーログの中国語の文字化けの解決策:

4. 適切なプロトコル例外エラーが発生しない

 5. java.lang.NoClassDefFoundError が表示される: javax/severlet/ error

11. まとめ



I.はじめに

  プログラミング レベルを向上させ、特定の方向性を理解して適用するための最速の方法は、実践的な練習、絶え間ないデバッグでの自己拷問、そしてあきらめることです。! ! (ジョーク)

この記事は、基本的な SSM フレームワーク ( Sping+SpringMVC+Mybatis )  の統合に関する単なるチュートリアルであり、発生する可能性のある問題に対する簡単な解決策です! プロジェクトを開始するだけでも当然、移行段階が必要になるため、初期段階では非常に困難になります。

  それでは早速始めましょう!

乾物は全行程面倒くさがらず、列車発進!!

最終試験結果:

2. 推奨開発・動作環境

1. システム: Windows 10

2、IDE:Interiljの理想 2021.3

3.サーバー:Tomacat-8(Severlet-4.0以降のバージョンに対応するとエラーになるので注意!!

4. データベース: MySQL5.7

5. ブラウザ: FireFox

6、JavaKit:JDK18

3. プロジェクトの基本構造

4. 通常の JAVAEE-WEB プロジェクトを作成する

1. [ファイル]、[新規]、[プロジェクト] をクリックします。

 2. JavaEnterpri プロジェクトを作成し、対応する JDK を選択して SSM という名前を付け、JavaEE8 を選択して終了します。

 5. データベースを構築する

1. SSM データベースと tb_book データ テーブルを作成する

create database SSM;
create table tb_book(
id int(11) primary key,
name varchar(20) not null,
press varchar(20) not null,
author varchar(20) not null);

 2. データの挿入

insert into tb_book(id,name,press,author) values(1,"Moving Earth","人民出版社","刘慈欣");

 6、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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>com.example</groupId>
    <artifactId>SSM</artifactId>
    <version>1.0-SNAPSHOT</version>
    <name>SSM</name>
    <packaging>war</packaging>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <maven.compiler.target>1.8</maven.compiler.target>
        <maven.compiler.source>1.8</maven.compiler.source>
        <junit.version>5.8.1</junit.version>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>5.2.8.RELEASE</version>
        </dependency>
        <!--Spring事务管理-->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-tx</artifactId>
            <version>5.2.8.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-jdbc</artifactId>
            <version>5.2.8.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-test</artifactId>
            <version>5.2.8.RELEASE</version>
        </dependency>
        <!--Spring MVC的相关依赖-->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-webmvc</artifactId>
            <version>5.2.8.RELEASE</version>
        </dependency>
        <!--MyBatis相关依赖-->
        <dependency>
            <groupId>org.mybatis</groupId>
            <artifactId>mybatis</artifactId>
            <version>3.5.2</version>
        </dependency>
        <!--MyBatis与Spring整合相关依赖-->
        <dependency>
            <groupId>org.mybatis</groupId>
            <artifactId>mybatis-spring</artifactId>
            <version>2.0.1</version>
        </dependency>
        <!--数据源-->
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>druid</artifactId>
            <version>1.1.20</version>
        </dependency>
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>javax.servlet-api</artifactId>
            <version>4.0.1</version>
        </dependency>
        <dependency>
            <groupId>org.apache.cxf</groupId>
            <artifactId>cxf-rt-frontend-jaxrs</artifactId>
            <version>3.4.5</version>
        </dependency>
        <dependency>
            <groupId>org.junit.jupiter</groupId>
            <artifactId>junit-jupiter-api</artifactId>
            <version>${junit.version}</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.junit.jupiter</groupId>
            <artifactId>junit-jupiter-engine</artifactId>
            <version>${junit.version}</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>javax.servlet.jsp</groupId>
            <artifactId>jsp-api</artifactId>
            <version>2.2</version>
            <scope>provided</scope>
        </dependency>
        <!-- 数据库驱动相关依赖-->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>8.0.11</version>
        </dependency>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.13</version>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <build>
        <finalName>SSM</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>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-war-plugin</artifactId>
                <version>3.3.2</version>
            </plugin>
        </plugins>
        <!-- 如果不添加此节点mybatis的mapper.xml文件都会被漏掉。 -->
        <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>


</project>

セブン、エンティティクラスを作成

src/main/java の下に com.iheima.domain パッケージを作成し、Book エンティティ クラスを作成します。

1-1Book.java

package com.itheima.domain;
public class Book {
    private Integer id;
    private String name;
    private  String press;
    private  String author;
    public Integer getId()
    {
        return id;
    }
    public void setId(Integer id){
        this.id=id;
    }
    public void setName(String name) {
        this.name = name;
    }
    public String getName() {
        return name;
    }
    public void setAuthor(String author) {
        this.author = author;
    }
    public String getAuthor() {
        return author;
    }
    public void setPress(String press) {
        this.press = press;
    }
    public String getPress() {
        return press;
    }
}

 8. 3 層アーキテクチャーの対応するモジュールのクラスとインターフェースを作成する

src/main/java の下に com.iheima.dao パッケージを作成し、BookMapper インターフェイスを作成します。

1-2 BookMapper.java

package com.itheima.dao;
import com.itheima.domain.Book;
public interface BookMapper {
         public Book findBookById(Integer id);
}

同じディレクトリにマッピング ファイルを作成します。

1-3BookMapper.xml

<?xml version="1.0" encoding="utf-8" ?>
<!DOCTYPE mapper
        PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.itheima.dao.BookMapper">
    <!--根据id查询图书信息 -->
    <select id="findBookById" parameterType="int"
            resultType="com.itheima.domain.Book">
  		select * from tb_book where id = #{id}
  	</select>
</mapper>

     src/main/java の下に com.iheima.service パッケージを作成し、BookService インターフェイスを作成します。

        1-4 BookService.java

package com.itheima.service;
import com.itheima.domain.Book;
public interface BookService {
    Book findBookById(Integer id);
}

src/main/java の下に com.iheima.service.Impl パッケージを作成し、実装クラス BookServiceImpl を作成します。

1-5 BookServiceImpl.java

package com.itheima.service.impl;
import com.itheima.dao.BookMapper;
import com.itheima.domain.Book;
import com.itheima.service.BookService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
@Service
public class BookServiceImpl implements BookService {
    @Autowired
    private BookMapper bookMapper;
    public Book findBookById (Integer id)
    {
        return bookMapper.findBookById(id);
    }
}

src/main/java の下に com.iheima.controller パッケージを作成し、BookController プロセッサ クラスを作成します。

1-5 BookController.java


package com.itheima.controller;
import com.itheima.domain.Book;
import com.itheima.service.BookService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
@Controller
public class BookController {
    @Autowired
    private BookService bookService;
    @RequestMapping("/book")
    public ModelAndView findBookById(Integer id)
    {
        Book book=bookService.findBookById(id);
        ModelAndView modelAndView=new ModelAndView();
        modelAndView.setViewName("book.jsp");
        modelAndView.addObject("book",book);
        return modelAndView;
    }
}

9. Spring と Mybatis の統合

1.Spring 構成ファイル:

src/main/resources ディレクトリに application-service.xml 構成ファイルを作成します。

2-1アプリケーションサービス.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.xsd
           http://www.springframework.org/schema/context
	       http://www.springframework.org/schema/context/spring-context.xsd
    	">
 <context:component-scan base-package="com.itheima.service"/>
</beans>

2.SpringとMybatisの統合構成

src/main/resources ディレクトリにプロパティ ソース ファイル jdbc.properties を作成して、MySQL データベースを設定します。

2-2 jdbc.プロパティ

jdbc.driverClassName=com.mysql.cj.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/ssm?useUnicode=true\
  &characterEncoding=utf-8&serverTimezone=Asia/Shanghai
jdbc.username=root
jdbc.password=123456

src/main/resources ディレクトリに application-dao.xml 構成情報を作成する

2-3 アプリケーションdao.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.xsd
 	       http://www.springframework.org/schema/context
 	       http://www.springframework.org/schema/context/spring-context.xsd
 	">
    <context:property-placeholder location="classpath:jdbc.properties"/>
    <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">
        <property name="driverClassName" value="${jdbc.driverClassName}"/>
        <property name="url" value="${jdbc.url}"/>
        <property name="username" value="${jdbc.username}"/>
        <property name="password" value="${jdbc.password}"/>
    </bean>
    <bean id="sqlSessionFactory"
          class="org.mybatis.spring.SqlSessionFactoryBean">
        <property name="dataSource" ref="dataSource"/>
    </bean>
    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        <property name="basePackage" value="com.itheima.dao"/>
    </bean>
</beans>

3. 統合テスト

src/test/java に BookServiceTest クラスを作成します。

2-4BookServiceTest.java

import com.itheima.domain.Book;
import com.itheima.service.BookService;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = {"classpath:application-service.xml","classpath:application-dao.xml"})
public class BookServiceTest {
@Autowired
    private BookService bookService;
@Test
    public void findBookById()
{
    Book book=bookService.findBookById(2);
    System.out.println("id:"+book.getId());
    System.out.println("name:"+book.getName());
    System.out.println("author:"+book.getAuthor());
    System.out.println("press:"+book.getPress());
}
}

4. 試験結果

テストの成功:

Nine、Spring、SpringMVC の統合

1.スプリング構成

プロジェクト webapp/WEB-INF の web.xml に次のコードを入力します。

3-1 web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
         version="4.0">
      <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>classpath:application-*.xml</param-value>
      </context-param>
      <listener>
        <listener-class>
          org.springframework.web.context.ContextLoaderListener
        </listener-class>
      </listener>
      <servlet>
        <servlet-name>DispatcherServlet</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>
      <servlet-mapping>
        <servlet-name>DispatcherServlet</servlet-name>
        <url-pattern>/</url-pattern>
      </servlet-mapping>
</web-app>

2.Spring MVC の構成

src/main/resources ディレクトリに Spring-mvc.xml 構成情報を作成する

 3-2 spring-mvc.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:mvc="http://www.springframework.org/schema/mvc"
       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">
    <context:component-scan base-package="com.itheima.controller"/>
    <mvc:annotation-driven/>
</beans>

3. SSM フレームワーク統合テスト

src/main/webapp の下に book.jsp ファイルを作成します。

3-4 book.jsp

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>图书信息查询</title>
</head>
<body>
<table border="1">
<tr>
    <th>图书id</th>
    <th>图书名称</th>
    <th>出版社</th>
    <th>作者</th>
</tr>
<tr>
    <td>${book.id}</td>
    <td>${book.name}</td>
    <td>${book.press}</td>
    <td>${book.author}</td>
</tr>
</table>
</body>
</html>

Tomcat8 を起動し、ブラウザに入力します

http://localhost:8082/SSM_war_exploded/book?id=1

 その中で、8082はポート番号、私のものは 8082、デフォルトは 8080 です。自分の構成に従って変更してください。

 結果:

10. 注意事項とバグフィードバック

1. コンソールの中国語文字化けソリューション:

Settings->Editor->File Encodings->図に示すように、3つの部分をGBKに変更できます

 2. Mysql データベースの中国語文字化けソリューション:

データベースの文字セットを変更します。

alter database SSM default convert to character set utf8;

 3. サーバーログの中国語の文字化けの解決策:

対応するTomcat-8ストレージアドレスを見つけ、confで見つけます

logging.properties ファイルで、すべての GBk をUTF-8に置き換えるだけです

 

4. 適切なプロトコル例外エラーが発生しない

対応する場所のjdkを見つけて、java.securityを入れます

dk.tls.disabledAlgorithms=SSLv3、TLSv1、TLSv1.1 が削除された後、

図のように724行目あたりです。削除後は図のようになります。

 

 5. java.lang.NoClassDefFoundError が表示される: javax/severlet/ error

これは、Tomcat の上位バージョンが Sevrlet の 4.0 バージョンをサポートしていないためです。Tomcat の下位バージョンを置き換えるだけです。

11. まとめ

 一般的に、WEBプロジェクトを立ち上げると、様々な理由で必ず様々なバグが発生し、最終的なプロジェクトが段階的に実行できるようになるまでに調査とデバッグに多くの時間がかかるため、考慮すべき点がたくさんあります.実行可能な解決策を分析するには、コンパイラがエラーを報告する理由を理解することが重要です。

 常に自分自身に練習と学習を促し、デバッグ能力と経験を蓄積することによってのみ、ゆっくりと進歩することができます!

投稿するのは簡単ではありません。大物に手を挙げてもらいたいです。


いいね: いいねは一種の美徳であり、上司による私の作成の認識です!


コメント:ホワイトコンタクトはありません、あなたと私のコミュニケーションの始まりです!


コレクション: もっと選んでくれませんか、それは私への上司の感謝です!

おすすめ

転載: blog.csdn.net/m0_55278347/article/details/129711245