查询专栏:MP默认查询原则
查询的是数据库列名非实体类属性
文章目录
AR 模式
1. 实体类对象继承Model
@EqualsAndHashCode(callSuper = false)
@Data
public class User extends Model<User> {
// 主键
private Long id;
//姓名
@TableField(condition = SqlCondition.LIKE)
private String name;
//年龄
@TableField(condition ="%s<#{%s}")
private Integer age;
//邮箱
private String email;
// 上级id
private Long manager_id;
//创建时间
private LocalDateTime createTime;
}
2. 接口服务类继承baseMapper接口
public interface UserMapper extends BaseMapper<User> {}
3. 测试AR
@Component
public class UserMapperARTest extends MybatisPlusSamplesChapter1ApplicationTests {
@Test
public void insertAR(){
User user = new User();
user.setName("刘华");
user.setAge(20);
user.setEmail("test@gblfy.com");
user.setManager_id(1111L);
user.setCreateTime(LocalDateTime.now());
boolean insert = user.insert();
System.out.println(insert);
}
@Test
public void selectByIdAR(){
User user = new User();
User user1 = user.selectById(111L);
System.out.println(user == user1);
System.out.println(user1);
}
@Test
public void selectByIdAR2(){
User user = new User();
user.setId(11L);
User user1 = user.selectById();
System.out.println(user == user1);
System.out.println(user1);
}
@Test
public void updateByIdAR(){
User user = new User();
user.setId(11L);
user.setName("你的纳斯达克");
boolean user1 = user.updateById();
System.out.println(user1);
}
@Test
public void delByIdAR(){
User user = new User();
user.setId(11L);
boolean user1 = user.deleteById();
System.out.println(user1);
}
}
特点:直接操纵数据库
想学习更多微服务、分布式、中间件、数据库、项目快速构建等系列技术
请访问http://gblfy.com
让我们一起进步!!!