Day091

RISK_TAKER·2023년 6월 12일
0

DB연결을 위한 dependency 추가하기

https://mvnrepository.com/

Spring JDBC » 5.2.24.RELEASE

Apache Commons DBCP » 2.9.0

MyBatis » 3.5.13 » 3.5.10으로 버전 다운

MyBatis Spring » 3.0.2 » 2.0.7로 버전 다운

root-context.xml
  • 다음의 코드를 추가한다.
<bean class="org.apache.commons.dbcp2.BasicDataSource" id="data_source">
	<property name="driverClassName" value="oracle.jdbc.driver.OracleDriver" />
	<property name="url" value="jdbc:oracle:thin:@localhost:1521:orcl" />
	<property name="username" value="scott" />
	<property name="password" value="tiger" />
</bean>
	
<bean class="org.mybatis.spring.SqlSessionFactoryBean" id="sqlSessionFactory">
	<property name="dataSource" ref="data_source" />
	<property name="mapperLocations" value="/WEB-INF/mapper/**/*_mapper.xml"></property>
</bean>
	
<bean class="org.mybatis.spring.SqlSessionTemplate" id="sqlSessionTemplate">
	<constructor-arg name="sqlSessionFactory" ref="sqlSessionFactory" />
</bean>

mapper.xml 파일 작성하는 방법

  1. WEB-INF/mapper/reservation/reservation_mapper.xml 파일 생성
  2. 다음과 같이 코드를 작성한다.
<?xml version="1.0" encoding="UTF-8"?>

<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
						"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
						

<mapper namespace="reservation_mapper">

	<insert id="insert_reservation" parameterType="com.app.dto.reservation.ReservationDto">
		insert into t_reservation
		values ( #{roomNumber} , #{customerNumber} )
	</insert>
	
	<select id="select_reservation_list" resultType="com.app.dto.reservation.ReservationDto">
		select room_number roomNumber, customer_number customerNumber
				from t_reservation
	</select>
	
	<select id="select_reservation_list_by_roomNumber" resultType="com.app.dto.reservation.ReservationDto"
													   parameterType="com.app.dto.reservation.ReservationDto">
		select room_number roomNumber, customer_number customerNumber
				from t_reservation
				where room_number = #{roomNumber}
	</select>

</mapper>

0개의 댓글