2 MyBatis Mapper 详解

1 Mapper.xml

  • statement 标签:select、update、delete、insert 分别对应查询、修改、删除、添加操作
  • parameterType:参数数据类型

1、基本数据类型。如:通过 id 查询 Account

<select id="findById" parameterType="long" resultType="com.southwind.entity.Account">
    select * from t_account where id = #{id}
</select>

2、String 类型。如:通过 name 查询 Account

<select id="findByName" parameterType="java.lang.String" resultType="com.southwind.entity.Account">
	select * from t_account where id = #{id}
</select>
<!-- 可以直接写 resultType="String" -->

3、包装类。如:通过 id 查询 Account

<select id="findById2" parameterType="java.lang.Long" resultType="com.southwind.entity.Account">
	select * from t_account where id = #{id}
</select>

4、多个参数。如:通过 name 和 age 查询 Account

<select id="findByNameAndAge" resultType="com.southwind.entity.Account">
	select * from t_account where username = #{arg0} and age = #{arg1}
</select>
<!-- 或者可写为:select * from t_account where username = #{param1} and age = #{param2} -->

5、Java Bean

<update id="update" parameterType="com.southwind.entity.Account">
    update t_account set username = #{username},password=#{password},age=#{age}
</update>
  • resultType:结果类型

1、基本数据类型。如:统计 Account 总数

<select id="count" resultType="int">
    select count(id) from t_account
</select>

2、包装类。如:统计 Account 总数

<select id="count" resultType="java.lang.Integer">
    select count(id) from t_account
</select>

3、String 类型。如:通过 id 查询 Account 的 name

<select id="findNameById" resultType="java.lang.String">
    select username from t_account where id = #{id}
</select>
<!-- 可以直接写 resultType="String" -->

4、Java Bean

<select id="findById" parameterType="long" resultType="com.southwind.entity.Account">
    select * from t_account where id = #{id}
</select>


版权声明:本文为qq_52077925原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。