1.一对一:association 查询对象
代码如下:
实体类:
mapper和映射文件:
<mapper namespace="com.mybatis.mapper.PersonMapper">
<resultMap type="person" id="personmap">
<id property="id" column="id"/>
<result property="name" column="name"/>
<result property="age" column="age"/>
<!-- 使用association元素配置一对一关联
property:指定映射到的实体类对象属性
column:指定表中对应的字段
javaType:指定映射到实体对象属性的类型
select:引入嵌套查询的子SQL语句
fetchType:是否启动延迟加载,默认是lazy-->
<!-- 嵌套查询方式 -->
<association property="card" column="cid" javaType="idCard"
select="com.mybatis.mapper.IdCardMapper.findIdCardById">
</association>
<!-- 嵌套结果方式 -->
<!-- <association property="card" javaType="idCard">
<id property="id" column="id"/>
<result property="code" column="code"/>
</association> -->
</resultMap>
<select id="findPersonById" resultMap="personmap">
<!-- 嵌套结果方式 -->
<!-- select * from person p,idcard c where p.cid=c.id and p.id=#{id} -->
<!-- 嵌套查询方式 -->
select * from person where id=#{id}
</select>
</mapper>
<mapper namespace="com.mybatis.mapper.IdCardMapper">
<select id="findIdCardById" resultType="idCard">
select * from idcard where id=#{id}
</select>
</mapper>
测试类:
public class TestOneToOne {
private static Logger logger = Logger.getLogger(TestOneToOne.class);
public static void main(String[] args) {
findPersonById();
}
/*
* 根据id查询Person信息以及关联信息
*/
public static void findPersonById() {
SqlSession session=null;
try {
session = MybatisUtil.getSqlSession();
System.out.println(session);
//执行,调用session.getMapper()方法得到一个代理对象,代理对象进行增删改查操作;MapperProxyFactory类
PersonMapper mapper = session.getMapper(PersonMapper.class);
Person person= mapper.findPersonById(1);
System.out.println(person);
} catch (Exception e) {
e.printStackTrace();
}finally{
session.close();
}
核心配置注意两点:
2 一对多:collection 查询结果集
用户与订单是一对多关联
订单类
用户类
UserMapper.xml
<mapper namespace="com.mybatis.mapper.UserMapper">
<resultMap type="user" id="usermap">
<id property="id" column="id"/>
<result property="username" column="username"/>
<result property="address" column="address"/>
<!-- 使用collection元素配置一对多关联
property:指定映射到的实体类对象属性名称
column:指定表中对应的字段
ofType:指定实体对象中集合属性所包含的元素类型
javaType:指定映射到实体对象属性的类型
select:引入嵌套查询的子SQL语句
fetchType:是否启动延迟加载,默认是lazy-->
<!-- 嵌套查询方式 -->
<!-- <collection property="orders" column="id" ofType="orders"
select="com.mybatis.mapper.OrdersMapper.findOrderById" fetchType="lazy">
</collection> -->
<!-- 嵌套结果集查询 -->
<collection property="orders" ofType="orders">
<id property="id" column="oid"/>
<result property="number" column="number"/>
</collection>
</resultMap>
<select id="findUserById" resultMap="usermap">
<!-- 嵌套查询方式 -->
<!-- select * from user where id=#{id} -->
<!-- 嵌套结果集查询 -->
select u.*,o.id oid,o.number number from user u,orders o
where u.id=o.uid and u.id=#{id}
</select>
</mapper>
OrdersMapper.xml
<mapper namespace="com.mybatis.mapper.OrdersMapper">
<!-- 用户与订单 -->
<select id="findOrderById" resultType="orders">
select * from orders where uid=#{id}
</select>
<!-- 订单与商品1-->
<select id="findOrderWithProductById" resultMap="ordersmapp">
select * from orders where id=#{id}
</select>
<resultMap type="orders" id="ordersmapp">
<id property="id" column="id"/>
<result property="number" column="number"/>
<!-- 关联的商品对象之嵌套查询 -->
<collection property="pList" column="id" ofType="product"
select="com.mybatis.mapper.ProductMapper.findProductByOid" >
</collection>
</resultMap>
<!-- 订单与商品2-->
<select id="findOrderWithProductById2" resultMap="ordersmapp2">
select o.*,p.id idp ,p.name pname,p.price pprice from orders o,product p,product_orders po
where po.pid=p.id and po.oid=o.id and o.id=#{id}
</select>
<resultMap type="orders" id="ordersmapp2">
<id property="id" column="id"/>
<result property="number" column="number"/>
<!-- 关联的商品对象之嵌套结果 -->
<collection property="pList" ofType="product">
<id property="id" column="idp"/>
<result property="name" column="pname"/>
<result property="price" column="pprice"/>
</collection>
</resultMap>
</mapper>
使用collection元素和association元素配置的区别
property:指定映射到的实体类对象属性名称
column:指定表中对应的字段
javaType:指定映射到实体对象属性的类型
select:引入嵌套查询的子SQL语句
fetchType:是否启动延迟加载,默认是lazy
**collection元素**:多了一个 ofType:指定实体对象中集合属性所包含的元素类型
3.鉴别器:discriminator