mybatis的常用标签及一些使用笔记

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/BeauXie/article/details/54582719

本文主要记录在使用mybatis时,一些重要的标签以及一些使用笔记,很多都是参考自网上,总结出来以便以后使用。


一、resultMap标签:

使用场景:数据库中的表字段与javaBean对象属性不一致时、复杂联合查询;

1.解决字段名与实体类属性名不相同的冲突:

 <!--通过<resultMap>映射实体类属性名和表的字段名对应关系 -->
    <resultMap type="me.gacl.domain.Order" id="orderResultMap">
        <!-- 用id属性来映射主键字段 -->
        <id property="id" column="order_id"/>
        <!-- 用result属性来映射非主键字段 -->
        <result property="orderNo" column="order_no"/>
        <result property="price" column="order_price"/>
    </resultMap>

本段参考:http://www.cnblogs.com/xdp-gacl/p/4264425.html

2.复杂联合查询:

2.1一对一:根据班级id查询班级信息(带老师的信息)
 <!-- 
        根据班级id查询班级信息(带老师的信息)
        ##1. 联表查询
        SELECT * FROM class c,teacher t WHERE c.teacher_id=t.t_id AND c.c_id=1;
        
        ##2. 执行两次查询
        SELECT * FROM class WHERE c_id=1;  //teacher_id=1
        SELECT * FROM teacher WHERE t_id=1;//使用上面得到的teacher_id
     -->


    <!-- 
    方式一:嵌套结果:使用嵌套结果映射来处理重复的联合结果的子集
             封装联表查询的数据(去除重复的数据)
        select * from class c, teacher t where c.teacher_id=t.t_id and c.c_id=1
    -->
    <select id="getClass" parameterType="int" resultMap="ClassResultMap">
        select * from class c, teacher t where c.teacher_id=t.t_id and c.c_id=#{id}
    </select>
    <!-- 使用resultMap映射实体类和字段之间的一一对应关系 -->
    <resultMap type="me.gacl.domain.Classes" id="ClassResultMap">
        <id property="id" column="c_id"/>
        <result property="name" column="c_name"/>
        <association property="teacher" javaType="me.gacl.domain.Teacher">
            <id property="id" column="t_id"/>
            <result property="name" column="t_name"/>
        </association>
    </resultMap>
    
    <!-- 
    方式二:嵌套查询:通过执行另外一个SQL映射语句来返回预期的复杂类型
        SELECT * FROM class WHERE c_id=1;
        SELECT * FROM teacher WHERE t_id=1   //1 是上一个查询得到的teacher_id的值
    -->
     <select id="getClass2" parameterType="int" resultMap="ClassResultMap2">
        select * from class where c_id=#{id}
     </select>
     <!-- 使用resultMap映射实体类和字段之间的一一对应关系 -->
     <resultMap type="me.gacl.domain.Classes" id="ClassResultMap2">
        <id property="id" column="c_id"/>
        <result property="name" column="c_name"/>
        <association property="teacher" column="teacher_id" select="getTeacher"/>
     </resultMap>
     
     <select id="getTeacher" parameterType="int" resultType="me.gacl.domain.Teacher">
        SELECT t_id id, t_name name FROM teacher WHERE t_id=#{id}
     </select>
2.2一对多:根据classId查询对应的班级信息,包括学生,老师
<!-- 
        根据classId查询对应的班级信息,包括学生,老师
     -->
    <!-- 
    方式一: 嵌套结果: 使用嵌套结果映射来处理重复的联合结果的子集
    SELECT * FROM class c, teacher t,student s WHERE c.teacher_id=t.t_id AND c.C_id=s.class_id AND  c.c_id=1
     -->
    <select id="getClass3" parameterType="int" resultMap="ClassResultMap3">
        select * from class c, teacher t,student s where c.teacher_id=t.t_id and c.C_id=s.class_id and  c.c_id=#{id}
    </select>
    <resultMap type="me.gacl.domain.Classes" id="ClassResultMap3">
        <id property="id" column="c_id"/>
        <result property="name" column="c_name"/>
        <association property="teacher" column="teacher_id" javaType="me.gacl.domain.Teacher">
            <id property="id" column="t_id"/>
            <result property="name" column="t_name"/>
        </association>
        <!-- ofType指定students集合中的对象类型 -->
        <collection property="students" ofType="me.gacl.domain.Student">
            <id property="id" column="s_id"/>
            <result property="name" column="s_name"/>
        </collection>
    </resultMap>
    
    <!-- 
        方式二:嵌套查询:通过执行另外一个SQL映射语句来返回预期的复杂类型
            SELECT * FROM class WHERE c_id=1;
            SELECT * FROM teacher WHERE t_id=1   //1 是上一个查询得到的teacher_id的值
            SELECT * FROM student WHERE class_id=1  //1是第一个查询得到的c_id字段的值
     -->
     <select id="getClass4" parameterType="int" resultMap="ClassResultMap4">
        select * from class where c_id=#{id}
     </select>
     <resultMap type="me.gacl.domain.Classes" id="ClassResultMap4">
        <id property="id" column="c_id"/>
        <result property="name" column="c_name"/>
        <association property="teacher" column="teacher_id" javaType="me.gacl.domain.Teacher" select="getTeacher2"></association>
        <collection property="students" ofType="me.gacl.domain.Student" column="c_id" select="getStudent"></collection>
     </resultMap>
     
     <select id="getTeacher2" parameterType="int" resultType="me.gacl.domain.Teacher">
        SELECT t_id id, t_name name FROM teacher WHERE t_id=#{id}
     </select>
     
     <select id="getStudent" parameterType="int" resultType="me.gacl.domain.Student">
        SELECT s_id id, s_name name FROM student WHERE class_id=#{id}
     </select>

3.主要属性

property:对象属性的名称;

javaType:对象属性的类型;

column:所对应的外键字段名称;

select:使用另一个查询封装的结果;

collection:解决一对多的关联查询,ofType属性指定集合中元素的对象类型.


参考自:http://www.cnblogs.com/xdp-gacl/p/4264440.html

二、foreach标签:

1.使用场景 

  批量查询、插入,删除等。比如:根据角色id集合查找对应的角色,代码如下:

<select id="getRolesByIds" parameterMap="java.util.Map" resultMap="role">
        SELECT * FROM t_role WHERE id IN
        <foreach collection="roles" item="role" open="(" separator="," close=")">
            #{role.id}
        </foreach>
    </select>
假如集合roles包含两个元素:“1”,“2”,则打印出来的sql语句为:

SELECT * FROM t_role WHERE id IN(?,?)

返回的是一个List<Role>集合。

2.foreach标签的属性

主要有 item,index,collection,open,separator,close

collection:迭代的集合名;

item:集合中每一个元素进行迭代时的别名;

open:表示该语句以什么开始;

separator:每次进行迭代之间以什么符合作为分隔符;

close:表示该语句以什么结束.



猜你喜欢

转载自blog.csdn.net/BeauXie/article/details/54582719
今日推荐