1.一个参数
//一个值的查询 Student find01(String id); //接口
<!-- 当数据库列名和实体类属性名不一样的时候不能使用resultType返回数据 当输入参数为一个的时候 取值的名字可以随便写 #{} ${}取值可以随便写吗?可以只是不能写特殊符号
区别
#{} 先生成SQL,再传值也就是 jdbc中的 ?
${}先放值后生成sql语句!!(很危险,如果放入1=1永远成立。俗称sql注入) --> <select id="find01" parameterType="string" resultMap="stuMap"> select stu_id,stu_name from student where stu_id=#{value } </select>
<resultMap id="stuMap" type="com.sun.pojo.Student"> <id property="stuId" column="stu_id"></id> <result property="stuName" column="stu_name"></result> </resultMap>
2.多个参数
Student find02(int id, String name);
<!-- 多参数查询 mybtis 把参数放到数组中
按下标取值:
arg0 表示取第一个参数,
arg1 表示取第二参数。
按参数位置:
param1 表示第一个参数
param2 按第二个参数 --> <select id="find02" resultMap="stuMap"> SELECT stu_id,stu_name from student where stu_id=#{arg0} and stu_name=#{arg1} </select>
<select id="find02" resultMap="stuMap"> SELECT stu_id,stu_name from student where stu_id=#{param1} and stu_name=#{param2} </select>
3.多参数注解
Student find03(@Param("id") int id, @Param("name") String name);
<select id="find03" resultMap="stuMap"> SELECT stu_id,stu_name from student where stu_id=#{id} and stu_name=#{name} </select>
多参数推荐使用注解 4.50个参数怎么办
//有50个参数怎么办?使用数组 List<Student> find03(int []id);
<select id="find03" resultMap="stuMap"> SELECT stu_id,stu_name from student where stu_id in <foreach collection="arg0" item="item" open="(" close=")" separator="," index="index"> #{item} </foreach> </select>
5.多参数多类型
List<Student> find04(@Param("id") int []id,@Param("sex") String sex);
<select id="find04" resultMap="stuMap"> SELECT stu_id,stu_name from student where stu_id in <foreach collection="id" item="item" open="(" close=")" separator="," index="index"> #{item} </foreach> and stu_sex=#{sex} </select>
————————————————————————————————————————
2个整形一个对象
List<Student>findAll(@Param("page") int page,@Param("row") int row,@Param("stu") Student stu);
<select id="findAll" resultMap="stuMap"> select stu_id,stu_name,stu_age,stu_sex,stu_brth,stu_idcard,cls_id from student <where> <if test="stu !=null "> <if test="stu.stuName != null and stu.stuName !='' "> and stu_name=#{stu.stuName} </if> <if test="stu.stuAge != null and stu.stuAge != ''"> and stu_age=#{stu.stuAge} </if> <if test="stu.stuSex !=null and stu.stuSex !=''"> and stu_sex=#{stu.stuSex} </if> </if> </where> limit #{page},#{row} </select>
6.map传值:
不推荐参数没语义,
通过看参数不知道需要什么值,必须看sql才清楚具体值。
取值用map的key取就可以了
//用map传值 List<Student> find05(Map<String,Object> map);
|