springboot 起動時のエラー報告とインターフェース注入失敗について

springboot 起動エラー報告に関するいくつかの質問

  • 起動エラー問題

    最初の起動は、起動クラスに加えて @SpringBootApplication(exclude{DataSourceAutoConfiguration.class}) にある必要があります

@SpringBootApplication(exclude{
    
    DataSourceAutoConfiguration.class})
  • 次に、dao またはマッパーでのインターフェイスの挿入に失敗しました. com.example.controller.HelloController のフィールド personProperties には、見つからないタイプ「com.example.PersonProperties」の Bean が必要でした。
  1. dao レイヤーまたはマッパーのインターフェースに @Mapper および @Component を追加します。
package com.example.demo.Dao;

import com.example.demo.bean.Account;
import org.apache.ibatis.annotations.Mapper;
import org.springframework.stereotype.Component;

import java.util.List;
@Mapper
@Component
public interface accoutDao {
    
    
    List<Account> findAll();
}

次に、インポートされたパッケージは対応するものをインポートする必要があります

  1. @ComponentScan(basePackages = "com.example") をスタートアップ クラスに追加して、example の下のすべてのパッケージをスキャンし、削除します

@SpringBootApplication(exclude{DataSourceAutoConfiguration.class})

次の exclude{DataSourceAutoConfiguration.class} は、データを自動的に挿入します。コードは以下のように表示されます:

package com.example.demo;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ComponentScan;

@SpringBootApplication()
@ComponentScan(basePackages = "com.example")
public class DemoApplication {
    
    

	public static void main(String[] args) {
    
    
		SpringApplication.run(DemoApplication.class, args);
	}

}

以下に添付されているのは、私のディレクトリと各レイヤーのコードです
ここに画像の説明を挿入

エンティティークラス Account.java

package com.example.demo.bean;

public class Account {
    
    
    private Integer id;
    private String user;
    private String psw;

    public Integer getId() {
    
    
        return id;
    }

    public void setId(Integer id) {
    
    
        this.id = id;
    }

    public String getUser() {
    
    
        return user;
    }

    public void setUser(String user) {
    
    
        this.user = user;
    }

    public String getPsw() {
    
    
        return psw;
    }

    public void setPsw(String psw) {
    
    
        this.psw = psw;
    }
}

DAO 層 accoutDao.java

package com.example.demo.Dao;

import com.example.demo.bean.Account;
import org.apache.ibatis.annotations.Mapper;
import org.springframework.stereotype.Component;

import java.util.List;
@Mapper
@Component
public interface accoutDao {
    
    
    List<Account> findAll();
}

マッパーの accountMapper.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.example.demo.Dao.accoutDao">

    <select id="findAll" resultType="com.example.demo.bean.Account">
        SELECT * FROM account
    </select>

</mapper>

サービス層 accountService.java

package com.example.demo.Service;

import com.example.demo.bean.Account;

import java.util.List;

public interface accountService {
    
    
    List<Account> findAll();
}

クラス accountServiceImp.java を実装する

package com.example.demo.serviceImp;

import com.example.demo.Dao.accoutDao;
import com.example.demo.Service.accountService;
import com.example.demo.bean.Account;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
@Service
public class accountServiceImp implements accountService {
    
    

    @Autowired
    private accoutDao ado;

    @Override
    public List<Account> findAll() {
    
    
        return ado.findAll();
    }
}

制御レイヤーの helloController.java は、ここでは @RestController アノテーションを使用してコンテンツをブラウザーに直接入力します。

package com.example.demo.Controller;

import com.example.demo.Dao.accoutDao;
import com.example.demo.Service.accountService;
import com.example.demo.bean.Account;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
public class helloController {
    
    

    @Autowired
    private accountService aService;

    @RequestMapping("/hello")
    public List hello(){
    
    
        return aService.findAll() ;
    }
}

結果
ここに画像の説明を挿入
構成ファイル application.ymlを実行します。

spring:
  datasource:
    url: jdbc:mysql://localhost:3306/testspringboot
    username: root
    password: haroot
    driver-class-name: com.mysql.jdbc.Driver
server:
  port: 8083
mybatis:
  mapper-locations: classpath:Mapper/*.xml
  type-aliases-package: com.example.demo.bean

上記は、私が springboot を学習する過程で遭遇したすべての問題です. 質問がある場合は、コメント欄にメッセージを残してください. 一緒に議論して交換することができます.

おすすめ

転載: blog.csdn.net/qq_44874270/article/details/112347773