(Spring Boot)解決したよ!Doma、Dao、Autowired!
大苦戦していたORMにDoma2を使用したDBアクセス、ツイートしたらご指摘頂き無事実装することができました。御礼申し上げます。
(Spring Boot)助けてください!DaoがAutowiredされない、もう嫌だ。https://t.co/3dE17c21P9
— まさわ@β世界線 (@masawa200ok) 2019年3月30日
「Domaの実装クラスが生成されていないんだと思います。アノテーションプロセッサの設定を見直した方が良いです。」
とのこと。ドキュメントのURLも教えていただきました。
https://doma.readthedocs.io/en/stable/annotation-processing/
Setting options in Eclipse Select “Project > Properties” from the menu bar and open the dialog Select “Java Compiler > Annotation Processing” from the left menu of the dialog Add “Processor options”
Eclipseは日本語化してしまっています。
「Annotation Processing」は「注釈処理」と訳されています。「注釈処理」の配下メニューに「ファクトリー・パス」があるので、ここに使っているDomaのjarを追加します。
今回の場合、doma-2.16.1.jar
でした。
これでも動いたんですが、Twitterでソースに関してもレビュー頂いたので改めて修正ソースを記載します。修正部分ソース中にコメントしました。
@SpringBootApplication public class Doma2Application { public static void main(String[] args) { SpringApplication.run(Doma2Application.class, args); } }
@RestController //@ResponseBody <=いらないそうです public class DomaController { @Autowired private DomaService service; @GetMapping("/selectall") public List<DomaEntity> selectAll() { System.out.println("selectAll"); List<DomaEntity> list = service.selectAll(); return list; } }
@ConfigAutowireable @Dao public interface DomaDao { @Select public List<DomaEntity> selectAll(); }
import lombok.Data; @Entity @Data public class DomaEntity { Integer id; String name; }
//@Service <= いらないそうです。 public interface DomaService { public List<DomaEntity> selectAll(); }
@Service public class DomaServiceImpl implements DomaService { @Autowired private DomaDao dao; public List<DomaEntity> selectAll() { List<DomaEntity> list = dao.selectAll(); return list; } }
SQL SQLファイルの置き場所は、META-INF/com/springboot/dao/DomaDao/selectAll.sqlに置く。
select id , name from users ;
あとDB接続の設定。
# application.properties server.port = 8090 spring.datasource.url=jdbc:mysql://localhost:3306/test_db spring.datasource.username=root spring.datasource.password=password
Yes!!!