mybatis有結果返回null


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

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM