<!-- DB -->
<!-- ojdbc8 -->
<dependency>
<groupId>com.oracle.database.jdbc</groupId>
<artifactId>ojdbc8</artifactId>
<version>21.1.0.0</version>
</dependency>
<!-- spring jdbc -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>${org.springframework-version}</version>
</dependency>
<!-- dbcp -->
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-dbcp2</artifactId>
<version>2.7.0</version>
</dependency>
<!-- DB 설정 관련 -->
<!-- spring-jdbc -->
<beans:bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<beans:property name="driverClassName" value="oracle.jdbc.OracleDriver"></beans:property>
<beans:property name="url" value="jdbc:oracle:thin:@127.0.0.1:1521:xe"></beans:property>
<beans:property name="username" value="kh"></beans:property>
<beans:property name="password" value="kh"></beans:property>
</beans:bean>
<!-- dbcp -->
<beans:bean id="dbcpSource" class="org.apache.commons.dbcp2.BasicDataSource">
<beans:property name="driverClassName" value="oracle.jdbc.OracleDriver"></beans:property>
<beans:property name="url" value="jdbc:oracle:thin:@127.0.0.1:1521:xe"></beans:property>
<beans:property name="username" value="kh"></beans:property>
<beans:property name="password" value="kh"></beans:property>
<beans:property name="maxTotal" value="20"></beans:property>
<beans:property name="maxIdle" value="10"></beans:property>
<beans:property name="maxWaitMillis" value="3000"></beans:property>
</beans:bean>
<!-- Jdbc Template -->
<beans:bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
<!-- dataSource 에 객체 넣기 -->
<beans:property name="dataSource" ref="dataSource"></beans:property>
</beans:bean>
추가
@Controller
@RequestMapping("/menu")
public class MenuController {
@Autowired
private JdbcTemplate jt;
// private JdbcTemplate jt = new JdbcTemplate(); 의 효과
@GetMapping("/insert")
public String insert() {
return "menu/insert";
}
@PostMapping("/insert")
public String insert(String menu, String price) {
System.out.println(jt);
System.out.println("post called...");
return "menu/insert";
}
}
@Controller
@RequestMapping("/menu")
public class MenuController {
@Autowired
private JdbcTemplate jt;
// private JdbcTemplate jt = new JdbcTemplate(); 의 효과
@GetMapping("/insert")
public String insert() {
return "menu/insert";
}
@PostMapping("/insert")
public String insert(String menu, String price) {
//값을 db에 insert~
String sql = "INSERT INTO MENU(MENU, PRICE) VALUES(?, ?)";
// Object[] params = new Object[] {menu, price};
Object[] params = {menu, price};
//jt.update(쿼리, 파라미터), 파라미터는 Object[]
jt.update(sql, params);
System.out.println("post called...");
return "menu/insert";
}
}
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<h1>menu insert page</h1>
<form action="insert" method="post">
<!-- 메뉴 이름, 가격 -->
메뉴 이름 : <input type="text" name="menu"><br>
메뉴 가격 : <input type="text" name="price"><br>
<input type="submit" value="메뉴등록">
</form>
</body>
</html>
<!-- mybatis -->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.5.6</version>
</dependency>
<!-- mybatis-spring -->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
<version>2.0.6</version>
</dependency>
추가