MyBatis中的Mapper接口和Example类的实例

最近在实习过程中,对Mybatis的使用有了更加深刻的理解。自己之前写MyBatis时都是手写,后来用了mybatis-generator来自动生成代码(上一次发布的文章),生成了XXXExample.java文件。后来在老大教了之后,才发现原来是这么好用的,闲话少说,Talking is cheap , showing you the code。
1.Mapper接口:

public interface AuthenticationAppMapper {
	//按条件计数
    int countByExample(AuthenticationAppExample example);
	//按条件删除
    int deleteByExample(AuthenticationAppExample example);
	//按主键删除
    int deleteByPrimaryKey(String id);
	//插入数据(返回值为ID)
    int insert(AuthenticationApp record);
    int insertSelective(AuthenticationApp record);
    List<AuthenticationApp> selectByExampleWithRowbounds(AuthenticationAppExample example, RowBounds rowBounds);
	//按条件查询
    List<AuthenticationApp> selectByExample(AuthenticationAppExample example);
	//按主键查询
    AuthenticationApp selectByPrimaryKey(String id);
	//按条件更新值不为null的字段
    int updateByExampleSelective(@Param("record") AuthenticationApp record, @Param("example") AuthenticationAppExample example);
	//按条件更新
    int updateByExample(@Param("record") AuthenticationApp record, @Param("example") AuthenticationAppExample example);
	//按主键更新值不为null的字段
    int updateByPrimaryKeySelective(AuthenticationApp record);
	//按主键更新
    int updateByPrimaryKey(AuthenticationApp record); 
}

2.Example类实例函数:

example.setOrderByClause(“字段名 ASC”); 	添加升序排列条件,DESC为降序
example.setDistinct(false) 	去除重复,boolean型,true为选择不重复的记录。
criteria.andXxxIsNull 	添加字段xxx为null的条件
criteria.andXxxIsNotNull 	添加字段xxx不为null的条件
criteria.andXxxEqualTo(value) 	添加xxx字段等于value条件
criteria.andXxxNotEqualTo(value) 	添加xxx字段不等于value条件
criteria.andXxxGreaterThan(value) 	添加xxx字段大于value条件
criteria.andXxxGreaterThanOrEqualTo(value) 	添加xxx字段大于等于value条件
criteria.andXxxLessThan(value) 	添加xxx字段小于value条件
criteria.andXxxLessThanOrEqualTo(value) 	添加xxx字段小于等于value条件
criteria.andXxxIn(List<?>) 	添加xxx字段值在List<?>条件
criteria.andXxxNotIn(List<?>) 	添加xxx字段值不在List<?>条件
criteria.andXxxLike(“%”+value+”%”) 	添加xxx字段值为value的模糊查询条件
criteria.andXxxNotLike(“%”+value+”%”) 	添加xxx字段值不为value的模糊查询条件
criteria.andXxxBetween(value1,value2) 	添加xxx字段值在value1和value2之间条件
criteria.andXxxNotBetween(value1,value2) 	添加xxx字段值不在value1和value2之间条件

3.举例说一下一些Example函数的使用:
这个是建的表数据:
在这里插入图片描述
1.查询:selectByExample

@Test
	public void testSelect() throws Exception{//查看
		List<String> list = new ArrayList<>();
		list.add("感冒灵");
		list.add("小柴胡颗粒1");
		list.add("小柴胡颗粒2");
		AuthenticationAppExample authenticationAppExample = new AuthenticationAppExample(); 
		authenticationAppExample.createCriteria().andAppNameIn(list);//查询的是一个list集合
		List<AuthenticationApp> apps = authenticationAppMapper.selectByExample(authenticationAppExample);
		for(AuthenticationApp value:apps) {
			System.out.println("Id:" + value.getId() + "Name:" + value.getAppName());
		}
		System.out.println("测试结束!");
	}

2.删除:deleteByExample

@Test
	public void testDelete() {
		try {
			AuthenticationAppExample authenticationAppExample = new AuthenticationAppExample();
			authenticationAppExample.createCriteria().andAppNameEqualTo("感冒灵1");
			authenticationAppMapper.deleteByExample(authenticationAppExample);
			System.out.println("删除成功!");
		} catch (Exception e) {
			logger.info("输出错误信息" + e);
		}
	}

3.更新:updateByExampleSelective

@Test
	public void testUpdate() {//更新
		Date createTime = new Date();
		try {
			AuthenticationApp authenticationApp = new AuthenticationApp();
			AuthenticationAppExample authenticationAppExample = new AuthenticationAppExample();
			authenticationAppExample.createCriteria().andAppNameEqualTo("小柴胡颗粒");
			authenticationApp.setAppName("小柴胡颗粒2");
			authenticationApp.setCreateTime(createTime);
			authenticationAppMapper.updateByExampleSelective(authenticationApp, authenticationAppExample);
			System.out.println("更新成功!");
		} catch (Exception e) {
			logger.info("输出错误信息" + e);
		}
	}

4.插入:insert

@Test
	public void testInsert() throws Exception{//插入
	    AuthenticationApp authenticationApp  = new AuthenticationApp();
	    Date createTime = new Date();
		String id = "00012";
		String appName = "小柴胡颗粒99";
		authenticationApp.setId(id);
		authenticationApp.setAppName(appName);
		authenticationApp.setCreateTime(createTime);
		int insertsql = 2;
		try {
			 insertsql = authenticationAppMapper.insert(authenticationApp);
		} catch (Exception e) {
			System.out.println(e);
		}finally {
			if (insertsql == 1) {
				System.out.println("插入成功!");
			}else {
				System.out.println("插入不成功!");
			}
		}
	}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值