创建映射文件
首先 在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);
}