Java – how to run arbitrary SQL with mybatis?

I have an application that uses mybatis for object persistence But I have the opportunity to run arbitrary SQL (from users) Can I do it with mybatis?

to update:

I chose to use dbutils (JDBC) to run user-defined SQL, but I needed a datasource instance to create queryrunner Is there any way to get the data source from mybatis?

Solution

I use this utility class:

import java.util.List;
import org.apache.ibatis.annotations.SelectProvider;

public interface sqlMapper {
    static class PuresqlProvider {
        public String sql(String sql) {
            return sql;
        }

        public String count(String from) {
            return "SELECT count(*) FROM " + from;
        }
    }

    @SelectProvider(type = PuresqlProvider.class,method = "sql")
    public List<?> select(String sql);

    @SelectProvider(type = PuresqlProvider.class,method = "count")
    public Integer count(String from);

    @SelectProvider(type = PuresqlProvider.class,method = "sql")
    public Integer execute(String query);
}
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>