MyBatis的关系
一对一
人和身份证
<?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="users">
<!--查询用户表中的所有用户信息-->
<select id="findAllUsers" resultType="Users">
select * from users;
</select>
<!--自定义返回类型 自定义返回结果集-->
<resultMap id="usersAndCard" type="Users">
<!--查询用户的同时查询卡的信息-->
<id property="uid" column="UID"></id><!--主键-->
<result property="uname" column="UNAME"></result>
<!--用户对应的卡的信息-->
<association property="card" javaType="Card">
<result property="uid" column="UID"></result>
<result property="number" column="NUMBER"></result>
</association>
</resultMap>
<select id="findUsersAndCard" parameterType="int" resultMap="usersAndCard">
<!--select u.*,c.* from users u,card c where u.uid=c.uid and u.uid=#{uid};-->
select * from users u inner join card c on u.uid=c.uid where u.uid=#{uid}
</select>
</mapper>
一对多
省对市
<?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.sw.dao.ProvinceDao">
<select id="findAllProvince" parameterType="int" resultType="Province">
select * from province where pid=#{pid}
</select>
<!--自定义返回结果集 包含省份和城市-->
<resultMap id="provinceAndCity" type="Province">
<id column="pid" property="pid"></id>
<result column="pname" property="pname"></result>
<!--省份对应的城市集合
ofType:集合中的类型是什么类型
-->
<collection property="cities" ofType="City" column="pid">
<id column="cid" property="cid"></id>
<result column="cname" property="cname"></result>
</collection>
</resultMap>
<!--查询省份的同时查询对应的城市-->
<select id="findProvinceAndCity" parameterType="int" resultMap="provinceAndCity">
select * from province p inner join city c on p.pid=c.pid where p.pid=#{pid}
</select>
</mapper>
ps:有关系的两个类都要对应配置