zoukankan      html  css  js  c++  java
  • Mybatis-学习笔记(3)mapper配置文件

    1、mapper配置文件常用的元素

       

         parameterMap已经废弃,老式风格的参数映射。

    2、select元素

       映射查询语句。#{...}用于预处理语句参数,通过JDBC,这样一个参数在SQL中会由一个“?”来标识,并被传递到一个新的的预处理语句中。

       

       select元素的属性描述如下:

        

        

    3、insert、update、delete

       映射DML语句。与select的配置十分相似。

       

       配置属性除了大部分与select的一致外,特有的部分如下:

       

        如果某些数据库支持生成主键值,且驱动支持获取insert后返回的主键值,则可以设置useGeneratedKeys=“true”,keyProperty=“id”。(id是随意的,主要用来接收返回的主键值)。

        对于不支持自动生成类型的数据库(如oracle)或JDBC驱动不支持,Mybatis还可以通过以下方式生成主键:

         

        上面的示例中,selectKey元素将会先运行,通过查询SEQUENCE序列,得到的序列值会设置到id上。

         

    4、sql

      用来定义可重用的SQL代码段。可以包含在其他语句中,也可以被静态地(在加载参数时)参数化。

      

    5、Parameters

      即parameterType。

     或者

     

    6、ResultMaps

      从结果集中取出的数据转换成开发者所需要的对象。

      

      1》或者转换成我们的POJO。

    <select id="selectAllUser" resultType="com.lfy.bean.User">
        select * from t_user
    </select>

      当POJO,即我们的Java Bean的属性字段与数据库的字段不一致时,需要进行转换(也可以在sql语句中给字段起别名):

    <resultMap id="userResultMap" type="com.lfy.bean.User">
         <!--使用id属性指定主键字段-->
        <id property="id" column="user_id"/>
         <!--使用result属性匹配普通字段-->
         <result property="name" column="user_name"/>
         <result property="sex" column="user_sex"/>
    </resultMap>

        

      2》当表间发生关联,Java Bean中嵌套这另外一个Java Bean时。

          以下是1对1关系,一个学生只对应一个班级,即Student中只有一个Clazz属性。

    <!-- 映射学生对象的resultMap -->
      <resultMap id="studentResultMap" type="com.lfy.bean.Student">
          <id property="id" column="id" />
          <result property="name" column="name"/>
          <result property="sex" column="sex"/>
          <result property="age" column="age"/>
          <!-- 关联映射 -->
          <association property="clazz" column="clazz_id" 
             javaType="com.lfy.bean.Clazz"
             select="selectClazzWithId"/>
        </resultMap>
        
      <!-- 根据班级id查询班级 -->
      <select id="selectClazzWithId" resultType="com.lfy.bean.Clazz">
          SELECT * FROM TB_CLAZZ where id = #{id}
      </select>
      
      <!-- 查询所有学生信息 -->
      <select id="selectStudent" resultMap="studentResultMap">
          SELECT * FROM TB_STUDENT
      </select>

        

        (PS:如果是关联查询,也可以在resultMap中直接将关联表的字段映射到内嵌“内前对象”的对应字段上,如多表关联查询及Java bean中组合了其他bean

    <association property="teacher" javaType="com.lfy.bean.Teacher">
         <id property="id" column="t_id"/>
         <result property="name" column="t_name"/>
    </association>

         反过来,查所有班级,并查询出各班级的所有学生,将会是1对多的关系,即Clazz中有一个List<Student>属性。

    <!-- 映射班级对象的resultMap -->
      <resultMap id="clazzResultMap" type="com.lfy.bean.Clazz">
          <id property="id" column="id" />
          <result property="code" column="code"/>
          <!-- 班级的学生属性,因为一个班级有多个学生,所以该属性是一个集合 -->
          <collection property="students" javaType="ArrayList"
          column="id" ofType="com.lfy.bean.Student" 
          select="selectStudentWithId"/>
        </resultMap>
        
        <!-- 根据班级id查询学生 -->
        <select id="selectStudentWithId" resultType="com.lfy.bean.Student">
              SELECT * FROM TB_STUDENT where clazz_id = #{id}
        </select>
          
        <!-- 查询所有班级信息 -->
      <select id="selectClazz" resultMap="clazzResultMap">
          SELECT * FROM TB_CLAZZ
      </select>

       

  • 相关阅读:
    hibernate建表默认为UTF-8编码
    XML和JSON
    chrome 模拟发送请求的方法
    什么时候需要使用缓存?
    eclipse中查找类、方法及变量被引用的地方
    用户内容与商业
    2019第48周日
    ajax与重定向
    ifream
    Windows下找到JVM占用资源高的线程
  • 原文地址:https://www.cnblogs.com/ZeroMZ/p/11415373.html
Copyright © 2011-2022 走看看