文章目录
前言
示例
前言
客观世界中的对象很少有孤立存在的,例如班级,往往与班级的学生存在关联关系,如果得到某个班级的实例,那么应该可以直接获取班级对应的全部学生。反过来,如果已经得到个学生的实例,那么也应该可以访问该学生对应的班级。这种实例之间的互相访问就是关联关系。
关联关系是面向对象分析、面向对象设计最重要的知识, MyBatis完全可以理解这种关联关系,如果映射得当, My Batis的关联映射将可以大大简化持久层数据的访问。
示例
这里只给出映射文件(XML文件)中的关键代码
- 一对一关系
例如:一个人对应一个身份证号
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="org.arunner.mapper.PersonMapper"> <resultMap id="personMapper" type="org.arunner.domain.Person"> <id property="id" column="id"/> <result property="name" column="name"/> <result property="age" column="age"/> <result property="sex" column="sex"/> <!--一对一关联映射--> <association property="card" column="card_id" javaType="org.arunner.domain.Card" select="org.arunner.mapper.CardMapper.selectCardById"/> </resultMap> <!--根据ID查询人员--> <select id="selectPersonById" parameterType="int" resultMap="personMapper"> select * from tb_person where id = #{id} ; </select> </mapper>
CardMapper.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="org.arunner.mapper.CardMapper"> <!--根据ID查询身份证信息--> <select id="selectCardById" parameterType="int" resultType="org.arunner.domain.Card"> select * from tb_card where id = #{id} ; </select> </mapper>
- 一对多关系
例如:一个班级对应多个学生
ClazzMapper.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="org.arunner.mapper.ClazzMapper"> <resultMap id="clazzResultMap" type="org.arunner.domain.Clazz"> <id property="id" column="id"/> <result property="code" column="code"/> <result property="name" column="name"/> <!--班级的学生属性,因为一个班级有多个学生,所以该属性是一个集合--> <collection property="students" javaType="ArrayList" column="id" ofType="org.arunner.domain.Student" select="org.arunner.mapper.StudentMapper.selectStudentByClazzId" fetchType="lazy"> <id property="id" column="id"/> <result property="name" column="name"/> <result property="age" column="age"/> <result property="sex" column="sex"/> </collection> </resultMap> <!--根据班级ID查询班级--> <select id="selectClazzById" parameterType="int" resultMap="clazzResultMap"> select * from tb_clazz where id = #{id} ; </select> </mapper>
StudentMapper.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="org.arunner.mapper.StudentMapper"> <resultMap id="studentResultMap" type="org.arunner.domain.Student"> <id property="id" column="id"/> <result property="name" column="name"/> <result property="age" column="age"/> <result property="sex" column="sex"/> <!--一对一关系--> <association property="clazz" javaType="org.arunner.domain.Clazz"> <id property="id" column="id"/> <result property="code" column="code"/> <result property="name" column="name"/> </association> </resultMap> <!--根据班级ID查询班级--> <select id="selectStudentByClazzId" parameterType="int" resultMap="studentResultMap"> select * from tb_student where clazz_id = #{id} ; </select> <!--根据ID查询学生和班级信息--> <select id="selectStudentById" parameterType="int" resultMap="studentResultMap"> select * from tb_student s,tb_clazz c where c.id = s.clazz_id and s.id= #{id} ; </select> </mapper>
- 多对多关系
例如:订单、商品和用户的关系
OrderMapper.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"> <!-- namespace指用户自定义的命名空间。 --> <mapper namespace="org.arunner.mapper.OrderMapper"> <resultMap type="org.arunner.domain.Order" id="orderResultMap"> <id property="id" column="oid"/> <result property="code" column="code"/> <result property="total" column="total"/> <!-- 多对一关联映射:association --> <association property="user" javaType="org.arunner.domain.User"> <id property="id" column="id"/> <result property="username" column="username"/> <result property="loginname" column="loginname"/> <result property="password" column="password"/> <result property="phone" column="phone"/> <result property="address" column="address"/> </association> <!-- 多对多映射的关键:collection --> <collection property="articles" javaType="ArrayList" column="oid" ofType="org.arunner.domain.Article" select="org.arunner.mapper.ArticleMapper.selectArticleByOrderId" fetchType="lazy"> <id property="id" column="id"/> <result property="name" column="name"/> <result property="price" column="price"/> <result property="remark" column="remark"/> </collection> </resultMap> <!-- 注意,如果查询出来的列同名,例如tb_user表的id和tb_order表的id都是id,同名,需要使用别名区分 --> <select id="selectOrderById" parameterType="int" resultMap="orderResultMap"> SELECT u.*,o.id AS oid,CODE,total,user_id FROM tb_user u,tb_order o WHERE u.id = o.user_id AND o.id = #{id} </select> <!-- 根据userid查询订单 --> <select id="selectOrderByUserId" parameterType="int" resultType="org.arunner.domain.Order"> SELECT * FROM tb_order WHERE user_id = #{id} </select> </mapper>
UserMapper.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"> <!-- namespace指用户自定义的命名空间。 --> <mapper namespace="org.arunner.mapper.UserMapper"> <resultMap type="org.arunner.domain.User" id="userResultMap"> <id property="id" column="id"/> <result property="username" column="username"/> <result property="loginname" column="loginname"/> <result property="password" column="password"/> <result property="phone" column="phone"/> <result property="address" column="address"/> <!-- 一对多关联映射:collection --> <collection property="orders" javaType="ArrayList" column="id" ofType="org.arunner.domain.User" select="org.arunner.mapper.OrderMapper.selectOrderByUserId" fetchType="lazy"> <id property="id" column="id"/> <result property="code" column="code"/> <result property="total" column="total"/> </collection> </resultMap> <select id="selectUserById" parameterType="int" resultMap="userResultMap"> SELECT * FROM tb_user WHERE id = #{id} </select> </mapper>
ArticleMapper.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"> <!-- namespace指用户自定义的命名空间。 --> <mapper namespace="org.arunner.mapper.ArticleMapper"> <select id="selectArticleByOrderId" parameterType="int" resultType="org.arunner.domain.Article"> SELECT * FROM tb_article WHERE id IN ( SELECT article_id FROM tb_item WHERE order_id = #{id} ) </select> </mapper>