一、创建maven工程,引入依赖,设置配置文件
依赖和配置文件的设置可以参考,之前的博客《springdata__jpa》
二、创建实体类
1.customer类
package cn.dzl.jpa.entity; import javax.persistence.*; @Entity@Table(name = "cust_customer")public class Customer { //设置主键自增策略 @GeneratedValue(strategy = GenerationType.IDENTITY) @Id private long custId; private String custName; private String custSource; private String custIndustry; private String custLevel; private String custAddress; private String custPhone; @PrimaryKeyJoinColumn//主键关联 @OneToOne(cascade = CascadeType.ALL)//设置联级关联,效果是,两个有关联的表,可以单独地改变其中一个表 private CustomerExt ext; //该字段表明关联的是哪个表对象 public CustomerExt getExt() { return ext; } public void setExt(CustomerExt ext) { this.ext = ext; } public long getCustId() { return custId; } public void setCustId(long custId) { this.custId = custId; } public String getCustName() { return custName; } public void setCustName(String custName) { this.custName = custName; } public String getCustSource() { return custSource; } public void setCustSource(String custSource) { this.custSource = custSource; } public String getCustIndustry() { return custIndustry; } public void setCustIndustry(String custIndustry) { this.custIndustry = custIndustry; } public String getCustLevel() { return custLevel; } public void setCustLevel(String custLevel) { this.custLevel = custLevel; } public String getCustAddress() { return custAddress; } public void setCustAddress(String custAddress) { this.custAddress = custAddress; } public String getCustPhone() { return custPhone; } public void setCustPhone(String custPhone) { this.custPhone = custPhone; } @Override public String toString() { return "Customer{" + "custId=" + custId + ", custName=‘" + custName + ‘\‘‘ + ", custSource=‘" + custSource + ‘\‘‘ + ", custIndustry=‘" + custIndustry + ‘\‘‘ + ", custLevel=‘" + custLevel + ‘\‘‘ + ", custAddress=‘" + custAddress + ‘\‘‘ + ", custPhone=‘" + custPhone + ‘\‘‘ + ‘}‘; }} 2.CustomerExt类
package cn.dzl.jpa.entity; import javax.persistence.*; @Entity@Table(name = "customer_ext")public class CustomerExt { @Id//该注解在哪一个字段上,哪个字段就是主键 @GeneratedValue(strategy = GenerationType.IDENTITY)//设置主键为自增状态 private long extId; private String memo; private String info; @PrimaryKeyJoinColumn//该注解表明两个表之间是主键关联 @OneToOne//两个表之间的关系是一对一 private Customer customer;//该字段表明,关联的是哪个表对象 public Customer getCustomer() { return customer; } public void setCustomer(Customer customer) { this.customer = customer; } public long getExtId() { return extId; } public void setExtId(long extId) { this.extId = extId; } public String getMemo() { return memo; } public void setMemo(String memo) { this.memo = memo; } public String getInfo() { return info; } public void setInfo(String info) { this.info = info; } @Override public String toString() { return "CustomerExt{" + "extId=" + extId + ", memo=‘" + memo + ‘\‘‘ + ", info=‘" + info + ‘\‘‘ + ‘}‘; }} 三、创建dao接口(一定要继承JpaRepository<实体类型,主键类型>) 1.CustomerDao
package cn.dzl.jpa.dao; import cn.dzl.jpa.entity.Customer;import org.springframework.data.jpa.repository.JpaRepository; public interface CustomerDao extends JpaRepository<Customer,Long> {} 2.CustomerExtDao
package cn.dzl.jpa.dao; import cn.dzl.jpa.entity.CustomerExt;import org.springframework.data.jpa.repository.JpaRepository; public interface CustomerExtDao extends JpaRepository<CustomerExt,Long> {} 四、测试类OneToOne
package cn.dzl; import cn.dzl.jpa.dao.CustomerDao;import cn.dzl.jpa.dao.CustomerExtDao;import cn.dzl.jpa.entity.Customer;import cn.dzl.jpa.entity.CustomerExt;import org.junit.Test;import org.junit.runner.RunWith;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.test.annotation.Commit;import org.springframework.test.context.ContextConfiguration;import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;import org.springframework.transaction.annotation.Transactional; @RunWith(SpringJUnit4ClassRunner.class)@ContextConfiguration("classpath:applicationContext.xml")public class OneToOne { @Autowired CustomerDao customerDao; @Autowired CustomerExtDao customerExtDao; @Test //更改数据库,需要用到事物 @Transactional //提交 @Commit public void addCustomer(){ //创建customer对象 Customer customer = new Customer(); customer.setCustName("丁老大"); customer.setCustAddress("北京"); //创建customerExt对象 CustomerExt customerExt = new CustomerExt(); customerExt.setInfo("今天心情不错"); customerExt.setMemo("打球去"); //设置对象关联关系 customer.setExt(customerExt); customerExt.setCustomer(customer); //调用dao层方法保存到数据库中 customerDao.save(customer);// customerExtDao.save(customerExt); }}
原文地址:https://www.cnblogs.com/Hubert-dzl/p/11665058.html
时间: 2024-11-06 13:22:50