SpringBoot はデータベース情報を照会し、フロントデスクに表示します。

1. SpringBootの基本環境。

開発ツール: IntelliJ IDEA 2020.3.3 x64

JDKバージョン:jdk1.8.0_51

JREバージョン:jdk1.8.0_51

2. Spring Boot のインストールを開始します。

1) プロジェクトを右クリック > [新規] > [モジュール]

2) Spring Initializr をクリックし、モジュール SDK はデフォルトを使用できます。サービス URL はデフォルトの公式 Web サイトのアドレスを選択し、次へ

3) グループ名、アーティファクトプロジェクト名、選択した Java バージョン 8、パッケージ名、その他はデフォルトで、次へ

4) 「Web」をクリックし、「Spring Web」を選択し、必要に応じて Spring Boot バージョンを選択します。他のバージョンは選択しないで、「次へ」

 5) インストールを完了します

6) Finsh の後、公式 Web サイトから関連する jar パッケージを取得して待つ必要があります (ネットワークが良好でない場合、ネットワークがダウンしない可能性があります)。

 インストール後のプロジェクト構造は次のとおりです

2. アクセスするための簡単なページを作成します

1) まずパッケージ構造を作成し、Java の com.zzz 配下にコントローラー、マッパー、モデル、サービスを作成し、リソースにマッピング ディレクトリを作成します。

 com.zzz.down

資力

 作成後のパッケージ構造

2) 簡単なアクセス テスト用に、application.properties で Tomcat のポート番号を設定します。

注: アプリケーションには .yml と .properties という 2 つの構成方法があります。ファイルを選択し、Shift+f6 を押して名前を変更します。(ここでは最初に .yml メソッドを使用します)

3) コントローラー層に HelloController を作成する

コードの内容

@RestController は @Controller と @RequestMapping を組み合わせたものですが、ページをジャンプする場合は別々に使用します。

4) DemoApplication を通じて DemoApplication を直接実行し、テストのためにブラウザーを開きます。

5) Tomcat は正常に実行されています。

ブラウザを開いてhttp://localhost:8080/にアクセスします 。これは通常の動作です。

次に、hello URL パスに再度アクセスすると、訪問は終了です。

 6) application.yml を application.properties に変更してアクセスし、アプリケーションを修復した後に DemoApplication を再実行します。

リフレッシュしてから訪問するか、こんにちはということは問題がないことを証明します。

3. 次のステップは、Mybatis+springBoot にアクセスすることです。

1) パッケージ構造の作成 (2.1)

2) テーブルの作成とデータのテスト

ジェネレーターを介してマッパー、XML、およびモデル層構成ファイルを直接生成します (ジェネレーターは Baidu ネットワーク ディスクにアップロードされており、必要なものを抽出できます)

リンク: https://pan.baidu.com/s/14V0RSxpLYHAZiLhFUc61wQ?pwd=narc 
抽出コード: narc

 保存後、「cmd」と入力してコマンドラインを開き、プロダクションステートメントを実行します。

成功 

生成されたファイルを表示する

3) 関連ファイルを対応するレイヤーの下に置きます

4) ShopControllerの作成

ShopServiceの作成

インターフェースインターフェースを選択してください

サービス層の下に置く

5) サービス層の下に impl パッケージ (Page) を作成し、ShopServiceImpl を作成します

6) ShopController にクエリ コードを記述し (17 行の赤いコードは影響を受けません)、関連パッケージをインポートして関連メソッドを生成します (赤いコードに配置します)。

ShopServiceImpl でメソッドを生成し、クエリを作成する

 7) queryShopList メソッドをマッピング下の xml ファイルにコピーし、クエリ SQL を記述します 

 8) application.properties 構成を変更します。

server.port=8080 
mybatis.mapper-locations=classpath:mapping/*.xml 
mybatis.type-aliases-package=com.zzz.model 
#追加画像の配置
spring.web.resources.static-locations=file:${ web.web-root},file:${web.upload-path},classpath:/resources/,classpath:/static/,classpath:/templates/ 
spring.datasource.driver-class-name=com.mysql.jdbc .Driver 
spring.datasource.druid.initial-size=5 
spring.datasource.druid.max-active=20 
spring.datasource.druid.max-wait=60000 
spring.datasource.druid.min-evictable-idle-time-millis =300000 
spring.datasource.druid.min-idle=5 
spring.datasource.druid.test-on-borrow=false 
spring.datasource.druid.test-on-return=false 
spring.datasource.druid.test-while-idle =本当
spring.datasource.druid.time-between-eviction-runs-millis=60000 
spring.datasource.druid.validation-query=SELECT 1 
spring.datasource.druid.validation-query-timeout=2000 
spring.datasource.password=データベースパスワード
spring.datasource.url=jdbc:mysql://localhost:3306/database?useUnicode=true&characterEncoding=utf8&serverTimezone=UTC 
spring.datasource.username=database user 
spring.thymeleaf.cache=false 
#pagehelper 設定エラー処理
spring.main.allow -circular-references=true 
#ファイルアップロードの絶対パス
web.upload-path=D:/upload 
web.web-root=D:/upload 
#ページ分割プラグイン
pagehelper.helper-dialect=mysql 
pagehelper.params=count= countSql 
pagehelper.reasonable=true 
pagehelper.support-methods-arguments=true

9) pom.xml ファイルを修復します。

この内容を修正してください:

<?xml version="1.0"coding="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> 
    <parent> 
        <groupId>org.springframework.boot</groupId> 
        <artifactId>spring-boot-starter-parent</artifactId> 
        <version>2.6.12</version> 
        < relativePath/> <!-- リポジトリから親を検索 --> 
    </parent> 
    <groupId>com.zzz</groupId> 
    <artifactId>springboot-mybatis</artifactId> 
    <version>0.0.1-SNAPSHOT</version>
    <name>springboot-mybatis</name> 
            <groupId>org.mybatis.spring.boot</groupId>
    <description>Spring Boot のデモ プロジェクト</description> 
    <properties> 
        <java.version>1.8</java.version> 
    </properties> 
    <dependency> 
        <dependency> 
            <groupId>org.springframework.boot</groupId> 
            < artifactId>spring-boot-starter-web</artifactId> 
        </dependency> 
        <dependency> 
            <groupId>com.alibaba</groupId> 
            <artifactId>druid-spring-boot-starter</artifactId> 
            <version>1.1.22 </version> 
        </dependency> 
        <依存関係>artifactId>mybatis-spring-boot-starter</artifactId> 
            <version>1.3.2</version>
            <artifactId>mybatis-spring-boot-starter</artifactId> 
        </dependency> 
        <dependency> 
            <groupId>mysql</groupId> 
            <artifactId>mysql-connector-java</artifactId> 
            <version>5.1.30</version > 
            <scope>runtime</scope> 
        </dependency> 
        <dependency> 
            <groupId>org.springframework.boot</groupId> 
            <artifactId>spring-boot-starter-test</artifactId> 
            <scope>test</scope> 
        </dependency> 
        <dependency> 
            <groupId>org.springframework。boot</groupId> 
            <artifactId>spring-boot-starter-thymeleaf</artifactId>
        </dependency>  
        <依存関係> 
            <groupId>commons-fileupload</groupId>
            <artifactId>commons-fileupload</artifactId> 
            <version>1.3.1</version> 
        </dependency> 
        <dependency> 
            <groupId>commons-io</groupId> 
            <artifactId>commons-io</artifactId > 
            <version>2.4</version> 
        </dependency> 
        <dependency> 
            <groupId>commons-codec</groupId> 
            <artifactId>commons-codec</artifactId> 
            <version>1.9</version> 
        </dependency> 
        <dependency > 
            <グループ ID>org.sonatype。aether</groupId> 
            <artifactId>aether-spi</artifactId>
            <バージョン>1.7</バージョン> < 
        /依存関係> 
    </依存関係>

    <build> 
        <plugins> 
            <plugin> 
                <groupId>org.springframework.boot</groupId> 
                <artifactId>spring-boot-maven-plugin</artifactId> 
                <version>2.7.2</version> 
            </plugin> 
        < /plugins> 
    </build> 

</project>

右側の「Maven」をクリックして更新し、jar パッケージをプルします。

4. フロントエンド クエリ ページを作成します。

1) 関連する依存関係をインポートする

共通リンク: https://pan.baidu.com/s/1AuvSTxZ2OLbIUb3c2Y_-pg?pwd=narc 
抽出コード: narc

Common はリソースにコピーされます。

jsリンク: https://pan.baidu.com/s/10vAuNb8TLX-rAfuc8A1YPg?pwd=narc 
抽出コード: narc

jsは静的に配置されています。

2) テンプレートにショップフォルダーを作成します。

showShop.html ページを作成します。

<!DOCTYPE html> 
<html lang="ja" xmlns:th="http://www.thymeleaf.org"> 
<head> 
    <meta charset="UTF-8"> 
    <title>タイトル</title> 
< /head> 
<body> 
<table border="1"> 
    <tr> 
        <td>ID</td> 
        <td>名前</td> 
        <td>棚にありますか</td> 
        <td>タイプ< /td> 
        <td>対象者</td> 
        <td>日付</td> 
        <td>価格</td> 
        <td>写真</td> 
        <td>紹介</td> 
        <td>運営</ td> 
    < /tr> 
    <tr th:each="s:${list}"> 
        <td th:text="${s.shopid}"></td> 
        <td th:text="${s.shopname}"></た、>
        <td th:if="${s.shoponsale==1}">否</td> <td th:if="${s.shoponsale==2}">否</td> <td th 
        : if="${s.shoptype==1}">零食</td><td th:if="${s.shoptype==2}">饮料</td><td th:if="$ {s.shoptype==3}">生活用品</td> 
        <td> 
            <span th:if="${s.shopsuitable.contains('1')}">儿童</span> 
            <span th: if="${s.shopsuitable.contains('1')}">青年</span> 
            <span th:if="${s.shopsuitable.contains('1')}"> 壮年</span> 
            <span th:if="${s.shopsuitable.contains('1')}">老年</span> 
        </td> 
        <td th:text="${s.shopdate}"></td> 
        <td th:text="${s.shopprice}"></td> 
        <td> 
            <img th:src="${s.shopimg} " width="50px" height="50px"> 
        </td>
        <td th:text="${s.shopshow}"></td> 
        <td>操作</td> 
    </tr> 
</table> 
</body> 
</html>

 5. @MapperScan("com.zzz.mapper") を DemoApplication に追加します

Tomcatを実行する

ブラウザにアクセスします(クエリは完了しました。今回はクエリを 1 つだけ記述します)

6. 注: 上記の構成がすべてインストールされている場合、次の 2 つのエラーが報告される場合があります。

1) java: org.springframework.beans.factory.annotation パッケージが存在しません。

2)原因: org.springframework.beans.factory.UnsatisfiedDependencyException: 'shopsController' という名前の Bean 作成エラー: フィールド 'shopService' を通じて表現される満たされていない依存関係。找不到依赖

最初のエラーの解決策:

ファイル内の設定を選択します

  1. Maven を見つけて、常にスナップショットを更新することをオンにして、「適用」>「OK」を選択します。

最後に、pom.xml ファイルのすべての依存関係を切り取り、再度コピーして、インポートを更新します。(または、Idea を再起動します。再起動後は問題ありません)

2 番目の問題の解決策:

1. まず、Service 層クラスのアノテーションが追加されているかどうかを確認します (@Service である必要があります)。アノテーションが Spring クラスを参照しているかどうか、別のパッケージにインポートしていないこと、パッケージのパスを確認します。

2. また、Service 層がインターフェースと実装クラスに分かれている場合は、実装クラスにアノテーション (@Service) が付いているかどうか、実装クラスが存在するかどうかを確認します。

ShopServiceImpl実装クラスに@Serviceアノテーションを追加するのを忘れていましたが、追加することで解決しました。 

おすすめ

転載: blog.csdn.net/kking_Ran/article/details/127352709