Spring _jdbcTemplate_RowMapper_사용

JoMinJun·2021년 4월 5일
0

DAO - RowMapper 객체를 활용해 데이터를 빼오기 위해 사용하는 방법

@Repository
public class EmpDAO implements IEmpDAO {

	@Autowired
	JdbcTemplate jdbcTemplate;
	
	
	private class EmpMapper implements RowMapper<EemployeeVO>{
		@Override
		public EemployeeVO mapRow(ResultSet rs, int rowNum) throws SQLException {
			EemployeeVO emp = new EemployeeVO();
			emp.setEmployeeId(rs.getInt("EMPLOYEE_ID"));
			emp.setFirstName(rs.getString("FIRST_NAME"));
			emp.setLastName(rs.getString("LAST_NAME"));
			emp.setEmail(rs.getString("EMAIL"));
			emp.setPhoneNumber(rs.getString("PHONE_NUMBER"));
			emp.setHireDate(rs.getDate("HIRE_DATE"));
			emp.setJobId(rs.getString("JOB_ID"));
			emp.setSalary(rs.getDouble("SALARY"));
			emp.setCommissionPct(rs.getDouble("COMMISSION_PCT"));
			emp.setMangerId(rs.getInt("MANAGER_ID"));
			emp.setDepartmentId(rs.getInt("DEPARTMENT_ID"));
			return emp;
		}
		
		
	}

RowMapper를 사용하면 알아서 하나면 하나의 vo, 여러개면 list로 묶어서 반환 해줌

	@Override
	public List<EemployeeVO> getEemList() {
		String sql = "select * from employees";
		return jdbcTemplate.query(sql, new EmpMapper());
	}

	@Override
	public EemployeeVO getEmplist(int empId) {
	// TODO Auto-generated method stub
	String sql = "select * from employees where employee id=? ";
	return jdbcTemplate.queryForObject(sql, new EmpMapper(),empId);
	}

Controller

@RequestMapping(value = "hr/list")
	public void getAllEmps(Model model) {
		List<EemployeeVO> list = empService.getEemList();
	
	model.addAttribute("list",list);

	
	}
	@RequestMapping(value = "hr/{employeeId}")
	public String getinfo(@PathVariable int employeeId,Model model) {
	
		model.addAttribute("emp",empService.getEmplist(employeeId));
	
		return "hr/view";
	}

list veiw , 객체 하나 view

//list veiw
	<c:forEach var = "emp" items = "${list}">
	<tr>
	<th><a href = "/hr/${emp.employeeId }">${emp.employeeId }</a></th>
		<th>${emp.firstName }</th>
		<th>${emp.lastName }</th>
		<th>${emp.email }</th>
		<th>${emp.phoneNumber }</th>
		<th>${emp.hireDate }</th>
		<th>${emp.jobId }</th>
		<th>${emp.salary }</th>
		<th>${emp.commissionPct }</th>
		<th>${emp.mangerId }</th>
		<th>${emp.departmentId }</th>
	</tr>
	</c:forEach>
    
---------------------------------------------------------------------------------------------
 //객체 하나
	${emp.employeeId }의 정보<br>
	${emp.firstName } <br>
	${emp.lastName } <br>
	${emp.email } <br>
	${emp.phoneNumber } <br>
	${emp.hireDate } <br>
	${emp.jobId } <br>
profile
기술정리

0개의 댓글