myBatis 추가
https://mvnrepository.com/artifact/org.mybatis/mybatis/3.4.4
<!-- https://mvnrepository.com/artifact/org.mybatis/mybatis -->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.4.4</version>
</dependency>
시작하기
mybatis-config-test.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.sf.log4jdbc.sql.jdbcapi.DriverSpy"/>
<property name="url" value="jdbc:log4jdbc:mysql://127.0.0.1:3306/team4"/>
<property name="username" value="wjheo"/>
<property name="password" value="1111"/>
</dataSource>
</environment>
</environments>
<mappers>
<mapper resource="Userapper.xml"/>
</mappers>
</configuration>
Userapper.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="UserMapper">
<select id="findById" resultType="com.java.dto.User">
select * from USERS where userId=#{userId}
</select>
<insert id="create">
insert into USERS values(#{userId}, #{password} , #{name} , #{email})
</insert>
</mapper>
class MyBatisTest
package com.java.dao.users;
import static org.hamcrest.CoreMatchers.is;
import static org.junit.Assert.assertThat;
import java.io.IOException;
import java.io.InputStream;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.junit.Before;
import org.junit.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.java.dto.User;
public class MyBatisTest {
private static final Logger log = LoggerFactory.getLogger(MyBatisTest.class);
private SqlSessionFactory sqlSessionFactory;
@Before
public void setUp() throws IOException{
String resource="mybatis-config-test.xml";
InputStream inputStream=Resources.getResourceAsStream(resource);
sqlSessionFactory=new SqlSessionFactoryBuilder().build(inputStream);
}
@Test
public void gettingStarted() throws IOException{
try(SqlSession session = sqlSessionFactory.openSession()) {
User user =session.selectOne("UserMapper.findById", "braverokmc");
log.debug(" gettingStarted {} " , user.toString());
}catch(Exception e){
e.printStackTrace();
}
}
@SuppressWarnings("unused")
@Test
public void insert() {
SqlSession session=null;
try{
session =sqlSessionFactory.openSession();
User user =new User("snajgi79", "password" , "sanjigi" , "sangigi@slipp.net");
session.insert("UserMapper.create", user);
session.commit();
session.close();
session=sqlSessionFactory.openSession();
User actual =session.selectOne("UserMapper.findById", user.getUserId());
assertThat(actual.getUserId(), is(user.getUserId()));
}catch(Exception e){
e.printStackTrace();
}finally {
if(session!=null)session.close();
}
}
}
댓글 ( 4)
댓글 남기기