微前端架构如何改变企业的开发模式与效率提升
887
2022-10-27
SpringMVC(15):使用springmvc优化订单管理系统的示例(Bug解决 -- 用户查询显示的内存对象赋值)
2018/1/18
解决了一个springmvc的bug,这是有关内存与对象指针的问题。实现功能:查询数据库,获取数据后,全部输出到前台。
一、Bug复现
在Mysql有以下数据(7个记录):
但是前台输出:
很奇怪,为什么数据都是一样的呢?
二、代码中插入调试日志
log.info("loop:"+user); userList.add(user); log.info("inside--result:"+userList); } log.info("outside--result:"+userList);
打印结果(整理后):
18/01/18 17:35:16 INFO Controller.UserController: getUserList ====> queryUserName: null18/01/18 17:35:16 INFO Controller.UserController: getUserList ====> queryUserRole: null18/01/18 17:35:16 INFO Controller.UserController: getUserList ====> pageIndex: nullsql: select COUNT('id') from smbms_useruserdao执行的sql: select u.userCode,u.userName,u.gender,u.birthday,u.phone,r.roleName from smbms_user u,smbms_role r where u.userRole=r.id;first:18/01/18 17:35:17 INFO Controller.UserController: loop:User [id=null, userCode=test01, userName=mmb02, userPassword=null, gender=2, birthday=1991-12-29, phone=0000, address=null, userRole=null, createdBy=null, creationDate=null, modifyBy=null, modifyDate=null, roleNameAdministrator]18/01/18 17:35:17 INFO Controller.UserController: inside--result:[User [id=null, userCode=test01, userName=mmb02, userPassword=null, gender=2, birthday=1991-12-29, phone=0000, address=null, userRole=null, createdBy=null, creationDate=null, modifyBy=null, modifyDate=null, roleNameAdministrator]]second:18/01/18 17:35:17 INFO Controller.UserController: loop:User [id=null, userCode=test01, userName=mmb02, userPassword=null, gender=2, birthday=1991-12-29, phone=0000, address=null, userRole=null, createdBy=null, creationDate=null, modifyBy=null, modifyDate=null, roleNameAdministrator]18/01/18 17:35:17 INFO Controller.UserController: inside--result:[User [id=null, userCode=test01, userName=mmb02, userPassword=null, gender=2, birthday=1991-12-29, phone=0000, address=null, userRole=null, createdBy=null, creationDate=null, modifyBy=null, modifyDate=null, roleNameAdministrator], User [id=null, userCode=test01, userName=mmb02, userPassword=null, gender=2, birthday=1991-12-29, phone=0000, address=null, userRole=null, createdBy=null, creationDate=null, modifyBy=null, modifyDate=null, roleNameAdministrator]]third:18/01/18 17:35:17 INFO Controller.UserController: loop:User [id=null, userCode=test01, userName=mmb02, userPassword=null, gender=2, birthday=1991-12-29, phone=0000, address=null, userRole=null, createdBy=null, creationDate=null, modifyBy=null, modifyDate=null, roleNameAdministrator]18/01/18 17:35:17 INFO Controller.UserController: inside--result:[User [id=null, userCode=test01, userName=mmb02, userPassword=null, gender=2, birthday=1991-12-29, phone=0000, address=null, userRole=null, createdBy=null, creationDate=null, modifyBy=null, modifyDate=null, roleNameAdministrator], User [id=null, userCode=test01, userName=mmb02, userPassword=null, gender=2, birthday=1991-12-29, phone=0000, address=null, userRole=null, createdBy=null, creationDate=null, modifyBy=null, modifyDate=null, roleNameAdministrator], User [id=null, userCode=test01, userName=mmb02, userPassword=null, gender=2, birthday=1991-12-29, phone=0000, address=null, userRole=null, createdBy=null, creationDate=null, modifyBy=null, modifyDate=null, roleNameAdministrator]]forth:18/01/18 17:35:17 INFO Controller.UserController: loop:User [id=null, userCode=test01, userName=mmb08, userPassword=null, gender=2, birthday=1991-12-29, phone=0000, address=null, userRole=null, createdBy=null, creationDate=null, modifyBy=null, modifyDate=null, roleNameAdministrator]18/01/18 17:35:17 INFO Controller.UserController: inside--result:[User [id=null, userCode=test01, userName=mmb08, userPassword=null, gender=2, birthday=1991-12-29, phone=0000, address=null, userRole=null, createdBy=null, creationDate=null, modifyBy=null, modifyDate=null, roleNameAdministrator], User [id=null, userCode=test01, userName=mmb08, userPassword=null, gender=2, birthday=1991-12-29, phone=0000, address=null, userRole=null, createdBy=null, creationDate=null, modifyBy=null, modifyDate=null, roleNameAdministrator], User [id=null, userCode=test01, userName=mmb08, userPassword=null, gender=2, birthday=1991-12-29, phone=0000, address=null, userRole=null, createdBy=null, creationDate=null, modifyBy=null, modifyDate=null, roleNameAdministrator], User [id=null, userCode=test01, userName=mmb08, userPassword=null, gender=2, birthday=1991-12-29, phone=0000, address=null, userRole=null, createdBy=null, creationDate=null, modifyBy=null, modifyDate=null, roleNameAdministrator]]firth:18/01/18 17:35:17 INFO Controller.UserController: loop:User [id=null, userCode=test02, userName=yuanshikai, userPassword=null, gender=1, birthday=2018-01-09, phone=1212, address=null, userRole=null, createdBy=null, creationDate=null, modifyBy=null, modifyDate=null, roleNametour]18/01/18 17:35:17 INFO Controller.UserController: inside--result:[User [id=null, userCode=test02, userName=yuanshikai, userPassword=null, gender=1, birthday=2018-01-09, phone=1212, address=null, userRole=null, createdBy=null, creationDate=null, modifyBy=null, modifyDate=null, roleNametour], User [id=null, userCode=test02, userName=yuanshikai, userPassword=null, gender=1, birthday=2018-01-09, phone=1212, address=null, userRole=null, createdBy=null, creationDate=null, modifyBy=null, modifyDate=null, roleNametour],User [id=null, userCode=test02, userName=yuanshikai, userPassword=null, gender=1, birthday=2018-01-09, phone=1212, address=null, userRole=null, createdBy=null, creationDate=null, modifyBy=null, modifyDate=null, roleNametour], User [id=null, userCode=test02, userName=yuanshikai, userPassword=null, gender=1, birthday=2018-01-09, phone=1212, address=null, userRole=null, createdBy=null, creationDate=null, modifyBy=null, modifyDate=null, roleNametour], User [id=null, userCode=test02, userName=yuanshikai, userPassword=null, gender=1, birthday=2018-01-09, phone=1212, address=null, userRole=null, createdBy=null, creationDate=null, modifyBy=null, modifyDate=null, roleNametour]]sixth:18/01/18 17:35:17 INFO Controller.UserController: loop:User [id=null, userCode=test03, userName=mmb03, userPassword=null, gender=1, birthday=2018-01-16, phone=21312, address=null, userRole=null, createdBy=null, creationDate=null, modifyBy=null, modifyDate=null, roleNameManager]18/01/18 17:35:17 INFO Controller.UserController: inside--result:[User [id=null, userCode=test03, userName=mmb03, userPassword=null, gender=1, birthday=2018-01-16, phone=21312, address=null, userRole=null, createdBy=null, creationDate=null, modifyBy=null, modifyDate=null, roleNameManager], User [id=null, userCode=test03, userName=mmb03, userPassword=null, gender=1, birthday=2018-01-16, phone=21312, address=null, userRole=null, createdBy=null, creationDate=null, modifyBy=null, modifyDate=null, roleNameManager], User [id=null, userCode=test03, userName=mmb03, userPassword=null, gender=1, birthday=2018-01-16, phone=21312, address=null, userRole=null, createdBy=null, creationDate=null, modifyBy=null, modifyDate=null, roleNameManager], User [id=null, userCode=test03, userName=mmb03, userPassword=null, gender=1, birthday=2018-01-16, phone=21312, address=null, userRole=null, createdBy=null, creationDate=null, modifyBy=null, modifyDate=null, roleNameManager], User [id=null, userCode=test03, userName=mmb03, userPassword=null, gender=1, birthday=2018-01-16, phone=21312, address=null, userRole=null, createdBy=null, creationDate=null, modifyBy=null, modifyDate=null, roleNameManager], User [id=null, userCode=test03, userName=mmb03, userPassword=null, gender=1, birthday=2018-01-16, phone=21312, address=null, userRole=null, createdBy=null, creationDate=null, modifyBy=null, modifyDate=null, roleNameManager]]seventh:18/01/18 17:35:17 INFO Controller.UserController: loop:User [id=null, userCode=test05, userName=mmb05, userPassword=null, gender=2, birthday=2018-01-16, phone=5534, address=null, userRole=null, createdBy=null, creationDate=null, modifyBy=null, modifyDate=null, roleNameOrdinaryUser]18/01/18 17:35:17 INFO Controller.UserController: inside--result:[User [id=null, userCode=test05, userName=mmb05, userPassword=null, gender=2, birthday=2018-01-16, phone=5534, address=null, userRole=null, createdBy=null, creationDate=null, modifyBy=null, modifyDate=null, roleNameOrdinaryUser], User [id=null, userCode=test05, userName=mmb05, userPassword=null, gender=2, birthday=2018-01-16, phone=5534, address=null, userRole=null, createdBy=null, creationDate=null, modifyBy=null, modifyDate=null, roleNameOrdinaryUser], User [id=null, userCode=test05, userName=mmb05, userPassword=null, gender=2, birthday=2018-01-16, phone=5534, address=null, userRole=null, createdBy=null, creationDate=null, modifyBy=null, modifyDate=null, roleNameOrdinaryUser], User [id=null, userCode=test05, userName=mmb05, userPassword=null, gender=2, birthday=2018-01-16, phone=5534, address=null, userRole=null, createdBy=null, creationDate=null, modifyBy=null, modifyDate=null, roleNameOrdinaryUser], User [id=null, userCode=test05, userName=mmb05, userPassword=null, gender=2, birthday=2018-01-16, phone=5534, address=null, userRole=null, createdBy=null, creationDate=null, modifyBy=null, modifyDate=null, roleNameOrdinaryUser], User [id=null, userCode=test05, userName=mmb05, userPassword=null, gender=2, birthday=2018-01-16, phone=5534, address=null, userRole=null, createdBy=null, creationDate=null, modifyBy=null, modifyDate=null, roleNameOrdinaryUser], User [id=null, userCode=test05, userName=mmb05, userPassword=null, gender=2, birthday=2018-01-16, phone=5534, address=null, userRole=null, createdBy=null, creationDate=null, modifyBy=null, modifyDate=null, roleNameOrdinaryUser]]---------------------------------------------18/01/18 17:35:17 INFO Controller.UserController: outside--result:[
得出结论:(1)从DB获取新的记录后,记录被赋值于user;
(2)userList 添加 user元素;
(3)最后userList的所以元素全部指向user,而user的最终被赋值为最后一条记录,因此解释了为什么全部元素相同了。
三、修改Bug
每次获取元素,重新申请一个user对象的内存,这样所有的数据都被分配到内存地址不一样的空间中,避免新值覆盖旧值。如下:
User user = new User();//很重要,开辟一个新的空间存储新的值!!
四、正确结果:
五、小例子:
eg:
package test01;import java.util.ArrayList;import java.util.List;class a{ private Integer value; public Integer getValue() { return value; } public void setValue(Integer value) { this.value = value; } }public class test6 { public static void main(String [] args){ List arrayList = new ArrayList(); Integer b = 0; a a = new a(); while(b<14){ arrayList.add(a); b++; a.setValue(b); } System.out.println(arrayList.toString());
输出结果,里面的元素全部都是 test01.a@15db9742,即都是指向相同的内存地址:
[test01.a@15db9742, test01.a@15db9742, test01.a@15db9742, test01.a@15db9742, test01.a@15db9742, test01.a@15db9742, test01.a@15db9742, test01.a@15db9742, test01.a@15db9742, test01.a@15db9742, test01.a@15db9742, test01.a@15db9742, test01.a@15db9742, test01.a@15db9742]
版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。
发表评论
暂时没有评论,来抢沙发吧~