简体   繁体   English

MyBatis 3.1.1,如何直接执行自定义SQL代码

[英]MyBatis 3.1.1, how to execute custom SQL code directly

I use PostrgeSQL 9.6 and MyBatis 3.1 (under there is also Java 8 and Spring), and I need to execute a custom query, which I create dynamically with a StringBuilder because of it's complexity.我使用 PostrgeSQL 9.6 和 MyBatis 3.1(下面还有 Java 8 和 Spring),我需要执行一个自定义查询,我使用 StringBuilder 动态创建它,因为它很复杂。

How can I pass it to my class mapper.xml file ?如何将它传递给我的类 mapper.xml 文件?

I've heard about @SelectProvider, but ?我听说过@SelectProvider,但是? can't find complete examples... Can someone give me a step by step guide ?找不到完整的例子......有人可以给我一个分步指南吗?

I was also reading of The SQL Builder Class, of MyBatis, following the official guide but I miss how to launch that query/object I've created.我也在阅读 MyBatis 的 SQL Builder Class,遵循官方指南,但我想念如何启动我创建的查询/对象。 By the way this doesn't seems the right way to me, because of the complexity of the query I have to build.顺便说一句,这对我来说似乎不是正确的方法,因为我必须构建的查询很复杂。 Following the guide it seems I can't use contidional operators like IF or FOR to create the query string... So it can't work for my use.按照指南,我似乎不能使用像 IF 或 FOR 这样的条件运算符来创建查询字符串......所以它不能供我使用。

thanks.谢谢。

I only use @SelectProvider for java annotation ,this is a easy way to use it , this is my simple example earlier this year我只将 @SelectProvider 用于 java 注释,这是一种简单的使用方法,这是我今年早些时候的简单示例

1. create your provider class 1. 创建你的提供者类

package com.mybatis;

import java.util.Map;
import java.util.logging.Logger;

import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.jdbc.SQL;

public class SqlProvider {

    public String sqlProvider(Map<String, Object> map){
        Logger.getLogger(SqlProvider.class.getName()).info("select * from student where id="+map.get("0"));
        return "select * from student where id="+map.get("0");
    }

    public String findById(@Param("id") int id){
        return new SQL(){{
            SELECT("id,name,info");
            FROM("student");
            WHERE("id="+id);
        }
            }.toString();
    }
}

you can use a string produce your sql query ,but also new SQL(){{some clauses such as SELECT(string),WHERE(string) etc. }}.toString();您可以使用字符串生成您的 sql 查询,但也可以使用 new SQL(){{some 子句,例如 SELECT(string),WHERE(string) 等 }}.toString();

2. use your provider class in Mapper interface 2. 在 Mapper 接口中使用您的提供程序类

we can use @SelectProvider(type=Class,method="methodName") specified class and method我们可以使用@SelectProvider(type=Class,method="methodName") 指定的类和方法

package com.mybatis;    
import java.util.List;
import java.util.Map;

import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Options;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Result;
import org.apache.ibatis.annotations.Results;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.SelectProvider;
import org.apache.ibatis.annotations.Update;

/*
*
*Stu is an entity map to your table in db
*
*/
@Mapper
public interface StuMapper {

    @SelectProvider(type=SqlProvider.class,method="sqlProvider")
    public Stus sqlProvider(Map<String, Object> map);

    @SelectProvider(type=SqlProvider.class,method="findById")
    public Stus findById_(@Param("id") int id);

}

Last , using you mapper .最后,使用你的映射器。

more details ,see https://github.com/v5developer/maven-framework-project/blob/master/reference/Java.Persistence.with.MyBatis.3.pdf更多细节,见https://github.com/v5developer/maven-framework-project/blob/master/reference/Java.Persistence.with.MyBatis.3.pdf

especially in chapter 4.特别是在第 4 章。

<select id="methodName" resultMap="alarmPieEntry">
  ${your_raw_sql}
</select>

Just use ${your_raw_sql} variables in myClass_mapper.xml file , don't use #{your_raw_sql} .只需在myClass_mapper.xml文件中使用${your_raw_sql}变量,不要use #{your_raw_sql}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM