Mybatis单表自动映射;使用<resultMap>设置映射结果集;多表查询

来源:互联网 发布:生死狙击刷矩阵技巧 编辑:程序博客网 时间:2024/06/06 02:56

1. 自动映射【数据库字段名与实体类的属性名一致】

    <selectid="selectList"resultType="cn.bjsxt.pojo.User">

       select id,name,pwd,age from

       t_user

    </select>

 

2. 使用resultMap设置映射结果集【数据库字段名与实体类的属性名不一致】

<!-- resultMap:定义结果集映射 id:代表结果集的唯一标记 type:结果集的类型,类的全路径名,或者别名 -->

     <resultMaptype="cn.bjsxt.pojo.User"id="userone">

         <!-- id:用于设置主键字段于实体类属性的映射关系 -->

         <idproperty="id"column="id"/>

         <!-- result:用于设置普通字段与实体类属性的映射关系-->

         <resultproperty="uname"column="name"/>

         <resultproperty="pwd"column="pwd"/>

         <resultproperty="age"column="age"/>

     </resultMap>

     <selectid="selectList"resultMap="userone">

         select id,name, pwd,age from

         t_user

     </select>

 

3. Mybatis查询方式

a)  一对一关系, 查询学生所在的班级。

第一种方式

<?xmlversion="1.0"encoding="UTF-8"?>

<!DOCTYPEmapper

PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"

"http://mybatis.org/dtd/mybatis-3-mapper.dtd">

<!-- namespace:SQL标签命名空间 -->

<mappernamespace="cn.bjsxt.mapper.StudentMapper">

     <selectid="selectList"resultType="cn.bjsxt.pojo.Student">

     SELECT s.id,s.name ,s.gender ,s.age ,c.id as 'classes.cid',c.name as

     'classes.cname',c.beginTime as 'classes.beginTime' from t_student s

     LEFT JOIN t_classes c ON c.id = s.cid

     </select>

</mapper>


第二种方式                  

<resultMaptype="cn.bjsxt.pojo.Student"id="stuMap">

         <idproperty="id"column="id"/>

         <resultproperty="name"column="name"/>

         <resultproperty="gender"column="gender"/>

         <resultproperty="age"column="age"/>

         <!-- 映射的实体类

              property:关系数据属性名

              javaType:关系数据属性类型

         -->

         <associationproperty="classes"javaType="cn.bjsxt.pojo.Classes">

              <idproperty="cid"column="id"/>

              <resultproperty="cname"column="name"/>

              <resultproperty="beginTime"column="beginTime"/>

         </association>

     </resultMap>

     <selectid="selectList"resultMap="stuMap">

     SELECT s.id,s.name ,s.gender ,s.age, c.id,c.name,c.beginTime from t_student s LEFT JOIN t_classes c ON c.id = s.cid

     </select>


b)  一对多关系:查询所有班级,并查询班级中所有学生集合

i.    一次访问数据库的方式

<!-- 1对多关系 -->

     <resultMaptype="cn.bjsxt.pojo.Classes"id="clsMap">

         <idproperty="cid"column="cid"/>

         <resultproperty="cname"column="cname"/>

         <resultproperty="beginTime"column="beginTime"/>

         <!-- 定义集合关系

              property:关系属性名

              javaType:关系对象属性:list:ArrayList

              ofType:集合的泛型

         -->

              <collectionproperty="students"javaType="java.util.ArrayList"

                   ofType="cn.bjsxt.pojo.Student">

                   <idproperty="id"column="id"/>

                   <resultproperty="name"column="name"/>

                   <resultproperty="gender"column="gender"/>

                   <resultproperty="age"column="age"/>

              </collection>

     </resultMap>

    

     <selectid="selectList"resultMap="clsMap">

         SELECT s.id,s.name ,s.gender ,s.age, c.id as cid ,c.name ascname,c.beginTime from t_student s right JOIN t_classes c ON c.id = s.cid

     </select>

注意:id冲突问题

 

c)    N+1次访问数据方式

<!-- 多次访问数据库 -->

     <resultMaptype="cn.bjsxt.pojo.Classes"id="clsMap1">

         <idproperty="cid"column="id"/>

         <resultproperty="cname"column="name"/>

         <resultproperty="beginTime"column="beginTime"/>

         <!-- 定义集合关系

              如何查询班级中的学生

              添加查询idselect

              添加id值:column

         -->

              <collectionproperty="students"javaType="java.util.ArrayList"

                   ofType="cn.bjsxt.pojo.Student"select="selStu"column="id">

              </collection>

     </resultMap>

     <!-- 查询班级信息 -->

     <selectid="selectList1"resultMap="clsMap1">

         select id , name , begintime from t_classes

     </select>

     <!-- 查询学生信息 -->

     <selectid="selStu"resultType="cn.bjsxt.pojo.Student">

         select * from t_student where cid = #{id}

     </select>


阅读全文
1 0