本文介绍了如何根据不列表中的所有属性标注通过MyBatis的插入对象(超过10属性)到mysql的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想通过基于注解的MyBatis插入超过10属性的对象到MySQL。但是,我必须列出所有的属性,这是太不方便了。我想知道的是有一些方法通过MyBatis的,而不单轻松插入对象的所有属性。这是我的片段。非常感谢。

  @Insert(插入poi_shop(name,brand,tags,status,phone,mobile,business_time,address,city,lng,lat,business_type,attribute_json) values(#{name},#{brand},#{tags},#{status},#{phone},#{mobile},#{business_time},#{address},#{city},#{lng},#{lat},#{business_type},#{attribute_json})\")
@Options(useGeneratedKeys = TRUE,keyProperty =ID,keyColumn =ID)
众长insertPoiInfo(PoiBo poiBo);


解决方案

这是不可能的MyBatis中(至少3版)。

MyBatis的从数据库中读取数据时,但没有选择在插入自动映射领域拥有自动映射。

这样做的原因是,MyBatis的十分SQL中心就是您需要手动编写SQL。它可以在SQL SELECT语句( SELECT * FROM表)隐场所以自动映射在这种情况下POJO但它是不可能有内隐场更新或插入,因此没有自动映射。

I want to insert an Object with more than 10 properties into mysql via mybatis based on annotation. But I must list all properties, it's too inconvenient. I want to know is there some ways to insert an object easily without list all properties via mybatis. Here is my snippet. Thanks a lot.

@Insert("insert into poi_shop(name,brand,tags,status,phone,mobile,business_time,address,city,lng,lat,business_type,attribute_json) values(#{name},#{brand},#{tags},#{status},#{phone},#{mobile},#{business_time},#{address},#{city},#{lng},#{lat},#{business_type},#{attribute_json})")
@Options(useGeneratedKeys = true, keyProperty = "id", keyColumn = "id")
public Long insertPoiInfo(PoiBo poiBo);
解决方案

It is not possible in MyBatis (at least version 3).

MyBatis has auto mapping when reading data from database but doesn't have option to automatically map fields on insertion.

The reason for this is that MyBatis is very SQL centric that is you need to write SQL manually. It is possible to have implicit fields in SQL select statement (select * from table) so there is automatic mapping to POJO in this case but it is not possible to have implicit fields in update or insert hence no auto-mapping.

这篇关于如何根据不列表中的所有属性标注通过MyBatis的插入对象(超过10属性)到mysql的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-15 16:35