MyBatis 如何通过XML中配置的NameSpace和SQL的ID获取SQL字符串到JAVA

tonghuizx 2012-05-08 01:30:25
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 = "老张"

请高手解答,谢谢,在线等回复,分太少,希望大家帮帮忙。
...全文
1004 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
tonghuizx 2012-05-09
  • 打赏
  • 举报
回复
[Quote=引用 6 楼 的回复:]
XML code

<!-- 根据用户名查找对应的用户 -->
<select id="selectAllUser" parameterClass="com.wmxny.systemwork.entity.StartIndexEntity" resultClass="com.wmxny.systemwork.entity.StartIndexEntity">
<isNotNul……
[/Quote]谢谢,不过我想要的是与Entity无关的那种

我看到过代码

public <E> List<E> selectList(String statement, Object parameter, RowBounds rowBounds) {
try {
MappedStatement ms = configuration.getMappedStatement(statement);
List<E> result = executor.<E>query(ms, wrapCollection(parameter), rowBounds, Executor.NO_RESULT_HANDLER);
return result;
} catch (Exception e) {
throw ExceptionFactory.wrapException("Error querying database. Cause: " + e, e);
} finally {
ErrorContext.instance().reset();
}
}


不知道 executor.<E>query 里面是怎么实现的,请教了
tonghuizx 2012-05-09
  • 打赏
  • 举报
回复
自已顶自已
德薄才浅 2012-05-08
  • 打赏
  • 举报
回复
  <!-- 根据用户名查找对应的用户 -->
<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>

其中sqlSyntax必须为StartIndexEntity的一个成员变量有setter getter 给方法调用后会返回一个StartIndexEntity对象,通过该对象取sqlSyntax。
tonghuizx 2012-05-08
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 的回复:]
不知道你这样写有什么目的 , 不过你可以解析xml 得到你要的吧 ,个人觉得sax不错
[/Quote]

解析的话太累了,我就是希望可以通过MyBatis的接口拿到字符串,有没有什么方法呢?
huage 2012-05-08
  • 打赏
  • 举报
回复
不知道你这样写有什么目的 , 不过你可以解析xml 得到你要的吧 ,个人觉得sax不错
tonghuizx 2012-05-08
  • 打赏
  • 举报
回复
我在线等呢,没人捧场啊,真是冷清
tonghuizx 2012-05-08
  • 打赏
  • 举报
回复
自已顶吧

81,092

社区成员

发帖
与我相关
我的任务
社区描述
Java Web 开发
社区管理员
  • Web 开发社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧