如何解析Mybatis xml文件中配置的sql

 我来答
wf...2@163.com
2017-03-23 · TA获得超过109个赞
知道小有建树答主
回答量:302
采纳率:0%
帮助的人:273万
展开全部
首先抽象一个SQL语句类:

[java] view plain copy
public abstract class AbstractSQLStatementNode implements ISQLStatement {

protected List<ISQLStatement> sqlStatements = new ArrayList<>();

@Override
public void appendSQL(ISQLStatement sql) {
sqlStatements.add(sql);
}

@Override
public String getSQL(Object object) {
StringBuilder sb = new StringBuilder();
for (ISQLStatement stmt : sqlStatements) {
sb.append(stmt.getSQL(object));
}
return sb.toString().trim();
}

}

然后是文本SQL:

[java] view plain copy
public class PlainSQLNode extends AbstractSQLStatementNode {

private final String sql;

public PlainSQLNode(String sql) {
this.sql = sql;
}

@Override
public String getSQL(Object object) {
return sql;
}

@Override
public String toString() {
return sql;
}

}

以Mybatis xml中<if test="...">...</if>为例

[java] view plain copy
public class IfSQLNode extends AbstractSQLStatementNode {

private final String test;

public IfSQLNode(String test) {
this.test = test;
}

public String getTest() {
return test;
}

@Override
public String getSQL(Object object) {
try {
Boolean b = (Boolean) Ognl.getValue(test, object, Boolean.TYPE);
return b ? super.getSQL(object) : "";
}
catch (OgnlException e) {
throw new SQLSyntaxException(e);
}
}

}

再增加一个Include标签例

[java] view plain copy
public class IncludeSQLNode extends AbstractSQLStatementNode {

private final TableNode node;
private final String refid;

public IncludeSQLNode(TableNode node, String refid) {
this.node = node;
this.refid = refid;
}

@Override
public String getSQL(Object object) {
SQLNode sqlnode = node.getSQLNode(refid);
return sqlnode.getSQL(object);
}

}

其它的xml标签类似,完成最终的sql

调用AbstractSQLStatementNode.getSQL方法,参数为客户端传入参数

XML例子如下:

[html] view plain copy
<select id="findPage" parameterType="java.util.Map" resultMap="ORG">
SELECT * FROM XT_ORG WHERE BDELETE<>1 <if test="name != null"> AND NAME=#{name}</if> LIMIT #{OFFSET}, #{ROWS}
</select>

根据是否传入了name,即可成功执行出最终的sql

SELECT * FROM XT_ORG WHERE BDELETE<>1 AND NAME=#{name} LIMIT #{OFFSET}, #{ROWS}

再解析SQL得到,并提出参数

SELECT * FROM XT_ORG WHERE BDELETE<>1 AND NAME=? LIMIT ?,?
即可生成最终的PrepareStatement
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式