The use of example class in Mybatis

To use the example class, first import the jar package of mybatis.mapper in the project.

The Mapper interface includes the addition, deletion, modification, and paging functions of a single table.

Give an example:

CountryMappermapper = sqlSession.getMapper(Country.class);

//Country.class is the entity class

// query operation

List<Country>cList = mapper.select(new Country());

Now query with Example

Example example =new Example(Country.class);

example.createCriteria().andEqualTo(“id”,100);

//The query given here is id=100

cList = mapper.selectByExample(example);

 

example.setOrderByClause("field name ASC"); sort by a field in ascending order

example.setDistinct(false)// Remove duplicates, boolean type, true is to select non-duplicate records

selectByExample() returns a collection

Instance function of mapper in mybatis:
int countByExample(UserExample example) thorws SQLException: count by condition.
int deleteByPrimaryKey(Integer id) thorws SQLException: Delete by primary key.
int deleteByExample(UserExample example) thorws SQLException: Delete by condition.
String/Integer insert(User record) thorws SQLException: Insert (return value is id value)
User selectByPrimaryKey(Integer id) thorws SQLException: Query by primary key.
List<?>selectByExample(UserExample example) thorws SQLException: Query by condition
List<?>selectByExampleWithBLOGs(UserExample example) thorws SQLException: Press

Conditional queries (including BLOB fields). It will only be generated if the field type in the data table is binary.
int updateByPrimaryKey(User record) thorws SQLException: Updating by primary key
int updateByPrimaryKeySelective(User record) thorws SQLException: Updating fields whose value is not null by primary key

int updateByExample(User record, UserExample example) thorws SQLException: Update by condition

int updateByExampleSelective(User record, UserExample example)thorws  

SQLException: Conditionally update field whose value is not null

Detailed explanation of the instance function of mapper in mybatis:
 selectByPrimaryKey()

Country country = ##Mapper.selectByPrimaryKey(100);

Equivalent to select * from user where id = 100

There are also some methods that are not described here, you can refer to the example in mybatis



Reprinted from http://blog.csdn.net/qq_36743013/article/details/71144508


Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325481814&siteId=291194637