Mapper 객체를 이용해서 공지 목록 출력하기
list객체를 service객체를 통해 가져온다.
NoticeController.java
NoticeController.java
package com.newlecture.web.controller.customer;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import com.newlecture.web.entity.Notice;
import com.newlecture.web.service.NoticeService;
//@RestController 요청한 모든것들 문자열로 반환 Controller는 뷰페이지만 인식
@Controller
@RequestMapping("/customer/notice/")
public class NoticeController {
@Autowired
private NoticeService service;
@RequestMapping("list") //list,detail,edit,reg
public String list(Model model){
List<Notice> list = service.getList();
model.addAttribute("list", list);
//return "customer/notice/list";
return "customer.notice.list"; // TilesVeiwResolver
}
@RequestMapping("detail") //list,detail,edit,reg
public String detail(){
return "customer.notice.detail";
}
}
실제 존제할수 있도록(IOC 컨테이너에 담아주기 위해)
NoticeServiceImp 구현체로 이동해서
@Service 적용
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;
@Service
public class NoticeServiceImp implements NoticeService{
@Autowired
private NoticeDao noticeDao;
@Override
public List<Notice> getList() {
List<Notice> list = noticeDao.getList();
return list;
}
@Override
public Notice get(int id) {
Notice notice = noticeDao.get(id);
return notice;
}
}
NoticeDao를 구현한 구현체
MyBatis @Mapper 사용 -> 재구성해서 IOC에 담아준다.
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;
@Mapper
public interface NoticeDao {
@Select("select * from notice")
List<Notice> getList();
Notice get(int id);
}
Notice 객체 구현하기
MySQL 값 넣기
spring datasource DB연결
application.properties
spring.mvc.view.prefix=/WEB-INF/view/
spring.mvc.view.suffix=.jsp
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost:3306/springboot?serverTimezone=Asia/Seoul
spring.datasource.username=spring
spring.datasource.password=111
Notice.java
package com.newlecture.web.entity;
import java.util.Date;
public class Notice {
private int id;
private String title;
private String content;
private Date regdate;
private int hit;
private boolean pub;
private int memberId;
public Notice() {
// TODO Auto-generated constructor stub
}
public Notice(int id, String title, String content, Date regdate, int hit, boolean pub, int memberId) {
super();
this.id = id;
this.title = title;
this.content = content;
this.regdate = regdate;
this.hit = hit;
this.pub = pub;
this.memberId = memberId;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getContent() {
return content;
}
public void setContent(String content) {
this.content = content;
}
public Date getRegdate() {
return regdate;
}
public void setRegdate(Date regdate) {
this.regdate = regdate;
}
public int getHit() {
return hit;
}
public void setHit(int hit) {
this.hit = hit;
}
public boolean isPub() {
return pub;
}
public void setPub(boolean pub) {
this.pub = pub;
}
public int getMemberId() {
return memberId;
}
public void setMemberId(int memberId) {
this.memberId = memberId;
}
@Override
public String toString() {
return "Notice [id=" + id + ", title=" + title + ", content=" + content + ", regdate=" + regdate + ", hit="
+ hit + ", pub=" + pub + ", memberId=" + memberId + "]";
}
}
list.jsp taglib core사용
서버 실행