MyBatis一对一级联查询

创建映射文件

首先 在mybatis-config.xml文件中打开延迟加载开关,不要忘记配置<mapper source=" ">
注意:需要将此代码块放置在mappers之前!

	<!-- 设置延迟加载 -->
	<settings>
		<!-- 打开延迟加载的开关 -->
		<setting name="lazyLoadingEnabled" value="true" />
		<!-- 将积极加载改为按需加载 aggressive:侵略性的 -->
		<setting name="aggressiveLazyLoading" value="false" />
	</settings>

然后,在com.mybatis中创建两张表对应的映射文件
例IdCardMapper.xml:

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper  PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"  
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.dao.IdCardDao">
	<select id="selectCodeById" parameterType="Integer" resultType="com.po.Idcard">
		select * from idcard where id = #{id}
	</select>
</mapper>

PersonMapper.xml:

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper  PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"  
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.dao.PersonDao">
	<!-- 一对一 根据id查询个人信息:级联查询的第一种方法(嵌套查询,执行两个SQL语句) -->
	<resultMap type="com.po.Person" id="cardAndPerson1">
		<id property="id" column="id"></id>
		<result property="name" column="name" />
		<result property="age" column="age" />
		<!-- 一对一级联查询 -->
		<association property="card" column="idcard_id" javaType="com.po.Idcard"
			select="com.dao.IdCardDao.selectCodeById" />
	</resultMap>
	<select id="selectPersonById1" parameterType="Integer"
		resultMap="cardAndPerson1">
		select * from person where id=#{id}
	</select>
	<!-- 一对一 根据id查询个人信息:及联查询的第二种方法(嵌套结果,执行一个SQL语句) -->
	<resultMap type="com.po.Person" id="cardAndPerson2">
		<id property="id" column="id"></id>
		<result property="name" column="name" />
		<result property="age" column="age" />
		<association property="card" javaType="com.po.Idcard">
			<id property="id" column="idcard_id" />
			<result property="code" column="code" />
		</association>
	</resultMap>
	<select id="selectPersonById2" parameterType="Integer"
		resultMap="cardAndPerson2">
		select p.*,ic.code
		from person p,idcard ic
		where
		p.idcard_id=ic.id and p.id=#{id}
	</select>
	<!-- 一对一 根据id查询个人信息,连接查询(使用POJO存储结果) -->
	<select id="selectPersonById3" parameterType="Integer"
		resultType="com.pojo.SelectPersonById">
		select p.*,ic.code
		from person p,idcard ic
		where
		p.idcard_id=ic.id and p.id=#{id}
	</select>
</mapper>

创建POJO类

若使用pojo类来接收查询结果信息,则需要创建一个pojo类:

public class SelectPersonById {
	private Integer id;
	private String name;
	private Integer age;
	private String code;
	//此处重写set和get方法
	@Override
	public String toString() {
		return "SelectPersonById [id=" + id + ", name=" + name + ", age=" + age + ", code=" + code + "]";
	}
}

数据操作接口

注意,需要加上注解@Repository和@Mapper
例:

package com.dao;

import org.apache.ibatis.annotations.Mapper;
import org.springframework.stereotype.Repository;

import com.po.Idcard;

@Repository("idCardDao")
@Mapper
public interface IdCardDao {
	public Idcard selectCodeById(Integer i);
}

package com.dao;

import org.apache.ibatis.annotations.Mapper;
import org.springframework.stereotype.Repository;

import com.po.Person;
import com.pojo.SelectPersonById;

@Repository("personDao")
@Mapper
public interface PersonDao {
	public Person selectPersonById1(Integer id);

	public Person selectPersonById2(Integer id);

	public SelectPersonById selectPersonById3(Integer id);
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值