在mybatis的mapper.xml文件中编写了一段SQL语句如下:
我编写的SQL如下:
<!-- 根据角色ID、角色类型、用户ID查询出角色列表--> <select id="getUsersByRoleAuthorize" resultMap="RoleResultMap"> SELECT u.USER_ID, u.user_name, u.user_status, ur.ROLE_ID, r.role_name, ur.ROLE_TYPE, ur.created_by, ur.creation_date, ur.modified_by, ur.modified_date FROM t_user_roles ur,t_user u ,t_roles r WHERE ur.user_id = u.user_id AND r.role_id = ur.role_id <if test="roleAuthorize.roleAuthorizeId != null and roleAuthorize.roleAuthorizeId != ''"> and ur.role_id = #{roleAuthorize.roleAuthorizeId} </if> <if test="roleAuthorize.roleAuthorizeType != null and roleAuthorize.roleAuthorizeType != ''"> and ur.roleType = #{roleAuthorize.roleAuthorizeType} </if> <if test="roleAuthorize.userId != null and roleAuthorize.userId != ''"> and ur.user_id = #{roleAuthorize.userId} </if> </select>
解决方案:
错误的原因在于在Mybatis接口类传入进来的对象不需要xxx对象.xxx属性,如果使用这种就会抛出以下异常,. 直接使用属性名即可。
改成以下就没有问题了:
<!-- 根据角色ID、角色类型、用户ID查询出角色列表--> <select id="getUsersByRoleAuthorize" resultMap="RoleResultMap"> SELECT u.USER_ID, u.user_name, u.user_status, ur.ROLE_ID, r.role_name, ur.ROLE_TYPE, ur.created_by, ur.creation_date, ur.modified_by, ur.modified_date FROM t_user_roles ur,t_user u ,t_roles r WHERE ur.user_id = u.user_id AND r.role_id = ur.role_id <if test="roleAuthorizeId != null and roleAuthorizeId != ''"> and ur.role_id = #{roleAuthorizeId} </if> <if test="roleAuthorizeType != null and roleAuthorizeType != ''"> and ur.roleType = #{roleAuthorizeType} </if> <if test="userId != null and userId != ''"> and ur.user_id = #{userId} </if> </select>
异常信息:
org.apache.ibatis.builder.BuilderException: Error creating document instance. Cause: org.xml.sax.SAXParseException: The entity name must immediately follow the '&' in the entity reference.
at org.apache.ibatis.parsing.XPathParser.createDocument(XPathParser.java:238)
at org.apache.ibatis.parsing.XPathParser.<init>(XPathParser.java:107)
at org.apache.ibatis.builder.xml.XMLMapperBuilder.<init>(XMLMapperBuilder.java:61)
at org.apache.ibatis.builder.xml.XMLMapperBuilder.<init>(XMLMapperBuilder.java:56)
at org.apache.ibatis.builder.annotation.MapperAnnotationBuilder.loadXmlResource(MapperAnnotationBuilder.java:118)
at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(XML11Configuration.java:808)
at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(XML11Configuration.java:737)
at com.sun.org.apache.xerces.internal.parsers.XMLParser.parse(XMLParser.java:119)
at com.sun.org.apache.xerces.internal.parsers.DOMParser.parse(DOMParser.java:232)
at com.sun.org.apache.xerces.internal.jaxp.DocumentBuilderImpl.parse(DocumentBuilderImpl.java:284)
at org.apache.ibatis.parsing.XPathParser.createDocument(XPathParser.java:236)
... 60 more