• 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");
        }
    
    }
  • 相关阅读:
    【Python之路】第三篇--Python基本数据类型
    【Python之路】第二篇--初识Python
    【Python之路】第一篇--Linux基础命令
    noip模拟测试7
    noip模拟测试6
    动态添加select的option [转载]
    javaweb报错:java.lang.NumberFormatException: null
    Javascript获取select的选中值和选中文本(转载)
    动态生成select框内容
    IO(Input&Output)流の介绍
  • 原文地址:https://www.cnblogs.com/niwotaxuexiba/p/10259206.html
Copyright © 2020-2023  润新知