java调用mybatis,如何使用mybatis直接从Java代码获取查询?

I need to execute query which has been generated by java code in runtime (not static method). I already know how to build dynamic query by using annotation and static method or using xml mapper, but it is not suitable in my case.

Is there any way to execute query from java code directly?

解决方案

Mybatis has already this function, but you must use the adapter as follows.

create an adapter class;

public class SQLAdapter {

String sql;

public SQLAdapter(String sql) {

this.sql = sql;

}

public String getSql() {

return sql;

}

public void setSql(String sql) {

this.sql = sql;

}

}

create typeAlias of class SQLAdapter

put select tag in each object xml where you need to execute the sql directly.

${sql}

call this select method like

String _sql = "select * from table where... order by... limit...";

xxxxx.findRecords(new SQLAdapter(_sql));

Things have been all done. you can no longer writer complex sql language in the xml file. Good Luck.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值