mybatis 的条件查询的三个方法实现

来源:互联网 发布:linux服务器开发流程 编辑:程序博客网 时间:2024/04/30 13:46

Mybatis的多条件查查询,传递参数,

第一种方法 传递map 型,

第二种方法 传递pojo 

带三种方法 多个参数如果不封装成Map,就用序列号代替。

如果参数比较多且乱建议用map 型,如果有定义的pojo 则建议用pojo类型,如果传递的参数不多,则建议用序列号代替的方法。

1.Map (当传入多个参数时可以使用map型)

例 :  mapper.java

//分页查询教师信息 

public List<Teacher> findTeacherByPage(Map<String, Object> map); 

相应地,这里用到了Map接口,就应该引入此接口:import java.util.Map。

 在执行类Collection中,调用findTeacherByPage方法的相关代码如下: 

Map<String,Object> params=new HashMap<String, Object>();

//以name字段升序排序, 

params.put("sort", "name");  

params.put("dir", "asc");  

//查询结果从第0条开始,查询2条记录 

params.put("start", 0);  

params.put("limit", 2);  

//查询职称为教授或副教授的教师 

params.put("title", "%教授");  

//分页查询教师信息 

List<Teacher> teachers=mapper.findTeacherByPage(params); 

 可以看出,我们先把参数放在了一个Map中,这样我们就可以在相应的SQL语句中以#{…}的形式引用这些参数了。如下所示:

<selectid="findTeacherByPage"resultMap="upervisorResultMap"

parameterType="java.util.Map">

select * from teacher where title like #{title} order by ${sort} ${dir} limit #{start},#{limit}  

</select>

2.pojo

2.1 . mapper.xml

<sqlid="query_items_where">

 <!-- 使用动态sql,通过if判断,满足条件进行sql拼接

     商品的查询条件需要通过ItemsQueryVo包装对象中itemsCustom属性传递

     ${}字符串的拼接

  -->

 <iftest="itemsCustom!=null">

 <iftest="itemsCustom.name!=null and itemsCustom.name!=''">

     items.name LIKE'%${itemsCustom.name}%'

 </if>

 </if>

  </sql>

 

<selectid="findItemsList"parameterType="cn.hpu.ssm.po.ItemsQueryVo" resultType="cn.hpu.ssm.po.ItemsCustom">

 SELECT * FROM items

 <where>

 <includerefid="query_items_where"></include>

  </where>

2. 2 Mapper.java

public interfaceItemsMapperCustom {

 //商品类表查询

public List<ItemsCustom> findItemsList (ItemsQueryVo itemsQueryVo)throws Exception;

}

3.Serviceimpl.java 在服务实现类中调用

   public List<ItemsCustom>findItemsList(ItemsQueryVo itemsQueryVo)throws Exception {

   //通过ItemsMapperCustom查询数据库

      //itemsQueryVoservice直接传递到dao

return itemsMapperCustom.findItemsList(itemsQueryVo);

   }

2.3controller 中调用 itemsCustom可以输入itemsCustom中的某个属性值具体看mapper.xml中的sql片段

@RequestMapping("/queryItems")

public ModelAndView queryItems(HttpServletRequest request,

ItemsQueryVo itemsCustom) throws Exception {

// 调用service查找数据库,查询商品列表,使用静态数据模

   // 测试froward后能否拿到id

   System.out.println(request.getParameter("id"));

   List<ItemsCustom> itemsList = itemsService.findItemsList(itemsCustom);

// 返回ModelAndView

ModelAndView modelAndView = new ModelAndView();

// 相当于requesrsetAttribut,在jsp页面中通过itemsList来取得数据

modelAndView.addObject("itemsList", itemsList);

   // 指定视图

   // 下边的路径在试图解析器中配置前缀和后缀

   modelAndView.setViewName("items/itemsList");

//modelAndView.setViewName("/WEBINF/jsp/items/itemsList.jsp");

   return modelAndView;

}

3.序列号代替

如果不想用map 也不想用pojo可以用序列号代替

Mapper.xml

<selectid="login"parameterType="String"resultType="cn.hpu.back.po.Users">

 SELECT *  FROM users WHERE userphone= #{0} AND userpass=#{1}

 </select>

Mapper.java

Users login (String userphone ,String userpass)throwsException;

Serviceimpl.java调用

//登录

   public String logion(Stringuserphone, String userpass)throws Exception {

      // TODO Auto-generated method stub

      Usersstr= usersMapperCustom.login(userphone,userpass);

      Stringresult=null;

      if(str!=null)

        result="ok";    

      else

        result="no";

      return result;

   }

1 0
原创粉丝点击