Mybatis异常 org.apache.ibatis.DefaultVFS 中文乱码问题结局

原因:org.apache.ibatis.VFS中  

is = url.openStream();
//这里 Reader 没有写UTF-8
                        BufferedReader reader = new BufferedReader(new InputStreamReader(is,"UTF-8"));
                        List<String> lines = new ArrayList<String>();
                        for (String line; (line = reader.readLine()) != null;) {
                            if (log.isDebugEnabled()) {
                                log.debug("Reader entry: " + line);
                            }
                            lines.add(line);
                            if (getResources(path + "/" + line).isEmpty()) {
                                lines.clear();
                                break;
                            }
                        }

 

public class SmalloneVFS extends DefaultVFS {
    private static final Log log = LogFactory.getLog(DefaultVFS.class);
    @Override
    public List<String> list(URL url, String path) throws IOException {
        InputStream is = null;
        try {
            List<String> resources = new ArrayList<String>();

            // First, try to find the URL of a JAR file containing the requested resource. If a JAR
            // file is found, then we'll list child resources by reading the JAR.
            URL jarUrl = findJarForResource(url);
            if (jarUrl != null) {
                is = jarUrl.openStream();
                if (log.isDebugEnabled()) {
                    log.debug("Listing " + url);
                }
                resources = listResources(new JarInputStream(is), path);
            }
            else {
                List<String> children = new ArrayList<String>();
                try {
                    if (isJar(url)) {
                        // Some versions of JBoss VFS might give a JAR stream even if the resource
                        // referenced by the URL isn't actually a JAR
                        is = url.openStream();
                        JarInputStream jarInput = new JarInputStream(is);
                        if (log.isDebugEnabled()) {
                            log.debug("Listing " + url);
                        }
                        for (JarEntry entry; (entry = jarInput.getNextJarEntry()) != null;) {
                            if (log.isDebugEnabled()) {
                                log.debug("Jar entry: " + entry.getName());
                            }
                            children.add(entry.getName());
                        }
                        jarInput.close();
                    }
                    else {
                        /*
                         * Some servlet containers allow reading from directory resources like a
                         * text file, listing the child resources one per line. However, there is no
                         * way to differentiate between directory and file resources just by reading
                         * them. To work around that, as each line is read, try to look it up via
                         * the class loader as a child of the current resource. If any line fails
                         * then we assume the current resource is not a directory.
                         */
                        is = url.openStream();
                        BufferedReader reader = new BufferedReader(new InputStreamReader(is,"UTF-8"));
                        List<String> lines = new ArrayList<String>();
                        for (String line; (line = reader.readLine()) != null;) {
                            if (log.isDebugEnabled()) {
                                log.debug("Reader entry: " + line);
                            }
                            lines.add(line);
                            if (getResources(path + "/" + line).isEmpty()) {
                                lines.clear();
                                break;
                            }
                        }

                        if (!lines.isEmpty()) {
                            if (log.isDebugEnabled()) {
                                log.debug("Listing " + url);
                            }
                            children.addAll(lines);
                        }
                    }
                } catch (FileNotFoundException e) {
                    /*
                     * For file URLs the openStream() call might fail, depending on the servlet
                     * container, because directories can't be opened for reading. If that happens,
                     * then list the directory directly instead.
                     */
                    if ("file".equals(url.getProtocol())) {
                        File file = new File(url.getFile());
                        if (log.isDebugEnabled()) {
                            log.debug("Listing directory " + file.getAbsolutePath());
                        }
                        if (file.isDirectory()) {
                            if (log.isDebugEnabled()) {
                                log.debug("Listing " + url);
                            }
                            children = Arrays.asList(file.list());
                        }
                    }
                    else {
                        // No idea where the exception came from so rethrow it
                        throw e;
                    }
                }

                // The URL prefix to use when recursively listing child resources
                String prefix = url.toExternalForm();
                if (!prefix.endsWith("/")) {
                    prefix = prefix + "/";
                }

                // Iterate over immediate children, adding files and recursing into directories
                for (String child : children) {
                    String resourcePath = path + "/" + child;
                    resources.add(resourcePath);
                    URL childUrl = new URL(prefix + child);
                    resources.addAll(list(childUrl, resourcePath));
                }
            }

            return resources;
        } finally {
            if (is != null) {
                try {
                    is.close();
                } catch (Exception e) {
                    // Ignore
                }
            }
        }
    }
}

 

 

参考链接:https://blog.csdn.net/weixin_30298497/article/details/98790842

根据提供的引用内容,解决Mybatis报错org.apache.ibatis.exceptions.PersistenceException的方法如下: 1. 检查配置文件:首先,确保你的Mybatis配置文件正确无误。检查是否存在语法错误、路径错误或其他配置错误。特别注意检查数据库连接信息是否正确。 2. 检查SQL语句:如果报错信息中包含"Error querying",则可能是SQL语句的问题。检查你的SQL语句是否正确,包括表名、列名、参数等是否与数据库中的实际情况一致。 3. 检查映射文件:如果使用了映射文件,检查映射文件中的配置是否正确。确保映射文件中的命名空间、SQL语句的id、参数等与实际情况一致。 4. 检查数据库连接:如果报错信息中包含"Error querying",还需要检查数据库连接是否正常。确保数据库服务已启动,并且数据库连接信息正确。 5. 查看详细错误信息:如果以上方法都无法解决问题,可以查看详细的错误信息,以便更好地定位问题。可以通过打印异常堆栈信息或查看日志文件来获取更多的错误信息。 6. 检查依赖:如果以上方法都无法解决问题,还可以检查你的项目依赖是否正确。确保你的项目中包含了正确版本的Mybatis依赖。 下面是一个示例代码,演示了如何解决Mybatis报错org.apache.ibatis.exceptions.PersistenceException: ```java try { // 执行Mybatis操作 } catch (PersistenceException e) { // 处理异常 e.printStackTrace(); } ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值