본문 바로가기
ORM

Java + MyBatis

by 수앙 2011. 9. 21.

필요 라이브러리

commons-logging-1.1.1.jar
jtds-1.2.5.jar
log4j-1.2.16.jar
mybatis-3.0.5.jar
slf4j-api-1.6.2.jar
slf4j-log4j12-1.6.2.jar


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>
	<environments default="development">
		<environment id="development">
			<transactionManager type="JDBC" />
			<dataSource type="POOLED">
				<property name="driver" value="net.sourceforge.jtds.jdbc.Driver" />
				<property name="url" value="jdbc:jtds:sqlserver://localhost:1433/bbs;tds=8.0;lastupdatecount=true" />
				<property name="username" value="bbs" />
				<property name="password" value="bbs" />
			</dataSource>
		</environment>
	</environments>
	
	<mappers>
		<mapper resource="local/dao/dao.xml" />
	</mappers>
</configuration>


Test.java

import java.io.Reader;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

public class Test {

	public static void main(String[] args) throws Exception {
		// .class 파일 output 폴더 바로 밑에 mybatis-config.xml 파일 존재
		Reader reader = Resources.getResourceAsReader("mybatis-config.xml");
		SqlSession session = new SqlSessionFactoryBuilder().build(reader).openSession();
		reader.close();

		Map<String, Object> params = new HashMap<String, Object>();
		params.put("no", 1);
		List<Map<String, Object>> resultSet = session.selectList("DAO.selectList", params);
	}

}


log4j.properties

# 전역 로깅 설정
log4j.rootLogger=INFO, stdout

# MyBatis 로깅 설정...
log4j.logger.org.apache.ibatis=DEBUG
log4j.logger.java.sql.Connection=DEBUG
log4j.logger.java.sql.Statement=DEBUG
log4j.logger.java.sql.PreparedStatement=DEBUG
log4j.logger.java.sql.ResultSet=DEBUG

# Console 출력...
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%d{yyyy/MM/dd HH:mm} %-5p (%F:%L) - %m%n

 

댓글