mybatis的paramType

MyBatis传多个参数:
1、采用#{0},#{1}获得参数:
Dao层函数方法:
public User selectUser(String name,String area);
对应的Mapper.xml
<select id="selectUser" resultMap="BaseResultMap">
select * from user where user user_name=#{0} and user_area=#{1}
</select>
2、采用Map传递参数:
Dao层的函数方法:
public User selectUser(Map paramMap);
对应的Mapper.xml
<select id="selectUser" resultMap="BaseResultMap">
select * from user where user_name=#{userName,jdbcType=VARCHAR}
and user_area = #{userArea,jdbctType=VARCHAR}
</select>
Service层调用:
private User xxSelectUser(){
Map paramMap = new HashMap();
paramMap.put("userName","xxx");
paramMap.put("userArea","xxxx");
User user = xxx.selectUser(paramMap);
}
3、采用@param("name") String name
Dao层的函数方法:
public User selectUser(@param("userName") String name,@param("userArea") String area);
对应的Mapper.xml
<select id="selectUser" resultMap="BaseResultMap">
select * from user where user_name=#{userName,jdbcType=VARCHAR} and
user_area = #{userArea,jdbcType=VARCHAR}
</select>
MyBatis传入参数与ParameterType:
1、简单数据类型
mapper接口方法:
User selectByPrimaryKey(Integer id);
sql映射:
<select id="selectByPrimaryKey" resultMap="BaseResultMap" parameterType="java.lang.Integer" >
select <include refid = "Base_Colum_List">
from user where id = #{id,jdbcType=INTEGER}
</select>
<select id="selectByPrimaryKey" resultMap="BaseResultMap" parameterType="java.lang.Integer" >
select
<include refid="Base_Column_List" />
from tb_user
<if test="_parameter != 0">
where id = #{id,jdbcType=INTEGER}
</if>
</select>
2、对象类型:
mapper 接口方法:
int insert(User user);
sql映射:
<insert id="insert" parameterType="User" useGeneratedKeys="true" keyProperty="id">
insert into tb_user (name, sex) values (#{name,jdbcType=CHAR}, #{sex,jdbcType=CHAR})
3、map类型
传入map类型,直接通过#{keyname}就可以引用到键对应的值。使用@param注释的多个参数值也会组装成一个map数据结构,和直接传递map进来没有区别。
mapper接口:
int updateByExample(@Param("user") User user, @Param("example") UserExample example);
sql映射:
<update id="updateByExample" parameterType="map" >
update tb_user
set id = #{user.id,jdbcType=INTEGER},
...
<if test="_parameter != null" >
<include refid="Update_By_Example_Where_Clause" />
</if>
4、集合类型:
集合类型通常用于构造IN条件,sql映射文件中使用foreach元素来遍历List或Array元素。
mapper接口:
User selectUserInList(List<Interger> idlist);
sql映射文件:
<select id="selectUserInList" resultType="User">
SELECT * FROM USER WHERE ID in
<foreach item="item" index="index" collection="list"
open="(" separator="," close=")"> #{item}
</foreach>
</select>
5、对象类型中的集合属性:
对于单独传递的List或Array,在SQL映射文件中映射时,只能通过list或array来引用。但是如果对象类型有属性的类型为List或Array,则在sql映射文件的foreach元素中,可以直接使用属性名字来引用。
mapper接口:
List<User> selectByExample(UserExample example);
sql映射文件:
<foreach collection="oredCriteria" item="criteria" separator="or" >
<if test="criteria.valid" >
在这里,UserExample有一个属性叫oredCriteria,其类型为List,所以在foreach元素里直接用属性名oredCriteria引用这个List即可。item="criteria"表示使用criteria这个名字引用每一个集合中的每一个List或Array元素
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值