MyBatis 常识

来源:互联网 发布:网络与端口有何区别 编辑:程序博客网 时间:2024/05/21 08:16

1:当实体类中的属性名和表中的字段名不一致时?

使用MyBatis进行查询操作时无法查询出相应的结果的问题以及针对问题采用的两种办法:

方法一: 通过在查询的sql语句中定义字段名的别名

让字段名的别名和实体类的属性名一致,这样就可以表的字段名和实体类的属性名一一对应上了,这种方式是通过在sql语句中定义别名来解决字段名和属性名的映射关系的。


方法二: 通过<resultMap>来映射字段名和实体类属性名的一一对应关系

这种方式是使用MyBatis提供的解决方式来解决字段名和属性名的映射关系的。


例:

<mapper namespace="me.mybat.mapping.orderMapper">
      <select id="selectOrder" parameterType="int"
         resultType="Order">
         select order_id id, order_no orderNo,order_price price from orders where order_id=#{id}
     </select>
     
     
      <!--
         根据id查询得到一个order对象,使用这个查询是可以正常查询到我们想要的结果的,
         这是因为我们通过<resultMap>映射实体类属性名和表的字段名一一对应关系
      -->
      <select id="selectOrderResultMap" parameterType="int" resultMap="orderResultMap">
            select * from orders where order_id=#{id}
      </select>
      <resultMap type="Order" id="orderResultMap">
           <!-- 用id属性来映射主键字段 -->
          <id property="id" column="order_id"/>
         <!-- 用result属性来映射非主键字段 -->
          <result property="orderNo" column="order_no"/>
          <result property="price" column="order_price"/>
      </resultMap>
</mapper>



2:使用MyBatis对表执行CRUD操作

方法一:基于XML的实现


方法二:基于注解的实现


在conf.xml文件中注册这个xml文件或映射接口

     <mappers>
         <!-- 注册userMapper.xml文件,
         userMapper.xml位于me.mybat.mapping这个包下,所以resource写成me/mybat/mapping/userMapper.xml-->
         <mapper resource="me/mybat/mapping/userMapper.xml"/>

         
          <!-- 注册UserMapper映射接口-->
         <mapper class="me.mybat.mapping.UserMapperI"/>
     </mappers>



3:为实体类定义别名,简化sql映射xml文件中的引用

之前,我们在sql映射xml文件中的引用实体类时,需要写上实体类的全类名(包名+类名),如下:

<!-- 创建用户(Create) -->
<insert id="addUser" parameterType="me.mybat.entity.User">
    insert into users(name,age) values(#{name},#{age})
</insert>

每次都写这么一长串内容挺麻烦的,而我们希望能够简写:

(1)单独为某一个实体类设置别名

在conf.xml文件中<configuration></configuration>标签中添加如下配置:

<typeAliases>
    <typeAlias type="me.mybat.entity.User" alias="_User"/>
</typeAliases
>


(2)批量为某个包下的所有实体类设置别名

      <!-- 配置实体类的别名,配置实体类别名的目的是为了在引用实体类时可以使用实体类的别名来代替实体类,达到简写的目的 -->
       <typeAliases>
            <!-- 为me.mybat.entity包下的所有实体类配置别名,MyBatis默认的设置别名的方式就是去除类所在的包后的简单的类名
                比如me.mybat.entity.User这个实体类的别名就会被设置成User
         -->
        <package name="me.mybat.entity"/>
     </typeAliases>

    

4:实现关联表查询

(1)一对一关联

MyBatis中使用association标签来解决一对一的关联查询,association标签可用的属性如下:

property:对象属性的名称
javaType:对象属性的类型
column:所对应的外键字段名称
select:使用另一个查询封装的结果

  <!--
     方式一:嵌套结果:使用嵌套结果映射来处理重复的联合结果的子集
             封装联表查询的数据(去除重复的数据)
         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.mybat.entity.Classes" id="ClassResultMap">
          <id property="id" column="c_id"/>
          <result property="name" column="c_name"/>
          <association property="teacher" javaType="me.mybat.entity.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.mybat.entity.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.mybat.entity.Teacher">
         SELECT t_id id, t_name name FROM teacher WHERE t_id=#{id}
      </select>


(2)一对多关联

MyBatis中使用collection标签来解决一对多的关联查询,ofType属性指定集合中元素的对象类型。


  <!--
        根据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.mybat.entity.Classes" id="ClassResultMap3">
        <id property="id" column="c_id"/>
        <result property="name" column="c_name"/>
         <association property="teacher" column="teacher_id" javaType="me.mybat.entity.Teacher">
             <id property="id" column="t_id"/>
             <result property="name" column="t_name"/>
         </association>
         <!-- ofType指定students集合中的对象类型 -->
         <collection property="students" ofType="me.mybat.entity.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="Classes" id="ClassResultMap4">
                <id property="id" column="c_id"/>
                <result property="name" column="c_name"/>
                
                <association property="teacher" column="teacher_id" javaType="Teacher" select="getTeacher2"></association>
                <collection property="students" ofType="Student" column="c_id" select="getStudent"></collection>
             </resultMap>
              <select id="getTeacher2" parameterType="int" resultType="Teacher">
                SELECT t_id id, t_name name FROM teacher WHERE t_id=#{id}
             </select>
              <select id="getStudent" parameterType="int" resultType="Student">
                SELECT s_id id, s_name name FROM student WHERE class_id=#{id}
             </select>
    



5:MyBatis的动态SQL查询几种标签



http://pan.baidu.com/s/1mhZBNUo  密码:aquo  (MyBatis中文文档)











0 0
原创粉丝点击