MyBatis 如何通过XML中配置的NameSpace和SQL的ID获取SQL字符串到JAVA
XML中为如下配置
<mapper namespace="com.wmxny.systemwork.persistence.StartIndexMapper">
<!-- 定义返回结果的映射类型
<resultMap id="StartIndex" type="com.wmxny.systemwork.entity.StartIndexEntity">-->
<!-- 省略写法,当属性为基本类型或者基本类型的封装类型时,可以省略javaType和jdbcType
<result column="ID" property="id"/>
<result column="User_No" property="userNo"/>
<result column="USER_NAME" property="userName"/>
</resultMap> -->
<!-- 根据用户名查找对应的用户 -->
<select id="selectAllUser" resultType="com.wmxny.systemwork.entity.StartIndexEntity">
<![CDATA[
select ID,User_No as userNo,User_Name as userName from bas_users
]]>
<where>
<if test="userName!=null">
AND USER_NAME = #{userName}
</if>
</where>
</select>
</mapper>
-------------------------------------------------
我想请教一下高人,如果实现这样一个函数:
public String getXmlSqlSyntax(String statement,HashMap params) {
...
return sqlSyntax;
}
String statement 传入字符串 com.wmxny.systemwork.persistence.StartIndexMapper.selectAllUser
就是名字空间+Select ID.
HashMap params 传 userName="老张"
返回的字符串为 select ID,User_No as userNo,User_Name as userName from bas_users where USER_NAME = "老张"
请高手解答,谢谢,在线等回复,分太少,希望大家帮帮忙。
------解决方案--------------------不知道你这样写有什么目的 , 不过你可以解析xml 得到你要的吧 ,个人觉得sax不错
------解决方案--------------------XML code
<!-- 根据用户名查找对应的用户 -->
<select id="selectAllUser" parameterClass="com.wmxny.systemwork.entity.StartIndexEntity" resultClass="com.wmxny.systemwork.entity.StartIndexEntity">
<isNotNull property="userName">
<![CDATA[
select 'select ID,User_No as userNo,User_Name as userName from bas_users AND USER_NAME =' + #{userName}# as sqlSyntax
]]>
</isNotNull>
</select>