一对一关系数据库表 java类描述
时间:2014-05-26 01:26:33
收藏:0
阅读:331
一对一关系中 从表的主键是 主表的外键
sql语句
1 create table person( 2 id int primary key, 3 name varchar(100) 4 ); 5 create table idcard( 6 id int primary key, 7 num varchar(20), 8 constraint person_id_fk foreign key(id) references person(id) 9 );
domain
Person.java
1 package cn.itcast.domain; 2 3 import java.io.Serializable; 4 /* 5 create table person( 6 id int primary key, 7 name varchar(100) 8 ); 9 create table idcard( 10 id int primary key, 11 num varchar(20), 12 constraint person_id_fk foreign key(id) references person(id) 13 ); 14 */ 15 public class Person implements Serializable { 16 private Integer id; 17 private String name; 18 private IdCard idcard; 19 public Integer getId() { 20 return id; 21 } 22 public void setId(Integer id) { 23 this.id = id; 24 } 25 public String getName() { 26 return name; 27 } 28 public void setName(String name) { 29 this.name = name; 30 } 31 public IdCard getIdcard() { 32 return idcard; 33 } 34 public void setIdcard(IdCard idcard) { 35 this.idcard = idcard; 36 } 37 38 }
IdCard.java
1 package cn.itcast.domain; 2 3 import java.io.Serializable; 4 5 public class IdCard implements Serializable { 6 private Integer id; 7 private String num; 8 private Person person; 9 public Integer getId() { 10 return id; 11 } 12 public void setId(Integer id) { 13 this.id = id; 14 } 15 public String getNum() { 16 return num; 17 } 18 public void setNum(String num) { 19 this.num = num; 20 } 21 public Person getPerson() { 22 return person; 23 } 24 public void setPerson(Person person) { 25 this.person = person; 26 } 27 28 }
daoImpl.java
1 package cn.itcast.dao.impl; 2 3 import java.sql.SQLException; 4 5 import org.apache.commons.dbutils.QueryRunner; 6 import org.apache.commons.dbutils.handlers.BeanHandler; 7 8 import cn.itcast.domain.IdCard; 9 import cn.itcast.domain.Person; 10 import cn.itcast.util.DBCPUtil; 11 12 public class PersonDaoImpl { 13 private QueryRunner qr = new QueryRunner(DBCPUtil.getDataSource()); 14 public void addPerson(Person p) throws SQLException{ 15 //保存人的基本信息 16 qr.update("insert into person (id,name) values(?,?)", p.getId(),p.getName()); 17 //如果有身份证,保存身份证的基本信息 18 IdCard card = p.getIdcard(); 19 if(card!=null){ 20 qr.update("insert into idcard (id,num) values(?,?)", p.getId(),card.getNum()); 21 } 22 } 23 //要不要查IdCard的内容。都要求查,因为Idcard是少的一方的 24 public Person findPerson(Integer id) throws SQLException{ 25 Person p = qr.query("select * from person where id=?", new BeanHandler<Person>(Person.class), id); 26 if(p!=null){ 27 IdCard idcard = qr.query("select * from idcard where id=?", new BeanHandler<IdCard>(IdCard.class), id); 28 p.setIdcard(idcard); 29 } 30 return p; 31 } 32 }
test测试
1 package cn.itcast.test; 2 3 import static org.junit.Assert.fail; 4 5 import java.sql.SQLException; 6 7 import org.junit.Test; 8 9 import cn.itcast.dao.impl.PersonDaoImpl; 10 import cn.itcast.domain.IdCard; 11 import cn.itcast.domain.Person; 12 13 public class PersonDaoImplTest { 14 private PersonDaoImpl dao = new PersonDaoImpl(); 15 @Test 16 public void testAddPerson() throws SQLException { 17 Person p = new Person(); 18 p.setId(1); 19 p.setName("zql"); 20 21 IdCard idcard = new IdCard(); 22 idcard.setNum("110"); 23 24 p.setIdcard(idcard); 25 dao.addPerson(p); 26 } 27 28 @Test 29 public void testFindPerson() throws SQLException { 30 Person p = dao.findPerson(1); 31 System.out.println(p.getName()); 32 IdCard idcard = p.getIdcard(); 33 System.out.println(idcard.getNum()); 34 } 35 36 }
评论(0)