码迷,mamicode.com
首页 > 数据库 > 详细

使用mybatis动态执行SQL

时间:2020-10-31 01:41:28      阅读:18      评论:0      收藏:0      [点我收藏+]

标签:append   print   lxml   抽取   build   ack   esc   one   index   

抽取mybatis,根据xml方式sql,拼接参数

 

package com.jdcloud.zs.api.db;

import com.baomidou.mybatisplus.core.MybatisConfiguration;
import org.apache.ibatis.mapping.*;
import org.apache.ibatis.scripting.LanguageDriver;
import org.apache.ibatis.scripting.xmltags.XMLLanguageDriver;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.defaults.DefaultSqlSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Component
public class SqlSourceUtil {
    @Autowired
    private SqlSessionFactory sqlSessionFactory;

    /**
     * sql拼接
     * @param sqlString
     * @param parameters
     * @return
     */
    public String sqlSource(String sqlString, Map<String,Object> parameters){
        MybatisConfiguration configuration=(MybatisConfiguration)sqlSessionFactory.getConfiguration();
        LanguageDriver xmlLanguageDriver=new XMLLanguageDriver();
        final StringBuilder sqlXml = new StringBuilder();
        String[]strings=new String[]{sqlString};
        for (String fragment : strings) {
                sqlXml.append(fragment);
                sqlXml.append(" ");
        }
        SqlSource sqlSource=xmlLanguageDriver.createSqlSource(configuration, sqlXml.toString().trim(), Map.class);
        BoundSql boundSql=sqlSource.getBoundSql(parameters);
        final StringBuilder sql=new StringBuilder(boundSql.getSql());
        if(parameters!=null&&!parameters.isEmpty()){
            for (Object  parameter:parameters.values()) {
                int start=sql.indexOf("?");
                sql.replace(start,start+1,parameter.toString());
            }
        }
        return sql.toString();
    }

    /**
     * 带缓存sql,直接返回sql执行结果
     * @param sqlString
     * @param apiId
     * @param param
     * @return
     */
    public List<Map<String, Object>> query(String sqlString,Long apiId, Map<String,Object> param){
        String mastpId="sql."+apiId;
        MybatisConfiguration configuration=(MybatisConfiguration)sqlSessionFactory.getConfiguration();
        if(!configuration.hasStatement(mastpId)){
//            String[] strings=new String[]{"<script>select\n" +
//                    "        role_id as id,\n" +
//                    "        name,\n" +
//                    "        description\n" +
//                    "        from sys_role\n" +
//                    "        where 1=1\n" +
//                    "        <if test=\"name != null and name != ‘‘\">\n" +
//                    "            and name LIKE CONCAT(‘%‘, #{name},‘%‘)\n" +
//                    "\n" +
//                    "        </if></script>"};

            LanguageDriver xmlLanguageDriver=new XMLLanguageDriver();
            final StringBuilder sql = new StringBuilder();
            String[]strings=new String[]{sqlString};
            for (String fragment : strings) {
                sql.append(fragment);
                sql.append(" ");
            }
            SqlSource sqlSource=xmlLanguageDriver.createSqlSource(configuration, sql.toString().trim(), Map.class);

            BoundSql boundSql=sqlSource.getBoundSql(param);
            MappedStatement ms = new MappedStatement.Builder(configuration, mastpId, sqlSource, SqlCommandType.SELECT)
                    .resultMaps(new ArrayList<ResultMap>() {
                        {
                            add(new ResultMap.Builder(configuration, "defaultResultMap", Map.class, new ArrayList<>(0))
                                    .build());
                        }
                    }).build();
            // 缓存
            configuration.addMappedStatement(ms);
        }
        DefaultSqlSession defaultSqlSession=(DefaultSqlSession)sqlSessionFactory.openSession();
        List<Map<String, Object>> resutls=defaultSqlSession.selectList(mastpId,param);
        return  resutls;
    }

}

 

执行:

注入SqlSourceUtil

@Autowired
private SqlSourceUtil sqlSourceUtil;

  

调用:

public static void sqlTest() {

    //按照Mybatis xml的方式写sql
    StringBuffer sb=new StringBuffer();
        sb.append("<script>select ");
        sb.append(" role_id as id, ");
        sb.append("name, ");
        sb.append("description ");
        sb.append("from sys_role ");
        sb.append("where 1=1 ");
        sb.append("<if test=\"name != null and name != ‘‘\"> ");
        sb.append("and name LIKE CONCAT(‘%‘, #{name},‘%‘) ");
        sb.append("</if></script>");

    Map<String,Object> parameters=new HashMap<>();
    parameters.put("name","test");

    //拼接sql
    String s = sqlSourceUtil.sqlSource(sb.toString(), parameters);
    System.out.println(s);

    //执行sql,并缓存sql,直接返回执行结果

    Long cacheId=1L;//缓存id,唯一
    List<Map<String, Object>> query = sqlSourceUtil.query(sb.toString(), cacheId, parameters);

    System.out.println(query.toArray().toString());

}

  

  

使用mybatis动态执行SQL

标签:append   print   lxml   抽取   build   ack   esc   one   index   

原文地址:https://www.cnblogs.com/e206842/p/13887042.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!