Mybas 传递参数的几种方法
1、传递单个参数。通常单个参数直接写就可以,参数类型可以是 String,也可以是基本类
型,都可以自动识别。
例如:
public String getZdxxId(String zdbh);
---------------------------------------------对应的 xml---------------------------------------------
<select id="getZdxxId" parameterType="String" resultType="String">
select id from zdxx where zdbh = #{zdbh}
</select>
2、传递单个参数,但是参数类型是封装类,java 中不需要增加什么,xml 中增加说明就行。
例如:
public int heart(TermHeart th);
---------------------------------------------对应的 xml---------------------------------------------
<update id="heart" parameterType="com.beiyang.sqjw.bean.TermHeart">
update zdxt set xtsj = #{th.xtsj}, zdbb = #{th.zdbb}
where zdbh = #{th.zdbh}
</update>
3、传递多个参数,我们目前的常用做法是把多个参数都放在 Map 中,然后传递这个 Map
做为参数。例如:
public void insertAreaDivInfor(HashMap map);
---------------------------------------------对应的 xml---------------------------------------------
<insert id="insertAreaDivInfor" parameterType="HashMap" >
insert into xzqh (id,mc,dm,bzm) values(#{eid},#{emc},#{edm},#{ebzm} )
</insert>
这个方法没问题,但我感觉不好,主要有两点,一是没有参数检查,传输的参数名错了,
没法给出提示,只能在调试过程中去找错误;二是有时只传 2 个参数,但也要创建一个
Map,比较麻烦。
4、直接传参数,使用@Param 标注,例如
public int login(@Param("sfzh") String sfzh, @Param("kl") String kl);
---------------------------------------------对应的 xml---------------------------------------------
<select id="login" parameterType="String" resultType="Integer">
select count(*) from yhgl
where sfzh = #{sfzh} and kl = #{kl} and scbz = 0
</select>
可能有人会问,这个@Param 标注到底是做什么的,其实这个标注就是为了传递多个
参数,给参数起名的,如果不用这个标注行不行?其实也行,例子如下:
public int login(String sfzh, String kl);
---------------------------------------------对应的 xml---------------------------------------------
<select id="login" parameterType="String" resultType="Integer">