Mybatis如何直接執(zhí)行SQL語句
Mybatis直接執(zhí)行SQL語句
有時候我們?nèi)绻獙魅氲腟QL驗證語法方面怎么辦呢,首先我們是不是要有一條完整的SQL,而且讓mybatis去執(zhí)行,這是小白最近遇到的,對于用戶輸入進(jìn)來的語法與參數(shù),進(jìn)行拼接并且去執(zhí)行,判斷SQL語句有沒有語法錯誤。
第一種方法
建立一個SQL工具進(jìn)行SQL處理再返回完整的SQL語句
1.建立工具類
public class SqlProviderUtils { ? ? public String sqlProvider(Map<String, Object> para) { ? ? ? ? // 要替換一些字段的SQL(例子 select * from ${table} where ${filter}) ? ? ? ? String str = para.get("sql").toString(); ? ? ? ? // 參數(shù)用于替換SQL中的一些東西(例子: table ,filter 默認(rèn)值 1=1) ? ? ? ? List<Params> paramsList = (List<Params>) para.get("ParamsList"); ? ? ? ? List<Integer> head = new ArrayList(); ? ? ? ? List<Integer> tail = new ArrayList(); ? ? ? ? int headNum = 0; ? ? ? ? int tailNum = 0; ? ? ? ? List allStr = new ArrayList(); ? ? ? ? // 每個${ 與 } 的位置 ? ? ? ? int headPos = str.indexOf("${"); ? ? ? ? int tailPos = str.indexOf("}"); ? ? ? ? while (headPos > -1){ ? ? ? ? ? ? if (headPos == headNum + 3) { ? ? ? ? ? ? ? ? head.set(head.size()-1, headPos); ? ? ? ? ? ? ? ? tail.set(tail.size() - 1, tailNum); ? ? ? ? ? ? } else { ? ? ? ? ? ? ? ? head.add(headPos); ? ? ? ? ? ? ? ? tail.add(tailPos); ? ? ? ? ? ? } ? ? ? ? headNum = headPos; ? ? ? ? tailNum = tailPos; ? ? ? ? headPos = str.indexOf("${", headPos + 1); ? ? ? ? tailPos = str.indexOf("}", tailPos + 1); ? ? } ? ? ? ? for (int i = 0; i < head.size(); i++) { ? ? ? ? ? ? allStr.add(str.substring(head.get(i)+2, tail.get(i))); ? ? ? ? } ? ? ? ? // 這里是針對上面保存的一些要替換的字段進(jìn)行替換,具體怎么操作得詳細(xì)理解代碼 ? ? ? ? for (int i=0; i<paramsList.size(); i++) { ? ? ? ? ?str = str.replace("${"+allStr.get(par-1)+"}.${"+paramsList.get(i).getName()+"}","a表"); ? ? ? ? ? } ? ? ? ? return str; ? ? } }
2.在mapper中建立方法調(diào)用
// SqlProviderUtils上述類,sqlProvider上述的類方法 @SelectProvider(type = SqlProviderUtils.class, method = "sqlProvider") ?List<HaoDTO> sqlTemTest(@Param("sql") String sqlTemplate,@Param("ParamsList") List ParamsList);
3.controller與server沒啥東西就一些三層調(diào)用這里不寫
第二種方法
1.server層
?@Override ? ? @Transactional ? ? public Boolean testTemplate(Param param) { ? ? ? ? String str = Param.getSql(); ? ? ? ? List<Params> paramsList = Param.getParamsList(); ? ? ? ? List<Integer> head = new ArrayList(); ? ? ? ? List<Integer> tail = new ArrayList(); ? ? ? ? Boolean templateType = true; ? ? ? ? int headNum = 0; ? ? ? ? int tailNum = 0; ? ? ? ? List allStr = new ArrayList(); ? ? ? ? int headPos = str.indexOf("${"); ? ? ? ? int tailPos = str.indexOf("}"); ? ? ? ? while (headPos > -1){ ? ? ? ? ? ? if (headPos == headNum + 3) { ? ? ? ? ? ? ? ? head.set(head.size()-1, headPos); ? ? ? ? ? ? ? ? tail.set(tail.size() - 1, tailNum); ? ? ? ? ? ? } else { ? ? ? ? ? ? ? ? head.add(headPos); ? ? ? ? ? ? ? ? tail.add(tailPos); ? ? ? ? ? ? } ? ? ? ? ? ? headNum = headPos; ? ? ? ? ? ? tailNum = tailPos; ? ? ? ? ? ? headPos = str.indexOf("${", headPos + 1); ? ? ? ? ? ? tailPos = str.indexOf("}", tailPos + 1); ? ? ? ? } ? ? ? ? for (int i = 0; i < head.size(); i++) { ? ? ? ? ? ? try { ? ? ? ? ? ? ? ? allStr.add(str.substring(head.get(i)+2, tail.get(i))); ? ? ? ? ? ? } catch (StringIndexOutOfBoundsException e) { ? ? ? ? ? ? ? ? templateType = false; ? ? ? ? ? ? } ? ? ? ? } ? ? ? ? for (int i=0; i<paramsList.size(); i++) { ? ? ? ? ? ?str = str.replace("${"+allStr.get(par-1)+"}.${"+paramsList.get(i).getName()+"}", "b_quality_template"); ? ? ? ? } ? ? ? ? try { ? ? ? ? ? ? if(templateType){ ? ? ? ? ? ? ? ? baseMapper.sqlTest(str); ? ? ? ? ? ? } ? ? ? ? } catch (Exception e){ ? ? ? ? ? ? templateType = false; ? ? ? ? } ? ? ? ? return templateType; ? ? }
2.mapper層
@Select("${sqlStr}") List<Map<String, Object>> sqlTest(@Param(value = "sqlStr") String sqlStr);
3.其他東西也只是一些調(diào)用
以上只是說明下mybatis中怎么直接處理sql語句,重點(diǎn)在mapper其他是字段處理,這是小白弄畢設(shè)時考慮到的需求有點(diǎn)簡單莫見怪。
測試Mybatis執(zhí)行SQL語句步驟
調(diào)用mybatis某個對象的方法,執(zhí)行mapper文件中的sql語句
mybatis核心類:SqlSessionFactory
1.定義mybatis主配置文件的位置, 從類路徑開始的相對路徑——讀取主配置文件里面的信息(1.連接數(shù)據(jù)庫:創(chuàng)建、連接對象的必要數(shù)據(jù)。2.mapper:找到要執(zhí)行的sql語句)
2.讀取主配置文件。使用mybatis框架中的Resources類
3.創(chuàng)建SqlSessionFactory對象, 使用SqlSessionFactoryBuidler類
- 1)每個基于 MyBatis 的應(yīng)用都是以一個 SqlSessionFactory的實例為核心的。SqlSessionFactory的實例 可以通過 SqlSessionFactoryBuilder 獲得。而SqlSessionFactoryBuilder 則可以從 XML 配置文件或一個預(yù)先配置的 Configuration實例 來構(gòu)建出 SqlSessionFactory實例 。
- 2)既然有了 SqlSessionFactory,顧名思義,可以從中獲得SqlSession的實例。SqlSession 提供了在數(shù)據(jù)庫執(zhí)行 SQL 命令所需的所有方法??梢酝ㄟ^ SqlSession 實例來直接執(zhí)行已映射的 SQL 語句。
4.獲取SqlSession對象
5.指定要執(zhí)行的sql語句的id
- sql的id = namespace+"."+ select|update|insert|delete標(biāo)簽的id屬性值
6.通過SqlSession的方法,執(zhí)行sql語句
7.關(guān)閉SqlSession對象
舉例
@Test public void testSelectStudentById2() throws IOException { //調(diào)用mybatis某個對象的方法,執(zhí)行mapper文件中的sql語句 //mybatis核心類: SqlSessionFactory //1.定義mybatis主配置文件的位置, 從類路徑開始的相對路徑 String config = "mybatis.xml"; //2.讀取主配置文件。使用mybatis框架中的Resources類 InputStream inputStream = Resources.getResourceAsStream(config); //3.創(chuàng)建SqlSessionFactory對象, 使用SqlSessionFactoryBuidler類 // 每個基于 MyBatis 的應(yīng)用都是以一個 SqlSessionFactory 的實例為核心的。 // SqlSessionFactory 的實例可以通過 SqlSessionFactoryBuilder 獲得。 // 而 SqlSessionFactoryBuilder 則可以從 XML 配置文件或一個預(yù)先配置的 Configuration 實例來 // 構(gòu)建出 SqlSessionFactory 實例。 SqlSessionFactory factory = new SqlSessionFactoryBuilder().build(inputStream); //既然有了 SqlSessionFactory,顧名思義,可以從中獲得 SqlSession 的實例。 // SqlSession 提供了在數(shù)據(jù)庫執(zhí)行 SQL 命令所需的所有方法。 // 你可以通過 SqlSession 實例來直接執(zhí)行已映射的 SQL 語句。 //4.獲取SqlSession對象。 SqlSession session = factory.openSession(); //5.指定要執(zhí)行的sql語句的 id // sql的id = namespace+"."+ select|update|insert|delete標(biāo)簽的id屬性值 String sqlId = "com.bjpowernode.dao.StudentDao" + "." + "selectStudentById"; // 6.通過SqlSession的方法,執(zhí)行sql語句 Student student = session.selectOne(sqlId,1002); System.out.println("使用mybatis查詢一個學(xué)生:" + student); // 7.關(guān)閉SqlSession對象 session.close(); }
這些僅為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
構(gòu)建springboot自動生成mapper文件和dao接口項目的步驟和配置方法
這篇文章主要介紹了構(gòu)建springboot自動生成mapper文件和dao接口項目的步驟和配置方法,本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2023-05-05Spring框架JdbcTemplate數(shù)據(jù)庫事務(wù)管理完全注解方式
這篇文章主要介紹了Spring框架JdbcTemplate數(shù)據(jù)庫事務(wù)管理及完全注解方式,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-05-05Java中JSON字符串進(jìn)行各種轉(zhuǎn)換的方法小結(jié)
Gson和Hutool的JSONUtil都是常用的用于處理JSON數(shù)據(jù)的工具庫,它們提供了簡單易用的API來進(jìn)行JSON字符串的解析、轉(zhuǎn)換和操作,下面就跟隨小編一起學(xué)習(xí)一下如果使用他們實現(xiàn)JSON字符串的各種轉(zhuǎn)換吧2024-01-01