一、可能出现的问题

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>

再进行测试:

成功!

使用Mybatis出现的问题+配置优化+ResultMap的更多相关文章

  1. Mybatis系列(二):优化MyBatis配置文件中的配置和解决字段名与实体类属性名不相同的冲突

    原文链接:http://www.cnblogs.com/xdp-gacl/p/4264301.html     http://www.cnblogs.com/xdp-gacl/p/4264425.ht ...

  2. 【转】MyBatis学习总结(三)——优化MyBatis配置文件中的配置

    [转]MyBatis学习总结(三)——优化MyBatis配置文件中的配置 一.连接数据库的配置单独放在一个properties文件中 之前,我们是直接将数据库的连接配置信息写在了MyBatis的con ...

  3. mybatis mapper xml文件配置resultmap时,id行和result行有什么区别?

    mybatis mapper xml文件配置resultmap时,id行和result行有什么区别? <resultMap id = "CashInvoiceMap" typ ...

  4. mybatis配置优化

    1.加入日志log4j 1)加入jar包:log4j-1.2.17.jar 2)加入log4j配置文件: 可以使properties或者xml形式 log4j.rootLogger = DEBUG,C ...

  5. Mybatis非mapper代理配置

    转: Mybatis非mapper代理配置 2017年04月26日 20:13:48 待长的小蘑菇 阅读数:870   版权声明:本文为博主原创文章,未经博主允许不得转载. https://blog. ...

  6. MyBatis基础入门《九》ResultMap自动匹配

    MyBatis基础入门<九>ResultMap自动匹配 描述: Mybatis执行select查询后,使用ResultMap接收查询的数据结果. 实体类:TblClient.java 接口 ...

  7. MyBatis—mapper.xml映射配置

    SQL文件映射(mapper文件),几个顶级元素的配置: mapper元素:根节点只有一个属性namespace(命名空间)作用: 1:用于区分不同的mapper,全局唯一. 2:绑定DAO接口,即面 ...

  8. mybatis 基础(一) xml配置

    如果文章有误,请各位楼下评论,感谢各位积极修正! 一起学习,成为大佬! mybatis: 1.轻量级 2.高级映射(实体类与数据库表字段的映射) 这样就可以后续开发中去操作实体类而不需要去关注数据库, ...

  9. MyBatis(4)——配置文件优化

    配置文件优化 执行流程:读取配置流程->sqlSessionFactory->sqlSession(连接.读取sql并执行相应操作.关闭) a)配置优化:通过中文参考指南的说明可知-> ...

随机推荐

  1. [LGP2758]编辑距离

    目录 题目 题目描述 输入格式 输出格式 输入输出样例 题目分析 状态转移方程 初始状态 结束状态 Code 题目 题目描述 设A和B是两个字符串.我们要用最少的字符操作次数,将字符串A转换为字符串B ...

  2. Spring源码解读(二):Spring AOP

    一.AOP介绍 面向方面编程(AOP)通过提供另一种思考程序结构的方式来补充面向对象编程(OOP).OOP中模块化的关键单元是类,而在AOP中,模块化单元是方面.方面实现了诸如跨越多种类型和对象的事务 ...

  3. wifi 热点配置最优信道

    wifi热点服务hostapd启动需要配置hostad.conf文件,其中有一个参数channel是用来配置信道的,信道的可选参数如下: # channel 1-14 is 2.4 GHz ; cha ...

  4. reorder-list leetcode C++

    Given a singly linked list L: L 0→L 1→-→L n-1→L n, reorder it to: L 0→L n →L 1→L n-1→L 2→L n-2→- You ...

  5. docker 简单总结

    一.docker 安装 yum 方式在centos和rhce上的安装条件: 要安装Docker引擎,你需要一个维护版本的CentOS 7或8.不支持或测试存档版本.必须启用centos-extras存 ...

  6. 云主机新增swap分区

    云主机内存不够用,如何新增swap 分区. 如何启用swap分区? 步骤如下: 1.查看当前系统中是否已经启用swap分区 1 2 cat /proc/swaps    top 2.如果没有启用swa ...

  7. k8s入坑之路(10)kubernetes coredns详解

    概述 作为服务发现机制的基本功能,在集群内需要能够通过服务名对服务进行访问,那么就需要一个集群范围内的DNS服务来完成从服务名到ClusterIP的解析. DNS服务在kubernetes中经历了三个 ...

  8. centos yum更换阿里镜像

    #1.如果没有wget命令,则需要执行下面命令进行安装.为保险期间,先执行下面命令. yum install wget #2.备份原镜像源,以免出错后可以恢复. mv /etc/yum.repos.d ...

  9. Spring Cloud调用接口过程

    Spring Cloud 在接口调用上,大致会经过如下几个组件配合: Feign== >Hystrix ==>Ribbon ==>Http Client(apache http co ...

  10. C++ STL的一些应用

    STL一些应用 记录一些STL算法在开发中用得比较舒服的情况(不断添加...) lower_bound(begin,end,val)算法 算法说明 查找>=val的第一个元素,如果没有,返回en ...