首先,请允许我,先简单地把自己在项目中遇到的问题简要的描述一下。
比如,有个班级页面更新页面,允许在页面中添加学生信息(暂且不考虑实际业务)。问题产生了。 有如下问题:
1.在更新页面,应该初始化已经保存的学生列表信息(PO)已持久化到数据库中
2.可以对已保存的操作进行删除、修改操作(PO)
3.可以添加新的学生信息(VO),没有保存到数据库中。
当然,可以使用标示位 进行标记。在后台根据表示为进行不同操作,这是个不错的方案。但是我现在要说的事另外一个解决方案。这个方案,有个小问题存在,就是对于误删的学生信息,不能重新回退,客户体验不好。这是业务上的一个问题,暂且忽略吧,重点从技术的角度说一说。理解难免出现幼稚情况,希望高手批评指正。
下面,我用java 程序简单的模拟了一下这个情景。
第一步建立bean. 这时候要重写equals 和hashcode 方法。 最好也重写一下toString() 方法。
还要有id 字段,对应数据库中的主键。
package com.randy.list;
public class User {
private int id;
private String username;
private String password;
public int getId() {
return id;
}
public void setId( int id) {
this.id = id;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result
+ ((password == null) ? 0 : password.hashCode());
result = prime * result
+ ((username == null) ? 0 : username.hashCode());
return result;
}
@Override
public boolean equals(Object obj) {
if ( this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
User other = (User) obj;
if (password == null) {
if (other.password != null)
return false;
} else if (!password.equals(other.password))
return false;
if (username == null) {
if (other.username != null)
return false;
} else if (!username.equals(other.username))
return false;
return true;
}
@Override
public String toString() {
StringBuffer sb = new StringBuffer();
sb.append( "[")
.append( "id=").append( this.id).append( ",")
.append( "username=").append( this.username).append( ",")
.append( "password=").append( this.password).append( "]");
return sb.toString();
}
}
public class User {
private int id;
private String username;
private String password;
public int getId() {
return id;
}
public void setId( int id) {
this.id = id;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result
+ ((password == null) ? 0 : password.hashCode());
result = prime * result
+ ((username == null) ? 0 : username.hashCode());
return result;
}
@Override
public boolean equals(Object obj) {
if ( this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
User other = (User) obj;
if (password == null) {
if (other.password != null)
return false;
} else if (!password.equals(other.password))
return false;
if (username == null) {
if (other.username != null)
return false;
} else if (!username.equals(other.username))
return false;
return true;
}
@Override
public String toString() {
StringBuffer sb = new StringBuffer();
sb.append( "[")
.append( "id=").append( this.id).append( ",")
.append( "username=").append( this.username).append( ",")
.append( "password=").append( this.password).append( "]");
return sb.toString();
}
}
下面是测试业务代码
package com.randy.list;
import java.util.ArrayList;
import java.util.List;
public class CompareList {
public static List<User> initlistPO(){
List<User> polist = new ArrayList<User>();
User user1 = new User();
user1.setUsername( "杨过");
user1.setPassword( "yangguo");
user1.setId(100); //模拟从持久层存在的数据,所以存在ID
polist.add(user1);
User user2 = new User();
user2.setUsername( "小龙女");
user2.setPassword( "longnv");
user2.setId(120); //模拟从持久层存在的数据,所以存在ID
polist.add(user2);
User user3 = new User();
user3.setUsername( "李莫愁");
user3.setPassword( "mochou");
user3.setId(110); //模拟从持久层存在的数据,所以存在ID
polist.add(user3);
User user4 = new User();
user4.setUsername( "張無忌");
user4.setPassword( "wuji");
user4.setId(129); //模拟从持久层存在的数据,所以存在ID
polist.add(user4);
return polist;
}
public static List<User> initlistVO(){
// add
List<User> volist = new ArrayList<User>();
User user1 = new User();
user1.setUsername( "黄蓉");
user1.setPassword( "huangrong");
user1.setId(0); //
volist.add(user1);
// update
User user2 = new User();
user2.setUsername( "龙女");
user2.setPassword( "longnv");
user2.setId(120); //更新字段
volist.add(user2);
//no operation
User user3 = new User();
user3.setUsername( "李莫愁");
user3.setPassword( "mochou");
user3.setId(110); //模拟从持久层存在的数据,所以存在ID
volist.add(user3);
/* [至若]这样的对象应该不会存在的
User user4 = new User();
user4.setUsername("周芷若");
user4.setPassword("zhiruo");
user4.setId(112);//
volist.add(user4);*/
// delete yangguo
return volist;
}
public static void updateOrsave(){
List<User> polist = initlistPO();
List<User> volist = initlistVO();
List<User> pobacklist = new ArrayList<User>();
pobacklist.addAll(polist);
polist.removeAll(volist); // update and delete
volist.removeAll(pobacklist);
for(User out :volist){
if(out.getId()==0){
System.out.println( "insert"+out);
} else{
for(User inner:polist){
if(out.getId() == inner.getId()){
System.out.println( "update"+out);
} else{
System.out.println( "delete"+inner);
}
}
}
}
}
public static void printList(List<User> list){
for(User user:list){
System.out.println(user);
}
}
public static void main(String args[]){
updateOrsave();
}
}
import java.util.ArrayList;
import java.util.List;
public class CompareList {
public static List<User> initlistPO(){
List<User> polist = new ArrayList<User>();
User user1 = new User();
user1.setUsername( "杨过");
user1.setPassword( "yangguo");
user1.setId(100); //模拟从持久层存在的数据,所以存在ID
polist.add(user1);
User user2 = new User();
user2.setUsername( "小龙女");
user2.setPassword( "longnv");
user2.setId(120); //模拟从持久层存在的数据,所以存在ID
polist.add(user2);
User user3 = new User();
user3.setUsername( "李莫愁");
user3.setPassword( "mochou");
user3.setId(110); //模拟从持久层存在的数据,所以存在ID
polist.add(user3);
User user4 = new User();
user4.setUsername( "張無忌");
user4.setPassword( "wuji");
user4.setId(129); //模拟从持久层存在的数据,所以存在ID
polist.add(user4);
return polist;
}
public static List<User> initlistVO(){
// add
List<User> volist = new ArrayList<User>();
User user1 = new User();
user1.setUsername( "黄蓉");
user1.setPassword( "huangrong");
user1.setId(0); //
volist.add(user1);
// update
User user2 = new User();
user2.setUsername( "龙女");
user2.setPassword( "longnv");
user2.setId(120); //更新字段
volist.add(user2);
//no operation
User user3 = new User();
user3.setUsername( "李莫愁");
user3.setPassword( "mochou");
user3.setId(110); //模拟从持久层存在的数据,所以存在ID
volist.add(user3);
/* [至若]这样的对象应该不会存在的
User user4 = new User();
user4.setUsername("周芷若");
user4.setPassword("zhiruo");
user4.setId(112);//
volist.add(user4);*/
// delete yangguo
return volist;
}
public static void updateOrsave(){
List<User> polist = initlistPO();
List<User> volist = initlistVO();
List<User> pobacklist = new ArrayList<User>();
pobacklist.addAll(polist);
polist.removeAll(volist); // update and delete
volist.removeAll(pobacklist);
for(User out :volist){
if(out.getId()==0){
System.out.println( "insert"+out);
} else{
for(User inner:polist){
if(out.getId() == inner.getId()){
System.out.println( "update"+out);
} else{
System.out.println( "delete"+inner);
}
}
}
}
}
public static void printList(List<User> list){
for(User user:list){
System.out.println(user);
}
}
public static void main(String args[]){
updateOrsave();
}
}
有4个方法:
initlistPO() 方法是初始化 持久层对象列表。在实际业务中相当于从数据库读取的记录,在WEB页面上,表示为初始化的“学生”列表信息。
inintlistVO() 方法 初始化VO对象列表,在实际业务中相当于,提交的记录。有的记录在数据库中存在(在VO列表中有对应的记录),有的记录不存在(id=0)。只有这两种状态的记录。删除的记录,不进行提交。
另外是一个主要方法 saveOrupdate() 方法,主要作用,根据比对两个列表的内容,绝对对象的增删改操作。
请看具体代码:
package com.randy.list;
import java.util.ArrayList;
import java.util.List;
public class CompareList {
public static List<User> initlistPO(){
List<User> polist = new ArrayList<User>();
User user1 = new User();
user1.setUsername( "杨过");
user1.setPassword( "yangguo");
user1.setId(100); //模拟从持久层存在的数据,所以存在ID
polist.add(user1);
User user2 = new User();
user2.setUsername( "小龙女");
user2.setPassword( "longnv");
user2.setId(120); //模拟从持久层存在的数据,所以存在ID
polist.add(user2);
User user3 = new User();
user3.setUsername( "李莫愁");
user3.setPassword( "mochou");
user3.setId(110); //模拟从持久层存在的数据,所以存在ID
polist.add(user3);
User user4 = new User();
user4.setUsername( "張無忌");
user4.setPassword( "wuji");
user4.setId(129); //模拟从持久层存在的数据,所以存在ID
polist.add(user4);
return polist;
}
public static List<User> initlistVO(){
// add
List<User> volist = new ArrayList<User>();
User user1 = new User();
user1.setUsername( "黄蓉");
user1.setPassword( "huangrong");
user1.setId(0); //
volist.add(user1);
// update
User user2 = new User();
user2.setUsername( "龙女");
user2.setPassword( "longnv");
user2.setId(120); //更新字段
volist.add(user2);
//no operation
User user3 = new User();
user3.setUsername( "李莫愁");
user3.setPassword( "mochou");
user3.setId(110); //模拟从持久层存在的数据,所以存在ID
volist.add(user3);
/* [至若]这样的对象应该不会存在的
User user4 = new User();
user4.setUsername("周芷若");
user4.setPassword("zhiruo");
user4.setId(112);//
volist.add(user4);*/
// delete yangguo
return volist;
}
public static void updateOrsave(){
List<User> polist = initlistPO();
List<User> volist = initlistVO();
List<User> pobacklist = new ArrayList<User>();
pobacklist.addAll(polist);
polist.removeAll(volist); // update and delete
volist.removeAll(pobacklist);
for(User out :volist){
if(out.getId()==0){
System.out.println( "insert"+out);
} else{
for(User inner:polist){
if(out.getId() == inner.getId()){
System.out.println( "update"+out);
} else{
System.out.println( "delete"+inner);
}
}
}
}
}
public static void printList(List<User> list){
for(User user:list){
System.out.println(user);
}
}
public static void main(String args[]){
updateOrsave();
}
}
import java.util.ArrayList;
import java.util.List;
public class CompareList {
public static List<User> initlistPO(){
List<User> polist = new ArrayList<User>();
User user1 = new User();
user1.setUsername( "杨过");
user1.setPassword( "yangguo");
user1.setId(100); //模拟从持久层存在的数据,所以存在ID
polist.add(user1);
User user2 = new User();
user2.setUsername( "小龙女");
user2.setPassword( "longnv");
user2.setId(120); //模拟从持久层存在的数据,所以存在ID
polist.add(user2);
User user3 = new User();
user3.setUsername( "李莫愁");
user3.setPassword( "mochou");
user3.setId(110); //模拟从持久层存在的数据,所以存在ID
polist.add(user3);
User user4 = new User();
user4.setUsername( "張無忌");
user4.setPassword( "wuji");
user4.setId(129); //模拟从持久层存在的数据,所以存在ID
polist.add(user4);
return polist;
}
public static List<User> initlistVO(){
// add
List<User> volist = new ArrayList<User>();
User user1 = new User();
user1.setUsername( "黄蓉");
user1.setPassword( "huangrong");
user1.setId(0); //
volist.add(user1);
// update
User user2 = new User();
user2.setUsername( "龙女");
user2.setPassword( "longnv");
user2.setId(120); //更新字段
volist.add(user2);
//no operation
User user3 = new User();
user3.setUsername( "李莫愁");
user3.setPassword( "mochou");
user3.setId(110); //模拟从持久层存在的数据,所以存在ID
volist.add(user3);
/* [至若]这样的对象应该不会存在的
User user4 = new User();
user4.setUsername("周芷若");
user4.setPassword("zhiruo");
user4.setId(112);//
volist.add(user4);*/
// delete yangguo
return volist;
}
public static void updateOrsave(){
List<User> polist = initlistPO();
List<User> volist = initlistVO();
List<User> pobacklist = new ArrayList<User>();
pobacklist.addAll(polist);
polist.removeAll(volist); // update and delete
volist.removeAll(pobacklist);
for(User out :volist){
if(out.getId()==0){
System.out.println( "insert"+out);
} else{
for(User inner:polist){
if(out.getId() == inner.getId()){
System.out.println( "update"+out);
} else{
System.out.println( "delete"+inner);
}
}
}
}
}
public static void printList(List<User> list){
for(User user:list){
System.out.println(user);
}
}
public static void main(String args[]){
updateOrsave();
}
}
运行结果:
insert[id=0,username=黄蓉,password=huangrong]
delete[id=100,username=杨过,password=yangguo]
update[id=120,username=龙女,password=longnv]
delete[id=129,username=張無忌,password=wuji]
delete[id=100,username=杨过,password=yangguo]
update[id=120,username=龙女,password=longnv]
delete[id=129,username=張無忌,password=wuji]
可能有人会问,为什么不用Set,没办法,这是我实际项目中用到的都是List
实际上业务逻辑很简单,只是既然自己花时间研究了一下,就把心得整理出来,记录下自己的心路过程。
本文转自 randy_shandong 51CTO博客,原文链接:http://blog.51cto.com/dba10g/286779,如需转载请自行联系原作者