Spring Boot | Mapper 객체 | 공지사항 목록 출력 예제 | @Mapper | MySQL 연결 | MyBatis
kjh95.tistory.com/336 Spring Boot | MyBatis 설정 | Service와 Dao 준비 | MySQL 데이터베이스 연결 | Dao의 구현체 구현 | 스프링의 MyBatis 설정 MyBatis 가 담당하는 역할 Mybatis 라이브러리 다운로드 Serv..
kjh95.tistory.com
Notice View 만들기
id값으로 출력되는 작성자 이름을 name값으로 출력하기(정상적으로 출력하기)
Notice View 만들기
create view NoticeView
as
select n.*, m.name memberName
from notice n
join member m on n.memberId = m.id;
테이블 이름에 맞는 entity 생성하기
NoticeDao.java
NoticeView 만들어주기
생성자와 부모 클래스로부터 필드 만들기
memberName 추가, 초기화 geter, setter
NoticeView.java
package com.newlecture.web.entity;
import java.util.Date;
public class NoticeView extends Notice {
private String memberName;
public NoticeView() {
// TODO Auto-generated constructor stub
}
public NoticeView(int id, String title, String content, Date regdate, int hit, boolean pub, int memberId, String memberName) {
super(id, title, content, regdate, hit, pub, memberId);
this.memberName = memberName;
}
public String getMemberName() {
return memberName;
}
public void setMemberName(String memberName) {
this.memberName = memberName;
}
@Override
public String toString() {
return "NoticeView [memberName=" + memberName + ", getId()=" + getId() + ", getTitle()=" + getTitle()
+ ", getContent()=" + getContent() + ", getRegdate()=" + getRegdate() + ", getHit()=" + getHit()
+ ", isPub()=" + isPub() + ", getMemberId()=" + getMemberId() + ", toString()=" + super.toString()
+ ", getClass()=" + getClass() + ", hashCode()=" + hashCode() + "]";
}
}
NoticeDao.java
package com.newlecture.web.dao;
import java.util.List;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;
import com.newlecture.web.entity.Notice;
import com.newlecture.web.entity.NoticeView;
@Mapper
public interface NoticeDao {
@Select("select * from noticeview")
List<NoticeView> getList();
Notice get(int id);
}
객체 NoticeView로 변경
NoticeServiceImp.java
NoticeService 변경
NoticeServiceImp.java
package com.newlecture.web.service;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.newlecture.web.dao.NoticeDao;
import com.newlecture.web.entity.Notice;
import com.newlecture.web.entity.NoticeView;
@Service
public class NoticeServiceImp implements NoticeService{
@Autowired
private NoticeDao noticeDao;
@Override
public List<NoticeView> getList() {
List<NoticeView> list = noticeDao.getList();
return list;
}
@Override
public Notice get(int id) {
Notice notice = noticeDao.get(id);
return notice;
}
}
NoticeService.java
package com.newlecture.web.service;
import java.util.List;
import com.newlecture.web.entity.Notice;
import com.newlecture.web.entity.NoticeView;
public interface NoticeService {
List<NoticeView> getList();
Notice get(int id);
}
컨트롤러 수정
NoticeController.java
뷰 변경
list.jsp
서버 실행 작성자 출력 확인
데이터베이스의 칼럼과 entity 사이의 이름이 다를 경우 처리방법
칼럼명이 _로 구분되어 있을 경우
MyBatis Annotation 사용
@Result 사용
NoticeDao.java
package com.newlecture.web.dao;
import java.util.List;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Result;
import org.apache.ibatis.annotations.Select;
import com.newlecture.web.entity.Notice;
import com.newlecture.web.entity.NoticeView;
@Mapper
public interface NoticeDao {
@Result(property = "memberName", column = "member_Name")
@Select("select * from noticeview")
List<NoticeView> getList();
Notice get(int id);
}
다수의 칼럼이 맞지 않을 경우
@Results 사용
서버 실행 정상출력 확인