通过对象传参
< insert id= "insertEmp" parameterType= "org.zhang.pojo.Emp" >
insert into emp ( ename, job, hiredate, sal) values ( #{ ename} , #{ job} , #{ hiredate} , #{ sal} ) ;
< selectKey resultType= "int" keyColumn= "empno" keyProperty= "empno" order= "AFTER" >
select last_insert_id ( )
< / selectKey>
< / insert>
方式1:顺序传参法
< ! -- 此时因为有多个参数 所以参数类型不能逐一制定 可以省略 有mybatis自动匹配-- >
< update id= "updateEmpByNo2" >
update emp set ename= #{ param1} where empno= #{ param2}
< / update>
方式2:@Param注解传参法
public void updateEmpByNo2 ( @Param ( "name" ) String ename, @Param ( "no" ) Integer empno) throws IOException;
< update id= "updateEmpByNo2" >
update emp set ename= #{ name} where empno= #{ no}
< / update>
方式3:Map传参法
public void updateEmpByNo3 ( Map< String, Object> argMap) throws IOException;
< update id= "updateEmpByNo3" parameterType= "hashmap" >
update emp set ename= #{ name} where empno= #{ no}
< / update>
public void updateByNo2Test ( ) throws IOException {
SqlSession session = MBUtils. getSession ( ) ;
EmpMapper mapper = session. getMapper ( EmpMapper. class ) ;
Map< String, Object> argMap = new HashMap < > ( ) ;
argMap. put ( "name" , "赵四" ) ;
argMap. put ( "no" , 8899 ) ;
mapper. updateEmpByNo3 ( argMap) ;
session. commit ( ) ;
MBUtils. closeSession ( ) ;
}
方式4:Java Bean传参法
public User selectUser ( Emp emp) ;
2
3 < select id= "selectUser" parameterType= "org.zhang.pojo.Emp" resultMap= "Emp" >
4 select * from user
5 where user_name = #{ userName} and dept_id = #{ deptId}
6 < / select>