温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

怎么批量测试Mybatis项目中的Sql是否正确

发布时间:2021-02-07 10:09:02 来源:亿速云 阅读:361 作者:小新 栏目:编程语言

小编给大家分享一下怎么批量测试Mybatis项目中的Sql是否正确,相信大部分人都还不怎么了解,因此分享这篇文章给大家参考一下,希望大家阅读完这篇文章后大有收获,下面让我们一起去了解一下吧!

去Oracle行动

最近公司要发展海外项目,所以要将现有的系统全部平移过去,另外数据库也要从原来的Oracle变为Mysql。公司的数据库交互层面使用的是Mybatis,而Oracle与Mysql也有一些语法上的不同。所以在项目中的Sql要改动,但是多个项目中涉及到的Sql非常多,如果仅凭人工一条一条辨别的话,工作量有点大。所以就萌发出了直接将数据源变为Mysql,利用反射批量执行Mapper中的方法,然后如果有参数的话,就设置为默认的初始值,然后记录下来成功的数据和失败的数据,这样就可以根据失败原因进行修改。

能够节省很大的时间。

执行效果

怎么批量测试Mybatis项目中的Sql是否正确

代码介绍

总体思路就三步

  • 通过反射获得要执行的Mapper类的所有方法

  • 获得方法中的参数,并赋值

  • 执行

AutoTestMapper autoTestMapper = new AutoTestMapper("存放Mapper全路径名");
autoTestMapper.openSqlSession(sqlSessionFactory);

在构造函数中传入全路径名后,进行解析,解析出包名和所有的文件名并存储起来

 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;
 }

然后调用openSqlSession的方法,传入SqlSessionFactory参数

 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);
 //反射执行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<>();
 //给参数赋初始值
 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;
 }

这里面完成为参数赋初始值,和执行的逻辑。

使用说明

导入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'

创建mybatis-config.xml文件

在项目的resource文件夹下创建mybatis-config.xml文件,里面内容如下,里面value值为想要测的数据库的连接信息

<!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);

查看输出的信息

然后会打印出执行成功的Sql,执行失败的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|

这样就能够根据错误信息进行更改了。

以上是“怎么批量测试Mybatis项目中的Sql是否正确”这篇文章的所有内容,感谢各位的阅读!相信大家都有了一定的了解,希望分享的内容对大家有所帮助,如果还想学习更多知识,欢迎关注亿速云行业资讯频道!

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI