在IIs下建设一个网站,php网站上传教程,dnf辅助源码论坛,fomo3d网站开发1、问题描述
返回值为null#xff0c;程序不报错#xff0c;但是条数好像是正确的。我出现问题的代码如下#xff1a;
1、自定义类StudentMapper继承了BaseMapper接口
public interface StudentMapper extends BaseMapperStudent {
}
2、使用StudentMapper中的s…1、问题描述
返回值为null程序不报错但是条数好像是正确的。我出现问题的代码如下
1、自定义类StudentMapper继承了BaseMapper接口
public interface StudentMapper extends BaseMapperStudent {
}
2、使用StudentMapper中的selectList方法查询数据的时候打印结果的时候返回值都会null但是条数没有问题也就是有几条数据就返回几个null.
如下代码list.forEach(System.out::println);返回的结果都是null
RequestMapping(/testmybatisplus)ResponseBodypublic void testmybatisplus() {System.out.println(1);ListStudent liststudentMapper.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 CHARSETutf8;
Mybatisplus查询数据的时候会默认使用驼峰命名法也是就会使用stuId,stuName,stuSex,stuAge,stuAddr,stuPwd。
我java中对应的bean信息如下
Data
AllArgsConstructor
NoArgsConstructor
public 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