Java 找不到 Spring dao 类 org.springframework.dao.DuplicateKeyException

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20499200/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 02:22:29  来源:igfitidea点击:

Spring dao class not found org.springframework.dao.DuplicateKeyException

javaspringclassnotfoundexception

提问by Alex Burdusel

I am trying to use a jdbcTemplate.queryand I get the class not found error. I am using spring-dao 2.0.8 and looked inside the library and couldn't find any DuplicateKeyExceptionclass indeed. From maven repositorythis seems to be the latest release for spring-dao.

我正在尝试使用 ajdbcTemplate.query并且我收到 class not found 错误。我正在使用 spring-dao 2.0.8 并查看库内部,DuplicateKeyException确实找不到任何类。从maven 存储库来看,这似乎是 spring-dao 的最新版本。

The code where I am getting the error is:

我收到错误的代码是:

ResultSet resultSet = (ResultSet)jdbcTemplate.query (query, new ResultSetExtractor<ResultSet>() {
            @Override
            public ResultSet extractData(ResultSet rs) throws SQLException, DataAccessException {
                return rs;
            }
        });

And the stack:

和堆栈:

Exception in thread "main" java.lang.NoClassDefFoundError: org/springframework/dao/DuplicateKeyException
    at org.springframework.jdbc.support.JdbcAccessor.getExceptionTranslator(JdbcAccessor.java:99)
    at org.springframework.jdbc.core.JdbcTemplate.execute(JdbcTemplate.java:407)
    at org.springframework.jdbc.core.JdbcTemplate.query(JdbcTemplate.java:456)
    at DAO.MySQLAccess.getAccounts(MySQLAccess.java:95)
Caused by: java.lang.ClassNotFoundException: org.springframework.dao.DuplicateKeyException
    at java.net.URLClassLoader.run(URLClassLoader.java:366)
    at java.net.URLClassLoader.run(URLClassLoader.java:355)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.net.URLClassLoader.findClass(URLClassLoader.java:354)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:423)
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:356)
    ... 7 more

Isn't this the latest release or why is it giving the error in the first place?

这不是最新版本还是为什么它首先给出错误?

采纳答案by Ben Thurley

Sounds like you have a jar missing from the classpath. I guess you're not using maven.

听起来您的类路径中缺少一个 jar。我猜你没有使用maven。

I searched for jars containing this class and came up with this.
http://mavenhub.com/c/org/springframework/dao/DuplicateKeyException/jar

我搜索了包含这个类的 jars 并想出了这个。
http://mavenhub.com/c/org/springframework/dao/DuplicateKeyException/jar

Looks like you need the org.springframework.transaction jar. Or use maven and it should work out your dependencies for you.

看起来您需要 org.springframework.transaction jar。或者使用 maven,它应该为你解决你的依赖关系。