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");
} }

Mybatis测试用例的更多相关文章

  1. 用 Eclipse 搭建一个简单的 Maven spring mybatis 项目(包含测试用例)

    1: 先搭建一个Maven项目: 创建好后的目录: 2: 配置pom.xml文件: <project xmlns="http://maven.apache.org/POM/4.0.0& ...

  2. MyBatis参数绑定规则及原理分析

    MyBatis参数的传递有几种不同的方法,本文通过测试用例出发,对其中的方式进行总结和说明,并对其部分源码进行分析. 一.测试用例(环境参考之前博客SSM接口编程一文 http://www.cnblo ...

  3. Spring MVC 学习总结(六)——Spring+Spring MVC+MyBatis框架集成

    与SSH(Struts/Spring/Hibernate/)一样,Spring+SpringMVC+MyBatis也有一个简称SSM,Spring实现业务对象管理,Spring MVC负责请求的转发和 ...

  4. Spring学习总结(五)——Spring整合MyBatis(Maven+MySQL)一

    MyBatis-Spring 会帮助你将 MyBatis 代码无缝地整合到 Spring 中. 使用这个类库中的类, Spring 将会加载必要的MyBatis工厂类和 session 类. 这个类库 ...

  5. mybatis错误之配置文件属性配置问题

    在mybatis的配置文件SqlMapConfig.xml中,可以在开始的地方先加载一个properties节点,用来定义属性变量. <!-- 加载属性文件 --> <propert ...

  6. Mybatis学习错误之:重复加载mapper.xml

    学习mybatis的时候,突然遇到测试出错.测试mapper代理失败,现在钻研少了,不喜欢看未知的错误了,立即改正.错误打印说mapper.xml已经注册,仔细查看SQLMapConfig.xml发现 ...

  7. MyBatis使用总结+整合Spring

    MyBatis 本是apache的一个开源项目iBatis, 2010年这个项目由apache software foundation 迁移到了google code,并且改名为MyBatis .20 ...

  8. Spring与Mybatis整合的MapperScannerConfigurer处理过程源码分析

    前言 本文将分析mybatis与spring整合的MapperScannerConfigurer的底层原理,之前已经分析过java中实现动态,可以使用jdk自带api和cglib第三方库生成动态代理. ...

  9. MyBatis学习总结

    1.引入jar包到lib目录下:只需要mybatis的一个mybatis.jar及数据库的jar包. 2.在src下新建xml配置文件,即上图中的conf.xml <?xml version=& ...

随机推荐

  1. NGINX Load Balancing – TCP and UDP Load Balancer

    This chapter describes how to use NGINX Plus and open source NGINX to proxy and load balance TCP and ...

  2. NGINX Load Balancing - HTTP Load Balancer

    This chapter describes how to use NGINX and NGINX Plus as a load balancer. Overview Load balancing a ...

  3. 关于HashMap的一些深入探索与理解

    在java中.大家差点儿是离不开对集合的使用的,像Map系列,List系列.Set系列.可是非常多人没有了解过或者研究过这些集合类究竟能够用在什么地方,而且有什么注意的地方.因此本文分Map系列和Li ...

  4. UVA11059-Maximum Product(动态规划)

    Problem UVA11059-Maximum Product Accept:4769  Submit:38713 Time Limit: 3000 mSec  Problem Descriptio ...

  5. python入门学习:9.文件和异常

    python入门学习:9.文件和异常 关键点:文件.异常 9.1 从文件中读取数据9.2 写入文件9.3 异常9.4 存储数据 9.1 从文件中读取数据 9.1.1 读取整个文件  首先创建一个pi_ ...

  6. Python:Day19 正则表达式补充

    贪婪匹配 贪婪匹配是指字符后面是*+?的时候,都是尽可能多的匹配,如果不想尽可能多的匹配,那么在这三个字符后面加?号即可,这样变成惰性匹配,按最少匹配. ret = re.findall('ab??' ...

  7. 010_动态语言与鸭子类型及python2和3的区别

    一. 动态语言中经常提到鸭子类型,所谓鸭子类型就是:如果走起路来像鸭子,叫起来也像鸭子,那么它就是鸭子(If it walks like a duck and quacks like a duck, ...

  8. Node.js之mysql增删改查

    1.安装库 npm install mysql 2.编写db.js(用作公共模块) //连接MySQL数据库 var mysql = require("mysql"); var p ...

  9. 洛谷 P1596 [USACO10OCT]湖计数Lake Counting

    题目链接 https://www.luogu.org/problemnew/show/P1596 题目描述 Due to recent rains, water has pooled in vario ...

  10. 从零开始搭建django前后端分离项目 系列二(项目搭建)

    在开始项目之前,假设你已了解以下知识:webpack配置.vue.js.django.这里不会教你webpack的基本配置.热更新是什么,也不会告诉你如何开始一个django项目,有需求的请百度,相关 ...