解决: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

 

posted on 2019-07-02 21:43  pu20065226  阅读(2139)  评论(0编辑  收藏  举报