一、可能出现的问题

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. Java-基础-LinkedList

    1. 简介 LinkedList 同时实现了List和Deque接口,也就是说它既可以看作是一个顺序容器,又可以看作是双向队列. 既然是双向列表,那么它的每个数据节点都一定有两个指针,分别指向它的前驱 ...

  2. Shell脚本学习笔记之(自动填充函数模板)

    其实,vii 就是写的一个脚本,跟 vi 没半毛钱关系,只不过借用一下这个名字而已.那这个脚本长什么样呢?look: 下面来详细的解析上面的代码,来看第1行: #!/bin/bash 这是Shell脚 ...

  3. systemverilog 字符串类型

    转载:https://blog.csdn.net/Holden_Liu/article/details/100727957 传统的Veriog仅仅支持文字表述上的字符串, 而SystemVerilog ...

  4. PCIE学习链接集合

    <PCIE基础知识+vivado IP core设置> https://blog.csdn.net/eagle217/article/details/81736822 <一步一步开始 ...

  5. 第12课 OpenGL 显示列表

    显示列表: 想知道如何加速你的OpenGL程序么?这一课将告诉你如何使用OpenGL的显示列表,它通过预编译OpenGL命令来加速你的程序,并可以为你省去很多重复的代码. 这次我将教你如何使用显示列表 ...

  6. C语言图书管理借阅系统——ncurses库的使用

    一.前言 作为一只大四狗,最近还跟着大二同学修了一门课(当然不是之前没通过啦),课程是高级语言课程设计,高级语言指的是C语言 :),内容是做一个XX管理系统,我选择了图书管理系统,先介绍下我做的系统: ...

  7. leetcode 剪绳子系列

    ### 剪绳子一 利用动态规划 状态转移方程 为啥是这个样子?首先  代表 长度为i的绳子被剪去j,且继续剪(子问题)  表示长度为i的绳子被剪去j,不剪了的乘积 注意初始化: n<2 f=0 ...

  8. mac下将python2.7改为python3

    mac下将python2.7改为python3 查看当前电脑python版本 python -V 修改.bash_profile文件 vi ~/.bash_profile //编辑bash_profi ...

  9. 9组-Ahlpa-6/3

    一.基本情况 队名:不行就摆了吧 组长博客:https://www.cnblogs.com/Microsoft-hc/p/15546622.html 小组人数: 8 二.冲刺概况汇报 卢浩玮 过去两天 ...

  10. Python基础(__slots__)

    class Point(object): __slots__ = ('name','point') p1 = Point() p1.name = 100 print(p1.name)#100 #p1. ...