SSH dept-emp (one-to-many) SSH一对多

package com.action;

import java.util.List;

import com.dao.BaseDao;
import com.pojo.Emp;


public class AllAction {

	List list;
	BaseDao dao;
	Emp e;
	
	
	public Emp getE() {
		return e;
	}



	public void setE(Emp e) {
		this.e = e;
	}


	public List getList() {
		return list;
	}

	public void setList(List list) {
		this.list = list;
	}

	public BaseDao getDao() {
		return dao;
	}

	public void setDao(BaseDao dao) {
		this.dao = dao;
	}

	public String findAll(){
		
	list=dao.findAll("from Emp");
	return "findAll";
	}
	public String finddept(){
		
		list=dao.findAll("from Dept");
		return "finddept";
	}
	
	public String insert(){
		
		dao.save(e);
		return "insert";
		
	}
	
	public String delete(){
		
		Emp emp=(Emp) dao.findById(Emp.class, e.getEmpno());
		dao.delete(emp);
		return "delete";
		
	}
	
	public String toupdate(){
		e=(Emp)dao.findById(Emp.class, e.getEmpno());
		//System.out.println(e.getEname());
		list=dao.findAll("from Dept");
		return "toupdate";
		
	}
	public String update(){
		
		
		dao.update(e);
		return "update";
		
	}
}

 

package com.action;

import java.util.List;

import com.dao.BaseDao;
import com.pojo.Dept;
import com.pojo.Emp;

public class MyAction {
	
	List list;
	BaseDao dao;
	
	Dept d;
	String[] ename;
	String[] job;
	Double[] sal;
	public Dept getD() {
		return d;
	}

	public void setD(Dept d) {
		this.d = d;
	}

	public BaseDao getDao() {
		return dao;
	}

	public void setDao(BaseDao dao) {
		this.dao = dao;
	}

	public List getList() {
		return list;
	}

	public void setList(List list) {
		this.list = list;
	}
	
	public String findDept(){
		
		list=dao.findAll("from Dept");
		
		return "findDept";
	}
	
	public String delete(){
		
		Dept dept=(Dept) dao.findById(Dept.class, d.getDeptno());
		dao.delete(dept);
		
		return "delete";
		
	}
	
	public String insert(){
		
		if(ename!=null){
			
			for (int i = 0; i < ename.length; i++) {
				
				Emp emp=new Emp();
				emp.setEname(ename[i]);
				emp.setJob(job[i]);
				emp.setSal(sal[i]);
				emp.setDept(d);
				d.getEmps().add(emp);
			}
		}
		dao.save(d);
		return "insert";
	}
}

package com.dao;

import java.util.List;

public interface BaseDao {
	
	List findAll(String hql);
	
	void save(Object obj);
	
	void delete(Object obj);
	
	void update(Object obj);
	
	Object findById(Class class1,Long id);
}

package com.dao;

import java.util.List;

import org.springframework.beans.factory.BeanFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import com.pojo.Emp;

public class BaseDaoImpl extends HibernateDaoSupport implements BaseDao{

	/**
	 * @param args
	 */

	public void delete(Object obj) {
		
		getHibernateTemplate().delete(obj);
		
	}

	public List findAll(String hql) {
		
		return this.getHibernateTemplate().find(hql);
	}

	public Object findById(Class class1, Long id) {
		
		return this.getHibernateTemplate().get(class1, id);
	}

	public void save(Object obj) {
		
		getHibernateTemplate().save(obj);
		
	}

	public void update(Object obj) {
		
		getHibernateTemplate().update(obj);
		
	}
	public static void main(String[] args) {
		
	ApplicationContext ac=new ClassPathXmlApplicationContext("applicationContext.xml");
		
		BaseDao bd=(BaseDao) ac.getBean("dao");
		List list=bd.findAll("from Emp");
		System.out.println(list.size());
	}
}

package com.pojo;

import java.util.HashSet;
import java.util.Set;

/**
 * Dept entity.
 * 
 * @author MyEclipse Persistence Tools
 */

public class Dept implements java.io.Serializable {

	// Fields

	private Long deptno;
	private String dname;
	private String loc;
	private Set<Emp> emps = new HashSet<Emp>(0);

	// Constructors

	/** default constructor */
	public Dept() {
	}

	/** minimal constructor */
	public Dept(Long deptno) {
		this.deptno = deptno;
	}

	/** full constructor */
	public Dept(Long deptno, String dname, String loc, Set<Emp> emps) {
		this.deptno = deptno;
		this.dname = dname;
		this.loc = loc;
		this.emps = emps;
	}

	// Property accessors

	public Long getDeptno() {
		return this.deptno;
	}

	public void setDeptno(Long deptno) {
		this.deptno = deptno;
	}

	public String getDname() {
		return this.dname;
	}

	public void setDname(String dname) {
		this.dname = dname;
	}

	public String getLoc() {
		return this.loc;
	}

	public void setLoc(String loc) {
		this.loc = loc;
	}

	public Set<Emp> getEmps() {
		return this.emps;
	}

	public void setEmps(Set<Emp> emps) {
		this.emps = emps;
	}

	

}

package com.pojo;

import java.util.Date;

/**
 * Emp entity.
 * 
 * @author MyEclipse Persistence Tools
 */

public class Emp implements java.io.Serializable {

	// Fields

	private Long empno;
	private Dept dept;
	private String ename;
	private String job;
	private Long mgr;
	private Date hiredate;
	private Double sal;
	private Double comm;

	// Constructors

	/** default constructor */
	public Emp() {
	}

	/** minimal constructor */
	public Emp(Long empno) {
		this.empno = empno;
	}

	/** full constructor */
	public Emp(Long empno, Dept dept, String ename, String job, Long mgr,
			Date hiredate, Double sal, Double comm) {
		this.empno = empno;
		this.dept = dept;
		this.ename = ename;
		this.job = job;
		this.mgr = mgr;
		this.hiredate = hiredate;
		this.sal = sal;
		this.comm = comm;
	}

	// Property accessors

	public Long getEmpno() {
		return this.empno;
	}

	public void setEmpno(Long empno) {
		this.empno = empno;
	}

	public Dept getDept() {
		return this.dept;
	}

	public void setDept(Dept dept) {
		this.dept = dept;
	}

	public String getEname() {
		return this.ename;
	}

	public void setEname(String ename) {
		this.ename = ename;
	}

	public String getJob() {
		return this.job;
	}

	public void setJob(String job) {
		this.job = job;
	}

	public Long getMgr() {
		return this.mgr;
	}

	public void setMgr(Long mgr) {
		this.mgr = mgr;
	}

	public Date getHiredate() {
		return this.hiredate;
	}

	public void setHiredate(Date hiredate) {
		this.hiredate = hiredate;
	}

	public Double getSal() {
		return this.sal;
	}

	public void setSal(Double sal) {
		this.sal = sal;
	}

	public Double getComm() {
		return this.comm;
	}

	public void setComm(Double comm) {
		this.comm = comm;
	}

}



你可能感兴趣的:(java,ssh,one-to-many,SSH一对多)