###前提
- OS:Windows10
- 言語:java version 1.8.0_151
- フレームワーク:Spring Boot
- DB:MySQL
###困っていること
- Spring boot + domaの実行時にエラーとなる
1.下記URLを参考にSpring boot + DomaでDBアクセスするアプリを作成
https://qiita.com/NagaokaKenichi/items/4c680ec9b31725687e04
2.Spring Bootを実行するとエラーが発生(mavenによるコンパイルはOK)
3.下記サイトを見るとパッケージ構成を変更する必要があるようだったので構成を変更
http://www.saka-en.com/java/spring-boot-doma2-oracle/
https://teratail.com/questions/38462
4.再度実行したがエラーが発生。エラー内容は以下の通り
Error starting ApplicationContext. To display the auto-configuration report re-run your application with 'debug' enabled. 2018-01-12 03:36:08.638 ERROR 19288 --- [ main] o.s.b.d.LoggingFailureAnalysisReporter : *************************** APPLICATION FAILED TO START *************************** Description: Field sampleRepository in jp.fbdb.web.domain.service.SampleService required a bean of type 'jp.fbdb.web.domain.repository.SampleRepository' that could not be found. Action: Consider defining a bean of type 'jp.fbdb.web.domain.repository.SampleRepository' in your configuration.
パッケージ構成とソースは以下になります。
- パッケージ構成
- FbdbWebApplication
package jp.fbdb.web; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication public class FbdbWebApplication { public static void main(String[] args) { SpringApplication.run(FbdbWebApplication.class, args); } }
- SampleController
package jp.fbdb.web.controller; import java.util.List; 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.RequestMethod; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.servlet.ModelAndView; import jp.fbdb.web.domain.model.Sample; import jp.fbdb.web.domain.service.SampleService; @Controller public class SampleController { //----------------------------- //DI //----------------------------- @Autowired SampleService sampleService; //----------------------------- //画面 //----------------------------- @RequestMapping(value="/a", method=RequestMethod.GET) public ModelAndView index(ModelAndView mav) { mav.setViewName("index"); mav.addObject("msg", "input your name :"); // 表示メッセージ return mav; } @RequestMapping(value="/", method=RequestMethod.POST) public ModelAndView send(@RequestParam("name")String name, ModelAndView mav) { mav.setViewName("index"); mav.addObject("msg", "Hello " + name + " !"); // 表示メッセージ mav.addObject("value", name); // 入力テキストに入力値を表示 return mav; } @RequestMapping(value="/sample") public String sample() { List<Sample> sampleList = sampleService.getSampleList(); for(Sample e : sampleList) { System.out.println(e.id + ":" + e.name); } return "sample"; } }
- SampleService
package jp.fbdb.web.domain.service; import java.util.List; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; import jp.fbdb.web.domain.model.Sample; import jp.fbdb.web.domain.repository.SampleRepository; @Service @Transactional public class SampleService { @Autowired SampleRepository sampleRepository; public List<Sample> getSampleList(){ return sampleRepository.selectAll(); } }
- SampleRepository
package jp.fbdb.web.domain.repository; import java.util.List; import org.seasar.doma.Dao; import org.seasar.doma.Select; import org.seasar.doma.boot.ConfigAutowireable; import jp.fbdb.web.domain.model.Sample; @Dao @ConfigAutowireable public interface SampleRepository { @Select List<Sample> selectAll(); }
- Sample
package jp.fbdb.web.domain.model; import org.seasar.doma.Entity; import org.seasar.doma.Id; import org.seasar.doma.Table; import lombok.Data; @Data @Entity @Table(name = "sample") public class Sample { /** ID **/ @Id public Integer id; /** 名前 **/ public String name; }
回答1件
あなたの回答
tips
プレビュー