1. 一对一的关联关系
1.1. 人和身份证是一对一的关联关系
人类(对应表名: t_people): 一方
身份证类(对应表名: t_icard): 一方
1.2. 身份证表
1.3. 人类表
2. 一对一的关联关系例子
2.1. 创建一个名为spring-data-jpa-one2one的Java项目, 同时添加相关jar包, 并添加JUnit能力。
2.2. 新建Icard.java
package com.bjbs.pojo;
import java.io.Serializable;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.OneToOne;
import javax.persistence.Table;
@Entity // 指定该类是实体类
@Table(name = "t_icard") // 指定数据库表名(表名和实体类对应)
public class Icard implements Serializable {
private static final long serialVersionUID = 1L;
@Id // 指定为主键
@GeneratedValue(strategy = GenerationType.IDENTITY) // 指定主键生成策略
@Column(name = "id") // 指定表中列名(列名和属性名对应)
private Integer id;
@Column(name = "card_id")
private String cardId; // 身份证号
@Column(name = "ethnic")
private String ethnic; // 民族
// mappedBy="icard"映射对应实体类中的属性
@OneToOne(mappedBy="icard")
private People people;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getCardId() {
return cardId;
}
public void setCardId(String cardId) {
this.cardId = cardId;
}
public String getEthnic() {
return ethnic;
}
public void setEthnic(String ethnic) {
this.ethnic = ethnic;
}
public People getPeople() {
return people;
}
public void setPeople(People people) {
this.people = people;
}
@Override
public String toString() {
return "Icard [id=" + id + ", cardId=" + cardId + ", ethnic=" + ethnic + "]";
}
}
2.3. 新建People.java
package com.bjbs.pojo;
import java.io.Serializable;
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.JoinColumn;
import javax.persistence.OneToOne;
import javax.persistence.Table;
@Entity // 指定该类是实体类
@Table(name = "t_people") // 指定数据库表名(表名和实体类对应)
public class People implements Serializable {
private static final long serialVersionUID = 1L;
@Id // 指定为主键
@GeneratedValue(strategy = GenerationType.IDENTITY) // 指定主键生成策略
@Column(name = "id") // 指定表中列名(列名和属性名对应)
private Integer id;
@Column(name = "name")
private String name; // 人名
// cascade=CascadeType.PERSIST级联持久化
@OneToOne(cascade=CascadeType.PERSIST)
// @JoinColumn: 就是维护一个外键
@JoinColumn(name="ic_id")
private Icard icard;
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 Icard getIcard() {
return icard;
}
public void setIcard(Icard icard) {
this.icard = icard;
}
@Override
public String toString() {
return "People [id=" + id + ", name=" + name + "]";
}
}
2.4. 一对一实体类映射
2.5. 新建PeopleRepository.java
package com.bjbs.dao;
import org.springframework.data.jpa.repository.JpaRepository;
import com.bjbs.pojo.People;
/**
* 参数一T: 当前需要映射的实体; 参数二 T: 当前映射的实体中的id的类型
*/
public interface PeopleRepository extends JpaRepository<People, Integer> {
}
2.6. 新建TestPeopleRepository.java
package com.bjbs.test;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import com.bjbs.dao.PeopleRepository;
import com.bjbs.pojo.Icard;
import com.bjbs.pojo.People;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:applicationContext.xml")
public class TestPeopleRepository {
@Autowired
private PeopleRepository peopleRepository;
@Test
public void findOne() {
People people = peopleRepository.findOne(1);
System.out.println(people);
System.out.println(people.getIcard());
}
@Test
public void save() {
// 创建身份证
Icard icard = new Icard();
icard.setEthnic("俄罗斯族");
icard.setCardId("110100112120004");
// 创建人
People people = new People();
people.setName("王五");
// 建立关系
people.setIcard(icard);
icard.setPeople(people);
// 保存数据
peopleRepository.save(people);
}
}
2.7. 在src下新建application.properties
spring.datasource.driverClassName=com.mysql.cj.jdbc.Driver
spring.datasource.url=jdbc:mysql://192.168.25.138:3306/StudyMybatis?useSSL=true&useUnicode=true&characterEncoding=utf-8&serverTimezone=Asia/Shanghai
spring.datasource.username=root
spring.datasource.password=lyw123456
2.8. 在src下新建applicationContext.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:jpa="http://www.springframework.org/schema/data/jpa"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd">
<!-- 配置读取properties文件的工具类 -->
<context:property-placeholder location="classpath:application.properties" />
<!-- 配置c3p0数据库连接池 -->
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="jdbcUrl" value="${spring.datasource.url}" />
<property name="driverClass" value="${spring.datasource.driverClassName}" />
<property name="user" value="${spring.datasource.username}" />
<property name="password" value="${spring.datasource.password}" />
</bean>
<!-- Spring整合JPA 配置EntityManagerFactory -->
<bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="jpaVendorAdapter">
<bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
<!-- hibernate相关的属性的注入 -->
<!-- 配置数据库类型 -->
<property name="database" value="MYSQL" />
<!-- 正向工程 自动创建表 -->
<!-- <property name="generateDdl" value="true" /> -->
<!-- 显示执行的SQL -->
<property name="showSql" value="true" />
</bean>
</property>
<!-- 扫描实体的包 -->
<property name="packagesToScan">
<list>
<value>com.bjbs.pojo</value>
</list>
</property>
</bean>
<!-- 配置Hibernate的事务管理器 -->
<bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
<property name="entityManagerFactory" ref="entityManagerFactory" />
</bean>
<!-- 配置开启注解事务处理 -->
<tx:annotation-driven transaction-manager="transactionManager" />
<!-- 配置springIOC的注解扫描 -->
<context:component-scan base-package="com.bjbs.service" />
<!-- Spring Data JPA 的配置 -->
<!-- base-package: 扫描dao接口所在的包 -->
<jpa:repositories base-package="com.bjbs.dao" />
</beans>
2.9. 查询一个人
2.10. 保存一个人