1、现在在上面一章的基础上,我们引入一个address表,该表记录person类的地址,address表的格式如下所示
现在person类要和address表想关联,得到当前联系人的住宅地址,我们应该在person表中增加一个aid的字段,aid的值来自于address中
现在我们执行下面的操作:
我们如何通过代码把上面的结果集选择出来了。
可以使用MapHandle类来实现
我们来看下面的代码:
package com.weiyuan.test; public class Address {
private int aid;
private String province;
private String city;
private String district;
private String street;
public int getAid() {
return aid;
}
public void setAid(int aid) {
this.aid = aid;
}
public String getProvince() {
return province;
}
public void setProvince(String province) {
this.province = province;
}
public String getCity() {
return city;
}
public void setCity(String city) {
this.city = city;
}
public String getDistrict() {
return district;
}
public void setDistrict(String district) {
this.district = district;
}
public String getStreet() {
return street;
}
public void setStreet(String street) {
this.street = street;
}
@Override
public String toString() {
return "Address [aid=" + aid + ", province=" + province + ", city="
+ city + ", district=" + district + ", street=" + street + "]";
} }
package com.weiyuan.test; public class Person {
private String pid;
private String pname;
private int age;
private String sex;
private Address address;
public String getPid() {
return pid;
}
public void setPid(String pid) {
this.pid = pid;
}
public String getPname() {
return pname;
}
public void setPname(String pname) {
this.pname = pname;
}
public int getAge() {
return age;
}
public void setAge(int age) {
package com.weiyuan.test; import java.sql.SQLException;
import java.util.List;
import java.util.Map; import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;
import org.apache.commons.dbutils.handlers.MapHandler;
import org.apache.commons.dbutils.handlers.MapListHandler;
import org.apache.commons.dbutils.handlers.ScalarHandler;
import org.junit.Test;
import org.junit.runner.RunWith; import cn.itcast.commons.CommonUtils;
import cn.itcast.jdbc.JdbcUtils;
import cn.itcast.jdbc.TxQueryRunner; /**
* 对数据库操作类的测试
* */
public class TxRunnerTest { @Test
public void testQuery6() throws SQLException{
/*
* map ={sex=男, street=肖家河街道6号, age=20, province=四川省, pname=张三, pid=2, aid=1, district=武侯区, city=成都市}
* */
QueryRunner run = new TxQueryRunner();
String sql = "select * from person p, address a where p.aid=a.aid and p.pid = ?";
Map<String,Object> map= run.query(sql,new MapHandler(),"2");
Person p = CommonUtils.toBean(map, Person.class);
Address address = CommonUtils.toBean(map, Address.class);
p.setAddress(address);
System.out.println(map); }
}
this.age = age;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
@Override
public String toString() {
return "Person [pid=" + pid + ", pname=" + pname + ", age=" + age
+ ", sex=" + sex + ", address=" + address + "]";
}
public Address getAddress() {
return address;
}
public void setAddress(Address address) {
this.address = address;
} }