MyBatis-Plus之ActiveRecord
@Data
@NoArgsConstructor
@AllArgsConstructor
public class User extends Model < User > {
private Long id;
private String userName;
@TableField ( select = false )
private String password;
private String name;
private Integer age;
@TableField ( value = "email" )
private String mail;
@TableField ( exist = false )
private String address;
}
根据主键查询
不需要注入Bean 但是必须要有UserMapper接口
@Test
public void testSelectById ( ) {
User user = new User ( ) ;
user. setId ( 2L ) ;
User user1 = user. selectById ( ) ;
System . out. println ( user1) ;
}
新增数据
@Test
public void testInsert ( ) {
User user = new User ( ) ;
user. setId ( 2L ) ;
user. setUserName ( "liubei" ) ;
user. setPassword ( "123456" ) ;
user. setAge ( 20 ) ;
user. setName ( "刘备" ) ;
user. setMail ( "123456@qq.com" ) ;
boolean insert = user. insert ( ) ;
System . out. println ( insert) ;
}
INSERT INTO tb_user ( user_name, password, name, age, email ) VALUES ( ?, ?, ?, ?, ? )
更新数据
@Test
public void testUpadate ( ) {
User user = new User ( ) ;
user. setId ( 11L ) ;
user. setName ( "刘备" ) ;
boolean result = user. updateById ( ) ;
System . out. println ( result) ;
}
UPDATE tb_user SET name= ? WHERE id= ?
删除操作
@Test
public void testDelet ( ) {
User user = new User ( ) ;
boolean result = user. deleteById ( 11L ) ;
System . out. println ( result) ;
}
DELETE FROM tb_user WHERE id= ?
根据条件查询数据
@Test
public void testSelectByCondition ( ) {
User user = new User ( ) ;
QueryWrapper < User > wrapper = new QueryWrapper < > ( ) ;
wrapper. ge ( "age" , 30 ) ;
List < User > users = user. selectList ( wrapper) ;
users. forEach ( user1 -> System . out. println ( user1) ) ;
}
SELECT id, user_name, name, age, email AS mail FROM tb_user WHERE age >= ?