借鉴大佬纠正方法,贴出来防止以后找不到了,转载自:https://blog.csdn.net/u012972315/article/details/51725646
具体错误:
Exception in thread "main" org.apache.ibatis.exceptions.PersistenceException: ### Error opening session. Cause: java.lang.NullPointerException ### Cause: java.lang.NullPointerException at org.apache.ibatis.exceptions.ExceptionFactory.wrapException(ExceptionFactory.java:30) at org.apache.ibatis.session.defaults.DefaultSqlSessionFactory.openSessionFromDataSource(DefaultSqlSessionFactory.java:100) at org.apache.ibatis.session.defaults.DefaultSqlSessionFactory.openSession(DefaultSqlSessionFactory.java:47) at com.mybatis.test.UserDao.init(UserDao.java:32) at com.mybatis.test.UserDao.main(UserDao.java:41) Caused by: java.lang.NullPointerException at org.apache.ibatis.session.defaults.DefaultSqlSessionFactory.openSessionFromDataSource(DefaultSqlSessionFactory.java:95) ... 3 more
解决办法:< environment default=”“> 该值为下面配置的< environment id=”“>中需要使用的环境的id即可 此代码放在mybatis的xml文件中 具体代码:
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE configuration PUBLIC "-//mybatis.org/DTD Config 3.0//EN" "http://mybatis.org/dtd/mybatis-3-config.dtd"> <configuration> <environments default="database1"> <!-- default属性选择此次开发时需要的数据库环境的id值即可 (database1、database2、database3) --> <environment id="database1"> <transactionManager type="JDBC"></transactionManager> <dataSource type="POOLED"> <property name="driver" value="com.mysql.jdbc.Driver"/> <property name="url" value="jdbc:mysql://localhost/mybatis_test1"/> <property name="username" value="root"/> <property name="password" value="root"/> </dataSource> </environment> <environment id="database2"> <transactionManager type="JDBC"></transactionManager> <dataSource type="POOLED"> <property name="driver" value="com.mysql.jdbc.Driver"/> <property name="url" value="jdbc:mysql://localhost/mybatis_test2"/> <property name="username" value="root"/> <property name="password" value="root"/> </dataSource> </environment> <environment id="database3"> <transactionManager type="JDBC"></transactionManager> <dataSource type="POOLED"> <property name="driver" value="com.mysql.jdbc.Driver"/> <property name="url" value="jdbc:mysql://localhost/mybatis_test3"/> <property name="username" value="root"/> <property name="password" value="root"/> </dataSource> </environment> </environments> <mappers> <mapper resource="com/mybatis/map/User.xml"/> </mappers> </configuration>