导航菜单
首页 >  报考显示查询结果为空  > MybatisPlus查询结果返回值为null

MybatisPlus查询结果返回值为null

1、问题描述

返回值为null,程序不报错,但是条数好像是正确的。我出现问题的代码如下:

1、自定义类StudentMapper继承了BaseMapper接口

public interface StudentMapper extends BaseMapper {}

2、使用StudentMapper中的selectList方法查询数据的时候,打印结果的时候返回值都会null,但是条数没有问题,也就是有几条数据就返回几个null.

如下代码:list.forEach(System.out::println);返回的结果都是null

@RequestMapping("/testmybatisplus")@ResponseBodypublic void testmybatisplus() {System.out.println("===1===");List list=studentMapper.selectList(null);//Assert.isTrue(5 == list.size(), "");list.forEach(System.out::println);} 2、出现问题的原因

本质:命名规范的问题,在创建数据库表的时候,创建表信息如下:

CREATE TABLE student(    stu_id VARCHAR(50),    stu_name VARCHAR(30),    stu_sex VARCHAR(2),    stu_age VARCHAR(4),    stu_addr VARCHAR(50),    stu_pwd VARCHAR(50) )DEFAULT CHARSET=utf8;

Mybatisplus查询数据的时候,会默认使用驼峰命名法,也是就会使用stuId,stuName,stuSex,stuAge,stuAddr,stuPwd。

我java中对应的bean信息如下:

@Data@AllArgsConstructor@NoArgsConstructorpublic class Student {private String stu_id;private String stu_name;private String stu_sex;private String stu_age;private String stu_addr;private String stu_pwd;}

造成的结果:由于Mybatisplus的这个规则问题,造成了默认的映射失败,也就是数据库的字段被修改成了stuId,而bean字段为stu_id,这就造成了映射失败。

3、解决方法 3.1、解决办法1

我们在数据库和bean的命名上采用驼峰命名法,就可以避免这个这个。

3.2、解决办法2

关闭mybatisplus默认的驼峰命名法:关闭方式如下

org.apache.ibatis.logging.stdout.StdOutImpl:打印mybatisplus执行的sql语句

 map-underscore-to-camel-case:关闭驼峰命名法

mybatis-plus: configuration:log-impl: org.apache.ibatis.logging.stdout.StdOutImplmap-underscore-to-camel-case: false # 如果是放在src/main/java目录下 classpath:/com/*/*/mapper/*Mapper.xml # 如果是放在resource目录 classpath:/mapper/**.xml #mapper-locations: classpath:/mapper/**.xml

 

 

 

 

 

相关推荐: