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();
添加回答
举报
0/150
提交
取消