mybatis 基于xml 配置的映射器

时间:2019-11-27
本文章向大家介绍mybatis 基于xml 配置的映射器,主要包括mybatis 基于xml 配置的映射器使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。

 cache  给命名空间的缓存配置

 cache-ref 其他命名空间缓存配置的引用 

resultMap 描述如何从数据库结果集中来加载对象 

<!--column不做限制,可以为任意表的字段,而property须为type 定义的pojo属性-->
<resultMap id="唯一的标识" type="映射的pojo对象">
  <id column="表的主键字段,或者可以为查询语句中的别名字段" jdbcType="字段类型" property="映射pojo对象的主键属性" />
  <result column="表的一个字段(可以为任意表的一个字段)" jdbcType="字段类型" property="映射到pojo对象的一个属性(须为type定义的pojo对象中的一个属性)"/>
  <association property="pojo的一个对象属性" javaType="pojo关联的pojo对象">
    <id column="关联pojo对象对应表的主键字段" jdbcType="字段类型" property="关联pojo对象的主席属性"/>
    <result  column="任意表的字段" jdbcType="字段类型" property="关联pojo对象的属性"/>
  </association>
  <!-- 集合中的property须为oftype定义的pojo对象的属性-->
  <collection property="pojo的集合属性" ofType="集合中的pojo对象">
    <id column="集合中pojo对象对应的表的主键字段" jdbcType="字段类型" property="集合中pojo对象的主键属性" />
    <result column="可以为任意表的字段" jdbcType="字段类型" property="集合中的pojo对象的属性" /> 
  </collection>
</resultMap>

sql 可被其他语句引用的可重用语句块 

sql id="sqlid">
    res_type_id,res_type
</sql>

<select id="selectbyId" resultType="com.property.vo.PubResTypeVO">
    select
    <include refid="sqlid"/>
    from pub_res_type
</select>

insert 映射插入语句 

 <insert id="upateUserById" parameterType="User" >  
        insert into user (name,age) values (#{name},#{age})
  </insert>

update 映射更新语句 

 <updateid="upateUserById" parameterType="User" >  
        update user set username=#{username},age=#{age}
        where id=#{id}
  </update>

delete 映射删除语句 

 <delete id="delUserById" parameterType="int" >  
        delete from user 
        where id=#{id}
  </delete>

select 映射查询语句 

  <select id="getUserById" parameterType="int" resultType="User">  //或使用resultMap    resultType 用类的全名称 或别名
        select * from user 
        where id=#{id}
  </select>

原文地址:https://www.cnblogs.com/qin1993/p/11941674.html