In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
这篇文章主要介绍了springboot怎么嵌套子类的相关知识,内容详细易懂,操作简单快捷,具有一定借鉴价值,相信大家阅读完这篇springboot怎么嵌套子类文章都会有所收获,下面我们一起来看看吧。
springboot嵌套子类使用
在实际项目里,我们会使用到一个User用户含有子类Address、这种嵌套子类在开发中会遇到很多问题,现在主要针对前台和后台的开发问题进行整理。
实际Bean类
User类里含有Address类
public class User { private int userId; private String userName; private Address userAddress;}public class Address { private int addressId; private String city;//忽略get、set方法}UserMapper.java 对于一对一
使用@Result注解,调用查询子类数据。
public interface UserMapper{ //one to one @Select("select * from user where userId = #{userId}") @Results({ @Result(id=true,column="userId",property="userId"), @Result(column="userName",property="userName"),@Result(column="userId",property="userAddress",one=@One(select="com.whale.mapper.AddressMapper.getAddressByUserId",fetchType= FetchType.EAGER))})public interface AddressMapper { @Select("select * from address where userId=#{userId}") public Address getAddressByUserId(int userId);}前台使用bootstrap-table进行解析显示
在formmater里进行子类显示
columns: [{ checkbox : true }, { field: 'userId', title: '用户ID' }, { field: 'userName', title: '用户姓名' }, { field: 'userAddress', title: '地址', formatter : function(value,row, index){ //主要配置在这里 return value.city; } } ]后台Controller转换成Json会出现 $ref
解决办法如下:
注意:
String jsonSting=JSON.toJSONString(result, SerializerFeature.DisableCircularReferenceDetect);
结果中出现$ref,先把JSONObject转换成String,然后再转换回JSONObject
@ResponseBody @RequestMapping("allUser") public String findALL(){ PageHelper.startPage(pageNumber,pageSize); List userList= userService.findAll(); PageInfo page=new PageInfo(userList); //取出查询结果 List rows = page.getList(); JSONObject result = new JSONObject(); int total = (int) page.getTotal(); result.put("total",total); result.put("rows",rows); String jsonSting=JSON.toJSONString(result, SerializerFeature.DisableCircularReferenceDetect); //结果中出现$ref,先把JSONObject转换成String,然后再转换回JSONObject JSONObject json= JSON.parseObject(jsonSting); System.out.println(json.toJSONString());}注解在嵌套方法不生效问题例如在service中方法如下 @PermissionAop @Override public List getList() { List list = this.list(); return list; } @Override public void test(){ this.getList(); System.out.println(""); }
controller中调用test()方法,test方法中调用切面方法,切面不生效,无法进入切面。
使用如下方式可解决该问题(启动类上需要添加
@EnableAspectJAutoProxy(exposeProxy = true)) @PermissionAop @Override public List getList() { List list = this.list(); return list; } @Override public void test(){ IPoiPermissionService service = (IPoiPermissionService) AopContext.currentProxy(); service.getList(); System.out.println(""); }
其中IPoiPermissionService为该service类实现的接口。
关于"springboot怎么嵌套子类"这篇文章的内容就介绍到这里,感谢各位的阅读!相信大家对"springboot怎么嵌套子类"知识都有一定的了解,大家如果还想学习更多知识,欢迎关注行业资讯频道。
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.