mybatis有结果返回null

本是古典 何须时尚 2021-11-16 09:52 511阅读 0赞

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

  1. map-underscore-to-camel-case: true

问题:

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

  1. 2019-07-02 21:30:01.000 INFO 13908 --- [nio-8705-exec-1] o.s.web.servlet.DispatcherServlet : Completed initialization in 6 ms
  2. before:UserDto{id='null', name='aaa'}
  3. Creating a new SqlSession
  4. SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@365ae636] was not registered for synchronization because synchronization is not active
  5. 2019-07-02 21:30:05.548 INFO 13908 --- [nio-8705-exec-1] com.zaxxer.hikari.HikariDataSource : HikariPool-1 - Starting...
  6. 2019-07-02 21:30:06.108 INFO 13908 --- [nio-8705-exec-1] com.zaxxer.hikari.HikariDataSource : HikariPool-1 - Start completed.
  7. JDBC Connection [HikariProxyConnection@1029331665 wrapping com.mysql.jdbc.JDBC4Connection@6a9043f2] will not be managed by Spring
  8. ==> Preparing: select id as USER_ID from t_user where name=?
  9. ==> Parameters: aaa(String)
  10. <== Columns: USER_ID
  11. <== Row: 111
  12. <== Total: 1
  13. Closing non transactional SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@365ae636]
  14. after:null
  15. 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

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

service.java

  1. package com.example.mybatistest.service.impl;
  2. import com.example.mybatistest.mapper.ISelectIdMapper;
  3. import com.example.mybatistest.mapper.UserDto;
  4. import com.example.mybatistest.service.IQueryIdByName;
  5. import org.springframework.beans.factory.annotation.Autowired;
  6. import org.springframework.stereotype.Service;
  7. /**
  8. * @Title:
  9. * @Auther: test
  10. * @Date: 2019/6/24 17:25
  11. * @Version: 1.0
  12. * @Description:
  13. */
  14. @Service
  15. public class QueryIdByNameImpl implements IQueryIdByName {
  16. @Autowired
  17. private ISelectIdMapper selectIdMapper;
  18. @Override
  19. public String queryIdByName(String name) {
  20. UserDto userDto=new UserDto();
  21. userDto.setName(name);
  22. System.out.println("before:"+userDto);
  23. userDto=selectIdMapper.selectId(userDto);
  24. System.out.println("after:"+userDto);
  25. return ((UserDto) userDto).toString();
  26. }
  27. }

dto.java

  1. package com.example.mybatistest.mapper;
  2. /**
  3. * @Title:
  4. * @Auther: test
  5. * @Date: 2019/7/2 20:55
  6. * @Version: 1.0
  7. * @Description:
  8. */
  9. public class UserDto {
  10. private String userId;
  11. private String name;
  12. public String getUserId() {
  13. return userId;
  14. }
  15. public void setUserId(String userId) {
  16. this.userId = userId;
  17. }
  18. public String getName() {
  19. return name;
  20. }
  21. public void setName(String name) {
  22. this.name = name;
  23. }
  24. @Override
  25. public String toString() {
  26. return "UserDto{" +
  27. "id='" + userId + '\'' +
  28. ", name='" + name + '\'' +
  29. '}';
  30. }
  31. }

yml配置

  1. spring:
  2. application:
  3. name: service-mybatistest
  4. datasource:
  5. # 数据库配置
  6. 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
  7. username: test
  8. password: test
  9. driverClassName: com.mysql.jdbc.Driver
  10. server:
  11. port: 8705
  12. mybatis:
  13. configuration:
  14. #下面这项要开启
  15. map-underscore-to-camel-case: true
  16. log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
  17. mapper-locations: classpath:mappers/*.xml
  18. eureka:
  19. client:
  20. serviceUrl:
  21. #指向注册中心
  22. defaultZone: http://192.168.111.133:8888/eureka/
  23. instance:
  24. # 每间隔1s,向服务端发送一次心跳,证明自己依然”存活“
  25. lease-renewal-interval-in-seconds: 1
  26. # 告诉服务端,如果我2s之内没有给你发心跳,就代表我“死”了,将我踢出掉。
  27. lease-expiration-duration-in-seconds: 2

转载于:https://www.cnblogs.com/pu20065226/p/11123354.html

发表评论

表情:
评论列表 (有 0 条评论,511人围观)

还没有评论,来说两句吧...

相关阅读