국비수업/수업정리
스프링 / 4
밀김
2023. 6. 2. 15:26
728x90
< 오전 인터페이스 복습 >
package kr.co.rland.web.controller;
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.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import kr.co.rland.web.service.NoticeService;
@Controller
@RequestMapping("notice")
public class NoticeController {
//필드
@Autowired
private NoticeService service;
@GetMapping("list")
public String list(Model model) {
//컨트롤러의 리턴은 모두 스트링이다
List<Notice> list = service.getlist();
//노티스 서비스 구현체에서 겟리스트 함수로 데이터 받아올수 있음
return "notice/list";
//가상의 html경로..
}
}
package kr.co.rland.web.service;
import java.util.List;
public interface NoticeService {
//인터페이스에는 어노테이션 안단다
List<Notice> getlist();
//반환받을 필요 없음
}
package kr.co.rland.web.service;
import java.util.ArrayList;
import java.util.List;
import org.springframework.stereotype.Service;
@Service
//쓸 구현체한테 서비스 어노테이션 단다
public class NoticeServiceImp implements NoticeService{
//노티스 서비스의 구현체
@Override
public List<Notice> getlist() {
List<Notice> list = new ArrayList<>();
//데이터 담을 준비 완.
//이제 다오에서 데이터 받아와서~~하면됨
//Autowired는 받아야되는 쪽에 달아주는 어노테이션
return list;
}
}
< 오전 스프링 복습 >
- 파일 저장하는 방법
Map<String,String> map = new HashMap<>();
map.put("id", "1");
map.put("test", "test1");
Properties notice = new Properties();
notice.putAll(notice);
// notice.setProperty("id", "1");
// notice.setProperty("title", "hello");
// notice.setProperty("writerId", "newlec");
FileOutputStream fos = new FileOutputStream("test.properties");
notice.store(fos, null);
- 야물은 이런식으로 야물딱지게 쓰인다~~큭큭...계층만 같으면 됨. 근데 들여쓰기 잘 지켜야 오류가 안남
spring:
datasource:
dbcp2:
url: jdbc:mariadb://db.newlecture.com:3306/rlanddb
driver-class-name: org.mariadb.jdbc.Driver
username: 유저이름적기
password: 비번숫자적기
- 야물에 적고, 어노테이션 맵퍼 달면 서버가 실행됨
@Mapper
public interface MenuRepository {
@Select("select * from menu")
List<Menu> findAll();
}
- 서비스는 실제 구현되는걸 적으면 된다
- 좋아요 올리기
@GetMapping("likeup")
public String likeup(int id) {
service.likeUp(id);
//좋아요 해주는건 서비스에게 맡킴
return "menu/list";
//좋아도 된 상태의 웹을 반환해야함
}
- sql은 온전하게 어떠한 기능을 수행함
- 다오는 sql의 기능을 수반한다
<section class="menu" th:each="m : ${list}">
//리스트 목록 반복해서 꺼내오는 타임리프 구문
//타임리프에서 링크 연결하는것
<a href="detail" th:href="@{../menu/detail(id=${m.id})}">
728x90