建立数据库表
person表
id | sex | name | country_id |
---|---|---|---|
1 | 男 | 张三 | 1 |
2 | 女 | 李四 | 1 |
country表
id | name |
---|---|
1 | 中国 |
2 | 美国 |
role表
id | name | person_id |
---|---|---|
1 | 教师 | 1 |
2 | 学生 | 2 |
3 | 司机 | 1 |
从表的内容可以看出person与role是一对多的关系,例如张三可以同时是教师与司机。person与country是多对一的关系,例如张三与李四都是中国人。
创建Person实体类
import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.persistence.Table;
import java.io.Serializable;
import java.util.List;
/**
* 复杂对象测试实体
* person - country 多对一
* person - role 一对多
* Created by shiyufeng on 2017/2/14.
*/
@Entity
@Table(name = "person")
public class Person implements Serializable {
@Id
private String id;
@Column
private String name;
@Column
private String sex;
/*
* cascade:为级联操作,里面有级联保存,级联删除等,all为所有
* fetch:加载类型,有lazy和eager二种,
* eager为急加载,意为立即加载,在类加载时就加载,lazy为慢加载,第一次调用的时候再加载,由于数据量太大,onetomany一般为lazy
* mappedBy:这个为manytoone中的对象名,这个不要变哦.指向的是要关联的属性,而不是要关联的类
* Set<role>:这个类型有两种,一种为list另一种为set
*/
@OneToMany(cascade = CascadeType.ALL, fetch = FetchType.LAZY, mappedBy = "person")
private List<Role> roles;
@ManyToOne
@JoinColumn(name = "country_id")
private Country country;
public List<Role> getRoles() {
return roles;
}
public void setRoles(List<Role> roles) {
this.roles = roles;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
public Country getCountry() {
return country;
}
public void setCountry(Country country) {
this.country = country;
}
@Override
public String toString() {
return "Person{" +
"id='" + id + '\'' +
", name='" + name + '\'' +
", sex='" + sex + '\'' +
'}';
}
}
创建Role实体类
import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import java.io.Serializable;
@Entity
public class Role implements Serializable {
@Id
private String id;
@Column
private String name;
@ManyToOne(cascade = CascadeType.ALL)
@JoinColumn(name = "person_id")
private Person person;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Person getPerson() {
return person;
}
public void setPerson(Person person) {
this.person = person;
}
@Override
public String toString() {
return "Role{" +
"id='" + id + '\'' +
", name='" + name + '\'' +
'}';
}
}
创建Country实体类
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Table;
/**
* Created by shiyufeng on 2017/2/15.
*/
@Entity
@Table(name = "country")
public class Country implements Serializable{
@Id
private String id;
@Column
private String name;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Override
public String toString() {
return "Country{" +
"id='" + id + '\'' +
", name='" + name + '\'' +
'}';
}
}
创建PersonRepository数据库访问接口继承之前的BaseRepository通用Dao(BaseRepository中写好了基本的增删改查接口。也可以不继承,自己自定义接口)。
public interface PersonRepository extends BaseRepository<Person, String> {
public Person findByName(String name);
}
创建服务控制类ComplexEntityController
import cn.entity.Country;
import cn.entity.Person;
import cn.entity.Role;
import cn.service.PersonRepository;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.transaction.annotation.EnableTransactionManagement;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
/**
* 复杂对象测试用例
* Created by shiyufeng on 2017/2/14.
*/
@Transactional // 事务
@RestController
@EnableAutoConfiguration
@EnableTransactionManagement // 开启注解事务管理,等同于xml配置文件中的 <tx:annotation-driven />
public class ComplexEntityController {
@Resource
private PersonRepository personRepository;
/**
* person - country 多对一
* person - role 一对多
*
* @return
*/
@RequestMapping("/findPerson")
public String findPerson() {
Person person = personRepository.find(Person.class, "2");
System.out.println(person);
System.out.println(person.getCountry());
List<Role> roles = person.getRoles();
for (Role role : roles) {
System.out.println(role);
}
return "success!";
}
}
启动服务,通过访问localhost:8080/findPerson即可得到person的信息。