通过Junit测试出现错误:
java.lang.AbstractMethodError: Method com/mysql/jdbc/PreparedStatement.isClosed()Z is abstract
at com.mysql.jdbc.PreparedStatement.isClosed(PreparedStatement.java)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.apache.ibatis.logging.jdbc.PreparedStatementLogger.invoke(PreparedStatementLogger.java:78)
控制台可以进行打印输出参数和相应的日志文件:
DEBUG [main] - Logging initialized using 'class org.apache.ibatis.logging.slf4j.Slf4jImpl' adapter.
DEBUG [main] - PooledDataSource forcefully closed/removed all connections.
DEBUG [main] - PooledDataSource forcefully closed/removed all connections.
DEBUG [main] - PooledDataSource forcefully closed/removed all connections.
DEBUG [main] - PooledDataSource forcefully closed/removed all connections.
DEBUG [main] - Opening JDBC Connection
DEBUG [main] - Created connection 561247961.
DEBUG [main] - Setting autocommit to false on JDBC Connection [com.mysql.jdbc.Connection@2173f6d9]
DEBUG [main] - ==> Preparing: select * from user where id= ?
DEBUG [main] - ==> Parameters: 10(Integer)
DEBUG [main] - <== Total: 1
但是不能打印出现你想要的结果以下是测试代码:
有打印语句可是控制台未能够输入查询到结果
@Test
public void test1() throws IOException {
// TODO Auto-generated method stub
//加载核心配置文件
String resource="sqlMapConfig.xml";
InputStream in= Resources.getResourceAsStream(resource);
//创建SqlSessionFactory工厂
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(in);
//创建sqlSession
SqlSession sqlSession = sqlSessionFactory.openSession();
//执行sql语句,selectone:第一个参数是对的应的xml文件中的的id,第二个是对应的要传入的参数
User user = sqlSession.selectOne("test.selectid", 10);
System.out.println(user.getId());
//释放资源
sqlSession.close();
}
解决方式:看了一遍所有的jar包,肯定与原来mybatis提供的jar包无关(最新版本的),再看下自己引入的数据库连接的jar包是5.0的,果断选着更换jar包把jar包更换到5.1.之后测试绿条;可以打印出:
DEBUG [main] - Logging initialized using 'class org.apache.ibatis.logging.slf4j.Slf4jImpl' adapter.
DEBUG [main] - PooledDataSource forcefully closed/removed all connections.
DEBUG [main] - PooledDataSource forcefully closed/removed all connections.
DEBUG [main] - PooledDataSource forcefully closed/removed all connections.
DEBUG [main] - PooledDataSource forcefully closed/removed all connections.
DEBUG [main] - Opening JDBC Connection
DEBUG [main] - Created connection 1970881185.
DEBUG [main] - Setting autocommit to false on JDBC Connection [com.mysql.jdbc.JDBC4Connection@757942a1]
DEBUG [main] - ==> Preparing: select * from user where id= ?
DEBUG [main] - ==> Parameters: 10(Integer)
DEBUG [main] - <== Total: 1
10