###Spring Bootでテーブルからデータ取得したいです。
Spring Bootのサンプルを作成しまして、テストするとき、HTMLの初期画面が表示され、がエラーがありませんが、検索ボタンをクリックしても、データを画面に表示しません。
ログからSpringBootで自動生成したSQL文をコピーして、直接DBViewerに実行すると、3件のレコードを取得しました。
Eclipseのデバッグモードで調べつところ、repository.findAll()の実行結果は0件を取得したことがわかりました。
誰かが原因と解決策を教えていただけませんか?
■■環境:Eclipse4.6 java1.8 STS3.9.2 mySql lombok
###HTMLファイル
<!DOCTYPE html> <html xmlns:th="http://www.thymeleaf.org"> <head> <title>商品検索</title> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/> </head> <body> <form method="post" name="form1"> <table> <tr><td>商品ID : <input type="text" class="form-control" id="goods_id" name="goodsId" th:value="${goodsId}"/></td></tr> <tr><td>商品名 : <input type="text" class="form-control" id="goods_name" name="goodsName" th:value="${goodsName}"/></td></tr> <tr> <td>価格帯 : <input type="text" class="form-control" id="price_from" name="priceFrom" th:value="${priceFrom}"/> ~ <input type="text" class="form-control" id="price_to" name="priceTo" th:value="${priceTo}"/></td> </tr> <tr><td><input type="submit" value="検索"/></td></tr> </table> </form> <div th:if="${resultSize > 0}"><label th:text="${resultSize}"></label>件</div> <table border="1" th:if="${resultSize > 0}"> <tr> <td>商品ID</td> <td>商品名</td> <td>価格</td> </tr> <tr th:each="data : ${result}"> <td th:text="${data.goodsId}"/> <td th:text="${data.goodsName}"/> <td th:text="${data.price}"/> </tr> </table> </body> </html>
###Entityファイル
package jp.classmethod.entity; import java.math.BigDecimal; import javax.persistence.Column; import javax.persistence.Entity; import javax.persistence.Id; import javax.persistence.Table; import javax.validation.constraints.NotNull; import lombok.Getter; import lombok.Setter; @Entity @Table(name="goods_mst") public class Goods { @Id @Column(name="goods_id") @Getter @Setter private String goodsId; @Column(name="goods_name") @Getter @Setter private String goodsName; @NotNull @Getter @Setter private BigDecimal price; }
###Repositoryファイル
package jp.classmethod.repository; import org.springframework.data.jpa.repository.JpaRepository; import jp.classmethod.entity.Goods; public interface GoodsRepository extends JpaRepository<Goods, String> { }
###Serviceファイル
package jp.classmethod.service; import java.math.BigDecimal; import java.util.List; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import jp.classmethod.entity.Goods; import jp.classmethod.repository.GoodsRepository; import jp.classmethod.repository.GoodsRepositoryCustom; @Service public class GoodsService { @Autowired GoodsRepository repository; public List<Goods> search(String goodsId, String goodsName, BigDecimal priceFrom, BigDecimal priceTo) { List<Goods> result; result = repository.findAll(); return result; } }
###Controllerファイル
package jp.classmethod.controller; import java.math.BigDecimal; import java.util.List; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.context.annotation.ComponentScan; 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.classmethod.entity.Goods; import jp.classmethod.service.GoodsService; @ComponentScan @Controller @RequestMapping("/goods") public class GoodsController { private static final String VIEW = "/goods"; @Autowired public GoodsService service; @RequestMapping(method = RequestMethod.GET) public String index() { return VIEW; } @RequestMapping(method = RequestMethod.POST) public ModelAndView login(ModelAndView mav , @RequestParam("goodsId") String goodsId, @RequestParam("goodsName") String goodsName , @RequestParam("priceFrom") BigDecimal priceFrom, @RequestParam("priceTo") BigDecimal priceTo) { mav.setViewName(VIEW); mav.addObject("goodsId", goodsId); mav.addObject("goodsName", goodsName); mav.addObject("priceFrom", priceFrom); mav.addObject("priceTo", priceTo); List<Goods> result = service.search(goodsId, goodsName, priceFrom, priceTo); mav.addObject("result", result); mav.addObject("resultSize", result.size()); return mav; } }
###Application.ymlファイル
spring: datasource: url: jdbc:mysql://localhost/javatest?useSSL=true username: root password: Infra2013 driverClassName: com.mysql.jdbc.Driver jpa: hibernate: show-sql: true ddl-auto: update database-platform: org.hibernate.dialect.MySQLDialect thymeleaf: enabled: true cache: false messages: basename = messages cache-seconds = -1 encoding = UTF-8 endpoints: enabled: true logging: level: org: hibernate: SQL: DEBUG type: descriptor: sql: BasicBinder: TRACE
###ログから取得したSQL文
select goods0_.goods_id as goods_id1_0_, goods0_.goods_name as goods_na2_0_, goods0_.price as price3_0_ from goods_mst goods0_
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2018/01/09 13:57
2018/01/09 15:57