一、创建maven工程,引入依赖和创建配置文件
依赖和配置文件参考,之前的博客《springdata——jpa》
二、创建实体类
1.customer1
package cn.dzl.jpa.entity; import javax.persistence.*; import java.util.HashSet; import java.util.Set; @Entity @Table(name="cst_customer1") public class Customer1 { //配置主键生成的策略 @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; //all代表所有情况下都关联操作, lazy表示懒加载,该属性不会马上从数据库加载 @OneToMany(cascade = CascadeType.ALL,fetch = FetchType.LAZY) private Set<LinkMan>linkMen=new HashSet<>();//因为,联系人有多个,所有用集合存 public Set<LinkMan> getLinkMen() { return linkMen; } public void setLinkMen(Set<LinkMan> linkMen) { this.linkMen = linkMen; } 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 "Customer1{" + "custId=" + custId + ", custName='" + custName + '\'' + ", custSource='" + custSource + '\'' + ", custIndustry='" + custIndustry + '\'' + ", custLevel='" + custLevel + '\'' + ", custAddress='" + custAddress + '\'' + ", custPhone='" + custPhone + '\'' + '}'; } }
2.LinkMan
package cn.dzl.jpa.entity; import javax.persistence.*; @Entity @Table(name="cust_linkman") public class LinkMan { @GeneratedValue(strategy = GenerationType.IDENTITY) @Id private long id; private String name; private String gender; private String phone; private String position; @ManyToOne(fetch = FetchType.LAZY)//懒加载,该属性,不会马上在数据库加载 @JoinColumn(name = "custName",referencedColumnName = "custName")//要关联的列,在实体类中的名字和在表中的名字 private Customer1 customer1;//要关联的是哪一个表 public long getId() { return id; } public void setId(long id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getGender() { return gender; } public void setGender(String gender) { this.gender = gender; } public String getPhone() { return phone; } public void setPhone(String phone) { this.phone = phone; } public String getPosition() { return position; } public void setPosition(String position) { this.position = position; } public Customer1 getCustomer1() { return customer1; } public void setCustomer1(Customer1 customer1) { this.customer1 = customer1; } @Override public String toString() { return "LinkMan{" + "id=" + id + ", name='" + name + '\'' + ", gender='" + gender + '\'' + ", phone='" + phone + '\'' + ", position='" + position + '\'' + ", customer1=" + customer1 + '}'; } }
三、dao接口
1.customer1Dao
package cn.dzl.jpa.dao;import cn.dzl.jpa.entity.Customer1;import org.springframework.data.jpa.repository.JpaRepository;public interface Customer1Dao extends JpaRepository<Customer1,Long> {}2.LinkManDao
package cn.dzl.jpa.dao;import cn.dzl.jpa.entity.LinkMan;import org.springframework.data.jpa.repository.JpaRepository;public interface LinkManDao extends JpaRepository<LinkMan,Long> {}四、测试类
package cn.dzl; import cn.dzl.jpa.dao.Customer1Dao; import cn.dzl.jpa.dao.LinkManDao; import cn.dzl.jpa.entity.Customer1; import cn.dzl.jpa.entity.LinkMan; 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 OneToMany { @Autowired Customer1Dao customer1Dao; @Autowired LinkManDao linkManDao; @Test @Transactional @Commit public void addCustomer(){ //创建customer1对象 Customer1 customer1 = new Customer1(); customer1.setCustName("xiaoming"); customer1.setCustAddress("郑州"); customer1.setCustPhone("123"); customer1.setCustLevel("老师"); //创建第一个LinkMan对象 LinkMan linkMan1 = new LinkMan(); linkMan1.setName("小红"); linkMan1.setPhone("000"); //创建第二个LinkMan对象 LinkMan linkMan2 = new LinkMan(); linkMan2.setName("小东"); linkMan2.setPhone("111"); //设置对象之间的关联关系 customer1.getLinkMen().add(linkMan1); customer1.getLinkMen().add(linkMan2); linkMan1.setCustomer1(customer1); linkMan2.setCustomer1(customer1); //使用dao层把数据写入数据库 customer1Dao.save(customer1); // linkManDao.save(linkMan1); // linkManDao.save(linkMan2); } }