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

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

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值