大作业 员工管理系统CRUP

通过hibernate实现员工管理系统的CRUP。

对应关系

部门管理(Department):一个部门对应多个员工(one-to-many)

员工管理(Employee):多个员工对应一个部门(many-to-one)


实体类:

Department

public class Department {
	/**
	 * 部门编号
	 */
	private Integer did;
	/**
	 * 部门名称
	 */
	private String dname;
	/**
	 * 部门描述
	 */
	private String ddesc;

	/**
	 * 部门的员工集合
	 */
	private Set<Employee> emplyoees = new HashSet<Employee>();

	public Integer getDid() {
		return did;
	}

	public void setDid(Integer did) {
		this.did = did;
	}

	public String getDname() {
		return dname;
	}

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

	public String getDdesc() {
		return ddesc;
	}

	public void setDdesc(String ddesc) {
		this.ddesc = ddesc;
	}

	public Set<Employee> getEmplyoees() {
		return emplyoees;
	}

	public void setEmplyoees(Set<Employee> emplyoees) {
		this.emplyoees = emplyoees;
	}

	@Override
	public String toString() {
		return "Department [did=" + did + ", dname=" + dname + ", ddesc="
				+ ddesc + ", emplyoees=" + emplyoees + "]";
	}

}

Employe

public class Employee {
	/**
	 * 员工数据库编号
	 */
	private Integer eid;
	/**
	 * 员工姓名
	 */
	private String ename;
	/**
	 * 员工性别
	 */
	private String sex;
	/**
	 * 员工生日
	 */
	private Date birthday;
	/**
	 * 员工入职日期
	 */
	private Date joinDate;
	/**
	 * 员工编号
	 */
	private String eno;
	/**
	 * 员工登录名
	 */
	private String username;
	/**
	 * 员工登录密码
	 */
	private String password;
	/**
	 * 所属的部门
	 */
	private Department department;

	public Integer getEid() {
		return eid;
	}

	public void setEid(Integer eid) {
		this.eid = eid;
	}

	public String getEname() {
		return ename;
	}

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

	public String getSex() {
		return sex;
	}

	public void setSex(String sex) {
		this.sex = sex;
	}

	public Date getBirthday() {
		return birthday;
	}

	public void setBirthday(Date birthday) {
		this.birthday = birthday;
	}

	public Date getJoinDate() {
		return joinDate;
	}

	public void setJoinDate(Date joinDate) {
		this.joinDate = joinDate;
	}

	public String getEno() {
		return eno;
	}

	public void setEno(String eno) {
		this.eno = eno;
	}

	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;
	}

	public Department getDepartment() {
		return department;
	}

	public void setDepartment(Department department) {
		this.department = department;
	}

配置文件

Department.hbm.xml

<hibernate-mapping>
	<class name="com.charspan.employee.domain.Department" table="department">
		<id name="did" column="did">
			<!-- 主键的生成策略 -->
			<generator class="native" />
		</id>
		<property name="dname" column="dname" length="20" />
		<property name="ddesc" column="ddesc" length="100" />
		<!-- 映射关系 -->
		<set name="emplyoees" cascade="delete" inverse="true">
			<key column="dno" />
			<one-to-many class="com.charspan.employee.domain.Employee" />
		</set>
	</class>
</hibernate-mapping>

Employee.hbm.xml

<hibernate-mapping>
	<class name="com.charspan.employee.domain.Employee" table="employee">
		<id name="eid" column="eid">
			<!-- 主键的生成策略 -->
			<generator class="native" />
		</id>
		<property name="ename" column="ename" length="20" />
		<property name="sex" column="sex" length="10" />
		<property name="birthday" column="birthday" />
		<property name="joinDate" column="joinDate" />
		<property name="eno" column="eno" length="20" />
		<property name="username" column="username" length="20" />
		<property name="password" column="password" length="20" />
		<!-- 映射关系 -->
		<many-to-one name="department"
			class="com.charspan.employee.domain.Department" column="dno" />
	</class>
</hibernate-mapping>

DAO

DepartmentDao

public interface DepartmentDao {

	int findCount();

	List<Department> findByPage(int begin, int pageSize);

	void save(Department department);

	Department findById(Integer did);

	void update(Department department);

	void delete(Department department);

	List<Department> findAll();

}

EmployeeDao

public interface EmployeeDao {

	Employee findByUsernameAndPassword(Employee employee);

	int findCount();

	List<Employee> findByPage(int begin, int pageSize);

	void save(Employee employee);

	Employee findById(Integer eid);

	void update(Employee employee);

	void delete(Employee employee);

}

实现

EmployeeDaoImpl

public class EmployeeDaoImpl extends HibernateDaoSupport implements EmployeeDao {

	@Override
	public Employee findByUsernameAndPassword(Employee employee) {
		System.out.println(employee.getUsername() + "  "
				+ employee.getPassword());
		String hql = "from Employee where username = ? and password = ?";
		@SuppressWarnings("unchecked")
		List<Employee> list = this.getHibernateTemplate().find(hql,
				employee.getUsername(), employee.getPassword());
		if (list.size() > 0) {
			return list.get(0);
		}
		return null;
	}

	@SuppressWarnings("unchecked")
	@Override
	public int findCount() {
		String hql = "select count(*) from Employee";
		List<Long> list = this.getHibernateTemplate().find(hql);
		if (list.size() > 0) {
			return list.get(0).intValue();
		}
		return 0;
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<Employee> findByPage(int begin, int pageSize) {
		DetachedCriteria criteria = DetachedCriteria.forClass(Employee.class);
		List<Employee> list = this.getHibernateTemplate().findByCriteria(
				criteria, begin, pageSize);
		return list;
	}

	@Override
	public void save(Employee employee) {
		this.getHibernateTemplate().save(employee);
	}

	@Override
	public Employee findById(Integer eid) {
		return this.getHibernateTemplate().get(Employee.class, eid);
	}

	@Override
	public void update(Employee employee) {
		this.getHibernateTemplate().update(employee);
	}

	@Override
	public void delete(Employee employee) {
		this.getHibernateTemplate().delete(employee);
	}
	
}

DepartmentDaoImpl

public class DepartmentDaoImpl extends HibernateDaoSupport implements
		DepartmentDao {

	@SuppressWarnings("unchecked")
	@Override
	public int findCount() {
		String hql = "select count(*) from Department";
		List<Long> list = this.getHibernateTemplate().find(hql);
		if (list.size() > 0) {
			return list.get(0).intValue();
		}
		return 0;
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<Department> findByPage(int begin, int pageSize) {
		DetachedCriteria criteria = DetachedCriteria.forClass(Department.class);
		List<Department> list = this.getHibernateTemplate().findByCriteria(
				criteria, begin, pageSize);
		return list;
	}

	@Override
	public void save(Department department) {
		this.getHibernateTemplate().save(department);
	}

	@Override
	// 根据部门编号查询部门信息
	public Department findById(Integer did) {
		return this.getHibernateTemplate().get(Department.class, did);
	}

	@Override
	public void update(Department department) {
		this.getHibernateTemplate().update(department);
	}

	@Override
	public void delete(Department department) {
		this.getHibernateTemplate().delete(department);
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<Department> findAll() {
		return this.getHibernateTemplate().find("from Department");
	}

}

演示


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值