Mybatis-映射数据注意点<association><collecation> + 使用技巧

如果需要映射的对象中存在另一个对象和集合,那么在映射时应该用association在collection标签之前书写

例子:

public class UserEntity implements Serializable{
	
    private String userId;
    private String userNumber;
    private List<RoleEntity> roleList;
    private Depart depart;
<mapper namespace="com.mapper.UserEntityMapper">
  <resultMap id="BaseResultMap" type="com.entity.UserEntity">
    <id column="USER_ID" jdbcType="VARCHAR" property="userId" />
    <result column="USER_NUMBER" jdbcType="VARCHAR" property="userNumber" />
    ...
    ...
    <association property="depart" javaType="com.entity.Depart">
          <id column="UID" property="uid"/>
          <result column="depart_name" property="departName"/>
          <result column="depart_code" property="departCode"/>
          <result column="remarker" property="remarker"/>
      </association>
    <collection property="roleList" ofType="com.entity.RoleEntity">
        <id property="roleId" column="role_id" />
        <result property="roleName" column="role_name"/>
        <collection property="permissionList" ofType="com.foxconn.entity.PermissionEntity">      
          <id column="per_id" jdbcType="VARCHAR" property="perId" />
          <result column="per_name" jdbcType="VARCHAR" property="perName" />
          <result column="per_url" jdbcType="VARCHAR" property="perUrl" />
          <result column="per_description" jdbcType="VARCHAR" property="perDescription" />
        </collection>
    </collection>
	  
  </resultMap>

使用技巧: 《sql标签的使用》

<sql id="Base_Column_List">
    USER_ID, USER_NUMBER, NICKNAME, PWD, PHONE, EMAIL, USER_STATUS, depart_id, 
    LAST_LOGINTIME
</sql>
<select id="getUserByNickname" parameterType="java.lang.String" resultMap="BaseResultMap">
    select <include refid="Base_Column_List" />
    from T_USER
</select>

《bind标签的使用》

使用 con cat 函数连接字符串,在 MySQL 中,这个函数支持多个参数,但在 Oracle 中只
支持两个参数。由于不 同数据库之间的语法差异 ,如果更换数据库,有些 SQL 语句可能就需要
重写。针对这种情况,可 以使用 bind 标签来避免由于更换数据库带来的一些麻烦。将上面的
方法改为 bind 方式后,代码如下。

<if test=” userNarne != null and userNarne !=””>
    <bind narne= "userNarneLike" value = " '草'+ userNarne + '每' "/>
    and user name like #{userNarneLike}
</if>

bind 标签的两个属性都是必选项, name 为绑定到上下文的变量名, value 为 OGNL 表
达式。创建一个 bind 标签的变量后 , 就可以在下面直接使用,使用 bind 拼接字符串不仅可
以
1.避免因更换数据库而修改 SQL;
2.也能预防 SQL 注入。

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值