iBatis2 (1) 설정하기
예제실행환경
- Java SE 6
- iBatis 2.3.5
- JUnit 4
- Eclipse Java EE IDE (Juno)
MySQL Table 준비하기
CREATE TABLE `user` ( `id` int(5) NOT NULL AUTO_INCREMENT, `username` varchar(16) NOT NULL, `password` varchar(16) NOT NULL, `level` tinyint(2) NOT NULL DEFAULT '0', `reg_date` date NOT NULL, PRIMARY KEY (`id`) ) ENGINE=MyISAM DEFAULT CHARSET=utf8 ;
SQL Maps 환경설정
SqlMapConfig.xml
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE sqlMapConfig PUBLIC "-//ibatis.apache.org//DTD SQL Map Config 2.0//EN" "http://ibatis.apache.org/dtd/sql-map-config-2.dtd"> <sqlMapConfig> <properties resource="exercise/resource/development.properties"/> <typeAlias type="exercise.domain.User" alias="User"/> <transactionManager type="JDBC"> <dataSource type="SIMPLE"> <property name="JDBC.Driver" value="${driver}"/> <property name="JDBC.ConnectionURL" value="${url}"/> <property name="JDBC.Username" value="${user}"/> <property name="JDBC.Password" value="${pass}"/> </dataSource> </transactionManager> <sqlMap resource="exercise/ibatis2/persistence/SqlMap.xml"/> </sqlMapConfig>
SQL Map XML
SqlMap.xml
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE sqlMap PUBLIC "-//ibatis.apache.org//DTD SQL Map 2.0//EN" "http://ibatis.apache.org/dtd/sql-map-2.dtd"> <sqlMap> <parameterMap id="userParameterMap" class="User"> <parameter property="username" jdbcType="VARCHAR"/> <parameter property="password" jdbcType="VARCHAR"/> <parameter property="level" jdbcType="TINYINT"/> <parameter property="regDate" jdbcType="VARCHAR"/> </parameterMap> <insert id="add" parameterMap="userParameterMap"> INSERT INTO user ( id, username, password, level, reg_date ) VALUES ( NULL, ?, ?, ?, ? ) </insert> <delete id="deleteAll"> DELETE FROM user </delete> <select id="count" resultClass="int"> SELECT COUNT(*) FROM user </select> </sqlMap>
Unit Test
TestSqlMap.java
package exercise.ibatis2.test; import static org.hamcrest.CoreMatchers.is; import static org.junit.Assert.assertThat; import java.io.Reader; import java.sql.SQLException; import java.util.Arrays; import java.util.List; import org.junit.Before; import org.junit.Test; import com.ibatis.common.resources.Resources; import com.ibatis.sqlmap.client.SqlMapClient; import com.ibatis.sqlmap.client.SqlMapClientBuilder; import exercise.domain.User; public class TestSqlMap { static String resource = "exercise/ibatis2/persistence/SqlMapConfig.xml"; static Reader reader; static SqlMapClient sqlMap; List<user> users; @BeforeClass public static void setUpBeforeClass() throws Exception { reader = Resources.getResourceAsReader(resource); sqlMap = SqlMapClientBuilder.buildSqlMapClient(reader); } @Before public void setUp() throws Exception { users = Arrays.asList( new User("user1", "1234", 1, "2012-11-09"), new User("user2", "1234", 1, "2012-11-09"), new User("user3", "1234", 1, "2012-11-09") ); } @Test public void testAdd() { try { sqlMap.delete("deleteAll"); sqlMap.insert("add", users.get(0)); assertThat(1, is(sqlMap.queryForObject("count"))); } catch (SQLException e) { e.printStackTrace(); } } }