MyBatis Example Class Details

Posted by itsjareds on Wed, 31 Jul 2019 10:58:47 +0200

About the definition of Example class?
Mybatis-generator generates Criterion for each field and creates dynamic sql for the underlying mapper.xml. If there are more fields in the table, the example class will be very large. In theory, you can construct any filter condition you think of by using the example class. Configured in mybatis-generator, the generation operation of configuration data table can automatically generate example. There are no more explanations for defining types, just go straight to dry goods.

1. Query:
(1)selectByPrimaryKey

User user = UserMapper.selectByPrimaryKey("1001");
//Equivalent to: select * from user where id = 1001 

(2)selectByExample (and condition)

Example example = new Example(User.class);
example.createCriteria().andEqualTo("id", "1001" )
	.andEqualTo("userName", "petty thief");
User user = UserMapper.selectByExample(example);

//Equivalent to: select * from user where id = 1001 and username = Xiaoli'

selectByExample (or condition)

Example example = new Example(User.class);
example.or.andEqualTo("id", "1001" )
example.or.andEqualTo("userName", "petty thief");
User user = UserMapper.selectByExample(example);

//Equivalent to: select * from user where id = 1001 or username = Xiaoli'

selectByExample (and+or multi-condition query)

Example example = new Example(User.class);
        example.createCriteria().andEqualTo("id", "1" )
                .andEqualTo("userId", "1001");
        example.and().orEqualTo("userName","petty thief")
                .orEqualTo("passWord","123");
        List<User> user = UserMapper.selectByExample(example);

//Equivalent to:
SELECT id,user_id,user_name,pass_word FROM user WHERE ( id = "1" and user_id = "1001" ) and ( user_name = "petty thief" or pass_word = "123" )

2. Insert:

User user = new User();
user.setId("2");
user.setUserId("1002");
user.setUserName("Xiao Wang");
user.setPassWord("666")
UserMapper.insert(user);
//Equivalent to: insert into user (id, user_id, user_name, pass_word) values ('2','1002','Xiaowang','666');

3. Update:
(1)updateByPrimaryKey

User user = new User();
user.setId("2");
user.setUserId("1002");
user.setUserName("Xiao Wang");
user.setPassWord("666")
UserMapper.updateByPrimaryKey(user);
//Equivalent to: update user set user_id= "1002" user_name='Xiaowang', pass_word='666'where id='2'

(2)updateByExampleSelective

Note: updateByExample() updates all fields, including null fields. It is recommended that updateByExampleSelective() be used to update fields that need to be updated.

Example example = new Example(User.class);
example.createCriteria().andEqualTo("id","2");

User user = new User();
user.setUserName("Xiaowang");

UserMapper.updateByExampleSelective(user,example);

Equivalent to: update user set user_name='Xiaowang'where id='2'

4. Delete:
(1)deleteByPrimaryKey

UserMapper.deleteByPrimaryKey("1");  //Equivalent to: delete from user where id="1"

(2)deleteByExample

Example example = new Example(User.class);
example.createCriteria().andEqualTo("userId","1002");
UserMapper.deleteByExample(example);
//Equivalent to: delete from user where userId='1002'

5. Number of queries:

Example example = new Example(User.class);
example.createCriteria().andEqualTo("id", "1001" )

UserMapper.countByExample(example);
//Equivalent to: select count(*) from user where id='1001'

Topics: Mybatis SQL xml