Solve: application.yml in mybatis this (database fields to solve the hump and underlined the problem)
map-underscore-to-camel-case: true
problem:
mybatis debug mode results are known, but not binding on the return, return 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> </ >Folders
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 Configuration
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: test password: test driverClassName: com.mysql.jdbc.Driver server: port: 8705 mybatis: configuration:
#下面这项要开启 map-underscore-to-camel-case: true log-impl: org.apache.ibatis.logging.stdout.StdOutImpl locations-Mapper: the CLASSPATH: mappers / * xml. Eureka: Client: serviceUrl: # to registration centers defaultzone: http://192.168.111.133:8888/eureka/ instance: # intervals of 1s, the server sends a heartbeat, to prove himself still "alive" Lease-Renewal-interval- seconds the-in: 1 # tell the server if I did not give you the heartbeat within 2s, on behalf of my "dead", I will kick off. lease-expiration-duration-in- seconds: 2