Mybatis测试用例

package cn.zhangxueliang.mybatis.mapper;

import static org.junit.Assert.*;

import java.io.InputStream;
import java.util.List; 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 cn.zhangxueliang.mybatis.po.User; public class UserMapperTest { private SqlSessionFactory sqlSessionFactory = null; @Before
public void init() throws Exception {
// 第一步:创建一个SQLSessionFactoryBuilder对象。
SqlSessionFactoryBuilder sqlSessionFactoryBuilder = new SqlSessionFactoryBuilder();
// 第二步:加载配置文件。
InputStream inputStream = Resources.getResourceAsStream("SqlMapConfig.xml");
// 第三步:创建SQLSessionFactory对象
sqlSessionFactory = sqlSessionFactoryBuilder.build(inputStream);
} @Test
public void testGetUserById() {
//和spring整合后省略
SqlSession sqlSession = sqlSessionFactory.openSession(); //获得代理对象
UserMapper userMapper = sqlSession.getMapper(UserMapper.class);
User user = userMapper.getUserById(10);
System.out.println(user); //和spring整合后省略
sqlSession.close();
} @Test
public void testGetUserByName() {
SqlSession sqlSession = sqlSessionFactory.openSession();
UserMapper userMapper = sqlSession.getMapper(UserMapper.class);
List<User> list = userMapper.getUserByName("张");
for (User user : list) {
System.out.println(user);
}
sqlSession.close();
} @Test
public void testInsertUser() {
fail("Not yet implemented");
} }
上一篇:如何自学java迅速成为java高手


下一篇:消息中间件解决方案JMS