MyBatis的parameterType传⼊参数类型
在mybatis映射接⼝的配置中,有select,insert,update,delete等元素都提到了parameterType的⽤法,parameterType为输⼊参数,在配置的时候,配置相应的输⼊参数类型即可。parameterType有基本数据类型和复杂的数据类型配置。
parameter数据类型1. MyBatis的传⼊参数parameterType类型分两种
1. 1. 基本数据类型:int、string、long、Date;
1. 2. 复杂数据类型:类(JavaBean、Integer等)和Map
2. 如何获取参数中的值:
2.1 基本数据类型:#{参数} 获取参数中的值
2.2 复杂数据类型:#{属性名} ,map中则是#{key}
3.案例:
3.1 传⼊Long型
mapper接⼝代码:
public User findUserById(Long id);
xml代码:
<select id="findUserById" parameterType="java.lang.Long" resultType="User">
select * from user where id = #{id};
</select>
3.2 传⼊List
mapper接⼝代码:
public List<User> findUserListByIdList(List<Long> idList);
xml代码:
<select id="findUserListByIdList" parameterType="java.util.ArrayList" resultType="User">
select * from user user
<where>
user.ID in (
<foreach collection="list" item="id" index="index" separator=",">
#{id}
</foreach>
)
</where>
</select>
在使⽤foreach的时候最关键的也是最容易出错的就是collection属性。
该属性是必须指定的,但是在不同情况下,该属性的值是不⼀样的,主要有⼀下3种情况:
1. 如果传⼊的是单参数且参数类型是⼀个List的时候,collection属性值为list
2. 如果传⼊的是单参数且参数类型是⼀个array数组的时候,collection的属性值为array
3. 如果传⼊的参数是多个的时候,我们就需要把它们封装成⼀个Map了,当然单参数也可
3.3 传⼊数组:
mapper接⼝代码:
public List<User> findUserListByIdList(int[] ids);
xml代码:
<select id="findUserListByIdList" parameterType="java.util.HashList" resultType="User">
select * from user user
<where>
user.ID in (
<foreach collection="array" item="id" index="index" separator=",">
#{id}
</foreach>
)
</where>
</select>
3.4 传⼊map
mapper接⼝代码:
public boolean exists(Map<String, Object> map);
xml代码:
<select id="exists" parameterType="java.util.HashMap" resultType="java.lang.Integer">
SELECT COUNT(*) FROM USER user
<where>
<if test="code != null">
and user.CODE = #{code}
</if>
<if test="id != null">
and user.ID = #{id}
</if>
<if test="idList !=null ">
and user.ID in (
<foreach collection="idList" item="id" index="index" separator=",">
#{id}
</foreach>
)
</if>
</where>
</select>
MAP中有list或array时,foreach中的collection必须是具体list或array的变量名。
⽐如这⾥MAP含有⼀个名为idList的list,所以MAP中⽤idList取值,这点和单独传list或array时不太⼀样。
3.5传⼊JAVA对象
mapper接⼝代码:
public int findUserList(User user);
xml代码:
<select id="findUserList" parameterType="User" resultType="java.lang.Integer">
SELECT COUNT(*) FROM USER user
<where>
<if test="code != null">
and user.CODE = #{code}
</if>
<if test="id != null">
and user.ID = #{id}
</if>
<if test="idList !=null ">
and user.ID in (
<foreach collection="idList" item="id" index="index" separator=",">
#{id}
</foreach>
)
</if>
</where>
</select>
JAVA对象中有list或array时,foreach中的collection必须是具体list或array的变量名。
⽐如这⾥User含有⼀个名为idList的list,所以User中⽤idList取值,这点和单独传list或array时不太⼀样。
3.6注解@Param
例⼦1:
注解单⼀属性;这个类似于将参数重命名了⼀次
mapper接⼝代码:
List<User> selectUserByTime(@Param(value="startdate")String startDate);
xml代码:
<select id="selectUserByTime" resultType="User" parameterType="java.lang.String">
select * from t_user
where create_time >= to_date(#{startdate,jdbcType=VARCHAR},'YYYY-MM-DD')
</select>
例⼦2:
注解javaBean
mapper接⼝代码:
List<User> selectUserByTime(@Param(value="dateVO")DateVO dateVO);
xml代码:
<select id="selectUserByTime" resultType="User" parameterType="DateVO">
select *
from t_user
where create_time >= to_date(#{dateVO.startDate,jdbcType=VARCHAR},'YYYY-MM-DD') and create_time < to_date(#{dDate,jdbcType=VARCHAR},'YYYY-MM-DD') </select>
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论