Mybatis常见低级错误

Mybatis错误笔记

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

1.得到空指针null

得到空指针的问题有很多,我在项目中是因为传入的id属性是数据库中没有的,所以返回NULL类型。
在这里插入图片描述

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

<property name="driver" value="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 id="getByid" parameterType="java.lang.String" resultType="com.yuyi.Student">

设置正确的类型,尤其是parameterType, resultType一定要正确的设置!

7.驱动版本导致的错误

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一致的版本号,不然根本找不到。

8.maven依赖产生的问题

在使用maven的时候自动导入依赖的Jar包是非常的爽,但是一旦搞错了就麻烦大了!
比如jdbc的依赖,正确的依赖声明是这样的,

    <dependency>
      <groupId>mysql</groupId>
      <artifactId>mysql-connector-java</artifactId>
      <version>8.0.19</version>
    </dependency>

是在mysql-connector的项目中 ,然而我去找的时候忘了这茬了,直接搜的JDBC声明格式,导入名为jdbc的项目,还找了半条的错。

在我们学习的过程中,一定要小心小心再小心,不然,都会和我前边一模一样了!
最后附上我的xml配置,配合我前边的文章可以一起看!
Hello,Mybatis的简单demo文章

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>

    <!-- 配置Mybatis的事务及数据源等等 default="development" 开发状态会显示更多的日志信息-->
    <environments default="development">
        <environment id="development">
            <!-- 使用jdbc自带的事务管理器,进行简单的事务开启和提交 -->
            <transactionManager type="JDBC" />
            <!-- 使用jdbc自带的数据库连接池 -->
            <dataSource type="POOLED">
                <property name="driver" value="com.mysql.cj.jdbc.Driver" />
                <property name="url" value="jdbc:mysql://localhost:3306/student?serverTimezone=UTC" />
                <property name="username" value="root" />
                <property name="password" value="123456" />
            </dataSource>
        </environment>
    </environments>
    <!-- 映射文件的路径-->
    <mappers>
        <mapper resource="StudentMapper.xml"/>
    </mappers>
</configuration>


studentmapper.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="student" >
<select id="getByid" parameterType="java.lang.String" resultType="com.yuyi.Student">

    select * from studentno where id=#{id}
</select>

    </mapper>

猜你喜欢

转载自blog.csdn.net/qq_39428182/article/details/105879292