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

摘要:
--当存在多个环境时,默认值=“id”可以修改为选择--˃III。ResultMap是数据库字段和实体类字段当的属性名称不一致时,例如,这是一个表,这是对应的实体类,可以通过junit进行测试。可以发现返回了null。在这种情况下,可以通过ResultMap:UserMapper解决问题。xml:˂!

一、可能出现的问题

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的问题

比如:

  这是表:

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

  这是对应实体类:

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

通过junit进行测试:

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

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

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

  UserMapper.xml:

<!--<select   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>

再进行测试:

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

 成功!

免责声明:文章转载自《使用Mybatis出现的问题+配置优化+ResultMap》仅用于学习参考。如对内容有疑问,请及时联系本站处理。

上篇自我的SZmanjaro20安装TIM下篇

宿迁高防,2C2G15M,22元/月;香港BGP,2C5G5M,25元/月 雨云优惠码:MjYwNzM=

相关文章

MyBatis配置文件(八)--databaseIdProvider数据库厂商标识

databaseIdProvider元素主要是为了支持不同厂商的数据库,比如有时候我们在公司内部开发使用的数据库都是PG(Postgresql),但是客户要求使用MySql,那就麻烦了是吧?其实在mybatis中我们可以使用databaseIdProvider这个元素实现数据库兼容不同厂商,即配置多中数据库。 看一下配置方式: 1 <!--数据库厂商...

JavaWeb项目之博客系统(二)上

1.JDBC技术 来源:http://zengyiqiang2006.blog.163.com/blog/static/102868226201052673216733/ JDBC技术(访问数据库的一种技术) 核心主要是使用DriverManager类操作和管理实现Driver接口的实现类, 程序员只需要向这个驱动管理器类要连接对象就可以了 ( Conne...

使用Mybatis-Generator自动生成Dao、Model、Mapping相关文件

出处:http://www.cnblogs.com/lichenwei/p/4145696.html Mybatis属于半自动ORM,在使用这个框架中,工作量最大的就是书写Mapping的映射文件,由于手动书写很容易出错,我们可以利用Mybatis-Generator来帮我们自动生成文件。 1、相关文件 关于Mybatis-Generator的下载可以到这...

java oracle thin 和 oci 连接方式实现多数据库的故障切换

java oracle thin 和 oci 连接方式实现多数据库的故障切换 一、thin方式 该种方式简便易用非经常见。 当中URL为 jdbc:oracle:thin:@(DESCRIPTION=(LOAD_BALANCE=on) (ADDRESS=(PROTOCOL=TCP)(HOST=127.0.0.1)(PORT=1521)) (ADDRESS...

Spring配置JDBC连接Orcale、MySql、sqlserver

阅读指南:本文章主要讲述如何在Spring框架中配置JDBC连接方式连接Oracle、Mysql、SqlServer。 原理如下: 一、导包 连接oracle11g所需的jar包:ojdbc6.jar连接mysql5.1所需的jar包:mysql-connector-java-5.1.12-bin.jar连接sqlserver2008所需的jar包:sql...

MyBatis基础总结

1.1什么是MyBatis MyBatis(前身是iBatis)是一个支持普通SQL查询、存储过程以及高级映射的持久层框架, 它消除了几乎所有的JDBC代码和参数的手动设置以及对结果集的检索,并使用简单的XML或注解进行配置和原始映射, 用以将接口和Java的POJO(Plain Old Java Object,普通Java对象)映射成数据库中的记录,使得...