MyBatis Mapper Interface and example instance functions and explanations

Source: Internet
Author: User

Method parsing in Mapper interface

Functions and methods in the Mapper interface

Method function Description
int Countbyexample (userexample example) Thorws SQLException Count by criteria
int Deletebyprimarykey (Integer id) thorws SQLException Delete by primary key
int Deletebyexample (userexample example) Thorws SQLException Query by criteria
String/integer Insert (User record) Thorws SQLException Insert data (return value is ID)
User Selectbyprimarykey (Integer ID) thorws SQLException Query by PRIMARY key
Listselectbyexample (userexample example) Thorws SQLException Query by criteria
Listselectbyexamplewithblogs (userexample example) Thorws SQLException Query by criteria (including BLOB fields). Only if the field type in the data table is binary is it generated.
int Updatebyprimarykey (User record) Thorws SQLException Press the primary key to update
int updatebyprimarykeyselective (User record) Thorws SQLException Update a field with a value that is not NULL by the primary key
int Updatebyexample (User record, userexample example) Thorws SQLException Update by condition
int updatebyexampleselective (User record, userexample example) Thorws SQLException Update a field with a value that is not NULL by condition
Second, example case analysis

MyBatis in reverse engineering will generate instances and instances corresponding to the example,example used to add conditions, quite where the later part
xxxexample example = new Xxxexample ();
criteria = new Example (). Createcriteria ();

Method Description
Example.setorderbyclause ("Field name ASC"); Add ascending sort condition, desc is descending
Example.setdistinct (False) Remove Duplicates, Boolean, True to select records that are not duplicates.
Criteria.andxxxisnull Add a field XXX null condition
Criteria.andxxxisnotnull Add field XXX non-null condition
Criteria.andxxxequalto (value) Add xxx field equals value condition
Criteria.andxxxnotequalto (value) Adding the XXX field is not equal to the value condition
Criteria.andxxxgreaterthan (value) Add the XXX field is greater than the value condition
Criteria.andxxxgreaterthanorequalto (value) Add the XXX field is greater than or equal to the value condition
Criteria.andxxxlessthan (value) Adding the xxx field is less than the value condition
Criteria.andxxxlessthanorequalto (value) Add the xxx field is less than or equal to the value condition
Criteria.andxxxin (list<? >) Add xxx field value in List<? > Conditions
Criteria.andxxxnotin (list<? >) Add XXX field value not list<? > Conditions
Criteria.andxxxlike ("%" +value+ "%") Add a fuzzy query condition with a value of XXX field
Criteria.andxxxnotlike ("%" +value+ "%") Add a fuzzy query condition with the XXX field value not being value
Criteria.andxxxbetween (value1,value2) Add the XXX field values between value1 and value2 conditions
Criteria.andxxxnotbetween (value1,value2) Adding the XXX field value is not a condition between value1 and value2
Iii. examples of application 1. Enquiry

①selectbyprimarykey ()

User user = XxxMapper.selectByPrimaryKey(100); //相当于select * from user where id = 100
    • 1

②selectbyexample () and Selectbyexamplewithblogs ()

new UserExample();Criteria criteria = example.createCriteria();criteria.andUsernameEqualTo("wyw");criteria.andUsernameIsNull();example.setOrderByClause("username asc,email desc");List<?>list = XxxMapper.selectByExample(example);//相当于:select * from user where username = ‘wyw‘ and username is null order by username asc,email desc

Note: The Ibator reverse engineering generated file Xxxexample.java contains a static inner class Criteria,criteria method that defines the query condition after the SQL statement where.

2. Inserting data

①insert ()

User user = new User();user.setId("dsfgsdfgdsfgds");user.setUsername("admin");user.setPassword("admin")user.setEmail("[email protected]");XxxMapper.insert(user);//相当于:insert into user(ID,username,password,email) values (‘dsfgsdfgdsfgds‘,‘admin‘,‘admin‘,‘[email protected]‘);
3. Updating data

①updatebyprimarykey ()

User user =new User();user.setId("dsfgsdfgdsfgds");user.setUsername("wyw");user.setPassword("wyw");user.setEmail("[email protected]");XxxMapper.updateByPrimaryKey(user);//相当于:update user set username=‘wyw‘, password=‘wyw‘, email=‘[email protected]‘ where id=‘dsfgsdfgdsfgds‘

②updatebyprimarykeyselective ()

User user = new User();user.setId("dsfgsdfgdsfgds");user.setPassword("wyw");XxxMapper.updateByPrimaryKey(user);//相当于:update user set password=‘wyw‘ where id=‘dsfgsdfgdsfgds‘

③updatebyexample () and updatebyexampleselective ()

UserExample example = new UserExample();Criteria criteria = example.createCriteria();criteria.andUsernameEqualTo("admin");User user = new User();user.setPassword("wyw");XxxMapper.updateByPrimaryKeySelective(user,example);//相当于:update user set password=‘wyw‘ where username=‘admin‘

Updatebyexample () Update all fields, including NULL, are also updated, we recommend using Updatebyexampleselective () to update the fields you want to update

4. Delete data

①deletebyprimarykey ()

XxxMapper.deleteByPrimaryKey(1);  //相当于:delete from user where id=1

②deletebyexample ()

UserExample example = new UserExample();Criteria criteria = example.createCriteria();criteria.andUsernameEqualTo("admin");XxxMapper.deleteByExample(example);//相当于:delete from user where username=‘admin‘
5. Query the number of data

①countbyexample ()

UserExample example = new UserExample();Criteria criteria = example.createCriteria();criteria.andUsernameEqualTo("wyw");int count = XxxMapper.countByExample(example);//相当于:select count(*) from user where username=‘wyw‘

MyBatis Mapper Interface and example instance functions and explanations

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.