解决: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. [19/05/13-星期一] HTML_head标签 和 body标签_文本标签

    一.概念 概念:超文本标记语言 作用:需要将Java在后台根据用户的请求处理结果在浏览器显示给用户.数据已经过来了,但是显示可能比较凌乱,所以html应用而生,就像写作文要加标点看着舒服. 在浏览器中 ...

  2. POJ-1064.Cablemaster.(二分法枚举值求最优值)

    题目链接 本题大意:给你n个长度为value[ i ]的长木板,让你切割成为等长的k份,问你切割的最大长度是多少. 本题思路:其实很容易可以想到先找到一个上界和一个下界,开始枚举里面的所有长度,取最长 ...

  3. js Object的属性 Configurable,Enumerable,Writable,Value,Getter,Setter

    对象的数据属性 Configurable,Enumerable,Writable,Value var person = {} Object.defineProperty(person,'name',{ ...

  4. Linux内核模块(Module)初解

    #include <linux/init.h> // __init __exit #include <linux/module.h> // module_init module ...

  5. java 继承extends 的相关知识点

    java只有单继承,不能多继承 子类只能继承父类的非私有成员(成员变量.成员方法) 子类不能继承父类的构造方法,但是可以通过super关键字访问父类的构造方法 继承 要体现子类父类的 继承关系, ”i ...

  6. postgresql 口令: psql: 致命错误: 用户 认证失败

    在Windows环境下使用psql 命令出现认证失败. 网上找了很多,都没讲到要点 D:\program\PostgreSql\bin>psql口令: psql: 致命错误: 用户 " ...

  7. Cocos2d-x的Android配置以及相关參考文档

    版权声明:版权声明:本文为博主原创文章.转载请附上博文链接! https://blog.csdn.net/ccf19881030/article/details/24141181     关于Win7 ...

  8. asp.net 几种传值方法的分析

    本文转自:http://www.cnblogs.com/shengtianlong/archive/2010/08/11/1797608.html ASP.NET页面传值方法的优缺点及适用范围 1. ...

  9. 一条sql引发的“血案”

    前几天有一个项目要上线,需要对表的一个字段进行扩充,项目经理让我准备脚本,于是我准备了如下的脚本: )); )); )); 结果上线的时候,ord_log1和ord_log2表中有30万数据,在执行的 ...

  10. 利用C51单片机模拟SPI进行双机通信

    SPI协议简述 SPI,是英语Serial Peripheral interface的缩写,顾名思义就是串行外围设备接口.由Motorola首创.SPI接口主要应用在 EEPROM,FLASH,实时时 ...