当我们使用lombok插件时,对实体类加@Builder注解时:
@Data
@NoArgsConstructor
@AllArgsConstructor
@Builder
public class User {
private Long id;
private String name;
private String pass;
private Integer age;
private Integer roleId;
private Map<String, Object> dataMap = new HashMap<>();
private List<String> list = new ArrayList<>();
}
实体类如果存在集合属性,那么通过@Builder建造者模式和new生成的空对象中
class UserMapperTest {
@Test
public void test01() {
User user = User.builder().build();
User user2 = new User();
System.out.println(user);
System.out.println(user2);
}
}
会出现
User(id=null, name=null, pass=null, age=null, roleId=null, dataMap=null, list=null)
User(id=null, name=null, pass=null, age=null, roleId=null, dataMap={}, list=[])
结果。
当我们在进行数据库查询时,传入一个空的对象User,使用建造者模式会出现
Cause: org.apache.ibatis.ognl.OgnlException: source is null for getProperty(null, "name")] with root cause
org.apache.ibatis.ognl.OgnlException: source is null for getProperty(null, "name")
的错误。很坑,在此记录一下。