报错代码:
WARNING: An illegal reflective access operation has occurred
WARNING: Illegal reflective access by org.apache.ibatis.reflection.Reflector (file:/C:/Users/Administrator/.m2/repository/org/mybatis/mybatis/3.4.6/mybatis-3.4.6.jar) to method java.lang.Class.checkPackageAccess(java.lang.SecurityManager,java.lang.ClassLoader,boolean)
WARNING: Please consider reporting this to the maintainers of org.apache.ibatis.reflection.Reflector
WARNING: Use --illegal-access=warn to enable warnings of further illegal reflective access operations
WARNING: All illegal access operations will be denied in a future release
DEBUG [main] (PooledDataSource.java:335) - PooledDataSource forcefully closed/removed all connections.
DEBUG [main] (PooledDataSource.java:335) - PooledDataSource forcefully closed/removed all connections.
DEBUG [main] (PooledDataSource.java:335) - PooledDataSource forcefully closed/removed all connections.
DEBUG [main] (PooledDataSource.java:335) - PooledDataSource forcefully closed/removed all connections.
解决方法:
- 第一种:降低版本到java8
- 第二种:不降低版本,自己写一个方法,在启动的时候调用。(亲测可用)
public static void disableWarning() {
try {
Field theUnsafe = Unsafe.class.getDeclaredField("theUnsafe");
theUnsafe.setAccessible(true);
Unsafe u = (Unsafe) theUnsafe.get(null);
Class cls = Class.forName("jdk.internal.module.IllegalAccessLogger");
Field logger = cls.getDeclaredField("logger");
u.putObjectVolatile(cls, u.staticFieldOffset(logger), null);
} catch (Exception e) {
// ignore
}
}
调用:
@Test
public void test1() throws IOException {
//调用即可解决
disableWarning();
//获取核心配置文件
InputStream resourceAsStream = Resources.getResourceAsStream("sqlMapConfig.xml");
//获取session工厂对象
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(resourceAsStream);
//获取session回话对象
SqlSession sqlSession = sqlSessionFactory.openSession();
//执行操作 参数
List<User> userList = sqlSession.selectList("userMapper.findAll");
System.out.println(userList);
//释放资源
sqlSession.close();
}