目录
官网地址:mybatis – MyBatis 3 | 动态 SQLhttps://mybatis.org/mybatis-3/zh/dynamic-sql.html
直接展示自己的代码。
mysql
具体目录结构,以防写错
![](https://i-blog.csdnimg.cn/blog_migrate/110c748ac757c1db856908cb204c6954.png)
Mapping
<select id="User_Info_Ids" resultType="com.wu.pojo.User_Info">
select * from user_info
<where>
<if test="user != null and user!=''">
AND user = #{user}
</if>
<if test="password != null and password !=''">
AND password = #{password}
</if>
<if test="gender != null and gender != ''">
AND gender = #{gender}
</if>
<if test="grade != null and grade != ''">
AND grade = #{grade}
</if>
<if test="mailbox != null and mailbox != ''">
AND mailbox = #{mailbox}
</if>
<if test="phonenumber != null and phonenumber != ''">
AND phonenumber = #{phonenumber}
</if>
</where>
</select>
Dao层
//条件查询
public List<User_Info> User_Info_Ids(@Param("user")String user, @Param("password")Integer password, @Param("gender")String gender,@Param("grade")String grade,@Param("mailbox")String mailbox,@Param("phonenumber")Integer phonenumber);
Serverce层
serverce
public List<User_Info> User_Info_Ids(@Param("user")String user, @Param("password")Integer password, @Param("gender")String gender, @Param("grade")String grade, @Param("mailbox")String mailbox, @Param("phonenumber") Integer phonenumber);
ServerceImpl
@Override
public List<User_Info> User_Info_Ids(String user, Integer password, String gender, String grade, String mailbox, Integer phonenumber) {
return user_infoMapping.User_Info_Ids(user,password,gender,grade,mailbox,phonenumber);
}
Controller层
@ResponseBody
@RequestMapping("/Ids")
public List<User_Info> User_Info_Ids(){
System.out.println("开始查询");
List<User_Info> ids = user_infoService.User_Info_Ids("1",1,"1","1",null,null);
System.out.println(ids);
System.out.println("结束查询");
return ids;
}
// @ResponseBody
// @RequestMapping("/Ids")
// public List<User_Info> User_Info_Ids(){
// System.out.println("开始查询");
// List<User_Info> ids = user_infoService.User_Info_Ids("1",1,"1","1","1",1);
// System.out.println(ids);
// System.out.println("结束查询");
// return ids;
// }
试了两种方法,一种是全部sql语句都实现,一种是另外两个元素给控制
查询结果(成功实现!)
最后总结:SQL类型为int类型,Java对应类型应该为Integer这种包装类。个人尝试过如果给Java给int类型不能传入 null,所以要使用 Integer 包装类,应为具体查询别人可能就只是输入一个查询条件,其他条件为空,有其他解决放发欢迎私聊。