mybatis引用嵌套对象属性,导致ognlexception

bprjcwpo  于 2021-07-26  发布在  Java
关注(0)|答案(1)|浏览(535)

我在mybatis sql查询中遇到了一些问题。这是我的对象类:

public class UserRoleTO extends BaseObject {
    private String nric;
    private String unit;
    private String name;
    private String rankCode;
    private String rank;
    private String status;
    private RoleTO role;
    private Date dteUpdated = null;
    private String dateUpdated = null;
    private String updatedByNric = null;
    private String updatedByName = null;
    private String unitDesc = null;

    private MenuTO menu = null;
    private ArrayList accessRights = null;

    // setters and getters
}

以及mybatis sql查询:

<select id="getUserRole" resultMap="userRoleResult"
    parameterType="sc.securityMgt.integration.to.UserRoleTO">
    SELECT
    NRIC,
    NAME,
    ROLE_ID,
    UNIT_CODE,
    DATE_UPDATED,
    UPDATED_BY,
    UPDATED_BY_NAME,
    DEFUNCT_IND
    FROM
    USER_ROLE
    <trim prefix="WHERE" prefixOverrides="AND">
        <if test="nric != null">USER_ROLE.NRIC = #{nric} </if>
        <if test="role.roleID != null">AND USER_ROLE.ROLE_ID = #{role.roleID} </if>
    </trim>     
</select>

我打印了我的roleid,我打印了我的roleid值,它是空的,但是在这个例子中,既然它是空的,它不应该附加where子句吗?我收到错误消息:

Caused by: org.apache.ibatis.builder.BuilderException: Error evaluating expression 'role.roleID != null'. Cause: org.apache.ibatis.ognl.OgnlException: source is null for getProperty(null, "roleID")
    at org.apache.ibatis.scripting.xmltags.OgnlCache.getValue(OgnlCache.java:48)
    at org.apache.ibatis.scripting.xmltags.ExpressionEvaluator.evaluateBoolean(ExpressionEvaluator.java:32)
    at org.apache.ibatis.scripting.xmltags.IfSqlNode.apply(IfSqlNode.java:34)
    at org.apache.ibatis.scripting.xmltags.MixedSqlNode.lambda$apply$0(MixedSqlNode.java:32)
    at java.util.ArrayList.forEach(ArrayList.java:1257)
    at org.apache.ibatis.scripting.xmltags.MixedSqlNode.apply(MixedSqlNode.java:32)
    at org.apache.ibatis.scripting.xmltags.TrimSqlNode.apply(TrimSqlNode.java:55)
    at org.apache.ibatis.scripting.xmltags.MixedSqlNode.lambda$apply$0(MixedSqlNode.java:32)
    at java.util.ArrayList.forEach(ArrayList.java:1257)
    at org.apache.ibatis.scripting.xmltags.MixedSqlNode.apply(MixedSqlNode.java:32)
    at org.apache.ibatis.scripting.xmltags.DynamicSqlSource.getBoundSql(DynamicSqlSource.java:39)
    at org.apache.ibatis.mapping.MappedStatement.getBoundSql(MappedStatement.java:305)
    at org.apache.ibatis.executor.CachingExecutor.query(CachingExecutor.java:87)
    at org.apache.ibatis.session.defaults.DefaultSqlSession.selectList(DefaultSqlSession.java:147)
    at org.apache.ibatis.session.defaults.DefaultSqlSession.selectList(DefaultSqlSession.java:140)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at org.mybatis.spring.SqlSessionTemplate$SqlSessionInterceptor.invoke(SqlSessionTemplate.java:434)
    ... 47 more
Caused by: org.apache.ibatis.ognl.OgnlException: source is null for getProperty(null, "roleID")
    at org.apache.ibatis.ognl.OgnlRuntime.getProperty(OgnlRuntime.java:3331)
    at org.apache.ibatis.ognl.ASTProperty.getValueBody(ASTProperty.java:121)
    at org.apache.ibatis.ognl.SimpleNode.evaluateGetValueBody(SimpleNode.java:212)
    at org.apache.ibatis.ognl.SimpleNode.getValue(SimpleNode.java:258)
    at org.apache.ibatis.ognl.ASTChain.getValueBody(ASTChain.java:141)
    at org.apache.ibatis.ognl.SimpleNode.evaluateGetValueBody(SimpleNode.java:212)
    at org.apache.ibatis.ognl.SimpleNode.getValue(SimpleNode.java:258)
    at org.apache.ibatis.ognl.ASTNotEq.getValueBody(ASTNotEq.java:50)
    at org.apache.ibatis.ognl.SimpleNode.evaluateGetValueBody(SimpleNode.java:212)
    at org.apache.ibatis.ognl.SimpleNode.getValue(SimpleNode.java:258)
    at org.apache.ibatis.ognl.Ognl.getValue(Ognl.java:560)
    at org.apache.ibatis.ognl.Ognl.getValue(Ognl.java:524)
    at org.apache.ibatis.scripting.xmltags.OgnlCache.getValue(OgnlCache.java:46)
    ... 66 more

关于如何在mybatis sql查询中引用嵌套对象属性,有什么想法吗?谢谢!

waxmsbnn

waxmsbnn1#

错误是“source is null for getproperty(null,“roleid”)”,这意味着 rolenull .
添加一个空检查应该可以解决这个问题。

<where>
  <if test="nric != null">USER_ROLE.NRIC = #{nric}</if>
  <if test="role != null and role.roleID != null">AND USER_ROLE.ROLE_ID = #{role.roleID} </if>
</where>

在大多数情况下,您可以使用 <where /> 而不是 <trim /> .

相关问题