Mybatis-puls中select查询方法返回为空null

打印 上一主题 下一主题

主题 908|帖子 908|积分 2724

1、项目参数
springboot 2.6.13
jdk8
Mybatis-Plus3.5.4
2、问题描述
在3.5.4版本的MP中使用select方法查询到数据,却返回为空
实体类
  1. public class Flower {
  2.     @TableId(value = "flower_id",type = IdType.INPUT)
  3.     private int flower_id;
  4.     private String flower_name;
  5.     private double price;
  6.     private String main_material;
  7. }
复制代码
查询语句
  1. class SpringbootMpApplicationTests {
  2.     @Autowired
  3.     private FLowerDao fLowerDao;
  4.     @Test
  5.     void contextLoads() {
  6.         System.out.println(fLowerDao.selectById(2637775));
  7.     }
  8. }
复制代码
返回结果
  1. domainflower{flower_id=0, flower_name='null', price=2399.0, main_material='null'}
复制代码
3、问题原因
本质:定名规范的问题,在创建数据库表的时候,创建表信息如下:
  1. CREATE TABLE flower(
  2. flower_id BIGINT,
  3. flower_name VARCHAR(100),
  4. price  DECIMAL(8.2),
  5. main_material VARCHAR(10),
  6. )DEFAULT CHARSET=utf8;
复制代码
Mybatisplus查询数据的时候,会默认使用驼峰定名法,也是就会使用flowerId,flowerName,price,mainMaterial。
造成的结果:由于Mybatisplus的这个规则问题,造成了默认的映射失败,也就是数据库的字段被修改成了flowerId,而bean字段为flower_id,这就造成了映射失败
4、解决方案
4.1我们在数据库和bean的定名上接纳驼峰定名法,就可以避免这个这个。
实体类改为
  1. public class Flower {
  2.     @TableId(value = "flower_id",type = IdType.INPUT)
  3.     private int flowerId;
  4.     @TableField(value = "flower_name")
  5.     private String flowerName;
  6.     private double price;
  7.     @TableField(value = "main_material")
  8.     private String mainMaterial;
  9. }
复制代码
4.2关闭mybatisplus默认的驼峰定名法:关闭方式如下
org.apache.ibatis.logging.stdout.StdOutImpl:打印mybatisplus执行的sql语句
 map-underscore-to-camel-case:关闭驼峰定名法
  1. #在application.yml中插入
  2. mybatis-plus:
  3.   configuration:
  4.     log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
  5.     map-underscore-to-camel-case: false
复制代码
 

免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。
回复

使用道具 举报

0 个回复

倒序浏览

快速回复

您需要登录后才可以回帖 登录 or 立即注册

本版积分规则

傲渊山岳

金牌会员
这个人很懒什么都没写!
快速回复 返回顶部 返回列表