mybatis深入学习

1、select
id:必填,不可重复–和mapper对应
resultMap:select查询语句返回的类型和映射关系
resultType:
parameterType:

select可以有两种结果映射方式:
一、以resultMap返回结果映射,resultMap中配置property和column完成映射。
例子:

<resultMap id="FolderResultMap" type="com.suning.cybertron.superion.model.FolderRes">
        <result column="folder_id" property="folderId" jdbcType="INTEGER" />
        ...
</resultMap>

<select id="queryFolderBySystemIdAndIndex"  resultMap="FolderResultMap"></select>

二、以resultType返回结果映射:需要设置别名,达到与Javabean中字段一致,从而完成映射。如果是Map类型就无所谓。

<select id="" resultType="JavaBean/Map/Integer/String"        parameterType="java.util.Map">

2、resultMap的属性:
- id:必填,唯一
- type:必填,用于查询列所映射到的Java对象类型
- extends:选填,可以配置当前的resultMap继承其他的resultMap,属性值为继承resultMap的id值。
- autoMapping:选填,true或false,用于配置是否启用非映射字段(没有在resultMap中配置的字段)的自动映射功能。可以覆盖全局autoMappingBehavior配置
resultMap的标签
constructor:配置使用构造方法注入结果

<resultMap id="" type="" extends="" autoMapping="">

  <id column="数据库中的列名/select中的别名" 
     property="JavaBean中的字段" 
     jbdcType="数据库中的对应的类型"/>


  <constructor>
    <idArg></idArg>
    <arg></arg>
  </constructor>
  <assocation property="" javaType="映射结果的类型">
      <id column="" property="" jdbcType=""/>
      <result column="" property="" jdbcType=""/>
  </assocation>
  <collection property="" ofType="" javaType="">
      <id column="" property="" jdbcType=""/>
      <result column="" property="" jdbcType=""/>
  </collection>

</resultMap>

2、insert
返回主键两种方式:
(1)使用JDBC方式返回自增主键(适合数据库提供自增主键)

<insert id="insert" useGeneratedKeys="true" keyProperty="id"> 

(2)使用selectKey方式返回主键(适用于数据库不提供/提供自增主键)

<insert id="insert2">
    ...
    <selectKey keyColumn="id" resultType="long" keyProperty="id"
               order="AFTER">
          SELECT LAST_INSERT_ID()
    </selectKey>           
</>

3、传参数有多个时的用法
参数类型分为:
(1)基本类型:1个参数
(2)javaBean:存在与表作映射的实体类中
(3)使用Map的方式
(4)使用注解@Param(“systemId”) Integer systemId

4、foreach用法
关于collection中的值问题:原理都是转为Map类型,添加key值为list或array。
如果参数为集合类型,则为list;
如果参数为数组类型,则为array;

<foreach collection="list/array" open="("  separator="," close=")" item="item" index="i"></foreach>

此中的item是遍历的元素值,index指遍历的索引值。

4.1.foreach实现批量update
参数为Map类型时:

<foreach collection="_parameter" item="val" index="key" separator=",">
    ${key} = #{val}
</foreach>
此中的index是指map的key,item指的是value

5、bind用法
连接字符串:与concat区别是:concat在mysql中支持多个参数,但是oracle中只支持两个。而bind通用,都可以支持多个。

<bind name="userNameLike " value=" '%'+userName+ '%'">
  and user_name like #{userNameLike}
</bind>

6、关系映射配置

(1)一对一:
1.1:使用自动映射处理一对一关系(利用bean.字段)

  <select id="" resultType="model...">
    select u.id ,r.id role.id from sys_user u inner join sys_user_role ur on u.id = ur.user_id
    inner join sys_role r on r.id = ur.role_id
  </select>

1.2:使用resultMap配置一对一映射

<resultMap id="userRoleMap" type="..sysUser">
    <id property="id" column="id"/>
    <result property="" column=""/>
    ...
    <!--role相关属性 -->
    <result property="role.id" column="role_id"/>
    ...
</resultMap>

<select id="" resultMap="userRoleMap">
    select...
</select>

1.3:使用resultMap的association标签配置一对一映射:

<resultMap id="userRoleMap" extends="userMap" type="...SysUser">
    <association property="role" javaType="...SysRole">
        <result property="" column="" jdbcType=""/>
        ...
    </association>
</resultMap>

猜你喜欢

转载自blog.csdn.net/lwl20140904/article/details/80967954