欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

如何批量測試Mybatis項目中的Sql是否正確詳解

 更新時間:2018年12月03日 14:10:16   作者:不學無數(shù)的程序員  
這篇文章主要給大家介紹了關(guān)于如何批量測試Mybatis項目中Sql是否正確的相關(guān)資料,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧

去Oracle行動

最近公司要發(fā)展海外項目,所以要將現(xiàn)有的系統(tǒng)全部平移過去,另外數(shù)據(jù)庫也要從原來的Oracle變?yōu)镸ysql。公司的數(shù)據(jù)庫交互層面使用的是Mybatis,而Oracle與Mysql也有一些語法上的不同。所以在項目中的Sql要改動,但是多個項目中涉及到的Sql非常多,如果僅憑人工一條一條辨別的話,工作量有點大。所以就萌發(fā)出了直接將數(shù)據(jù)源變?yōu)镸ysql,利用反射批量執(zhí)行Mapper中的方法,然后如果有參數(shù)的話,就設(shè)置為默認的初始值,然后記錄下來成功的數(shù)據(jù)和失敗的數(shù)據(jù),這樣就可以根據(jù)失敗原因進行修改。

能夠節(jié)省很大的時間。

執(zhí)行效果

代碼介紹

總體思路就三步

  • 通過反射獲得要執(zhí)行的Mapper類的所有方法
  • 獲得方法中的參數(shù),并賦值
  • 執(zhí)行
AutoTestMapper autoTestMapper = new AutoTestMapper("存放Mapper全路徑名");
autoTestMapper.openSqlSession(sqlSessionFactory);

在構(gòu)造函數(shù)中傳入全路徑名后,進行解析,解析出包名和所有的文件名并存儲起來

 public AutoTestMapper(String path) throws IOException, ClassNotFoundException {
 String mapperContent = getFileContent(path);
 String pathPattern = "import [a-z,A-Z,/.]+;";
 String[] pathArr = matchMethod(pathPattern, mapperContent).split(";");
 for (int i = 0; i < pathArr.length; i++) {
 pathArr[i] = pathArr[i].replaceAll("import ", "");
 Class cls = Class.forName(pathArr[i]);
 if (!cls.isInterface()) {
 TYPE_ARRAY.add(cls);
 }
 }
 //獲得全路徑名的前綴
 String packPattern = "package [a-z,A-Z,/.]+;";
 String[] packPathArr = matchMethod(packPattern, mapperContent).split(";");
 String packPath = packPathArr[0].replaceAll("package ", "").replaceAll(";", "");
 this.PACK_PATH = packPath;
 }

然后調(diào)用openSqlSession的方法,傳入SqlSessionFactory參數(shù)

 List<Map<Class, Object>> list = new ArrayList<>();
 List<String> invokeSuccess = new ArrayList<>();
 List<String> invokeFail = new ArrayList<>();
 for (String fileName : FILE_NAME) {
 Class cls = Class.forName(PACK_PATH + "." + fileName);
 //添加Mapper
 if (!sqlSessionFactory.getConfiguration().hasMapper(cls)){
 sqlSessionFactory.getConfiguration().addMapper(cls);
 }
 //獲得Mapper
 Object mapper = sqlSessionFactory.openSession().getMapper(cls);
 //反射執(zhí)行Mapper的方法
 Map<String, List<String>> resultMap = autoTestInvoke(cls, mapper);
 invokeSuccess.addAll(resultMap.get(SUCCESS_FLG));
 invokeFail.addAll(resultMap.get(FAIL_FLG));
 }

然后通過Mybatyis提供的方法getMapper()傳入類名獲得所要Mapper類。核心方法就是autoTestInvoke()方法了

 	private Map<String, List<String>> autoTestInvoke(Class c, Object o)
 {
 Method[] declaredMethods = c.getDeclaredMethods();
 String fileName = c.getName().substring(c.getName().lastIndexOf("."));
 List<String> invokeSuccess = new ArrayList<>();
 List<String> invokeFail = new ArrayList<>();
 Map<String, List<String>> resultMap = new HashMap<>();
 //給參數(shù)賦初始值
 for (Method method : declaredMethods) {
 List<Object> list = new ArrayList<>();
 for (Class cls : method.getParameterTypes()) {
 Object par = new Object();
 if (TYPE_ARRAY.contains(cls)) {
  if (cls.equals(String.class)) {
  par = "1";
  } else {
  try {
  par = cls.newInstance();
  assignment(cls, par);
  } catch (InstantiationException e) {
  if (cls.isPrimitive()) {
  cls = primitiveClazz.get(cls.getName());
  }
  try {
  par = cls.getDeclaredConstructor(String.class).newInstance("1");

  }catch (NoSuchMethodException e1){
  System.out.println(cls.getName()+e);
  }
  }
  }
 }else if ("java.util.Map".equals(cls.getName())){
  par = getMapData(c.getName()+"."+method.getName());
 }
 list.add(par);
 }
 try {
 method.invoke(o, list.toArray());
 invokeSuccess.add("Success: " + fileName + "." + method.getName());
 } catch (Exception e) {
 invokeFail.add("Error:" + method.getName() + " Error Info:" + e);
 }
 }
 resultMap.put(SUCCESS_FLG, invokeSuccess);
 resultMap.put(FAIL_FLG, invokeFail);
 return resultMap;
 }

這里面完成為參數(shù)賦初始值,和執(zhí)行的邏輯。

使用說明

導入Jar包

Maven

<dependency>
 <groupId>com.github.modouxiansheng</groupId>
 <artifactId>convenientUtil</artifactId>
 <version>1.3-release</version>
</dependency>

Gradle

compile 'com.github.modouxiansheng:convenientUtil:1.1-release'

創(chuàng)建mybatis-config.xml文件

在項目的resource文件夾下創(chuàng)建mybatis-config.xml文件,里面內(nèi)容如下,里面value值為想要測的數(shù)據(jù)庫的連接信息

<!DOCTYPE configuration
 PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
 "http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
 <environments default="dev">
 <environment id="dev">
 <transactionManager type="JDBC"></transactionManager>
 <dataSource type="UNPOOLED">
 <property name="driver" value=""/>
 <property name="url" value=""/>
 <property name="username" value=""/>
 <property name="password" value=""/>
 </dataSource>
 </environment>
 </environments>
</configuration>

在測試類中編寫代碼

在測試類中編寫如下代碼

Reader resourceAsReader = Resources.getResourceAsReader("mybatis-config.xml");
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(resourceAsReader);
resourceAsReader.close();
AutoTestMapper autoTestMapper = new AutoTestMapper("想要測試的Mapper文件夾全路徑名");
autoTestMapper.openSqlSession(sqlSessionFactory); 

查看輸出的信息

然后會打印出執(zhí)行成功的Sql,執(zhí)行失敗的Sql。如果失敗的話會有原因。
Success: TSesSetManualMapper.updateFlgdelAutoInTimePay
Success: TSesSetManualMapper.getAutoSetManualOrdListCount
Success: TSesSetManualMapper.updateAutoSetManualOrd
Success: TSesSetManualMapper.queryAutoSetManualOrdDetail
Success: TSesSetManualMapper.querySetManualOrdListCount
Success: ShortMessageMapper.queryPayInsSmInfo
-------------------
|Error: |TSesSetManualMapper.queryAutoSetManualOrdList| Every derived table must have its own alias|
|Error: |TSesSetManualMapper.querySetManualOrdList| Every derived table must have its own alias|

這樣就能夠根據(jù)錯誤信息進行更改了。

github地址:https://github.com/modouxiansheng/convenientUtil (本地下載

總結(jié)

以上就是這篇文章的全部內(nèi)容了,希望本文的內(nèi)容對大家的學習或者工作具有一定的參考學習價值,如果有疑問大家可以留言交流,謝謝大家對腳本之家的支持。

相關(guān)文章

  • Mybatis全局配置及映射關(guān)系的實現(xiàn)

    Mybatis全局配置及映射關(guān)系的實現(xiàn)

    本文主要介紹了Mybatis全局配置及映射關(guān)系的實現(xiàn),文中通過示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2022-03-03
  • MyBatis標簽之Select?resultType和resultMap詳解

    MyBatis標簽之Select?resultType和resultMap詳解

    這篇文章主要介紹了MyBatis標簽之Select?resultType和resultMap,在MyBatis中有一個ResultMap標簽,它是為了映射select標簽查詢出來的結(jié)果集,下面使用一個簡單的例子,來介紹 resultMap 的使用方法,需要的朋友可以參考下
    2022-09-09
  • 一篇文章徹底弄懂SpringBoot項目jdk版本及依賴不兼容問題

    一篇文章徹底弄懂SpringBoot項目jdk版本及依賴不兼容問題

    這篇文章主要給大家介紹了關(guān)于徹底弄懂SpringBoot項目jdk版本及依賴不兼容問題的相關(guān)資料,文中通過圖文介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下
    2023-01-01
  • SpringCloud Bus 消息總線的具體使用

    SpringCloud Bus 消息總線的具體使用

    這篇文章主要介紹了SpringCloud Bus 消息總線的具體使用,詳細的介紹了什么是消息總線以及具體配置,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2018-12-12
  • SpringCloud 服務(wù)注冊IP錯誤的解決

    SpringCloud 服務(wù)注冊IP錯誤的解決

    這篇文章主要介紹了SpringCloud 服務(wù)注冊IP錯誤的解決方案,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2021-07-07
  • SpringBoot 使用WebSocket功能(實現(xiàn)步驟)

    SpringBoot 使用WebSocket功能(實現(xiàn)步驟)

    本文通過詳細步驟介紹了SpringBoot 使用WebSocket功能,首先需要導入WebSocket坐標,編寫WebSocket配置類,用于注冊WebSocket的Bean,結(jié)合示例代碼給大家介紹的非常詳細,感興趣的朋友跟隨小編一起看看吧
    2024-02-02
  • 解決springboot讀取application.properties中文亂碼問題

    解決springboot讀取application.properties中文亂碼問題

    初用properties,讀取java properties文件的時候如果value是中文,會出現(xiàn)亂碼的問題,所以本文小編將給大家介紹如何解決springboot讀取application.properties中文亂碼問題,需要的朋友可以參考下
    2023-11-11
  • Maven jar包沖突的解決方案

    Maven jar包沖突的解決方案

    這篇文章主要介紹了Maven jar包沖突的解決方案,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下
    2020-04-04
  • hibernate5.2的基本配置方法(詳解)

    hibernate5.2的基本配置方法(詳解)

    下面小編就為大家?guī)硪黄猦ibernate5.2的基本配置方法(詳解)。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2017-06-06
  • java中List、Array、Map、Set等集合相互轉(zhuǎn)換

    java中List、Array、Map、Set等集合相互轉(zhuǎn)換

    這篇文章主要介紹了java中List、Array、Map、Set等集合相互轉(zhuǎn)換的相關(guān)資料,需要的朋友可以參考下
    2017-05-05

最新評論