스프링03_데베, 마이바티스

charl hi·2022년 1월 15일
0

Spring

목록 보기
4/25

데이터베이스 다운 및 등록

1) oracle-jdbc, dbcp

pom.xml

		<!-- 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>


2) spring-jdbc, dbcp, Jdbc Template

servlet-context.xml

	<!-- 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>

추가

사용하는 생성자?




spring08prj

@Autowired

  • MenuController.java
@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";
	}
}
  • 스프링 컨테이너 안에, 그 타입에 맞는 객체(ex. JdbcTemplate)가 있다면 저렇게 생성됨

  • 와!! ✨jt가 null이 아니다!!!


@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";
	}
}


insert.jsp

<%@ 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

다운

		<!-- 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>

추가



등록

0개의 댓글