MyBatis使用Criteria

MyBatis使用Criteria

MyBatis使用Criteria能够不编写SQL语句,只是不断地拼接调用方法,就能创建新的SQL语句。

示例

效果如下:

该方法的作用是根据类型编号查找商品,能够获得一个商品列表

public List<Goods> selectByTypeId(Integer typeId){
	GoodsExample exmaple  = new GoodsExample();
	Criteria criteria = exmaple.createCriteria().andTypesEqualTo(typeId);
	
	List<Goods> goodsList = goodsMapper.selectByExample(exmaple);
	
	return goodsList;
}

数据库如下:

1561688184842

查询结果如下:

1561688192266

自动生成

在使用Ceriteria之前,需要做一些准备工作,如下:

  1. 在MyBatisGenerator中,配置可以生成Example

    添加eanblexxxxxx,并且配置为true

    
    <table catalog="ssmstoredb" tableName="%" 
           enableCountByExample="true" 
           enableUpdateByExample="true"
           enableDeleteByExample="true" 
           enableSelectByExample="true"
           selectByExampleQueryId="true">
    table>
  2. 执行MyBatisGenerator配置文件

    执行完后能够看到自动生成的文件,接下来看一下各个文件都有什么

POJO

生成了两个文件,一个是Goods实体类文件,另一个是GoodsExample文件,这里只详细说明后者。

1561688466617

生成的GoodsExample文件详解参考

MyBatis的Mapper接口以及Example的实例函数及详解

Mapper

查看另一个文档!

你可能感兴趣的:(mybatis)