drop procedure if exists update_user_by_id;
delimiter $$
create procedure update_user_by_id (
IN p_id int,
IN p_name varchar(18),
IN p_sex varchar(19),
IN p_age int
)
begin
update tb_user set name=p_name,sex=p_sex,age=p_age
where id=p_id;
end
$$
delimiter ;
dropprocedure if exists update_user_by_id; delimiter $$ createprocedure update_user_by_id ( IN p_id int, IN p_name varchar, IN p_sex varchar(19), IN p_age int ) begin update tb_user set name=p_name,sex=p_sex,age=p_age where id=p_id; end $$ delimiter ;
错误提示如下:
1 2 3 4
1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ' IN p_sex varchar(19), IN p_age int )
dropprocedure if exists select_user_by_id; delimiter $$ createprocedure select_user_by_id(IN in_id INTEGER) begin select id,name,sex,age from tb_user where id=in_id; end $$ delimiter ;
dropprocedure if exists insert_user; DELIMITER $$ CREATEPROCEDURE insert_user ( OUT v_id int, in v_name varchar(18), in v_sex varchar(19), in v_age int ) BEGIN INSERTINTO tb_user (name,sex,age) VALUES (v_name,v_sex,v_age); SET v_id=LAST_INSERT_ID(); END $$ DELIMITER ;
<select id="seletEmployeeInIdList" resultType="domain.Employee"> select * from tb_employee where id in <foreach item="item" index="index" collection="list" open="(" separator="," close=")"> #{item} </foreach> </select>
DEBUG [main] ==> Preparing: select * from tb_employee WHERE id=? and loginname=? and password=?
DEBUG [main] ==> Parameters: 1(Integer), xiaowang(String), xiaowang(String)
DEBUG [main] <== Total: 0
此时我们设置了id,loginname,password这三个参数,这三个参数满足where标签中的if标签的条件,则Mybatis会生成where子句,并将if标签体中的代码作为where子句的条件. 得到的SQL语句为:select * from tb_employee WHERE id=? and loginname=? and password=?
<!-- 测试choose标签 --> <selectid="selectEmployeeChoose"parameterType="hashmap" resultType="org.fkit.domain.Employee"> SELECT * FROM tb_employee WHERE state = 'ACTIVE' <!-- 如果传入了id,就根据id查询,没有传入id就根据loginname和password查询,否则查询sex等于男的数据 --> <choose> <whentest="id != null"> and id = #{id}</when> <whentest="loginname != null and password != null"> and loginname = #{loginname} and password = #{password} </when> <otherwise> and sex = '男'</otherwise> </choose> </select>