菜鸟一枚,在学习Mybatis的时候,搭建新手项目,出了不少错误,这里铭记一下,警示自己!因为需要还原错误,所以错误的顺序是倒着进行的!
Mybatis学习笔记1,一个简单的demo
得到空指针的问题有很多,我在项目中是因为传入的id属性是数据库中没有的,所以返回NULL类型。
org.apache.ibatis.exceptions.PersistenceException:
### Error querying database. Cause: java.sql.SQLException: The server time zone value '�й���ʱ��' is unrecognized or represents more than one time zone. You must configure either the server or JDBC driver (via the 'serverTimezone' configuration property) to use a more specifc time zone value if you want to utilize time zone support.
### The error may exist in StudentMapper.xml
### The error may involve student.getByid
### The error occurred while executing a query
### Cause: java.sql.SQLException: The server time zone value '�й���ʱ��' is unrecognized or represents more than one time zone. You must configure either the server or JDBC driver (via the 'serverTimezone' configuration property) to use a more specifc time zone value if you want to utilize time zone support.
这个的原因是因为时区误差导致的,我们需要把它调整成国际时差UTC 在url后面加上?serverTimezone=UTC。
WARNING: An illegal reflective access operation has occurred
WARNING: Illegal reflective access by org.apache.ibatis.reflection.Reflector (file:/D:/util/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
Loading class `com.mysql.jdbc.Driver'. This is deprecated. The new driver class is `com.mysql.cj.jdbc.Driver'. The driver is automatically registered via the SPI and manual loading of the driver class is generally unnecessary.
这是由于新版的mysql-connector的问题,对于jdbc的实现已经不在com.mysql.jdbc.Driver之内了,改为
com.mysql.cj.jdbc.Driver中了,所以我们修改驱动位置为
这里是一个低级的错误,是因为密码错误的问题导致的!
org.apache.ibatis.exceptions.PersistenceException:
### Error querying database. Cause: java.sql.SQLException: Access denied for user 'root'@'localhost' (using password: YES)
### The error may exist in StudentMapper.xml
### The error may involve student.getByid
### The error occurred while executing a query
### Cause: java.sql.SQLException: Access denied for user 'root'@'localhost' (using password: YES)
改成正确的密码就行了。
org.apache.ibatis.exceptions.PersistenceException:
### Error building SqlSession.
### The error may exist in StudentMapper.xml
### Cause: org.apache.ibatis.builder.BuilderException: Error parsing SQL Mapper Configuration. Cause: org.apache.ibatis.builder.BuilderException: Error creating document instance. Cause: org.xml.sax.SAXParseException; lineNumber: 2; columnNumber: 6; 不允许有匹配 "[xX][mM][lL]" 的处理指令目标。
产生上述问题的原因是因为你的配置文件的开头空空了一行,产生6; 不允许有匹配 “[xX][mM][lL]” 的处理指令目标,这种错误。
org.apache.ibatis.exceptions.PersistenceException:
### Error building SqlSession.
### The error may exist in StudentMapper.xml
### Cause: org.apache.ibatis.builder.BuilderException: Error parsing SQL Mapper Configuration. Cause: org.apache.ibatis.builder.BuilderException: Error parsing Mapper XML. The XML location is 'StudentMapper.xml'. Cause: org.apache.ibatis.builder.BuilderException: Error resolving class. Cause: org.apache.ibatis.type.TypeException: Could not resolve type alias ''. Cause: java.lang.ClassNotFoundException: Cannot find class:
产生这种错误的原因是因为在配置传入值的类型的时候,没有设置传入值的类型或者设置错误了。
设置正确的类型,尤其是parameterType, resultType一定要正确的设置!
org.apache.ibatis.exceptions.PersistenceException:
### Error querying database. Cause: java.sql.SQLException: Error setting driver on UnpooledDataSource. Cause: java.lang.ClassNotFoundException: Cannot find class: com.mysql.cj.jdbc.Driver
### The error may exist in StudentMapper.xml
### The error may involve student.getByid
### The error occurred while executing a query
### Cause: java.sql.SQLException: Error setting driver on UnpooledDataSource. Cause: java.lang.ClassNotFoundException: Cannot find class: com.mysql.cj.jdbc.Driver
这是驱动版本产生的错误,记得一定要和自己的Mysql一致的版本号,不然根本找不到。
在使用maven的时候自动导入依赖的Jar包是非常的爽,但是一旦搞错了就麻烦大了!
比如jdbc的依赖,正确的依赖声明是这样的,
mysql
mysql-connector-java
8.0.19
是在mysql-connector的项目中 ,然而我去找的时候忘了这茬了,直接搜的JDBC声明格式,导入名为jdbc的项目,还找了半条的错。
在我们学习的过程中,一定要小心小心再小心,不然,都会和我前边一模一样了!
最后附上我的xml配置,配合我前边的文章可以一起看!
Hello,Mybatis的简单demo文章
mybatis.xml
studentmapper.xml