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文件中取值。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM