使用Mybatis出现的问题+配置优化+ResultMap

一、可能出现的问题

1、Error querying database.

Cause: com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: Communications link failure

原因:jdbc.properties 文件


url=jdbc:mysql://localhost:3306/testmybatis?useSSL=true&useUnicode=true&characterEncoding=UTF-8

 

解决:将useSSL=true 改为 useSSL=false

useSSL=true 作用:使用JDBC跟你的数据库连接的时候,你的JDBC版本与MySQL版本不兼容,MySQL的版本更高一些,在连接语句后加上“useSSL=‘true’” ,就可以连接到数据库了

 

2、java.lang.ExceptionInInitializerError

Error building SqlSession

原因:UserMapper.xml文件

<select id="getUserList" resultType="User">
  select * from testmybatis.user
</select>

 

方法1:将resultType="User" 改为 resultType="com.zy.pojo.User"

方法2:在mybatis配置文件(我的是mybatis-config.xml)中添加类别名

<typeAliases>
  <package name="com.zy.pojo"/>
</typeAliases>

注意写在 <properties>、<settings> 的标签下面,<environments> 标签上面

 

这里推荐方法2,修改一次就不用再一个一个的写 包路径+实体类 了

 

 

3、java.io.IOException: Could not find resource com/zy/dao/UserMapper.xml

 

原因:因为maven约定大于配置,所以配置文件存在没有被导出或者生效

 

解决:在pom.xml中添加

<build>
    <resources>
        <resource>
            <directory>src/main/resources</directory>
            <includes>
                <include>**/*.properties</include>
                <include>**/*.xml</include>
            </includes>
            <filtering>true</filtering>
        </resource>
        <resource>
            <directory>src/main/java</directory>
            <includes>
                <include>**/*.properties</include>
                <include>**/*.xml</include>
            </includes>
            <filtering>true</filtering>
        </resource>
    </resources>
</build>

 

 

二、Mybatis配置文件优化

在Mybatis配置文件mybatis-config.xml中

configuration配置结构

properties 属性

settings 设置

typeAliases 类型命名

typeHandlers 类型处理器

objectFactory 对象工厂

plugins 插件

environments 环境

  environment 环境变量

    transactionManager 事务管理器

    dataSource 数据源

databaseIdProvider 数据库厂商标识

mappers 映射器

(必须严格按照这个结构写,不然无法通过)

 

1.引入外部配置文件

在 mybatis-config.xml 配置文件中,引入外部配置文件 jdbc.properties 配置环境,方便修改

jdbc.properties:

driver=com.mysql.jdbc.Driver
url=jdbc:mysql://localhost:3306/testmybatis?useSSL=false&amp;useUnicode=true&amp;characterEncoding=UTF-8
username=root
password=123456

mybatis-config.xml:

<properties resource="jdbc.properties"></properties>

<typeAliases>
    <!--<package name="com.zy.pojo"/>-->
</typeAliases>

<!--配置环境-->
<environments default="mysql">
<!--有多个环境时,通过修改default="id"来实现选择-->

<environment id="mysql">
    <!--JDBC事务管理-->
    <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>

 

 三、ResultMap(结果映射集)

当数据库字段与实体类字段的属性名不一致时,会出现返回null的问题

比如:

  这是表:

  这是对应实体类:

通过junit进行测试:

 可以发现返回的返回的是null

这时可以通过ResultMap(结果映射集)来解决

  UserMapper.xml:

<!--<select id="getUserList" resultType="User">
    select * from testmybatis.user
</select>-->

<resultMap id="UserMap" type="User">

    <!--column对应数据库的字段,property对应实体类属性 -->
    <result column="id" property="id"/>
    <result column="name" property="username"/>
    <result column="pwd" property="password"/>
</resultMap>

<select id="getUserList" resultMap="UserMap">
    select * from testmybatis.user
</select>


再进行测试:

 成功!

原文地址:https://www.cnblogs.com/kzyuan/p/12589437.html