MyBatis Mapper详解

MyBatis Mapper详解

Mapper.xml
  • ⭐statement标签:select、update、delete、insert分别对应查询、更新、删除、插入操作
  • ⭐parameterType:参数数据类型
  1. 基本数据类型,通过id查询Account
    <select id="findById" parameterType="long" resultType="com.oyrf.frist.Account">
        select * from t_account where id = #{id}
    </select>
  1. String类型,通过name查询Account
    <select id="findByName" parameterType="java.lang.String" resultType="com.oyrf.frist.Account">
        select * from t_account where username = #{username}
    </select>
  1. 包类型,通过id查询Account
    <select id="findById2" parameterType="java.lang.Long" resultType="com.oyrf.frist.Account">
        select * from t_account where id = #{id}
    </select>
  1. 多个参数,通过name和age查询Account
    可能会报以下错误:
### Error querying database.  Cause: org.apache.ibatis.binding.BindingException: Parameter 'username' not found. Available parameters are [arg1, arg0, param1, param2]

原因是输入参数的时候不可以用#{username}和#{age}

    <select id="findByNameAndAge" resultType="com.oyrf.frist.Account">
        select * from t_account where username = #{username} and age = #{age}
    </select>

应该用arg1, arg0, param1, param2

    <select id="findByNameAndAge" resultType="com.oyrf.frist.Account">
        select * from t_account where username = #{param1} and age = #{param2}
    </select>
  1. Java Bean
    <update id="update" parameterType="com.oyrf.frist.Account">
        update t_account set username = #{username},password = #{password},age = #{age} where id = #{id}
    </update>
  • ⭐resultType:结果类型
  1. 基本数据类型,统计Account总数
    <select id="count" resultType="int">
        select count(id) from t_account
    </select>
  1. 包装类,统计Accoun总数
    <select id="count2" resultType="java.lang.Integer">
        select count(id) from t_account
    </select>
  1. String类型,通过id查询Account的name
    <select id="findNameById" parameterType="long" resultType="java.lang.String">
        select username from t_account where id = #{id}
    </select>
  1. Java bean
<select id="findById" parameterType="long" resultType="com.oyrf.frist.Account">
        select * from t_account where id = #{id}
    </select>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

OYBox

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值