1. 程式人生 > >Mybatis框架中Mapper文件傳值參數獲取。【Mybatis】

Mybatis框架中Mapper文件傳值參數獲取。【Mybatis】

ram keyword ddr gem cli view ati copyto one

Mybatis框架中,Mapper文件參數獲取一般有以下幾種:

1、參數個數為1個(string或者int)

dao層方法為以下兩種:

[java] view plain copy 技術分享技術分享
  1. /**
  2. * 單個int型
  3. */
  4. public List<UserComment> findByDepartmentId(int dapartmentId);
  5. /**
  6. * 單個string型
  7. */
  8. 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. /*單個string型*/
  8. <select id="findByTitle" parameterType="java.lang.String" resultType="com.bonc.wechat.entity.publicserver.Source">
  9. select * from wx_source where
  10. title=#{title};
  11. </select>
  12. /*****或者直接使用通用方法獲取參數*****/
  13. <select id="findByDepartmentId" resultType="com.bonc.wechat.entity.publicserver.UserComment">
  14. select * from wx_user_comment where
  15. department_id=#{_parameter}
  16. order by createtime desc;
  17. </select>
  18. <select id="findByTitle" parameterType="java.lang.String" resultType="com.bonc.wechat.entity.publicserver.Source">
  19. select * from wx_source where
  20. title=#{_parameter};
  21. </select>

2、參數個數為多個。

dao層方法:

[java] view plain copy 技術分享技術分享
  1. /*****1.正常傳參*****/
  2. public Dailyuserinfo findStutaByUserAndDaily(String username,String dailyid);
  3. /*****2.註解傳參*****/
  4. public List<UserTab> selectUserListExceptUserId
  5. (@Param("USER_ID")String USER_ID,
  6. @Param("LIMIT_POS")int LIMIT_POS,
  7. @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. select * from daily_user_info
  6. where login_name=#{username}
  7. And daily_id=#{dailyid};
  8. </select>
  9. /****註解傳參方式參數獲取****/
  10. <select id="selectUserListExceptUserId"
  11. resultMap="userResMap">
  12. select * from MH_USER
  13. where USER_ID!=#{USER_ID}
  14. and USER_STATE>9
  15. order by NICK_NAME
  16. limit #{LIMIT_POS},#{LIMIT_SIZE}
  17. </select>

3、參數為map的形式。

mapper中使用map的key取值。

dao中的方法。

[java] view plain copy 技術分享技術分享
  1. /*****1.參數為map*****/
  2. public List<Source> search(Map<String,Object> param);
  3. /***2.map的內部封裝***/
  4. Map<String,Object> param=new HashMap<String,Object>();
  5. param.put("page", (page-1)*pageSize);
  6. param.put("pageSize",pageSize);
  7. param.put("keyword","%"+keyword+"%");
  8. param.put("type",type);
  9. 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. private int id;
  5. private int department_id;
  6. private String telphone;
  7. private String content;
  8. 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. public PageResult findPanoramaPage(Page page) throws Exception{
  3. List<PageData> panoramaList = new ArrayList<PageData>();
  4. try {
  5. panoramaList = (List<PageData>)dao.findForList("PanoramaMapper.findPagePanorama", page);
  6. } catch (Exception e) {
  7. e.printStackTrace();
  8. }
  9. PageResult pageResult = new PageResult(page.getTotalResult(),panoramaList);
  10. return pageResult;
  11. }

對應的Mapper取值:

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

推薦使用第三和第四種,將所傳的數據在controller層或者service層封裝起來,傳入mapper文件中取值。

Mybatis框架中Mapper文件傳值參數獲取。【Mybatis】