1、项目参数
springboot 2.6.13
jdk8
Mybatis-Plus3.5.4
2、问题描述
在3.5.4版本的MP中使用select方法查询到数据,却返回为空
实体类- public class Flower {
- @TableId(value = "flower_id",type = IdType.INPUT)
- private int flower_id;
- private String flower_name;
- private double price;
- private String main_material;
- }
复制代码 查询语句- class SpringbootMpApplicationTests {
- @Autowired
- private FLowerDao fLowerDao;
- @Test
- void contextLoads() {
- System.out.println(fLowerDao.selectById(2637775));
- }
- }
复制代码 返回结果- domainflower{flower_id=0, flower_name='null', price=2399.0, main_material='null'}
复制代码 3、问题原因
本质:定名规范的问题,在创建数据库表的时候,创建表信息如下:- CREATE TABLE flower(
- flower_id BIGINT,
- flower_name VARCHAR(100),
- price DECIMAL(8.2),
- main_material VARCHAR(10),
- )DEFAULT CHARSET=utf8;
复制代码 Mybatisplus查询数据的时候,会默认使用驼峰定名法,也是就会使用flowerId,flowerName,price,mainMaterial。
造成的结果:由于Mybatisplus的这个规则问题,造成了默认的映射失败,也就是数据库的字段被修改成了flowerId,而bean字段为flower_id,这就造成了映射失败
4、解决方案
4.1我们在数据库和bean的定名上接纳驼峰定名法,就可以避免这个这个。
实体类改为- public class Flower {
- @TableId(value = "flower_id",type = IdType.INPUT)
- private int flowerId;
- @TableField(value = "flower_name")
- private String flowerName;
- private double price;
- @TableField(value = "main_material")
- private String mainMaterial;
- }
复制代码 4.2关闭mybatisplus默认的驼峰定名法:关闭方式如下
org.apache.ibatis.logging.stdout.StdOutImpl:打印mybatisplus执行的sql语句
map-underscore-to-camel-case:关闭驼峰定名法- #在application.yml中插入
- mybatis-plus:
- configuration:
- log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
- map-underscore-to-camel-case: false
复制代码
免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。 |