CRUD処理追加
##CRUD
追加、更新、削除、検索ができるように修正します
##Spring Boot側
##DTO作成
dtoフォルダに以下の4つのファイルを作成します。
HomeRegistParamDto.java
package com.example.demo.dto;
import com.example.demo.entity.Employee;
public class HomeRegistParamDto {
public Employee employee;
}
HomeRegistResultDto.java
package com.example.demo.dto;
public class HomeRegistResultDto {
public boolean status;
}
HomeSearchParamDto.java
package com.example.demo.dto;
public class HomeSearchParamDto {
public Integer id;
public String name;
public String fromDate;
public String toDate;
public Integer departmentId;
}
HomeSearchResultDto.java
package com.example.demo.dto;
import java.util.List;
import com.example.demo.entity.EmployeeDepartment;
public class HomeSearchResultDto {
public List<EmployeeDepartment> employeeList;
}
###DAO修正
EmployeeDao.javaに追加、更新、削除、検索のためのメソッドを追加します。
EmployeeDao.java
package com.example.demo.dao;
import java.util.List;
import org.seasar.doma.Dao;
import org.seasar.doma.Delete;
import org.seasar.doma.Insert;
import org.seasar.doma.Select;
import org.seasar.doma.Update;
import org.seasar.doma.boot.ConfigAutowireable;
import com.example.demo.dto.HomeSearchParamDto;
import com.example.demo.entity.Employee;
import com.example.demo.entity.EmployeeDepartment;
@ConfigAutowireable
@Dao
public interface EmployeeDao {
@Select
List<EmployeeDepartment> selectAll();
@Select
List<EmployeeDepartment> selectByParams(HomeSearchParamDto searchParamDto);
@Insert(sqlFile = true)
int insert(Employee employee);
@Update(sqlFile = true)
int update(Employee employee);
@Delete(sqlFile = true)
int delete(List<Integer> deleteItemList);
}
###SQLファイル作成
これまでと同様に各メソッドの上で右クリック→Doma→Jump to SQLでSQLファイルを作成します。
selectByParams.sql
SELECT
T1.ID, T1.NAME AS employeeName, T2.NAME AS departmentName, DATE_FORMAT(BIRTHDAY,'%Y-%m-%d') AS birthday
FROM
EMPLOYEE T1
JOIN
DEPARTMENT T2
ON
T1.DEPARTMENT_ID = T2.ID
WHERE
/*%if searchParamDto.id != 0 */
T1.ID = /* searchParamDto.id */1
/*%end*/
/*%if !searchParamDto.name.isEmpty() */
AND T1.NAME LIKE /* @infix(searchParamDto.name) */'aaa'
/*%end*/
/*%if searchParamDto.departmentId != 0 */
AND T1.DEPARTMENT_ID = /*searchParamDto.departmentId*/1
/*%end*/
/*%if !searchParamDto.fromDate.isEmpty() && !searchParamDto.toDate.isEmpty()*/
AND BIRTHDAY BETWEEN /*searchParamDto.fromDate*/'2020-01-01' AND /*searchParamDto.toDate*/'2020-01-02'
/*%end*/
/*%if !searchParamDto.fromDate.isEmpty() && searchParamDto.toDate.isEmpty()*/
AND BIRTHDAY = /*searchParamDto.fromDate*/'2020-01-01'
/*%end*/
ORDER BY
T1.ID;
insert.sql
INSERT INTO
EMPLOYEE(name, department_id, birthday)
VALUES
(/* employee.name */'aaa',/*employee.department_id*/1, /*employee.birthday*/'1900/1/1');
update.sql
UPDATE
EMPLOYEE
SET
NAME = /* employee.name */'aaa',
DEPARTMENT_ID = /*employee.department_id*/2,
BIRTHDAY = /*employee.birthday*/'1999/1/1'
WHERE
ID = /* employee.id */1
delete.sql
DELETE FROM
EMPLOYEE
WHERE
ID IN /* deleteItemList */(1)
###Service修正
検索・追加・更新・削除処理を追加します。
EmployeeService.java
package com.example.demo.service;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.example.demo.dao.EmployeeDao;
import com.example.demo.dto.HomeSearchParamDto;
import com.example.demo.entity.Employee;
import com.example.demo.entity.EmployeeDepartment;
@Service
public class EmployeeService {
@Autowired
EmployeeDao employeeDao;
public List<EmployeeDepartment> selectAll() {
return employeeDao.selectAll();
}
public List<EmployeeDepartment> selectByParams(HomeSearchParamDto searchParamDto) {
return employeeDao.selectByParams(searchParamDto);
}
public int insert(Employee employee) {
return employeeDao.insert(employee);
}
public int update(Employee employee) {
return employeeDao.update(employee);
}
public int delete(List<Integer> delteItemList) {
return employeeDao.delete(delteItemList);
}
}
###Controller修正
登録・更新・削除・検索処理を追加します。
HomeController.java
package com.example.demo.controller;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import com.example.demo.dto.HomeInitResultDto;
import com.example.demo.dto.HomeRegistParamDto;
import com.example.demo.dto.HomeRegistResultDto;
import com.example.demo.dto.HomeSearchParamDto;
import com.example.demo.dto.HomeSearchResultDto;
import com.example.demo.entity.Employee;
import com.example.demo.service.DepartmentService;
import com.example.demo.service.EmployeeService;
@RestController
@RequestMapping(value = "/Home")
public class HomeController {
@Autowired
EmployeeService employeeService;
@Autowired
DepartmentService departmentService;
/**
* 初期化
* @return
*/
@RequestMapping(path="/init", method = RequestMethod.GET)
public HomeInitResultDto init() {
HomeInitResultDto resultDto = new HomeInitResultDto();
resultDto.employeeList = employeeService.selectAll();
resultDto.departmentList = departmentService.selectAll();
return resultDto;
}
/**
* 検索
* @param searchParamDto
* @return
*/
@RequestMapping(path = "/search", method = RequestMethod.POST)
public HomeSearchResultDto search(@RequestBody HomeSearchParamDto searchParamDto) {
HomeSearchResultDto resultDto = new HomeSearchResultDto();
resultDto.employeeList = employeeService.selectByParams(searchParamDto);
return resultDto;
}
/**
* 登録
* @param registParamDto
* @return
*/
@RequestMapping(path = "/", method = RequestMethod.POST)
public HomeRegistResultDto regist(@RequestBody HomeRegistParamDto registParamDto) {
HomeRegistResultDto resultDto = new HomeRegistResultDto();
employeeService.insert(registParamDto.employee);
resultDto.status = true;
return resultDto;
}
/**
* 更新
* @param employee
* @return
*/
@RequestMapping(path = "/", method = RequestMethod.PUT)
public HomeRegistResultDto update(@RequestBody Employee employee) {
HomeRegistResultDto resultDto = new HomeRegistResultDto();
employeeService.update(employee);
resultDto.status = true;
return resultDto;
}
/**
* 削除
* @param employee
* @return
*/
@RequestMapping(path = "/", method = RequestMethod.DELETE)
public HomeRegistResultDto delete(@RequestBody List<Integer> deleteItemList) {
HomeRegistResultDto resultDto = new HomeRegistResultDto();
employeeService.delete(deleteItemList);
resultDto.status = true;
return resultDto;
}
}
これでSpring側は完了です
次回はAngular側で登録・更新・削除・検索画面を作成します。
【Spring Boot + Angular + MySQLでWebアプリ作成】 CRUD編②【完成】へ