2019-12-24 mysql查询结果映射问题

前情描述:使用tkmybatis作为持久层的框架技术,我写了一个select * from t_user,user表中有一个t_username字段,自定义了一个方法User find username(long id)使用@Select(select user from t_user)注解,在返回的实例中username是空的,也就是说没有映射上

问题分析:
带有下划线的字段进行结果映射时,会自动转成小驼峰式的格式,也就是说t_username会转成tUsername,从而和实体类中的变量无法对应上,无法实现自动映射

解决方案:
1.在mybatis中加上如下配置

<?xml version="1.0" encoding="UTF-8"?><!DOCTYPE configuration
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
<!--开启驼峰命名规则自动转换-->
<settings>
    <setting name="mapUnderscoreToCamelCase" value="true"/>
    </settings>
</configuration>

在springboot的配置文件application.yml中
mybatis: typeAliasesPackage: com.example.mybaitsxml.dao.entity
mapperLocations: classpath:mapper/*.xml
configLocation: classpath:/mybatis-config.xml

2.在springboot的配置文件application.yml中
#mybatis配置
mybatis:
  typeAliasesPackage: com.example.mybaitsxml.dao.entity
  mapperLocations: classpath:mapper/*.xml
  configuration:
    map-underscore-to-camel-case: true

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。