typeAliases
타입 별칭은 자바 타입에 대한 짧은 이름이다. 오직 XML 설정에서만 사용되며, 타이핑을 줄이기 위해 존재한다. 예를들면:
<typeAliases>
<typeAlias alias="Author" type="domain.blog.Author"/>
<typeAlias alias="Blog" type="domain.blog.Blog"/>
<typeAlias alias="Comment" type="domain.blog.Comment"/>
<typeAlias alias="Post" type="domain.blog.Post"/>
<typeAlias alias="Section" type="domain.blog.Section"/>
<typeAlias alias="Tag" type="domain.blog.Tag"/>
</typeAliases>
slipp.sql
DROP TABLE IF EXISTS USERS;
CREATE TABLE USERS (
userId varchar(12) NOT NULL,
password varchar(12) NOT NULL,
name varchar(20) NOT null,
email varchar(50),
primary key (userId)
);
INSERT INTO USERS VALUES('javajigi', 'password', '자바지기', 'javajigi@slipp.net');
class MyBatisTest
@Before
public void setUp() throws IOException{
String resource="mybatis-config-test.xml";
InputStream inputStream=Resources.getResourceAsStream(resource);
sqlSessionFactory=new SqlSessionFactoryBuilder().build(inputStream);
ResourceDatabasePopulator populator=new ResourceDatabasePopulator();
populator.addScript(new ClassPathResource("slipp.sql"));
DatabasePopulatorUtils.execute(populator, getDataSource());
log.info("database initialized success!");
}
private DataSource getDataSource(){
BasicDataSource dataSource =new BasicDataSource();
dataSource.setDriverClassName("net.sf.log4jdbc.sql.jdbcapi.DriverSpy");
dataSource.setUrl("jdbc:log4jdbc:mysql://127.0.0.1:3306/team4");
dataSource.setUsername("wjheo");
dataSource.setPassword("1111");
return dataSource;
}
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 javax.sql.DataSource;
import org.apache.commons.dbcp.BasicDataSource;
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 org.springframework.core.io.ClassPathResource;
import org.springframework.jdbc.datasource.init.DatabasePopulatorUtils;
import org.springframework.jdbc.datasource.init.ResourceDatabasePopulator;
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);
ResourceDatabasePopulator populator=new ResourceDatabasePopulator();
populator.addScript(new ClassPathResource("slipp.sql"));
DatabasePopulatorUtils.execute(populator, getDataSource());
log.info("database initialized success!");
}
private DataSource getDataSource(){
BasicDataSource dataSource =new BasicDataSource();
dataSource.setDriverClassName("net.sf.log4jdbc.sql.jdbcapi.DriverSpy");
dataSource.setUrl("jdbc:log4jdbc:mysql://127.0.0.1:3306/team4");
dataSource.setUsername("wjheo");
dataSource.setPassword("1111");
return dataSource;
}
@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)
댓글 남기기