前言
前面我们已经学习了怎么整合SSH框架了。是时候拿一个小项目来练练手了….我们现在要设计一个企业人事管理系统…
需求:
- **要求对员工信息进行维护; **
- 后台系统先登陆,才能操作员工: 添加/修改/删除
- 没有登陆,只能查看列表,不能操作!
功能分类:
- 【管理员模块】
- 注册/登陆
- 【员工模块】
- 1) 添加一个员工, 指定添加的部门
- 2) 对指定的员工信息修改
- 3) 删除选择员工
- 4) 列表展示
数据库设计
- 管理员表: t_admin
- 员工表: t_user
- 部门: t_dept
搭建配置环境
关于搭建配置环境可参考上一篇博文:http://blog.csdn.net/hon_3y/article/details/72190638
编写JavaBean 和映射文件
JavaBean编写
Admin.java
public class Admin { 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; } }
User.java
public class User { private int id; private String username; private Dept dept; 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 Dept getDept() { return dept; } public void setDept(Dept dept) { this.dept = dept; } }
Dept.java
public class Dept { private int id; private String name; public int getId() { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } }
映射文件
Dept.hbm.xml
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd"> <hibernate-mapping package="zhongfucheng.entity"> <class name="Dept" table="t_dept"> <id name="id" > <generator class="native"></generator> </id> <property name="name" column="name"></property> </class> </hibernate-mapping>
User.hbm.xml
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd"> <hibernate-mapping package="zhongfucheng.entity"> <class name="User" table="t_user"> <id name="id" column="id"> <generator class="native"></generator> </id> <property name="username" column="userName"></property> <many-to-one name="dept" class="Dept" column="dept_id"/> </class> </hibernate-mapping>
Admin.hbm.xml
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd"> <hibernate-mapping package="zhongfucheng.entity"> <class name="Admin" table="t_admin"> <id name="id" column="id"> <generator class="native"></generator> </id> <property name="username" column="userName"></property> <property name="password" column="password"></property> </class> </hibernate-mapping>
编写Dao
编写BaseDao
为什么要写baseDao??可以参考我这篇博文:http://blog.csdn.net/hon_3y/article/details/70243918
package zhongfucheng.dao; import org.hibernate.SessionFactory; import org.springframework.beans.factory.annotation.Autowired; import java.lang.reflect.ParameterizedType; import java.util.List; /** * Created by ozc on 2017/5/16. */ public abstract class BaseDao<T> { // 容器注入sessionFactory @Autowired private SessionFactory sessionFactory; //子类的类型 private Class<T> clazz; //子类的名称 private String clazzName; public BaseDao(){ clazz = (Class<T>) this.getClass(); //拿到的是子类 ParameterizedType pt = (ParameterizedType) clazz.getGenericSuperclass(); //拿到子类的真实类型 clazz = (Class) pt.getActualTypeArguments()[0]; //拿到子类的名称【HQL都是通过类名来查询的】 clazzName = clazz.getSimpleName(); } public void add(T t){ sessionFactory.getCurrentSession().save(t); } public T find(String id){ return (T) sessionFactory.getCurrentSession().get(clazz, id); } public void update(T t){ sessionFactory.getCurrentSession().update(t); } public void delete(String id){ T t = (T) sessionFactory.getCurrentSession().get(clazz, id); sessionFactory.getCurrentSession().delete(t); } public List<T> getAll() { return sessionFactory.getCurrentSession().createQuery("from" + clazzName).list(); } }
编写UserDao
此时的UserDao已经有了baseDao的所有功能
/** * 1.添加员工--->add() * 2.修改员工--->find() * 3.删除员工--->delete() * 4.得到所有员工-->getAll() * 5.根据id得到员工-->find() * */ @Repository public class UserDao extends BaseDao<User>{ }
编写AdminDao
/** * * 1.保存管理员【注册】--->add() * 2.查找管理员【登陆】--->login() * * * */ @Repository public class AdminDao extends BaseDao<Admin> { @Autowired private SessionFactory sessionFactory; public Admin login(Admin admin) { return (Admin) sessionFactory. getCurrentSession(). createQuery("FROM Admin WHERE username=? AND password=?") .setParameter(0, admin.getUsername()) .setParameter(1, admin.getPassword()) .uniqueResult(); } }
编写DeptDao
import org.springframework.stereotype.Repository; import zhongfucheng.entity.Dept; /** * 1.查找所有的部门【在添加员工、修改员工时需要用】-->getAll() * 2.通过id查找所在的部门信息--->find() * * */ @Repository public class DeptDao extends BaseDao<Dept> { }