
こんにちは、私は現在springbootでwebアプリを作成するために、掲題の書籍でデータベースを勉強中です。
2.8演習エンティティーの作成で詰まってしまっています。
起こっている事:
実行は成功しているのですが、データベースのVALUESを取ってこれません。
実際の値画像:
期待値:
resorses配下のdata.sqlが読み込まれて出力される
html
1<!DOCTYPE html> 2<html xmlns="http://www.w3.org/1999/xhtml" 3 xmlns:th="http://www.thymeleaf.org"> 4<head> 5<meta charset="UTF-8"> 6<style type="text/css"> 7<!-- 8table { 9 border-collapse: collapse; 10} 11th, td { 12 border: 1px darkgray solid; 13} 14--> 15</style> 16</head> 17<body> 18<table> 19 <thead> 20 <tr> 21 <th>No</th> 22 <th>First Name</th> 23 <th>Last Name</th> 24 <th>Sex</th> 25 <th>Birthday</th> 26 <th>Mail Address</th> 27 </tr> 28 </thead> 29 <tbody> 30 <tr th:each="userdata : *{userdatas}"> 31 <td th:text="${userdata.no}" /> 32 <td th:text="${userdata.firstName}" /> 33 <td th:text="${userdata.lastName}" /> 34 <td th:text="${userdata.sex}" /> 35 <td th:text="${userdata.birthday}" /> 36 <td th:text="${userdata.mailAddress}" /> 37 </tr> 38 </tbody> 39</table> 40</body> 41</html> 42
html
1INSERT INTO userdata 2 (no, birthday, first_name, last_name, mail_address, sex ) 3 VALUES 4 (1,'1980-01-31','太郎','山田','taro.yamada@mail.com','male'); 5INSERT INTO userdata 6 (no, birthday, first_name, last_name, mail_address, sex ) 7 VALUES 8 (2,'1985-02-10','一郎','鈴木','ichiro.suzuki@mail.com','male'); 9INSERT INTO userdata 10 (no, birthday, first_name, last_name, mail_address, sex ) 11 VALUES 12 (3,null,'花子','石井',null,'female');
contoroller
1package com.spring.data.jpa.example.controller; 2 3import org.springframework.beans.factory.annotation.Autowired; 4import org.springframework.stereotype.Controller; 5import org.springframework.ui.Model; 6import org.springframework.web.bind.annotation.RequestMapping; 7 8import com.spring.data.jpa.example.service.UserDataService; 9 10 11@Controller 12@RequestMapping("userdata") 13public class UserDataController { 14 15 @Autowired 16 private UserDataService userDataService; 17 18 @RequestMapping("list") 19 public String list(Model model) { 20 model.addAttribute("userdatas",userDataService.getUserDataList()); 21 22 return "userdata/list"; 23 24 } 25 26}
service
1package com.spring.data.jpa.example.service; 2 3import java.util.List; 4 5import javax.persistence.EntityManager; 6import javax.persistence.PersistenceContext; 7 8import org.springframework.stereotype.Service; 9import org.springframework.transaction.annotation.Transactional; 10 11import com.spring.data.jpa.example.repository.entity.UserData; 12@Service 13@Transactional(readOnly=true) 14public class UserDataServiceImpl implements UserDataService{ 15 @PersistenceContext 16 private EntityManager entityManager; 17 18 @Override 19 public List<UserData> getUserDataList() { 20 return entityManager.createQuery("SELECT e FROM UserData e",UserData.class).getResultList(); 21 22 23 } 24 25} 26
entity
1package com.spring.data.jpa.example.repository.entity; 2 3import java.io.Serializable; 4import java.sql.Date; 5 6import javax.persistence.Column; 7import javax.persistence.Entity; 8import javax.persistence.EnumType; 9import javax.persistence.Enumerated; 10import javax.persistence.GeneratedValue; 11import javax.persistence.Id; 12 13import lombok.Getter; 14import lombok.Setter; 15import lombok.ToString; 16 17 18@Entity 19@Getter 20@Setter 21@ToString 22public class UserData implements Serializable { 23 private static final long serialVersionUID = 1L; 24 25 public static enum Sex{ 26 male,female 27 } 28 29 @Id 30 @GeneratedValue 31 private Integer no; 32 33 @Column(length = 20, nullable = false) 34 private String firstName; 35 36 @Column(length = 20, nullable = false) 37 private String lastName; 38 39 @Column(length = 10) 40 @Enumerated(EnumType.STRING) 41 private Sex sex; 42 43 private Date birthday; 44 45 @Column(unique=true) 46 private String mailAddress; 47 48 public Date getBirthday() { 49 return birthday; 50 } 51 52 public void setBirthday(Date birthday) { 53 this.birthday = birthday; 54 } 55 56} 57
階層:
ログ
2019-01-16 18:51:18.867 INFO 4117 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat started on port(s): 8080 (http) with context path '' 2019-01-16 18:51:18.871 INFO 4117 --- [ main] c.s.d.j.e.SpringDataJpaApplication : Started SpringDataJpaApplication in 3.59 seconds (JVM running for 4.201) 2019-01-16 18:51:21.552 INFO 4117 --- [nio-8080-exec-1] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring DispatcherServlet 'dispatcherServlet' 2019-01-16 18:51:21.552 INFO 4117 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet : Initializing Servlet 'dispatcherServlet' 2019-01-16 18:51:21.559 INFO 4117 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet : Completed initialization in 7 ms 2019-01-16 18:51:21.665 INFO 4117 --- [nio-8080-exec-1] o.h.h.i.QueryTranslatorFactoryInitiator : HHH000397: Using ASTQueryTranslatorFactory 2019-01-16 18:51:21.751 DEBUG 4117 --- [nio-8080-exec-1] org.hibernate.SQL : select userdata0_.no as no1_0_, userdata0_.birthday as birthday2_0_, userdata0_.first_name as first_na3_0_, userdata0_.last_name as last_nam4_0_, userdata0_.mail_address as mail_add5_0_, userdata0_.sex as sex6_0_ from userdata userdata0_ Hibernate: select userdata0_.no as no1_0_, userdata0_.birthday as birthday2_0_, userdata0_.first_name as first_na3_0_, userdata0_.last_name as last_nam4_0_, userdata0_.mail_address as mail_add5_0_, userdata0_.sex as sex6_0_ from userdata userdata0_
他に必要な情報があれば教えてください!データベース名を変更しているだけで他は写経しているだけなのでこれで成功するはずなのですが....。

回答2件
あなたの回答
tips
プレビュー