根据类所在包找到class文件位置

需求介绍
这个方法主要是服务于有对字节码操作的需求的人。
代码演示

    @Test
    public void test_path(){
        // 获取当前类所在的
        String classPath = V2Test.class.getResource("/").getPath();
        System.out.println(classPath);

        // 将路径中的.替换成/
        String mapperPath = "com.edu.mqs.mebatis.v2_test".replace(".", File.separator);
        System.out.println(mapperPath);

        // 拼接成.class文件所在的路径
        String path = StringUtils.join(classPath, mapperPath);
        System.out.println(path);

        // BigDecimal.ZERO 替换 new BigDecimal(0)方法
        System.out.println(BigDecimal.ZERO);
    }

运行结果

/Users/fenke/newMineWorkspace/mebatis/target/test-classes/
com/edu/mqs/mebatis/v2_test
/Users/fenke/newMineWorkspace/mebatis/target/test-classes/com/edu/mqs/mebatis/v2_test

验证结果
在这里插入图片描述
不能变小点

猜你喜欢

转载自blog.csdn.net/wildwolf_001/article/details/106054550