为了账号安全,请及时绑定邮箱和手机立即绑定

mybatis 类中没有方法,配置文件错误?java.lang.NoSuchMethodException: com.imoc.pojo.User.<init>()

mybatis 类中没有方法,配置文件错误?java.lang.NoSuchMethodException: com.imoc.pojo.User.<init>()

玄鉴 2016-12-18 23:31:27
log4j:WARN No appenders could be found for logger (org.apache.ibatis.logging.LogFactory).log4j:WARN Please initialize the log4j system properly.log4j:WARN See http://logging.apache.org/log4j/1.2/faq.html#noconfig for more info.Exception in thread "main" org.apache.ibatis.exceptions.PersistenceException: ### Error querying database.  Cause: org.apache.ibatis.reflection.ReflectionException: Error instantiating class com.imoc.pojo.User with invalid types () or values (). Cause: java.lang.NoSuchMethodException: com.imoc.pojo.User.<init>()### The error may exist in com/imoc/mapping/userManpper.xml### The error may involve com/imoc/mapping/userManpper.getUser-Inline### The error occurred while setting parameters### SQL: select * from users where id=?### Cause: org.apache.ibatis.reflection.ReflectionException: Error instantiating class com.imoc.pojo.User with invalid types () or values (). Cause: java.lang.NoSuchMethodException: com.imoc.pojo.User.<init>() at org.apache.ibatis.exceptions.ExceptionFactory.wrapException(ExceptionFactory.java:23) at org.apache.ibatis.session.defaults.DefaultSqlSession.selectList(DefaultSqlSession.java:107) at org.apache.ibatis.session.defaults.DefaultSqlSession.selectList(DefaultSqlSession.java:98) at org.apache.ibatis.session.defaults.DefaultSqlSession.selectOne(DefaultSqlSession.java:62) at com.imoc.test.Test.main(Test.java:24)Caused by: org.apache.ibatis.reflection.ReflectionException: Error instantiating class com.imoc.pojo.User with invalid types () or values (). Cause: java.lang.NoSuchMethodException: com.imoc.pojo.User.<init>() at org.apache.ibatis.reflection.factory.DefaultObjectFactory.instantiateClass(DefaultObjectFactory.java:83) at org.apache.ibatis.reflection.factory.DefaultObjectFactory.create(DefaultObjectFactory.java:45) at org.apache.ibatis.reflection.factory.DefaultObjectFactory.create(DefaultObjectFactory.java:38) at org.apache.ibatis.executor.resultset.FastResultSetHandler.createResultObject(FastResultSetHandler.java:386) at org.apache.ibatis.executor.resultset.FastResultSetHandler.createResultObject(FastResultSetHandler.java:365) at org.apache.ibatis.executor.resultset.FastResultSetHandler.getRowValue(FastResultSetHandler.java:261) at org.apache.ibatis.executor.resultset.FastResultSetHandler.handleRowValues(FastResultSetHandler.java:216) at org.apache.ibatis.executor.resultset.FastResultSetHandler.handleResultSet(FastResultSetHandler.java:188) at org.apache.ibatis.executor.resultset.FastResultSetHandler.handleResultSets(FastResultSetHandler.java:154) at org.apache.ibatis.executor.statement.PreparedStatementHandler.query(PreparedStatementHandler.java:57) at org.apache.ibatis.executor.statement.RoutingStatementHandler.query(RoutingStatementHandler.java:70) at org.apache.ibatis.executor.SimpleExecutor.doQuery(SimpleExecutor.java:57) at org.apache.ibatis.executor.BaseExecutor.queryFromDatabase(BaseExecutor.java:259) at org.apache.ibatis.executor.BaseExecutor.query(BaseExecutor.java:132) at org.apache.ibatis.executor.CachingExecutor.query(CachingExecutor.java:105) at org.apache.ibatis.executor.CachingExecutor.query(CachingExecutor.java:81) at org.apache.ibatis.session.defaults.DefaultSqlSession.selectList(DefaultSqlSession.java:104) ... 3 moreCaused by: java.lang.NoSuchMethodException: com.imoc.pojo.User.<init>() at java.lang.Class.getConstructor0(Class.java:2849) at java.lang.Class.getDeclaredConstructor(Class.java:2053) at org.apache.ibatis.reflection.factory.DefaultObjectFactory.instantiateClass(DefaultObjectFactory.java:57) ... 19 morepackage com.imoc.pojo; public class User { private int id;     private String name;     private int age;      public int getId() { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } public User(int id) { super(); this.id = id; } public User(int id, String name, int age) { super(); this.id = id; this.name = name; this.age = age; } @Override public String toString() { return "User [id=" + id + ", name=" + name + ", age=" + age + "]"; } }<mapper namespace="com/imoc/mapping/userManpper">        <select id="getUser" parameterType="int"          resultType="com.imoc.pojo.User">          select * from users where id=#{id}     </select> </mapper><configuration> <environments default="development"> <environment id="development"> <transactionManager type="JDBC"/> <dataSource type="POOLED"> <property name="driver" value="com.mysql.jdbc.Driver" />                  <property name="url" value="jdbc:mysql://localhost:3306/mybatis" />                  <property name="username" value="root" />                  <property name="password" value="" /> </dataSource> </environment> </environments> <mappers> <mapper resource="com/imoc/mapping/userManpper.xml"/> </mappers> </configuration> String resource = "config.xml"; InputStream is=Test.class.getClassLoader().getResourceAsStream(resource); SqlSessionFactory sFactory=new SqlSessionFactoryBuilder().build(is); SqlSession sqlSession=sFactory.openSession(); User user=sqlSession.selectOne("com/imoc/mapping/userManpper.getUser",1); System.out.println(user); sqlSession.close();
查看完整描述

1 回答

?
Bobzhangxiaotao

TA贡献9条经验 获得超5个赞

参数错了

查看完整回答
反对 回复 2016-12-19
  • 玄鉴
    玄鉴
    namespace="com/imoc/mapping/userManpper" 这里原来我写的就是<mapper namespace="com.imoc.mapping.userManpper">在测试类里面User user=sqlSession.selectOne("com/imoc/mapping/userManpper.getUser",1);是String statement ="com.imoc.mapping.userManpper.getUser"; User user=sqlSession.selectOne(statement,1);可是包同样的错误,所以修改如上。两者没区别
  • 1 回答
  • 0 关注
  • 3215 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信