Mybatis 自动构造SQL学习

Mybatis 自动构造SQL学习
输出均为

select count(*) from users u where u.id in
         (      
            1  
         ,     
            2  
         ,     
            3  
         ,     
            4  
         ,     
            5  
         )

三种方式


import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import org.apache.ibatis.builder.xml.XMLMapperBuilder;
import org.apache.ibatis.builder.xml.XMLMapperEntityResolver;
import org.apache.ibatis.mapping.BoundSql;
import org.apache.ibatis.mapping.SqlSource;
import org.apache.ibatis.parsing.XNode;
import org.apache.ibatis.parsing.XPathParser;
import org.apache.ibatis.scripting.xmltags.XMLLanguageDriver;
import org.apache.ibatis.session.Configuration;
import org.junit.jupiter.api.Test;

import java.io.ByteArrayInputStream;
import java.util.List;
import java.util.Map;

/**
 *
 */
class LanguageDriverRegistryTest2 {

  @Test
  void registerByInstanceNull() {
    String sql = "<script>" +
      "  select count(*) from users u where u.id in" +
      "  <foreach item='item' index='index' collection='ids' open='(' separator=',' close=')'>" +
      "    ${item}" +
      "  </foreach>" +
      "</script>";

    Configuration configuration = new Configuration();
    LanguageDriver languageDriver = new XMLLanguageDriver();
    SqlSource sqlSource = languageDriver.createSqlSource(configuration, sql, Object.class);
    Map<String, Object> map = Maps.newHashMap();
    map.put("ids", Lists.newArrayList(1, 2, 3, 4, 5));
    BoundSql boundSql = sqlSource.getBoundSql(map);
    System.out.println(boundSql.getSql());
  }

  @Test
  void XMLMapperBuilder() {
    String sql = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" +
      "<!DOCTYPE mapper PUBLIC \"-//mybatis.org//DTD Mapper 3.0//EN\" \"http://mybatis.org/dtd/mybatis-3-mapper.dtd\">\n" +
      "<mapper namespace=\"a\">\n" +
      "    <select id=\"query\" parameterType=\"java.util.Map\">   \n" +
      "    select count(*) from users u where u.id in\n" +
      "        <foreach item='item' index='index' collection='ids' open='(' separator=',' close=')'>    \n" +
      "            ${item}  \n" +
      "        </foreach>\n" +
      "    </select>\n" +
      "</mapper>";

    Configuration configuration = new Configuration();
    XMLMapperBuilder xmlMapperBuilder = new XMLMapperBuilder(new ByteArrayInputStream(sql.getBytes()),
      configuration, "", configuration.getSqlFragments());
    xmlMapperBuilder.parse();
    Map<String, Object> map = Maps.newHashMap();
    map.put("ids", Lists.newArrayList(1, 2, 3, 4, 5));
    BoundSql boundSql = configuration.getMappedStatement("query").getBoundSql(map);
    System.out.println(boundSql.getSql());


  }

  @Test
  void XMLMapperBuilder2() {
    String sql = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" +
      "<!DOCTYPE mapper PUBLIC \"-//mybatis.org//DTD Mapper 3.0//EN\" \"http://mybatis.org/dtd/mybatis-3-mapper.dtd\">\n" +
      "<mapper namespace=\"a\">\n" +
      "    <select id=\"query\" parameterType=\"java.util.Map\">   \n" +
      "    select count(*) from users u where u.id in\n" +
      "        <foreach item='item' index='index' collection='ids' open='(' separator=',' close=')'>    \n" +
      "            ${item}  \n" +
      "        </foreach>\n" +
      "    </select>\n" +
      "</mapper>";

    Configuration configuration = new Configuration();
    XPathParser parser = new XPathParser(new ByteArrayInputStream(sql.getBytes()),
      true, configuration.getVariables(), new XMLMapperEntityResolver());
    XNode xNode = parser.evalNode("/mapper");
    List<XNode> xNodes = xNode.evalNodes("select|insert|update|delete");
    LanguageDriver languageDriver = new XMLLanguageDriver();
    SqlSource sqlSource = languageDriver.createSqlSource(configuration, xNodes.get(0), Object.class);
    Map<String, Object> map = Maps.newHashMap();
    map.put("ids", Lists.newArrayList(1, 2, 3, 4, 5));
    BoundSql boundSql = sqlSource.getBoundSql(map);
    System.out.println(boundSql.getSql());
  }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值