kr.or.connect.guestbook.service
kr.or.connect.guestbook.service.impl
GuestbookService.java
package kr.or.connect.guestbook.service;
import java.util.List;
import kr.or.connect.guestbook.dto.Guestbook;
public interface GuestbookService {
public static final Integer LIMIT = 5;
public List<Guestbook> getGuestbooks(Integer start);
public int deleteGuestbook(Long id, String ip);
public Guestbook addGuestbook(Guestbook guestbook, String ip);
public int getCount();
}
@Transactional
어노테이션 -> 전체 메소드가 예외 없이 실행되어야만 메소드 실행 완료, 만약 예외 발생 시 실행 취소됨GuestbookServiceImpl.java
package kr.or.connect.guestbook.service.impl;
import java.util.Date;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import kr.or.connect.guestbook.dao.GuestbookDao;
import kr.or.connect.guestbook.dao.LogDao;
import kr.or.connect.guestbook.dto.Guestbook;
import kr.or.connect.guestbook.dto.Log;
import kr.or.connect.guestbook.service.GuestbookService;
@Service //Service 레이어임을 명시
public class GuestbookServiceImpl implements GuestbookService {
@Autowired // 자동으로 객체를 생성해 주입시켜줌
GuestbookDao guestbookDao;
@Autowired
LogDao logDao;
@Override
@Transactional // Read only 형태로 connection 사용함
public List<Guestbook> getGuestbooks(Integer start) {
//Guestbook 목록을 가져오는 메소드
List<Guestbook> list = guestbookDao.selectAll(start, LIMIT);
return list;
}
@Override
@Transactional(readOnly=false) //수정 가능한 형태로 connection 사용함
public int deleteGuestbook(Long id, String ip) {
//id에 해당하는 값을 delete, delete되었을 때 log에 저장
int deleteCount = guestbookDao.deleteById(id);
Log log = new Log();
log.setId(id);
log.setIp(ip);
log.setMethod("delete");
log.setRegdate(new Date());
logDao.insert(log);
return deleteCount;
}
@Override
@Transactional(readOnly=false)
public Guestbook addGuestbook(Guestbook guestbook, String ip) {
// regdate 값은 가져오지 않았으므로 따로 설정해줌
guestbook.setRegdate(new Date());
Long id = guestbookDao.insert(guestbook);
guestbook.setId(id);
Log log = new Log();
log.setIp(ip);
log.setMethod("insert");
log.setRegdate(new Date());
logDao.insert(log);
// 입력 완료된 guestbook 반환
return guestbook;
}
@Override
@Transactional
public int getCount() {
// 페이징 처리를 위해 전체의 건수를 가져옴
return guestbookDao.selectCount();
}
}
package kr.or.connect.guestbook.service.impl;
import java.util.Date;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import kr.or.connect.guestbook.config.ApplicationConfig;
import kr.or.connect.guestbook.dto.Guestbook;
import kr.or.connect.guestbook.service.GuestbookService;
public class GuestbookServiceTest {
public static void main(String[] args) {
ApplicationContext ac = new AnnotationConfigApplicationContext(ApplicationConfig.class);
GuestbookService guestbookService = ac.getBean(GuestbookService.class);
Guestbook guestbook = new Guestbook();
guestbook.setName("kim");
guestbook.setContent("test");
guestbook.setRegdate(new Date());
Guestbook result = guestbookService.addGuestbook(guestbook, "127.0.0.1");
System.out.println(result);
}
}