一、可能出现的问题

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. linux中的strip命令简介

    转载:https://blog.csdn.net/qq_37858386/article/details/78559490 strip:去除,剥去     一.下面是man strip获得到的信息,简 ...

  2. uvm_subscriber

    subscriber是消费,用户的意思 uvm_subscriber主要作为coverage的收集方式之一 uvm_subscriber的代码非常简单,继承于uvm_component,再加上一个an ...

  3. hdu 5083 Instruction (稍比较复杂的模拟题)

    题意: 二进制指令转汇编指令,汇编指令转二进制指令. 思路: 额,条理分好,想全,思维不能乱. 代码: int findyu(char yu[50],char c){ int l=strlen(yu) ...

  4. Spark记录(二):Spark程序的生命周期

    本文以Spark执行模式中最常见的集群模式为例,详细的描述一下Spark程序的生命周期(YARN作为集群管理器). 1.集群节点初始化 集群刚初始化的时候,或者之前的Spark任务完成之后,此时集群中 ...

  5. centos7.2安装rabbitmq教程

    环境: centos7.2 rabbitmq依赖erlang,需要先安装erlang 1 安装erlang rpm -Uvh https://download.fedoraproject.org/pu ...

  6. celery tasks always in pending

    Result backend doesn't work or tasks are always in PENDING state¶All tasks are PENDING by default, s ...

  7. 【JAVA】编程(2)---时间管理

    作业要求: 定义一个名为 MyTime 的类,其中私有属性包括天数,时,分,秒:定义一个可以初始化时,分,秒的构造方法,并对初始化数值加以限定,以防出现bug:定义一个方法,可以把第几天,时,分,秒打 ...

  8. 分布式链路追踪之Spring Cloud Sleuth+Zipkin最全教程!

    大家好,我是不才陈某~ 这是<Spring Cloud 进阶>第九篇文章,往期文章如下: 五十五张图告诉你微服务的灵魂摆渡者Nacos究竟有多强? openFeign夺命连环9问,这谁受得 ...

  9. 退出cmd命令

    中断cmd正在执行的任务:按 Ctrl+C退出cmd:exit最好不要直接关闭,而是用Ctrl+C中断任务后在关闭,以免造成程序运行异常.

  10. [cf1236F]Alice and the Cactus

    首先,我们要用到期望的一个性质: 对于两个随机变量$X$和$Y$(不需要相互独立),有$E(X+Y)=E(X)+E(Y)$ 另外,对于一个仙人掌,令$n$为点数,$m$为边数,$c$为简单环个数,$X ...