Mybatis常见低级错误

Mybatis错误笔记

菜鸟一枚,在学习Mybatis的时候,搭建新手项目,出了不少错误,这里铭记一下,警示自己!因为需要还原错误,所以错误的顺序是倒着进行的!
Mybatis学习笔记1,一个简单的demo

1.得到空指针null

得到空指针的问题有很多,我在项目中是因为传入的id属性是数据库中没有的,所以返回NULL类型。
Mybatis常见低级错误_第1张图片

2.serverTimezone=UTC产生的错误

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。
在这里插入图片描述

3.JDBC驱动产生的误差

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中了,所以我们修改驱动位置为


4.密码导致的错误

这里是一个低级的错误,是因为密码错误的问题导致的!

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)

改成正确的密码就行了。

5.空行产生的错误

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]” 的处理指令目标,这种错误。

6.传值设定导致的错误

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: 

产生这种错误的原因是因为在配置传入值的类型的时候,没有设置传入值的类型或者设置错误了。



    select * from studentno where id=#{id}


    

你可能感兴趣的:(笔记)