POst和User的代码操作

Post的操作:岗位:持久化对象,如果要修改也是需要放在事务中,否则也不会进行更新;
---
用户是一对多,多对多的集合;
三张表;
如果是多张表,解决方案是:使用VO,BO,展示信息需要转化为界面的对象,Add的时候将对象转化一下,这样就需要比较多的转化;
--
另一种解决方案是将数据直接进行展示;
--
如果是另有一个对象进行显示,就是在转化的时候将懒加载的操作全部做完成;因为没有将数据拿出来就不能转化为这个对象;
在Hibernate操作的时候,就需要有懒加载,这里是User和Post多对一 ,User和Department是多对多,就可能会引起异常;三张表的迫切左外连接,就可以出来了;
OpenSessionInView在ajax中是不起作用的;
但是如果使用迫切左外链接,也要分情况,这时候如果表中字段太多,界面字段过少,就会浪费资源;
企业的做法是:VO和BO企业中很少用了,转化太麻烦了;一般还是使用左外连接,或者将User拿出来,在页面中在将其拿出来,OpenSessionInView;
---
查询:
1、现象
在页面上存在多个字段,来自于多个表
2、解决方案
1、建立一个javabean,该javabean和页面上的字段关联
public class UserView{
private String username;
private String dname;
private Set<Post> posts;
}
优点:
类的组织一目了然
不存在懒加载的问题
缺点:
该结构和dao层的结构不一致,在后台需要进行转化
2、从页面到action-->service--->dao,整个的数据的传递采用hibernate的类型结构
优点:
传递比较简单,不用进行转化
缺点:
有可能出现懒加载的异常
----
直接使用from User,可能出现懒加载异常;
迫切左外链接;from User u left join fetch u.department d left join fetch u.posts p
这里去重的方法:List<User> userList = this.hibernateTemplate.find("from User u left join fetch u.department d left join fetch u.posts p ");
return new HashSet<User>(userList);
这样就可以去重;
hibernateTemplate需要改为public;

删除就不需要考虑太多,但是部门那边的就需要注意删除的时候;

增加的时候:
增加:
如果页面上的数据来自于很多张表,后台要获取所有的数据,可以采用模型驱动结合属性驱动的方式解决
public class UserAction extends BaseAction<User>{

private Long did;

private Long[] pids;

结合模型驱动和属性驱动去做;
public String add() throws Exception{
/**
* 1、保存用户
* 2、建立用户和岗位之间的关系
* 3、建立用户和部门之间的关系
*/
User user = new User();
/**
* 一般属性
*/
BeanUtils.copyProperties(this.getModel(), user);
//建立用户和部门之间的关系
Department department = this.departmentService.getDepartmentByID(this.did);
//Department department = new Department();
department.setDid(this.did);
user.setDepartment(department);
//建立用户和岗位之间的关系
Set<Post> posts = this.postService.getPostsByIds(this.pids);
user.setPosts(posts);
this.userService.saveUser(user);
return action2action;
}


@Override
public Collection<User> getAllUser() {
// TODO Auto-generated method stub
List<User> userList = this.hibernateTemplate.find("from User u left join fetch u.department d left join fetch u.posts p");
return new HashSet<User>(userList);
}

@Override
public Set<Post> getPostsByIds(Long[] ids) {
//ids = [1,2,3] in (1,2,3)
StringBuffer buffer = new StringBuffer();
buffer.append("from Post where pid in(");
for(int i=0;i<ids.length;i++){
if(i==ids.length-1){
buffer.append(ids[i]+")");
}else{
buffer.append(ids[i]+",");
}
}
return new HashSet<Post>(this.hibernateTemplate.find(buffer.toString()));
}

修改的操作:
public String updateUI() throws Exception{
/**
* 回显
* 1、一般属性
* 2、部门
* 3、岗位
* 部门的数据全部列出来
* 岗位的数据全部列出来
*/
User user = this.userService.getUserById(this.getModel().getUid());
//用于一般属性的回显
ActionContext.getContext().getValueStack().push(user);
//要回显岗位信息和部门信息,必须把did和pids放入到对象栈中
//针对department的加载是懒加载,但是OpenSessionInView模式,所以这个时候session并没有关闭
this.did = user.getDepartment().getDid();
Set<Post> posts = user.getPosts();
this.pids = new Long[posts.size()];
int index = 0;
for(Post post:posts){
pids[index] = post.getPid();
index++;
}

Collection<Post> postList = this.postService.getAllPost();
Collection<Department> dList = this.departmentService.getAllDepartment();
ActionContext.getContext().put("pList", postList);
ActionContext.getContext().put("dList", dList);
return updateUI;
}

public String update() throws Exception{
/**
* 修改用户的一般信息
* 重新建立用户和部门之间的关系
* 重新建立用户和岗位之间的关系
*/
User user = this.userService.getUserById(this.getModel().getUid());
//设置用户的一般信息
BeanUtils.copyProperties(this.getModel(), user);
//建立用户和部门之间的关系
Department department = this.departmentService.getDepartmentByID(this.did);
user.setDepartment(department);
//建立用户和岗位之间的关系
Set<Post> posts = this.postService.getPostsByIds(this.pids);
user.setPosts(posts);

this.userService.updateUser(user);
return action2action;
}

posted @ 2014-02-28 13:10  教程学习  阅读(464)  评论(0)    收藏  举报