解决:application.yml 中mybatis此项(解决驼峰及数据库字段有下划线问题)

map-underscore-to-camel-case: true

问题:

mybatis debug模式有结果,但返回时绑定不上,返回null

2019-07-02 21:30:01.000  INFO 13908 --- [nio-8705-exec-1] o.s.web.servlet.DispatcherServlet        : Completed initialization in 6 ms
before:UserDto{id='null', name='aaa'} Creating a new SqlSession
SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@365ae636] was not registered for synchronization because synchronization is not active
2019-07-02 21:30:05.548 INFO 13908 --- [nio-8705-exec-1] com.zaxxer.hikari.HikariDataSource : HikariPool-1 - Starting...
2019-07-02 21:30:06.108 INFO 13908 --- [nio-8705-exec-1] com.zaxxer.hikari.HikariDataSource : HikariPool-1 - Start completed.
JDBC Connection [HikariProxyConnection@1029331665 wrapping com.mysql.jdbc.JDBC4Connection@6a9043f2] will not be managed by Spring
==> Preparing: select id as USER_ID from t_user where name=?
==> Parameters: aaa(String)
<== Columns: USER_ID
<== Row: 111
<== Total: 1
Closing non transactional SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@365ae636]
after:null
2019-07-02 21:36:12.590 WARN 13908 --- [l-1 housekeeper] com.zaxxer.hikari.pool.HikariPool : HikariPool-1 - Thread starvation or clock leap detected (housekeeper delta=6m6s378ms486µs221ns).

mapper.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.example.mybatistest.mapper.ISelectIdMapper">
<select id="selectId" resultType="com.example.mybatistest.mapper.UserDto" parameterType="com.example.mybatistest.mapper.UserDto">
select id as USER_ID from t_user where name=#{name}
</select>
</mapper>

service.java

package com.example.mybatistest.service.impl;

import com.example.mybatistest.mapper.ISelectIdMapper;
import com.example.mybatistest.mapper.UserDto;
import com.example.mybatistest.service.IQueryIdByName;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service; /**
* @Title:
* @Auther: test
* @Date: 2019/6/24 17:25
* @Version: 1.0
* @Description:
*/
@Service
public class QueryIdByNameImpl implements IQueryIdByName {
@Autowired
private ISelectIdMapper selectIdMapper; @Override
public String queryIdByName(String name) {
UserDto userDto=new UserDto();
userDto.setName(name);
System.out.println("before:"+userDto); userDto=selectIdMapper.selectId(userDto);
System.out.println("after:"+userDto);
return ((UserDto) userDto).toString();
}
}

dto.java

package com.example.mybatistest.mapper;

/**
* @Title:
* @Auther: test
* @Date: 2019/7/2 20:55
* @Version: 1.0
* @Description:
*/
public class UserDto {
private String userId;
private String name; public String getUserId() {
return userId;
} public void setUserId(String userId) {
this.userId = userId;
} public String getName() {
return name;
} public void setName(String name) {
this.name = name;
} @Override
public String toString() {
return "UserDto{" +
"id='" + userId + '\'' +
", name='" + name + '\'' +
'}';
}
}

yml配置

spring:
application:
name: service-mybatistest
datasource:
# 数据库配置
url: jdbc:mysql://192.168.1.1:3306/test?serverTimezone=GMT%2B8&useUnicode=true&characterEncoding=UTF-8&useSSL=false&autoReconnect=true&failOverReadOnly=false&maxReconnects=10
username: aa
password: aa
driverClassName: com.mysql.jdbc.Driver
server:
port: 8705
mybatis:
configuration:
#下面这项要开启
map-underscore-to-camel-case: true
log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
mapper-locations: classpath:mappers/*.xml
eureka:
client:
serviceUrl:
#指向注册中心
defaultZone: http://192.168.111.133:8888/eureka/
instance:
# 每间隔1s,向服务端发送一次心跳,证明自己依然”存活“
lease-renewal-interval-in-seconds: 1
# 告诉服务端,如果我2s之内没有给你发心跳,就代表我“死”了,将我踢出掉。
lease-expiration-duration-in-seconds: 2

mybatis有结果返回null的更多相关文章

  1. JVM加载类冲突,导致Mybatis查数据库返回NULL的一个小问题

    今天碰到个bug,虽然小,但是有点意思 背景是SpringMVC + Mybatis的一个项目,mapper文件里写了一条sql 大概相当于 select a from tableA where b ...

  2. SpringMVC + Mybatis bug调试 SQL正确,查数据库却返回NULL

    今天碰到个bug,有点意思 背景是SpringMVC + Mybatis的一个项目,mapper文件里写了一条sql 大概相当于 select a from tableA where b = &quo ...

  3. mybatis查询返回null解决方案

    mybatis查询返回null解决方案: 问题:查询出的列与javabean中的字段名不一致. 解决方案: 1.将javabean中的字段改为和查询出的列名一致: 2.将sql加入as改变列名,和ja ...

  4. MyBatis 插入时返回刚插入记录的主键值

    MyBatis 插入时返回刚插入记录的主键值 一.要求: 1.数据库表中的主键是自增长的,如:id: 2.获取刚刚插入的记录的id值: 二.源代码: 1.User.java package cn.co ...

  5. spring boot整合mybatis查询数据库返回Map字段为空不返回解决

    1.出现问题原因原因1:mybatis的配置即mapper返回映射配置. 原因2:jackson的配置即@ResponseBody序列化配置. 2.解决方式步骤1:解决原因1 mybatis: con ...

  6. Mybatis参数传递及返回类型

    mybatis参数传递: 单个参数:不做特殊处理        #{参数名}:取出参数值    多个参数:做特殊处理        多个参数会被封装成一个map            key:para ...

  7. Java MyBatis 插入数据库返回主键

    最近在搞一个电商系统中由于业务需求,需要在插入一条产品信息后返回产品Id,刚开始遇到一些坑,这里做下笔记,以防今后忘记. 类似下面这段代码一样获取插入后的主键 User user = new User ...

  8. json_decode返回NULL

    最近在调用某公司的API时,将对方返回的数据,使用PHP的json_decode函数解析,但是返回NULL,最终排查为对方传送来的json格式有误 打印$_REQUEST,数据结构大致如下: arra ...

  9. $.parseJson 在 firefox 下返回 null 的问题

    最近调查一个浏览器兼容性问题,在 IE, chrome下都运行正常,但是在 firefox 下运行时: $.parseJson(xxx) 返回 null,所以导致了 无法正常运行,调查的结果是因为 返 ...

随机推荐

  1. C++学习笔记(七)--共用体、枚举、typedef

    1.共用体 union其定义与结构体类似:union 类型名{ 成员表列;};声明变量的方法也类似: a. union 类型名{            b. union { c.类型名 变量名; 成员 ...

  2. linux中cut命令

    cut命令 cut常用参数 cut命令用来显示行中的指定部分,删除文件中指定字段. 说明:该命令有两项功能,其一是用来显示文件的内容,它依次读取由参数file所指明的文件,将它们的内容输出到标准输出上 ...

  3. [BZOJ4182]Shopping (点分治+树上多重背包+单调队列优化)

    [BZOJ4182]Shopping (点分治+树上多重背包+单调队列优化) 题面 马上就是小苗的生日了,为了给小苗准备礼物,小葱兴冲冲地来到了商店街.商店街有n个商店,并且它们之间的道路构成了一颗树 ...

  4. Golang环境配置

    下载 下载地址 Go官网下载地址:https://golang.org/dl/ Go官方镜像站(推荐):https://golang.google.cn/dl/ 版本选择 安装 Windows安装 示 ...

  5. 洛谷 - P4008 - 文本编辑器 - 无旋Treap

    https://www.luogu.org/problem/P4008 无旋Treap也可以维护序列. 千万要注意要先判断p节点存在才进行Show操作,不然输出一个'\0'(或者RecBin里面的东西 ...

  6. P3379 【模板】最近公共祖先(LCA)(欧拉序+rmq)

    P3379 [模板]最近公共祖先(LCA) 用欧拉序$+rmq$维护的$lca$可以做到$O(nlogn)$预处理,$O(1)$查询 从这里剻个图 #include<iostream> # ...

  7. 利用 Python 进行批量更改文件后缀

    利用 Python 进行批量更改文件后缀 代码 import os files = os.listdir('.') for file_name in files: portion = os.path. ...

  8. mongoDB关系型数据库的对比

    一.基本操作 1.mongoDB和关系型数据库对比 对比项 mongoDB mysql oracle 表 集合list 二维表 表的一行数据 文档document 一条记录 表字段 键key 字段fi ...

  9. vue.js(13)--按键修饰符

    v-on监听事件时可添加按键修饰符 <!-- 只有在 `key` 是 `Enter` 时调用 `vm.submit()` --> <input v-on:keyup.enter=&q ...

  10. 线程屏障CyclicBarrier实现原理

    生产环境中,存在需要等待多个线程都达到某种状态后,才继续运行的情景.并发工具CyclicBarrier就能够完成这种功能.本篇从源码方面,简要分析CyclicBarrier的实现原理. 使用示例 pu ...