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>
入门 安装 要使用 MyBatis, 只需将 mybatis-x.x.x.jar 文件置于 classpath 中即可。 如果使用 Maven 来构建目,则需将下面的 dependency 代码置于 pom.xml 文件中: <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis</artifactId> <version>x.x.x</version> </dependency>从 XML 中构建 SqlSessionFactory 每个基于 MyBatis 的应用都是以一个 SqlSessionFactory 的实例为中心的。SqlSessionFactory 的实例可以通过 SqlSessionFactoryBuilder 获得。而 SqlSessionFactoryBuilder 则可以从 XML 配置文件或一个预先定制的 Configuration 的实例构建出 SqlSessionFactory 的实例。 从 XML 文件中构建 SqlSessionFactory 的实例非常简单,建议使用类路径下的资源文件进行配置。但是也可以使用任意的输入流(InputStream)实例,包括字符串形式的文件路径或者 file:// 的 URL 形式的文件路径来配置。MyBatis 包含一个名叫 Resources 的工具类,它包含一些实用方法,可使从 classpath 或其他位置加载资源文件更加容易。 String resource = "org/mybatis/example/mybatis-config.xml"; InputStream inputStream = Resources.getResourceAsStream(resource); SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);XML 配置文件(configuration XML)中包含了对 MyBatis 系统的核心设置,包含获取数据库连接实例的数据源(DataSource)和决定事务作用域和控制方式的事务管理器(TransactionManager)。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="development"> <environment id="development"> <transactionManager type="JDBC"/> <dataSource type="POOLED"> <property name="driver" value="${driver}"/> <property name="url" value="${url}"/> <property name="username" value="${username}"/> <property name="password" value="${password}"/> </dataSource> </environment> </environments> <mappers> <mapper resource="org/mybatis/example/BlogMapper.xml"/> </mappers> </configuration>当然,还有很多可以在XML 文件中进行配置,上面的示例指出的则是最关键的部分。要注意 XML 头部的声明,用来验证 XML 文档正确性。environment 元素体中包含了事务管理和连接池的配置。mappers 元素则是包含一组 mapper 映射器(这些 mapper 的 XML 文件包含了 SQL 代码和映射定义信息)。 不使用 XML 构建 SqlSessionFactory 如果你更愿意直接从 Java 程序而不是 XML 文件中创建 configuration,或者创建你自己的 configuration 构建器,MyBatis 也提供了完整的配置类,提供所有和 XML 文件相同功能的配置。 .....................................
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值