MyBatis 集成
JSONMap 可与 MyBatis 配合处理数据库中的 JSON 字段。
示例
// Mapper
@Mapper
public interface ProductMapper {
@Select("SELECT * FROM product WHERE id = #{id}")
Product selectById(Long id);
}
// Service
@Service
public class ProductService {
public Product getById(Long id) {
Product product = productMapper.selectById(id);
// JSON 字段自动转换
if (product.getExtInfo() != null) {
JSONMap ext = new JSONMap(product.getExtInfo());
product.setBrand(ext.getStr("brand"));
product.setModel(ext.getStr("model"));
}
return product;
}
}