zoukankan      html  css  js  c++  java
  • Mybatis框架中Mapper文件传值参数获取。【Mybatis】

    1、参数个数为1个(string或者int)

    dao层方法为以下两种:

    [java] view plain copy
     
    1. /** 
    2.  * 单个int型 
    3.  */  
    4.     public List<UserComment> findByDepartmentId(int dapartmentId);  
    5.   
    6. /** 
    7.  * 单个string型 
    8.  */  
    9.  public Source findByTitle(String title);  

    对应的Mapper取值:

    取值时应当注意,参数名字应该与dao层传入的参数名字相同。

    [html] view plain copy
     
    1. /*单个int型*/  
    2. <select id="findByDepartmentId"  resultType="com.bonc.wechat.entity.publicserver.UserComment">  
    3.     select * from wx_user_comment where   
    4.     department_id=#{departmentId}   
    5.     order by createtime desc;  
    6. </select>  
    7.   
    8.   
    9. /*单个string型*/  
    10. <select id="findByTitle"  parameterType="java.lang.String" resultType="com.bonc.wechat.entity.publicserver.Source">  
    11.     select * from wx_source where   
    12.         title=#{title};  
    13. </select>  
    14.   
    15.   
    16. /*****或者直接使用通用方法获取参数*****/  
    17.   
    18. <select id="findByDepartmentId"  resultType="com.bonc.wechat.entity.publicserver.UserComment">  
    19.     select * from wx_user_comment where   
    20.     department_id=#{_parameter}   
    21.     order by createtime desc;  
    22. </select>  
    23.   
    24.   
    25.   
    26. <select id="findByTitle"  parameterType="java.lang.String" resultType="com.bonc.wechat.entity.publicserver.Source">  
    27.     select * from wx_source where   
    28.         title=#{_parameter};  
    29. </select>  

    2、参数个数为多个。

    dao层方法:

    [java] view plain copy
     
    1. /*****1.正常传参*****/  
    2. public Dailyuserinfo findStutaByUserAndDaily(String username,String dailyid);  
    3.   
    4.   
    5. /*****2.注解传参*****/  
    6. public List<UserTab> selectUserListExceptUserId  
    7. (@Param("USER_ID")String USER_ID,   
    8.  @Param("LIMIT_POS")int LIMIT_POS,   
    9.  @Param("LIMIT_SIZE")int LIMIT_SIZE);  

    对应的Mapper取值:

    取值时应当注意,参数名字应该与dao层传入的参数名字相同。

    [html] view plain copy
     
    1. /****正常传参方式参数获取****/  
    2. <select id="findStutaByUserAndDaily"  
    3.            parameterType="java.lang.String"   
    4.            resultType="com.thinkgem.jeesite.modules.dailynews.entity.Dailyuserinfo">  
    5.   
    6.     select * from daily_user_info   
    7.     where login_name=#{username}   
    8.     And daily_id=#{dailyid};  
    9.   
    10. </select>  
    11.   
    12.   
    13. /****注解传参方式参数获取****/  
    14. <select id="selectUserListExceptUserId"   
    15.             resultMap="userResMap">  
    16.   
    17.     select * from MH_USER   
    18.         where USER_ID!=#{USER_ID}   
    19.         and USER_STATE>9   
    20.         order by NICK_NAME   
    21.         limit #{LIMIT_POS},#{LIMIT_SIZE}  
    22. </select>  

    3、参数为map的形式。

    mapper中使用map的key取值。

    dao中的方法。

    [java] view plain copy
     
    1. /*****1.参数为map*****/  
    2. public List<Source> search(Map<String,Object> param);  
    3.   
    4. /***2.map的内部封装***/  
    5.      Map<String,Object> param=new HashMap<String,Object>();  
    6.     param.put("page", (page-1)*pageSize);  
    7.     param.put("pageSize",pageSize);  
    8.     param.put("keyword","%"+keyword+"%");  
    9.     param.put("type",type);  
    10.     List<Source> sources=sourceDao.search(param);  

    对应的Mapper取值:

    [html] view plain copy
     
    1. <select id="search"   
    2.             parameterType="java.util.Map"   
    3.             resultType="com.bonc.wechat.entity.publicserver.Source">  
    4.     select * from wx_source   
    5.         where   
    6.      <if test="keyword != null and keyword != ''">  
    7.         (title like #{keyword} or content like #{keyword}) and  
    8.          </if>  
    9.           type=#{type}  
    10.       order by ordernum asc   
    11.           limit #{page},#{pageSize};  
    12. </select>  

    4、参数为对象。

    mapper中使用对象中的属性直接取值,或者【对象.属性】取值。

    dao中的方法。

    [java] view plain copy
     
    1. /*****使用对象传参*****/  
    2.  public int addUserComment(UserComment UC);  
    3.   
    4.   
    5.   
    6. /*****对象中的属性*****/  
    7. private int id;  
    8.     private int department_id;        
    9.     private String telphone;          
    10.     private String content;           
    11.     private String createtime;   

    对应的Mapper取值:

    [html] view plain copy
     
    1. <insert id="addUserComment"   
    2.             parameterType="com.bonc.wechat.entity.publicserver.UserComment">  
    3.     insert into wx_user_comment  
    4.                    (department_id,  
    5.                     telphone,  
    6.                     content,  
    7.                     createtime)   
    8.     values(#{department_id},  
    9.                    #{telphone},  
    10.                    #{content},  
    11.                    #{createtime});  
    12. </insert>  

    *使用【对象.属性】取值。

    dao层:

    [java] view plain copy
     
    1. /******此示例中直接省去dao层,service直接绑定mapper层******/  
    2.   
    3. public PageResult findPanoramaPage(Page page) throws Exception{  
    4.     List<PageData> panoramaList = new ArrayList<PageData>();  
    5.     try {  
    6.     panoramaList = (List<PageData>)dao.findForList("PanoramaMapper.findPagePanorama", page);  
    7.     } catch (Exception e) {  
    8.         e.printStackTrace();  
    9.         }  
    10.     PageResult pageResult = new PageResult(page.getTotalResult(),panoramaList);  
    11.     return pageResult;  
    12. }  

    对应的Mapper取值:

    [html] view plain copy
     
    1. <select id="findPagePanorama"   
    2.             parameterType="page"   
    3.             resultType="pd">  
    4.   
    5.     SELECT   
    6.         a.id as id,  
    7.         a.project_code as projectCode,  
    8.         a.project_name as projectName,  
    9.         a.project_addr as projectAddr,  
    10.         a.project_type as projectType  
    11.     FROM   
    12.         calm_project a   
    13.     WHERE   
    14.         a.is_valid=1      
    15.     <if test="page.projectType != null and page.projectType != ''">  
    16.         AND a.project_type = #{page.projectType}   
    17.         </if>  
    18.     <if test="page.keyword != null and page.keyword != ''">  
    19.        AND (a.project_name LIKE '%${page.keyword}%' OR a.project_code LIKE '%${page.keyword}%')  
    20.         </if>  
    21.         ORDER BY   
    22.              a.sort  
    23.              
    24. </select>  

    推荐使用第三和第四种,将所传的数据在controller层或者service层封装起来,传入mapper文件中取值。

  • 相关阅读:
    eclipse中常用快捷键
    js sort排序
    js parseInt函数
    Jquery常用方法
    jquery的call()和apply()方法
    Jquery中的事件命名机制
    CSS层叠样式表
    推荐博客园中好的博客主
    页面刷新或者子窗体刷新父窗体,不提示 "重试或取消”对话框
    FullCalendar日历插件使用说明
  • 原文地址:https://www.cnblogs.com/shizhijie/p/8778282.html
Copyright © 2011-2022 走看看