这篇实践一下一对一、一对多两种关系的映射操作,首先建立数据库
一共三张表 分别为 person表 card表 address表
关系是person有一个card,多个address数据
address表
address_id | address_name | address_person |
1 | nb | 1 |
2 | hz | 1 |
3 | bj | 1 |
card表
card_id | card_info |
1 | vip |
person表
person_id | person_name | person_card_id |
1 | pf | 1 |
新建一个工程,目录结构如下:
需要添加的jar包:
ibatis-3-core-3.0.0.242.jar
dglib-nodep-2.2.jar
asm-1.4.1.jar
ojdbc14_g.jar
可以在http://www.findjar.com/index.x这个网址上搜到相关jar包
SqlMapConfig.xml
xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration
PUBLIC "-//ibatis.apache.org//DTD Config 3.0//EN"
"http://ibatis.apache.org/dtd/ibatis-3-config.dtd">
<configuration>
<settings>
<setting name="cacheEnabled" value="true"/>
<setting name="lazyLoadingEnabled" value="true"/>
<setting name="multipleResultSetsEnabled" value="true"/>
<setting name="useColumnLabel" value="true"/>
<setting name="useGeneratedKeys" value="false"/>
<setting name="defaultExecutorType" value="SIMPLE"/>
<setting name="defaultStatementTimeout" value="25000"/>
settings>
<environments default="development">
<environment id="development">
<transactionManager type="JDBC"/>
<dataSource type="POOLED">
<property name="driver" value="oracle.jdbc.driver.OracleDriver"/>
<property name="url" value="jdbc:oracle:thin:@localhost:1521:xe"/>
<property name="username" value="puf"/>
<property name="password" value="pufang890505"/>
<property name="poolMaximumActiveConnections" value="10"/>
<property name="poolMaximumIdleConnections" value="10"/>
<property name="poolMaximumCheckoutTime" value="20000"/>
<property name="poolTimeToWait" value="20000"/>
<property name="poolPingQuery" value="NO PING QUERY SET"/>
<property name="poolPingEnabled" value="false"/>
<property name="poolPingConnectionsNotUsedFor" value="0"/>
dataSource>
environment>
environments>
<mappers>
<mapper resource="cn/pf/ibatis/domain/Person.xml"/>
mappers>
configuration>
相关的pojo
person.java
import java.util.List;
/**
* ...
* @author pf
* @version 2010-3-22下午08:07:20
*/
public class Person {
private int id;
private String name;
private Card card;
private List addressList;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Card getCard() {
return card;
}
public void setCard(Card card) {
this.card = card;
}
public List getAddressList() {
return addressList;
}
public void setAddressList(List addressList) {
this.addressList = addressList;
}
@Override
public String toString() {
String s = "Person [id=" + id + ", name=" + name;
s += ", card=[id:"+card.getId()+", info:"+card.getInfo()+"]";
s += ", address=[";
for(int i=0;i
s += "[id:"+addressList.get(i).getId()+", address:"+addressList.get(i).getAddress()+"]";
}
s += "]]";
return s;
}
}
Address.java
package cn.pf.ibatis.domain; /** * ... * @author pf * @version 2010-3-22下午08:09:46 */ public class Address { private int id; private String address; private Person person; public int getId() { return id; } public void setId(int id) { this.id = id; } public String getAddress() { return address; } public void setAddress(String address) { this.address = address; } public Person getPerson() { return person; } public void setPerson(Person person) { this.person = person; } }
Card.java
package cn.pf.ibatis.domain; /** * ... * @author pf * @version 2010-3-22下午08:07:48 */ public class Card { private int id; private String info; public int getId() { return id; } public void setId(int id) { this.id = id; } public String getInfo() { return info; } public void setInfo(String info) { this.info = info; } }
Person.xml
xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
PUBLIC "-//ibatis.apache.org//DTD Mapper 3.0//EN"
"http://ibatis.apache.org/dtd/ibatis-3-mapper.dtd">
<mapper namespace="cn.pf.ibatis.dao.PersonDAO">
<resultMap id="cardResultMap" type="cn.pf.ibatis.domain.Card">
<id property="id" column="card_id" />
<result property="info" column="card_info"/>
resultMap>
<resultMap id="addressResultMap" type="cn.pf.ibatis.domain.Address">
<id property="id" column="address_id" />
<result property="address" column="address_address"/>
resultMap>
<resultMap id="personResultMap" type="cn.pf.ibatis.domain.Person">
<id property="id" column="person_id" />
<result property="name" column="person_name"/>
<association property="card" column="person_card_id" javaType="cn.pf.ibatis.domain.Card" resultMap="cardResultMap"/>
<collection property="addressList" ofType="cn.pf.ibatis.domain.Address" resultMap="addressResultMap"/>
resultMap>
<select id="queryAllPerson" resultMap="personResultMap">
select
c.card_id as card_id,
c.card_info as card_info,
p.person_id as person_id,
p.person_name as person_name,
a.address_id as address_id,
a.address_address as address_address
from Person p ,Address a,Card c
where c.card_id = p.person_card_id
and p.person_id = a.address_person_id
select>
mapper>
PersonDAO.java
package cn.pf.ibatis.dao; import java.util.List; import cn.pf.ibatis.domain.Person; /** * ... * @author pf * @version 2010-3-22下午11:11:30 */ public interface PersonDAO { public List queryAllPerson(); }
test.java
import java.io.IOException; import java.io.Reader; import java.util.List; import org.apache.ibatis.io.Resources; import org.apache.ibatis.session.SqlSession; import org.apache.ibatis.session.SqlSessionFactory; import org.apache.ibatis.session.SqlSessionFactoryBuilder; import cn.pf.ibatis.dao.PersonDAO; import cn.pf.ibatis.domain.Person; /** * ... * @author pf * @version 2010-3-16下午03:13:24 */ public class test { /** * ... * @param args */ public static void main(String[] args) { String resource = "SqlMapConfig.xml"; Reader reader = null; try { //使用ibatis提供的Resources类读取资源文件 reader = Resources.getResourceAsReader(resource); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } //根据资源文件内容建立session工厂 SqlSessionFactory sqlMapper = new SqlSessionFactoryBuilder().build(reader); //session工厂打开一个session SqlSession session = sqlMapper.openSession(true); PersonDAO personDAO = session.getMapper(PersonDAO.class); List personList; try{ personList = personDAO.queryAllPerson(); }finally{ session.close(); } for(int i=0;i
输出结果:
Person [id=1, name=pf, card=[id:1, info:vip], address=[[id:1, address:nb][id:2, address:hz][id:3, address:bj]]]
上面采用的是Nested Results方法实现的,sql语句中的表关联均为手动实现,下面我们试着用Nested Select方法实现以下。
Person.xml
<!DOCTYPE mapper
PUBLIC "-//ibatis.apache.org//DTD Mapper 3.0//EN"
"http://ibatis.apache.org/dtd/ibatis-3-mapper.dtd">
<mapper namespace="cn.pf.ibatis.dao.PersonDAO">
<resultMap id="cardResultMap" type="cn.pf.ibatis.domain.Card">
<id property="id" column="card_id" />
<result property="info" column="card_info"/>
resultMap>
<resultMap id="addressResultMap" type="cn.pf.ibatis.domain.Address">
<id property="id" column="address_id" />
<result property="address" column="address_address"/>
resultMap>
<resultMap id="personResultMap" type="cn.pf.ibatis.domain.Person">
<id property="id" column="person_id" />
<result property="name" column="person_name"/>
<association property="card" column="person_card_id" javaType="cn.pf.ibatis.domain.Card" select="queryCardById"/>
<collection property="addressList" ofType="cn.pf.ibatis.domain.Address" column="person_id" select="queryAddressByPersonId"/>
resultMap>
<select id="queryCardById" parameterType="int" resultMap="cardResultMap">
select
card_id,
card_info
from card
where card_id = #{id}
select>
<select id="queryAddressByPersonId" parameterType="int" resultMap="addressResultMap">
select
address_id,
address_address
from address
where address_person_id = #{id}
select>
<select id="queryAllPerson" resultMap="personResultMap">
select
person_id,
person_name,
person_card_id
from person
select>
mapper>
跟踪变量的时候发现personList = personDAO.queryAllPerson();语句并没有将card和address属性查出,均为null,在后面for循环输出的时候才进行赋值,但是又发现一个问题,test.java代码段中for循环输出前以前调用session.close(),按照hibernate的理解应该不能懒加载了,可是居然可以,而使用session.getConnection().close();直接关闭链接就无法查出card和address属性。不明白session.close()关闭了什么东西。。。
发现ibatis还有很多事情是需要自己动手编码完成的,hibernate自动化程度就高很多了,还是喜欢hibernate一点,ibatis还有很多没搞明白,继续找资料,继续学习。