zoukankan      html  css  js  c++  java
  • mybatis 学习

    学习文档地址:

    http://www.mybatis.org/mybatis-3/zh/sqlmap-xml.html#Result_Maps

    resultMap

    • assocation(关联)子标签:标签中的属性 property对应实体类中的属性,javaType对应要关联的类,resultMap对应用使用resultMap的id。其子元素和resultMap类似。

    返回结果的时候,没有映射返回null。

    使用场景例:查看某个教师的某个学生的信息,在原来的教师实体类添加一个学生属性即可,对应的即是sql的关联查询。

      <resultMap id="teacher" type="Teacher">
       	<result column="teacher_id" property="teacherId" jdbcType="BIGINT" />
        <result column="teacher_name" property="teacherName" jdbcType="VARCHAR" />
        <association property="sdutent" javaType="Sdutent">
        	<result column="sdutent_id" property="sdutentId" jdbcType="BIGINT" />
        	<result column="sdutent_name" property="sdutentName" jdbcType="VARCHAR" />
        </association>
      </resultMap>
    

     也可以这样

    <resultMap id="teacher" type="Teacher">
       <result column="teacher_id" property="teacherId" jdbcType="BIGINT" />
       <result column="teacher_name" property="teacherName" jdbcType="VARCHAR" />
       <association property="sdutent" resultMap="sdutent" />
    </resultMap>
      
    <resultMap type="Sdutent" id="sdutent">
      <result column="sdutent_id" property="sdutentId" jdbcType="BIGINT" />
      <result column="sdutent_name" property="sdutentName" jdbcType="VARCHAR" />
    </resultMap>
    • 构造器 constructor 

    在指定类里面添加指定构造器,然后在constructor的标签下增加对用的配置,作用给构造器里面指定的参数赋值。

    例:

    <resultMap id="teacher" type="Teacher">
    <constructor><idArg column="teacherName" javaType="string"/></constructor> <result column="teacher_id" property="teacherId" jdbcType="BIGINT" /> </resultMap>

    对应的类中添加

    public(String teacherName){this.teacherName=teacherName}
    

    注:顺序要写在resultMap的第一行。也可以像assocation一样使用标签属性resultMap指定映射集

    collection集合:

    使用场景:查询指定老师的信息及其下所有的学生信息

      <resultMap id="teacher" type="Teacher">
        <result column="teacher_id" property="teacherId" jdbcType="BIGINT" />
        <result column="teacher_name" property="teacherName" jdbcType="VARCHAR" />
        <collection property="sdutentList" OfType="Sdutent">
        	<result column="sdutent_id" property="sdutentId" jdbcType="BIGINT" />
        	<result column="sdutent_name" property="sdutentName" jdbcType="VARCHAR" />
        </collection>
      </resultMap>
    

      

     动态sql

    • if
    <if test="title != null">
        title like #{title}
    </if>
    
    • choose, when, otherwise

      <choose>
        <when test="title != null">
          AND title like #{title}
        </when>
        <when test="author != null and author.name != null">
          AND author_name like #{author.name}
        </when>
        <otherwise>
          AND featured = 1
        </otherwise>
      </choose>
    
    • trim

    <trim suffix="WHERE" suffixOverrides="AND | OR">  
        <if test="id != null and id !='' ">  
            AND b.id =#{id}   
        </if>  
        <if test="name != null">  
            AND b.menu_name like #{name}  
        </if>  
    </trim>  

    属性 suffix和suffixOverrides 以及 prefix和prefixOverrides,分别是后缀和前缀,其中suffix和prefix分别设置最后和最前

    • foreach
    <foreach item="item" index="index" collection="list" open="(" separator=","close=")">
       #{item}
    </foreach>

    注:动态sql以及普通的传值平杰sql,使用非实体类,map,jsonObject传参时,需要在dao层为每个参数使用@Param("*")起别名,然后再mapper.xml中使用;否则会报There is no getter for property named 'id' in 'class java.lang.Integer'类似的错误。

  • 相关阅读:
    第六次实验报告
    第三次实验报告
    第五张循环语句总结
    第二次实验报告
    第一次实验报告
    第一次作业
    第九章 结构体与共用体
    第八章 指针实验
    第七章数组实验
    第六章实验报告(2)
  • 原文地址:https://www.cnblogs.com/mao-yan/p/8127016.html
Copyright © 2011-2022 走看看