Mybatis中的映射配置

Mybatis中的映射配置(一)

当Mybatis中的实体java类的属性名与数据库表中的列名不一致时,我们可以通过配置的方法来解决。有两种如下方法:
方法一:添加resultMap标签,开发效率比较高。

  <!-- 配置 查询结果的列名和实体类的属性名的对应关系 -->
    <resultMap id="userMap" type="uSeR">
        <!-- 主键字段的对应 -->
        <id property="userId" column="id"></id>
        <!--非主键字段的对应-->
        <result property="userName" column="username"></result>
        <result property="userAddress" column="address"></result>
        <result property="userSex" column="sex"></result>
        <result property="userBirthday" column="birthday"></result>
    </resultMap>

补充: 此处resultMap中的id 只是一个唯一标识,无其他含义,可以任意取名,但是如果采取这种转换方式,只需将select标签中的resultType=“com.zsh.domain.User”>改为resultMap=“userMap”。具体案例如下:

<!--根据queryVo的条件查询用户 -->
    <select id="findUserByVo" parameterType="com.zsh.domain.QueryVo" resultType="com.zsh.domain.User">
    select * from user where username like #{user.username}
    </select>

只需改为

<!-- 根据queryVo的条件查询用户 -->
    <select id="findUserByVo" parameterType="com.zsh.domain.QueryVo" resultMap="userMap">
        select * from user where username like #{user.username}
    </select>

方法二:sql层面修改,用as一一对应。

select id as userId,username as userName,address as userAddress,sex as userSex,birthday as userBirthday from user;
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值