设计一个方法,查询全部人两个表格的所有信息
只展示PersonDao和PersonMapper.xml内容,其余代码和上一文档一样
//设计一个方法,查询全部人两个表格的所有信息public List<Person> selectAll(){return sqlSession.selectList("selectAll");}
<?xml version="1.0" encoding="UTF-8" ?><!DOCTYPE mapperPUBLIC "-//mybatis.org//DTD Mapper 3.0//EN""http://mybatis.org/dtd/mybatis-3-mapper.dtd"><mapper namespace="dao.PersonDao"><!-- <select id="selectOne" resultMap="selectPerson">--><!-- select pid,pname,cardid from person where pid = #{pid}--><!-- </select>--><!--自定义一个赋值的规则--><resultMap id="selectPerson" type="domain.Person"><!--id一般用于主键列,result是普通列--><id property="pid" column="pid"></id><result property="pname" column="pname"></result><association property="idcard" javaType="domain.IDCard" select="selectIdCardForPerson" column="cardid"></association></resultMap><!--为了Person对象中的idcard属性再次查询用--><select id="selectIdCardForPerson" resultType="domain.IDCard">select * from idcard where cardid = #{cardid}</select><select id="selectAll" resultMap="selectPerson">select pid,pname,cardid from person</select></mapper>

执行底层数据库查询的时候 希望打印SQL语句
导入log4j.jar 还需要自己手写一个配置文件 log4j.properties(放在与configuration.xml同一层次)

<configuration>
<settings>
<!--开启延迟加载的机制,关联对象可以延迟加载-->
<setting name="lazyLoadingEnabled" value="true"/>
<!--关闭对象的侵略性,不用就不需要加载,用到了关联对象的任意属性就加载-->
<setting name="aggressiveLazyLoading" value="false"/>
</settings>
</configuration>
联合查询—根据pid查询这个人两个表格的所有信息—查询全部人两个表格的所有信息
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="dao.PersonDao">
<select id="selectOne" resultMap="selectPerson">
select p.pid,p.pname,i.cardid,i.address from person p inner join idcard i
on p.cardid = i.cardid where p.pid = #{pid}
</select>
<select id="selectAll" resultMap="selectPerson">
select p.pid,p.pname,i.cardid,i.address from person p inner join idcard i
on p.cardid = i.cardid
</select>
<!--自定义一个赋值的规则-->
<resultMap id="selectPerson" type="domain.Person">
<!--id一般用于主键列,result是普通列-->
<id property="pid" column="pid"></id>
<result property="pname" column="pname"></result>
<association property="idcard" javaType="domain.IDCard">
<id property="cardid" column="cardid"></id>
<result property="address" column="address"></result>
</association>
</resultMap>
</mapper>
