Spring - myBatis설정

김재령·2022년 2월 28일
0

SpringFramework

목록 보기
10/12

1) myBatis (XML) 형식을 사용하기 위한 준비

	<bean id="hikariConfig" class="com.zaxxer.hikari.HikariConfig">
		<property name="driverClassName" value="oracle.jdbc.driver.OracleDriver" />
		<property name="jdbcUrl" value="jdbc:oracle:thin:@192.168.1.100:1521:xe" />
		<property name="username" value="c##jr" />
		<property name="password" value="it" />
	</bean>
	
	<bean id="dataSource" class="com.zaxxer.hikari.HikariDataSource" destroy-method="close">
		<constructor-arg ref="hikariConfig" />
	</bean>
	
	<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
		<property name="dataSource" ref="dataSource" /> 
		<property name="configLocation" value="classpath:config/mybatis-config.xml" />
		<property name="mapperLocations">
			<list>
				<value>classpath:config/sqlmap-chat.xml</value>
				<value>classpath:config/sqlmap-member.xml</value>
				<value>classpath:config/sqlmap-myPage.xml</value>
			</list>
		</property>
	</bean>
	
	<bean id="sqlSessionTemplate" class="org.mybatis.spring.SqlSessionTemplate" destroy-method="close">
		<constructor-arg ref="sqlSessionFactory" />
	</bean>	

root-context.xml 에서

  • configLocation: mybatis-config.xml 이 등록되어 있고, 파일이 제자리에 있는지 확인
  • mapperLocations: sqlmap-***.xml 이 등록되어 있고, namespace 가 DAO와 연결되어 있는지 확인

2) 연결이 모두 완료되었다면 Controller -> Service -> DAO -> mapper.xml 로 연결됨

<mapper.xml 을 작성하는 요령>

  • namespace는 DAO의 이름을 작성한다

  • <select ,insert , update , delete> 태그를 사용한다

  • <select(sql)> 는 resultType을 명시해야 한다

  • <insert , update , delete> 는 resultType이 int 로 고정이기 때문에 생략한다

  • 매개변수를 전달한다면 parameterType 을 명시한다

  • parameterType 은 패키지이름.클래스이름을 모두 작성해야 한다

  • 자주 사용하는 클래스 자료형이 있다면, 마이바티스 설정파일에서 typeAliase 를 설정하여 줄일 수 있다

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE configuration PUBLIC 
	"-//mybatis.org//DTD Config 3.0//EN" 
	"http://mybatis.org/dtd/mybatis-3-config.dtd">
	
<configuration>
	<settings>
		<setting name="cacheEnabled" value="false" />
        <!--캐시 활성화 O-->
		<setting name="useGeneratedKeys" value="false" />
        <!--자동 데이터 번호 설정 X == sequence-->
        <!-- -->
		<setting name="mapUnderscoreToCamelCase" value="false" /><!--_를 카멜케이스로 변환X-->
		<!-- 	user_name => userName	-->
	</settings>
	
	<typeAliases>
		<typeAlias type="com.itbank.model.ProductDTO" alias="product"/>
		<typeAlias type="com.itbank.model.TradeDTO" alias="trade"/>
		<typeAlias type="com.itbank.model.MemberDTO" alias="member"/>		
		<typeAlias type="com.itbank.model.WishListDTO" alias="WishList"/>		
					
					
	</typeAliases>
</configuration>
profile
with me

0개의 댓글