十二、增加Department
生活随笔
收集整理的這篇文章主要介紹了
十二、增加Department
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
1.增加Department
1.1在com.myz.domain包下增加Department類
package com.myz.domain;import java.util.Set;public class Department {private Integer id;//部門編號private String name;//部門名稱private Set<Employee> emps;//員工public Department() {}public Department(Integer id, String name) {this.id = id;this.name = name;}public Set<Employee> getEmps() {return emps;}public void setEmps(Set<Employee> emps) {this.emps = emps;}public Integer getId() {return id;}public void setId(Integer id) {this.id = id;}public String getName() {return name;}public void setName(String name) {this.name = name;} }1.2增加Department.hbm.xml
<?xml version="1.0" encoding="utf-8"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"> <hibernate-mapping package="com.myz.domain"><class name="Department" table="department"><cache usage="read-write"/><id name="id" type="java.lang.Integer"><generator class="assigned"></generator></id><property name="name" type="java.lang.String"><column name="name"></column></property><!-- 一個部門可以有多個雇員--><set name="emps" cascade="save-update"><key column="department_id"></key><one-to-many class="Employee"/></set></class> </hibernate-mapping>?
1.3修改Employee類,修改Employee.hbm.xml
package com.myz.domain;import java.util.Date;public class Employee {private Integer id;//編號private String name;//名字private String email;//郵件private Date hiredate;//雇用日期private Float salary;//工資private String password;//密碼private Integer grade;//等級private Department department;//所屬部門public Department getDepartment() {return department;}public void setDepartment(Department department) {this.department = department;}public Employee() {}public Employee(Integer id, String name, String email, Date hiredate,Float salary, String password, Integer grade) {super();this.id = id;this.name = name;this.email = email;this.hiredate = hiredate;this.salary = salary;this.setPassword(password);this.setGrade(grade);}public Integer getId() {return id;}public void setId(Integer id) {this.id = id;}public String getName() {return name;}public void setName(String name) {this.name = name;}public String getEmail() {return email;}public void setEmail(String email) {this.email = email;}public Date getHiredate() {return hiredate;}public void setHiredate(Date hiredate) {this.hiredate = hiredate;}public Float getSalary() {return salary;}public void setSalary(Float salary) {this.salary = salary;}public void setGrade(Integer grade) {this.grade = grade;}public Integer getGrade() {return grade;}public void setPassword(String password) {this.password = password;}public String getPassword() {return password;} } <?xml version="1.0" encoding="utf-8"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"> <hibernate-mapping package="com.myz.domain"><class name="Employee" table="employee"><cache usage="read-write"/><id name="id" type="java.lang.Integer"><generator class="assigned"></generator></id><property name="name" type="java.lang.String"><column name="name"></column></property><property name="email" type="java.lang.String"><column name="email" length="64"></column></property><property name="hiredate"><column name="hiredate"></column></property><property name="salary" type="java.lang.Float"><column name="salary"></column></property><property name="password" type="java.lang.String"><column name="password"></column></property><property name="grade" type="java.lang.Integer"><column name="grade"></column></property><!-- 一個雇員屬于一個部門 --><many-to-one name="department" column="department_id"></many-to-one></class> </hibernate-mapping>1.4增加DepartmentServiceInterface接口和DepartmentService實現類到對應包下
package com.myz.service.interfaces;import com.myz.domain.Department;public interface DepartmentServiceInterface {public void addDepartment(Department d);//添加部門 } package com.myz.service.imps;import javax.annotation.Resource;import org.hibernate.SessionFactory; import org.hibernate.classic.Session; import org.springframework.transaction.annotation.Transactional;import com.myz.domain.Department; import com.myz.service.interfaces.DepartmentServiceInterface;@Transactional public class DepartmentService implements DepartmentServiceInterface {@Resourceprivate SessionFactory sessionFactory;public SessionFactory getSessionFactory() {return sessionFactory;}public void setSessionFactory(SessionFactory sessionFactory) {this.sessionFactory = sessionFactory;}public void addDepartment(Department d) {// TODO Auto-generated method stubSession session = sessionFactory.getCurrentSession();session.save(d);}}1.5在applicationContext.xml中配置DepartmentService
<!-- 配置 DepartmentService,注解方式注入屬性--><bean id="departmentService" class="com.myz.service.imps.DepartmentService"></bean>1.6在applicationContext.xml中讓spring管理Department.hbm.xml
<property name="mappingResources"><list><value>com/myz/domain/Employee.hbm.xml</value><value>com/myz/domain/Department.hbm.xml</value></list></property>?
2.測試添加數據
package com.myz.test;import java.util.Date; import java.util.HashSet; import java.util.List; import java.util.Set;import org.hibernate.Session; import org.hibernate.SessionFactory; import org.hibernate.Transaction; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext;import com.myz.domain.Department; import com.myz.domain.Employee; import com.myz.service.imps.EmployeeService; import com.myz.service.interfaces.DepartmentServiceInterface; import com.myz.service.interfaces.EmployeeServiceInterface;public class Test {public static void main(String[] args) {ApplicationContext ac=new ClassPathXmlApplicationContext("applicationContext.xml");DepartmentServiceInterface ds=(DepartmentServiceInterface) ac.getBean("departmentService");// 增加三個雇員到一個部門,均保存Employee e1=new Employee(13, "比爾蓋茨", "2466@qq.com", new Date(), 4500f, "123456", 1);Employee e2=new Employee(14, "比爾蓋茨", "2466@qq.com", new Date(), 4500f, "123456", 1);Employee e3=new Employee(15, "比爾蓋茨", "2466@qq.com", new Date(), 4500f, "123456", 1);Department d1=new Department(1, "財務部");Set<Employee> emps=new HashSet<Employee>();emps.add(e1);emps.add(e2);emps.add(e3);d1.setEmps(emps);ds.addDepartment(d1);} }?
?
3.在mainFrame.jsp中,顯示登錄者的信息
3.1顯示登錄者的姓名,由于登錄時候我們把loginUser放入session中了,可以顯示成功
<h2>歡迎${loginUser.name}登錄</h2><a href="#">添加雇員</a><br/><a href="#">顯示雇員</a><br/><a href="#">查詢雇員</a><br/><a href="#">退出系統</a><br/></body>3.2顯示登錄者所在部門,卻會報500的錯,could not initialize proxy - no Session,這是懶加載引起的,登錄時僅返回Employee對象就關閉session了,可后面發現還要查詢loginUser.department.name
<body><h2>歡迎${loginUser.name}登錄,你所在的部門為${loginUser.department.name}</h2><a href="#">添加雇員</a><br/><a href="#">顯示雇員</a><br/><a href="#">查詢雇員</a><br/><a href="#">退出系統</a><br/></body>?
轉載于:https://www.cnblogs.com/myz666/p/8438467.html
總結
以上是生活随笔為你收集整理的十二、增加Department的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: bzoj1816: [Cqoi2010]
- 下一篇: SPOJ - VLATTICE