「Spring Boot」@Entity(エンティティ)、@Table(テーブル)、@Controller(コントローラ)アノテーションを使う方法

2020年10月23日

1.エンティティ(Entity)作成

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
@Entity
@Table(name="emp_tbl")
public class EmpData {
@Id
@Column(precision = 4,nullable = false,unqiue = false,columnDefinition="部署ID")
private Integer departmentId =null;
@Column(length =32,nullable = false,qunique = false,columnDefinition="ユーザ名")
private String userName = null;
public void setDepartmentId(Integer val) {
departmentId = val;
}
public void setUserName(String val) {
userName = val;
}
public Integer getDepartmentId(){
return departmentId;
}
public String getUserName(){
return userName;
}
}
@Entity @Table(name="emp_tbl") public class EmpData { @Id @Column(precision = 4,nullable = false,unqiue = false,columnDefinition="部署ID") private Integer departmentId =null; @Column(length =32,nullable = false,qunique = false,columnDefinition="ユーザ名") private String userName = null; public void setDepartmentId(Integer val) { departmentId = val; } public void setUserName(String val) { userName = val; } public Integer getDepartmentId(){ return departmentId; } public String getUserName(){ return userName; } }
@Entity
@Table(name="emp_tbl")
public class EmpData {
  @Id
  @Column(precision = 4,nullable = false,unqiue = false,columnDefinition="部署ID")
  private Integer departmentId =null;
  
  @Column(length =32,nullable = false,qunique = false,columnDefinition="ユーザ名")
  private String userName = null;

 public void setDepartmentId(Integer val) {
      departmentId = val;
 }
 public void setUserName(String val) {
      userName = val;
      }
  public Integer getDepartmentId(){
    return departmentId;
  }
  public String getUserName(){
    return userName;
  }
}

2.コントローラ(Controller)作成

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
@Controller
public class EmpController{
@Autowired
private EmpRep empRep;
@RequestMapping("/create")
public String create(Model model){
List<EmpData> emp = empRep.findAll();
model.addAttribute("empList", emp);
//some code
return "xxx";
}
}
@Controller public class EmpController{ @Autowired private EmpRep empRep; @RequestMapping("/create") public String create(Model model){ List<EmpData> emp = empRep.findAll(); model.addAttribute("empList", emp); //some code return "xxx"; } }
@Controller
public class EmpController{
  @Autowired
  private EmpRep empRep;

  @RequestMapping("/create") 
  public String create(Model model){
    List<EmpData> emp = empRep.findAll();
    model.addAttribute("empList", emp);
      //some code
    return "xxx";
  }
}

3.JSP画面にビューを表示

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<tr th:each="emp : ${empList}">
<td th:text="${emp.departmentId}"></td>
<td th:text="${emp.userName}"></td>
</tr>
<tr th:each="emp : ${empList}"> <td th:text="${emp.departmentId}"></td> <td th:text="${emp.userName}"></td> </tr>
<tr th:each="emp : ${empList}">
    <td th:text="${emp.departmentId}"></td>
    <td th:text="${emp.userName}"></td>
</tr>

 

Spring Boot

Posted by arkgame