博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
JAVA入门[8]-测试mybatis
阅读量:7088 次
发布时间:2019-06-28

本文共 3192 字,大约阅读时间需要 10 分钟。

上一节通过mybatis-generator自动生成了CategoryMapper接口,pojo等类,接下来我们写几个简单的测试来进行调用。

一、添加依赖

com.alibaba
druid
${druid.version}
junit
junit
${junit.version}
test

 

二、测试SqlSessionFactroy

首先我们看下怎样实例化sqlSessionFactory,然后获取CategoryMapper。

1.新建文件resources/properties/config.property,用来保存数据库连接串和账号。

data_source_url=jdbc:mysql://localhost:3306/storedata_source_username=rootdata_source_password=root

 

2.在resources/spring目录新建spring的配置文件applicationContext.xml

3.配置数据连接。 在applicationContext.xml中添加bean节点dataSource,详细配置参考:

 

4.然后配置bean sqlSessionFactory和sqlSession。

4.测试

有了上面的配置,就可以直接以注解的方式实例化SqlSessionFactory.

@ContextConfiguration(locations = "classpath:spring/applicationContext.xml")@RunWith(SpringJUnit4ClassRunner.class)public class SqlSessionTests {    @Autowired    private SqlSession sqlSession;    @Test    public void testSqlSession(){        CategoryMapper categoryMapper= sqlSession.getMapper(CategoryMapper.class);        Category category= categoryMapper.selectByPrimaryKey(1);        if(category==null){            System.out.println("Do not exist");        }        else{            System.out.println(category.getName());        }    }}

 

三、测试直接实例化CategoryMapper

更加简洁的方式是直接通过注解实例化CategoryMapper

1.在applicationContext.xml新增bean节点

2.直接给CategoryMapper添加@Resource注解,来进行实例化。

@ContextConfiguration(locations = "classpath:spring/applicationContext.xml")@RunWith(SpringJUnit4ClassRunner.class)
public class CategoryDaoTests {
@Resource CategoryMapper categoryMapper; @Test public void test_selectById(){
Category category=categoryMapper.selectByPrimaryKey(1); System.out.println(category.getName());//Fish } @Test public void test_count(){
CategoryExample example=new CategoryExample(); long result=categoryMapper.countByExample(example); System.out.println(result); } @Test public void test_insert(){
System.out.println("before insert:count "+categoryMapper.countByExample(new CategoryExample())); Category category=new Category(); category.setName("Test"); categoryMapper.insert(category); System.out.println("after insert:count "+categoryMapper.countByExample(new CategoryExample())); } @Test public void test_delete(){
System.out.println("before insert:count "+categoryMapper.countByExample(new CategoryExample())); CategoryExample example=new CategoryExample(); example.createCriteria().andNameEqualTo("Test"); int result=categoryMapper.deleteByExample(example); System.out.println("删除条数:"+result); System.out.println("after insert:count "+categoryMapper.countByExample(new CategoryExample())); } }
 

转载地址:http://dwfql.baihongyu.com/

你可能感兴趣的文章
svn与web 同步更新
查看>>
通用社区登陆组件技术分享(开源)下篇:OAuth 源码下载及原理解说
查看>>
Windows Phone 7 程序等待页面的处理
查看>>
java.io.IOException: Connection reset by peer
查看>>
linux下的精确wait
查看>>
MySQL常见命令 [转]
查看>>
【大前端之打通账号系统】passport应该如何落地?
查看>>
虚拟化技术总览
查看>>
飞天,进化!
查看>>
20.3. PHP_INI
查看>>
72.11. this is incompatible with sql_mode=only_full_group_by
查看>>
C# 海康DVR客户端开发系列(3)—— 连接DVR和图像预览
查看>>
为创业我做了十年的程序员,你告诉我“程序员不适合创业”?!
查看>>
mokoid android open source HAL hacking in a picture
查看>>
RCF库ClientStub.setAutoReconnect
查看>>
Google Chrome Resize Plugin
查看>>
java编程之:Unsafe类
查看>>
序列作为主键使用的原理、优缺点讨论
查看>>
iOS - AutoLayout
查看>>
如何将dubbo封装成http协议
查看>>