How to use Example in MyBatis
MyBatis Example how to use, many novices are not very clear about this, in order to help you solve this problem, the following small series will explain in detail for everyone, there are people who need this can learn, I hope you can gain something.
To use the example class, import the jar package mybatis.mapper into your project.
Mapper interface includes the function of adding, deleting, checking and paging a single table.
Take a look at the example, we are in the implementation class:
Example example = new Example(SysMenu.class);
example.createCriteria().andEqualTo("status","0");//only get menu of available status
List menus = menuMapper.selectByExample(example);
where SysMenu is the entity class of the system menu.
Take a look at this menuMapper:
public interface MenuMapper extends Mapper {
/**
* Get menu list
* @param sysMenu
* @param pager
* @return
*/
List getList(@Param("menu") SysMenu sysMenu,
@Param("pager") Pager pager);
}
It inherits Mapper , and it has a method in it. But it's not what we gave above.
It can be seen that this selectByExample is provided by Mapper.
Steps:
First create an instance Example = new Example(SysMenu.class); Add query criteria: example.createCriteria().andEqualTo("status","0");//Get only available status menu Finally call selectByExample to query:
List menus = menuMapper.selectByExample(example);
Example function of mapper in mybatis:
example Example analysis
Mybatis reverse engineering will generate instances and examples corresponding to instances, examples are used to add conditions, equivalent to the part after where
xxxExample example = new xxxExample();
Criteria criteria = new Example().createCriteria();
Did reading the above help you? If you still want to have further understanding of related knowledge or read more related articles, please pay attention to the industry information channel, thank you for your support.