본문 바로가기
Programming/SpringFramework

[Spring] mybatis-config.xml (mybatis에 필요한 설정 정보)

by prinha 2020. 8. 12.
728x90
반응형

 

<!-- mybatis-config.xml -->

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

  <typeAliases>
    <package name="패키지명"/>    
  </typeAliases>

</configuration>
<!-- Mapper.xml -->

<?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="org.zerock.mapper.BoardMapper">
	
<!-- 원래 코드 / 파라미터타입에 클래스 풀네임을 작성해줘야함
	<insert id="create" parameterType="org.zerock.domain.BoardVO">
		insert into tbl_board(BNO, TITLE, CONTENT, WRITER)
		values(seq_board_bno.nextval, #{title}, #{content}, #{writer})
	</insert>
 -->

	<!-- 패키지명 생략 가능 -->
	<insert id="create" parameterType="BoardVO">
		insert into tbl_board(BNO, TITLE, CONTENT, WRITER)
		values(seq_board_bno.nextval, #{title}, #{content}, #{writer})
	</insert>
    
</mapper>
<!-- root-context.xml -->

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xmlns:context="http://www.springframework.org/schema/context"
	xsi:schemaLocation="http://www.springframework.org/schema/beans https://www.springframework.org/schema/beans/spring-beans.xsd
		http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd">
	
	<!-- 기본으로 DB연결설정 -->
	<!-- 
	<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
		<property name="driverClassName" value="oracle.jdbc.driver.OracleDriver"></property>
		<property name="url" value="jdbc:oracle:thin:@localhost:1521:orcl"></property>
		<property name="username" value="spring"></property>
		<property name="password" value="spring"></property>
	</bean>
	 -->
	 <!-- 
	 log4jdbc-log4j2-jdbc4라이브러리 추가후 사용가능 
	 위치 : src/main/resources

 log4jdbc.log4j2.properties, logback.xml 2개의 파일을 복사하여 구성한다.
	 -->
	
	<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
		<property name="driverClassName" value="net.sf.log4jdbc.sql.jdbcapi.DriverSpy"></property>
		<property name="url" value="jdbc:log4jdbc:oracle:thin:@localhost:1521/orcl"></property>
		<property name="username" value="ora_user"></property>
		<property name="password" value="1234"></property>
	</bean>
	 
	 <!-- 실질적인 DB연동 -->
	 <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
	 	<property name="dataSource" ref="dataSource"></property>
	 
	 	<property name="mapperLocations" value="classpath:mappers/**/*Mapper.xml"></property>
	 	<!-- mybatis-config.xml에 대한 정보를 기술함 -->
		<property name="configLocation" value="classpath:/mybatis-config.xml"></property>
	 </bean>
	 
	 <bean id="sqlSession" class="org.mybatis.spring.SqlSessionTemplate" destroy-method="clearCache">
	 	<constructor-arg name="sqlSessionFactory" ref="sqlSessionFactory"></constructor-arg>
	 </bean>
	<!-- Root Context: defines shared resources visible to all other web components -->
	<!-- BEAN으로 자동 생성 -->
	<context:component-scan base-package="com.demo.persistence,com.demo.service" />

</beans>

 

 

728x90
반응형