mybatis逆向生成映射文件实体类 单元测试test 增删改查方法

来源:互联网 发布:python zip函数 编辑:程序博客网 时间:2024/06/06 07:52
package cn.itcast.ssm.mapper;

import static org.junit.Assert.*;

import java.util.List;

import org.junit.Before;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import cn.itcast.ssm.po.Items;
import cn.itcast.ssm.po.ItemsExample;

public class ItemsMapperTest {

    private ApplicationContext applicationContext;
    
    private ItemsMapper itemsMapper;

    //在setUp这个方法得到spring容器
    @Before
    public void setUp() throws Exception {
        applicationContext = new ClassPathXmlApplicationContext("classpath:spring/applicationContext.xml");
        itemsMapper = (ItemsMapper) applicationContext.getBean("itemsMapper");
    }

    //根据主键删除
    @Test
    public void testDeleteByPrimaryKey() {
        
    }

    //插入
    @Test
    public void testInsert() {
        //构造 items对象
        Items items = new Items();
        items.setName("手机");
        items.setPrice(999f);
        itemsMapper.insert(items);
    }

    //自定义条件查询
    @Test
    public void testSelectByExample() {
        ItemsExample itemsExample = new ItemsExample();
        //通过criteria构造查询条件 select * from where id=1 and name='笔记本3‘;
        ItemsExample.Criteria criteria = itemsExample.createCriteria();
        criteria.andNameEqualTo("笔记本3");

        //可能返回多条记录
        List<Items> list = itemsMapper.selectByExample(itemsExample);
        
        System.out.println(list);
        
    }

    //根据主键查询
    @Test
    public void testSelectByPrimaryKey() {
        Items items = itemsMapper.selectByPrimaryKey(1);
        System.out.println(items);
    }

    //更新数据
    @Test
    public void testUpdateByPrimaryKey() {
        
        //对所有字段进行更新,需要先查询出来再更新
        Items items = itemsMapper.selectByPrimaryKey(1);
        
        items.setName("水杯");
        
        itemsMapper.updateByPrimaryKey(items);
        //如果传入字段不空为才更新,在批量更新中使用此方法,不需要先查询再更新
        //itemsMapper.updateByPrimaryKeySelective(record);
        
    }

}

阅读全文
0 0
原创粉丝点击