需要在表对象上加autoResultMap = true,resultMap = "xxx.BaseResultMap",字段上加typeHandler = JsonFieldTypeHandler.class,xml文件加typeHandler="xxx.JsonFieldTypeHandler" javaType="xxx.ShopCartVo"
1,@TableName(value = "exchange_point_cart",autoResultMap = true,resultMap = "xxx.BaseResultMap")
2,@TableField(value = "product_list",typeHandler = JsonArrayTypeHandler.class)
private List<ShopCartVo> productList;
3,
<resultMap id="BaseResultMap" type="xxx.ExchangePointCart">
<id column="id" property="id" />
<result column="member_code" property="memberCode" />
<result column="product_list" property="productList" typeHandler="xxx.JsonArrayTypeHandler" javaType="xxx.ShopCartVo" />
</resultMap>
autoResultMap设置为true会默认的创建一个resultMap并使用,如果设置了resultMap则不会创建,
对于List<?> 的对象可以使用JsonfieldTypeHandler,autoResultMap需要设置为true,
不需要设置resultMap;如果使用JsonArrayTypeHandler则需要配置resultMap,
两种情况都是需要xml配置JsonArrayTypeHandler以及javaType的
https://mp.baomidou.com/guide/annotation.html#tablename
关于`autoResultMap`的说明:
mp会自动构建一个
ResultMap
并注入到mybatis里(一般用不上).下面讲两句: 因为mp底层是mybatis,所以一些mybatis的常识你要知道,mp只是帮你注入了常用crud到mybatis里 注入之前可以说是动态的(根据你entity的字段以及注解变化而变化),但是注入之后是静态的(等于你写在xml的东西) 而对于直接指定
typeHandler
,mybatis只支持你写在2个地方:
- 定义在resultMap里,只作用于select查询的返回结果封装
-
定义在
insert
和
update
sql的
#{property}
里的
property
后面(例:
#{property,typehandler=xxx.xxx.xxx}
),只作用于
设置值
而除了这两种直接指定
typeHandler
,mybatis有一个全局的扫描你自己的
typeHandler
包的配置,这是根据你的
property
的类型去找
typeHandler
并使用.
版权声明:本文为cajole_zero原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。