1. 实战更新
package com. gblfy. springboot. mybatisplus. mapper; import com. baomidou. mybatisplus. core. conditions. query. LambdaQueryWrapper;
import com. baomidou. mybatisplus. core. conditions. query. QueryWrapper;
import com. baomidou. mybatisplus. core. conditions. update. LambdaUpdateWrapper;
import com. baomidou. mybatisplus. core. conditions. update. UpdateWrapper;
import com. baomidou. mybatisplus. core. toolkit. StringUtils;
import com. baomidou. mybatisplus. core. toolkit. Wrappers;
import com. baomidou. mybatisplus. extension. conditions. query. LambdaQueryChainWrapper;
import com. baomidou. mybatisplus. extension. conditions. update. LambdaUpdateChainWrapper;
import com. gblfy. springboot. mybatisplus. entity. User;
import lombok. extern . slf4j. Slf4j;
import org. junit. Assert;
import org. junit. Test;
import org. junit. runner. RunWith;
import org. springframework. beans. factory. annotation. Autowired;
import org. springframework. boot. test. context. SpringBootTest;
import org. springframework. test. context. junit4. SpringJUnit4ClassRunner; import java. util. Arrays;
import java. util. HashMap;
import java. util. List;
import java. util. Map; @RunWith ( SpringJUnit4ClassRunner. class)
@SpringBootTest
@Slf4j
public class UserUpdateTest { @Autowiredprivate UserMapper userMapper; @Testpublic void updateById ( ) { User user = new User ( ) ; user. setId ( 1238735228017610754l ) ; user. setEmail ( "gblfy@163.com" ) ; user. setAge ( 22 ) ; int rows = userMapper. updateById ( user) ; System. out. println ( "影响记录数:" + rows) ; } @Testpublic void updateByWrapper ( ) { UpdateWrapper< User> updateWrapper = new UpdateWrapper< > ( ) ; updateWrapper. eq ( "name" , "Tom" ) . eq ( "age" , 28 ) ; User user = new User ( ) ; user. setEmail ( "gblfy2@sino.com" ) ; user. setAge ( 22 ) ; int rows = userMapper. update ( user, updateWrapper) ; System. out. println ( "影响记录数:" + rows) ; } @Testpublic void updateByWrapper2 ( ) { UpdateWrapper< User> updateWrapper = new UpdateWrapper< > ( ) ; updateWrapper. eq ( "name" , "Tom" ) . eq ( "age" , 28 ) . set ( "age" , 30 ) ; int rows = userMapper. update ( null, updateWrapper) ; System. out. println ( "影响记录数:" + rows) ; } @Testpublic void updateByWrapperLambda ( ) { LambdaUpdateWrapper< User> lambdaUpdateWrapper = Wrappers. < User> lambdaUpdate ( ) ; lambdaUpdateWrapper. eq ( User: : getName, "Tom" ) . eq ( User: : getAge, 28 ) . set ( User: : getAge, 30 ) ; int rows = userMapper. update ( null, lambdaUpdateWrapper) ; System. out. println ( "影响记录数:" + rows) ; } @Testpublic void updateByWrapperLambdaChain ( ) { boolean sign = new LambdaUpdateChainWrapper< User> ( userMapper) . eq ( User: : getName, "Tom" ) . eq ( User: : getAge, 22 ) . set ( User: : getAge, 30 ) . update ( ) ; System. out. println ( sign) ; }
}