mybatis的动态sql
1.if 标签
注意:where 1=1是为了避免当uId不传值时,会导致生成bad sql
<select id="getStuByIf" resultType="com.itheima.pojo.TbStudent" parameterType="com.itheima.pojo.TbStudent">
select * from tb_student where 1=1
<if test="uId!=null and uId!=‘‘ ">
and u_id=#{uId}
</if>
<if test="uName!=null and uName!=‘‘">
and u_name=#{uName}
</if>
<if test="sex!=null and sex!=‘‘">
and sex=#{sex}
</if>
<if test="tId!=null and tId!=‘‘">
and t_id=#{tId}
</if>
</select>TbStudent student = new TbStudent();
// student.setuId(2);
student.setuName("lisi");
student.setSex("男");
List<TbStudent> stu = tbStudentMapper.getStuByIf(student);
System.out.println(stu); Preparing: select * from tb_student where 1=1 and u_name=? and sex=?2.where 标签
改进if标签
<select id="getStuByIf" resultType="com.itheima.pojo.TbStudent" parameterType="com.itheima.pojo.TbStudent">
select * from tb_student
<where>
<if test="uId!=null and uId!=‘‘ ">
and u_id=#{uId}
</if>
<if test="uName!=null and uName!=‘‘">
and u_name=#{uName}
</if>
<if test="sex!=null and sex!=‘‘">
and sex=#{sex}
</if>
<if test="tId!=null and tId!=‘‘">
and t_id=#{tId}
</if>
</where>
</select> 相关推荐
chenjiazhu 2020-07-08
xiuyangsong 2020-05-31
dingchaochao0 2020-03-11
liqiancao 2020-05-08
② Mapper接口方法的输入参数类型和mapper.xml中定义的每个sql 的parameterType的类型相同。④ Mapper.xml文件中的namespace即是mapper接口的类路径。
cuterabbitbaby 2020-05-08
技术驱动人生 2020-05-06
happinessaflower 2020-04-18
cnflat0 2020-04-17
kevincheung 2020-04-10
cuterabbitbaby 2020-03-08
GechangLiu 2020-02-02
dongxurr 2020-01-19
dongxurr 2019-12-31
cuterabbitbaby 2020-01-06
javamagicsun 2020-01-04
zhiyuan0 2020-01-04
技术驱动人生 2019-12-15