국비지원/백엔드

230203 Spring Boot + MyBatis 로 데이터베이스 연결

선SEON 2023. 2. 3. 10:40

MyBatis

 

templates 폴더

내용이 유동적인 것

 

static 폴더

css, 그림파일 등 꾸며주는 

index.html 파일이 여기 들어감

 

application.properties 설정

server.port=5050
# oracle set
spring.datasource.driver-class-name=oracle.jdbc.OracleDriver
spring.datasource.url=jdbc:oracle:thin:@localhost:1521/xe
spring.datasource.username=SCOTT
spring.datasource.password=tiger

index.html

메인 페이지

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
처음 페이지
</body>
</html>

 

Emp.java

package com.study.springboot;

public class Emp {
	private int empno;
	private String ename;
	private String job;
	private int mgr;
	private String hiredate;
	private int sal;
	private int comm;
	private int deptno;
	
	public int getEmpno() {
		return empno;
	}
	public void setEmpno(int empno) {
		this.empno = empno;
	}
	public String getEname() {
		return ename;
	}
	public void setEname(String ename) {
		this.ename = ename;
	}
	public String getJob() {
		return job;
	}
	public void setJob(String job) {
		this.job = job;
	}
	public int getMgr() {
		return mgr;
	}
	public void setMgr(int mgr) {
		this.mgr = mgr;
	}
	public String getHiredate() {
		return hiredate;
	}
	public void setHiredate(String hiredate) {
		this.hiredate = hiredate;
	}
	public int getSal() {
		return sal;
	}
	public void setSal(int sal) {
		this.sal = sal;
	}
	public int getComm() {
		return comm;
	}
	public void setComm(int comm) {
		this.comm = comm;
	}
	public int getDeptno() {
		return deptno;
	}
	public void setDeptno(int deptno) {
		this.deptno = deptno;
	}
	
}

 

EmpMapper.java

package com.study.springboot;

import java.util.List;

import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;

@Mapper
public interface EmpMapper {
	@Select("select empno, ename, job, sal from emp_temp")
	List<Emp> findAll();
	
	@Insert("INSERT INTO emp_temp(empno, ename, job, sal) VALUES(#{empno}, #{ename}, #{job}, #{sal})")
	int save(Emp emp);
	
	@Delete("DELETE FROM emp_temp WHERE empno= #{empno}")
	int delete(int empno);
}

MyController.java

package com.study.springboot;

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.PostMapping;

@Controller
public class MyController {
	@Autowired
	private EmpMapper empMapper;
	
	@GetMapping("/now")
	public String asdasd() {
		return "root";
	}
	
	@GetMapping("/list")
	public String list(Model model) {
		List<Emp> elst = empMapper.findAll();
		model.addAttribute("list", elst);
		return "list";
	}
	
	@PostMapping("/insert")
	public String insert(Emp emp, Model model) {
		int res = empMapper.save(emp);
		/* model.addAttribute("res", res); */
		return "redirect:/list";
	}
	
	@GetMapping("/delete")
	public String delete(int empno) {
		empMapper.delete(empno);
		return "redirect:/list";
	}
}

list.html

<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<table border="1">
<tr>
<th>사번</th>
<th>이름</th>
<th>직무</th>
<th>급여</th>
<th>삭제</th>
</tr>
<tr th:each="emp : ${list}">
<td th:text="${emp.empno}"></td>
<td th:text="${emp.ename}"></td>
<td th:text="${emp.job}"></td>
<td th:text="${emp.sal}"></td>
<td><a th:href="@{/delete(empno=${emp.empno})}">X</a></td>
</tr>
</table>
</body>
</html>

insert.html

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<form action="/insert" method="post">
사번<input type="text" name="empno" /><br/>
이름<input type="text" name="ename" /><br/>
직무<input type="text" name="job" /><br/>
급여<input type="text" name="sal" /><br/>
<input type="submit"/>
</form>
</body>
</html>

'국비지원 > 백엔드' 카테고리의 다른 글

230209 Thymeleaf + Bootstrap  (0) 2023.02.09
230207 Spring - JUnit Test  (0) 2023.02.07
230130 JSP 예제  (0) 2023.01.30
230117 JSP란?  (0) 2023.01.17
230113 JSP 참고 링크  (0) 2023.01.13