版权声明:本文为博主吕小布原创文章,未经允许不得转载。 https://blog.csdn.net/qq_36769100/article/details/79655721
摘要:
如题,简单记一下,ssh框架中,使用注解形式,怎么级联新增主外键关联表数据
一、前期准备:
1.创建工程,导入ssh框架及数据源的相关jar包;
2.创建和数据库有关的properties文件,该文件中只有数据源连接数据库所需的相关信息(数据源再applicationContext.xml中配置)
3.创建applicationContext.xml文件,该文件中包含:
3.1.注解的使用前提配置
3.2.对数据源相关信息的properties文件的加载
3.3.数据源的配置
3.4.对sessionFactory的dataSource注入、方言等 以及 对@Entity的 注解所在的包的扫描
3.5.事物的配置
4.创建web.xml,如果是作为小demo的话,这里可以只配置对structs的监听 和 对applicationContext.xml的加载
5.创建相关mvc三层及structs.xml的配置等
6.其他
综合网上其他资源的原因,我只记录model实体类的相关代码和Action中的insert代码
二、实体类分为部门表和员工表,一个部门对应多个员工,且是双向的关系
2.1.部门表(department)
其中@OneToMany注解中的mappedBy属性是员工表(employee)中的类属性
package com.entity;
import java.util.HashSet;
import java.util.Set;
import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.OneToMany;
import javax.persistence.Table;
import org.hibernate.annotations.LazyCollection;
import org.hibernate.annotations.LazyCollectionOption;
@Entity
@Table(name = "department")
public class Department {
@Id
@GeneratedValue(strategy= GenerationType.AUTO)
private Integer id;
@Column
private String name;
@Column
private String monitor;
@OneToMany(mappedBy = "department",cascade = {CascadeType.ALL})
@LazyCollection(LazyCollectionOption.EXTRA) //LazyCollection属性设置成EXTRA指定了当如果查询数据的个数时候,只会发出一条 count(*)的语句,提高性能
private Set<Employee> employees = new HashSet<Employee>();
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 getMonitor() {
return monitor;
}
public void setMonitor(String monitor) {
this.monitor = monitor;
}
public Set<Employee> getEmployees() {
return employees;
}
public void setEmployees(Set<Employee> employees) {
this.employees = employees;
}
}
2.2.员工表(employee)
@JoinClomun注解中的属性,是员工表里管理部门表的外键名称
package com.entity;
import javax.persistence.Basic;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.Table;
@Entity
@Table(name = "employee")
public class Employee {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Integer id;
@Column
private String name;
@Column
private String email;
@ManyToOne(targetEntity=Department.class)
@JoinColumn(name = "departmentNo") //JoinColumn 的name属性指定了外键的名称
@Basic(fetch=FetchType.LAZY)
private Department department;
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 Department getDepartment() {
return department;
}
public void setDepartment(Department department) {
this.department = department;
}
public Employee(Integer id, String name, String email) {
super();
this.id = id;
this.name = name;
this.email = email;
}
public Employee() {
super();
// TODO Auto-generated constructor stub
}
}
2.3.所谓关联表的两个实体,说起来就是你中有我,我中有你。
一方表中(本例中为部门表)拥有多方的(员工表)的集合,因为一个一方对应多个多方;
反过来,多方表中(本例中为员工表)拥有一方表中(部门表)的一个实体,因为一个多方只能对应一个一方。
最后,还需要把两张表给关联起来,这个时候就需要注解来起关联作用。
三、Action部分代码(insert方法)
public String insert(){
//部门
Department d = new Department();
d.setName("行政管理部门");
d.setMonitor("胡小菲");
//员工1
Employee e1 = new Employee();
e1.setName("aa");
e1.setEmail("[email protected]");
e1.setDepartment(d);
//员工2
Employee e2 = new Employee();
e2.setName("bb");
e2.setEmail("[email protected]");
e2.setDepartment(d);
//关联
d.getEmployees().add(e1);
d.getEmployees().add(e2);
int flag = baseService.save(d);
return "success";
}
这样就实现了关联表的级联插入.