★프로젝트 생성★
<dependencies>
Lombok
JDBC
Mysql
Thymeleaf
Spring Web
Spring Boot DevTools
Spring Data Jpa
MyBatis Framework
-------------------------------------------------------------------------------------------
★build.gradle★
implementation group: 'mysql', name: 'mysql-connector-java', version: '8.0.29'
-------------------------------------------------------------------------------------------
★application.properties★
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost:3306/jsptest?useSSL=false&characterEncoding=UTF-8&allowPublicKeyRetrieval=true&serverTimezone=UTC
spring.datasource.username=root
spring.datasource.password=rootpw
spring.jpa.hibernate.ddl-auto=update (테이블 생성할때는 create)
spring.jpa.properties.hibernate.format_sql=true
spring.jpa.show-sql=true
-------------------------------------------------------------------------------------------
1. controller 패키지 만들기
(1). HelloController.java 만들기
@RestController
public class HelloController {
@GetMapping("/hello")
String root() {
return "안녕!";
}
}
-> localhost:8080/hello 실행 후 출력되면 연결 성공
(2). MyController.java 만들기
@Controller
public class MyController {
@GetMapping("/")
String index() {
return "index";
}
}
(2)-1. templates 폴더에 index.html 만들기
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<h1>나왔다!</h1>
</body>
</html>
-> localhost:8080/ 실행 후 출력되면 성공
-------------------------------------------------------------------------------------------
1. entity 패키지 만들고, 생성 할 테이블을 클래스로 만든다. (ex test_fellow)
@Entity
@Table(name = "test_fellow")
@ToString
@Getter
@Builder
@AllArgsConstructor
@NoArgsConstructor
public class Fellow {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long fno;
@Column(length = 20, nullable = false)
private String name;
@Column(length = 200, nullable = false)
private String email;
}
2. repository 패키지 만들고, interface 생성한다.
public interface FellowRepository extends JpaRepository<Fellow, Long> {
}
3. HelloController에 객체 생성하고, 테스트 자료 입력한다.
@Autowired
FellowRepository fellowRepository;
@PostConstruct //update할때마다 계속 추가되니까 테스트 성공하면 주석처리한다.
public void insertDate() {
IntStream.rangeClosed(1,10).forEach(i->{
Fellow fellow = Fellow.builder()
.name("홍"+i).email("hong"+i+"@korea.com").build();
fellowRepository.save(fellow);
});
}
4. MyController 클래스에 멤버로 등록 한다.
@Autowired
FellowRepository fellowRepository;
-> @GetMapping("/~")로 필요한 요청을 작성한다(ex; select, insert, update, delete...)
-------------------------------------------------------------------------------------------
1. select 하기 (list.html)
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<ul>
<li th:each="dto : ${list}">
[[${dto.fno}]],
[[${dto.name}]],
[[${dto.email}]]
</li>
</ul>
</body>
</html>
1-1.MyController에 /select 요청 만들기
@GetMapping("/list")
String list(Model model) {
model.addAttribute("list", fellowRepository.findAll());
return "list";
}
2. insert 폼 만들기 (insert.html)
<form action="/insert" method="post">
<input type="text" name="name">
<input type="text" name="email">
<input type="submit" value="확인">
</form>
2-2. MyController에 /insert 요청 만들기
@RequestMapping(value="/insert", method= RequestMethod.POST)
String insert(@RequestParam String name, @RequestParam String email) {
Fellow fellow = Fellow.builder().name(name).email(email).build();
fellowRepository.save(fellow);
return "redirect:/list";
}
'MySQL' 카테고리의 다른 글
[eclipse] ajax사용해서 DB insert하기 (0) | 2022.08.30 |
---|---|
[eclipse] Jquery ajax 사용해서 JSP로 DB 조회하기 (0) | 2022.08.30 |
[eclipse]DBtable에 데이터 insert하기 (0) | 2022.08.30 |
[eclipse] Dbtable 만들고 JDBC로 데이터 select하는 JSP 코드 (0) | 2022.08.30 |