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

dao层方法为以下两种:

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

对应的Mapper取值:

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

  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层方法:

  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层传入的参数名字相同。

  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中的方法。

  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取值:

  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中的方法。

  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取值:

  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层:

  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取值:

  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】的更多相关文章

  1. mybatis框架中XxxxMaper.xml的文件

    我们知道在mybatis框架中,config.xml中会关联到许多的XxxxMapper的xml文件,这些文件又对应着一个个的接口,来观察下这些xml文件 从以下这个文件为例子: <?xml v ...

  2. 逆向工程生成的mybatis中mapper文件。mapper接口,实例化成对象

    逆向工程生成的mybatis中mapper文件中,*mapper文件只是接口,而不是类文件.但是却可以通过spring的容器获得实例. 例如: //1.获得mapper代理对象,从spring容器获得 ...

  3. idea插件(mybatis框架下mapper接口快速跳转对应xml文件)亲测好用!

    我相信目前在绝大部分公司里,主要使用的框架是S(spring)S(spring MVC)M(mybatis),其中mybatis总体架构是编写mapper接口,框架扫描其对应的mapper.xml文件 ...

  4. 详解Java的MyBatis框架中SQL语句映射部分的编写

    这篇文章主要介绍了Java的MyBatis框架中SQL语句映射部分的编写,文中分为resultMap和增删查改实现两个部分来讲解,需要的朋友可以参考下 1.resultMap SQL 映射XML 文件 ...

  5. Mybatis框架中实现双向一对多关系映射

    学习过Hibernate框架的伙伴们很容易就能简单的配置各种映射关系(Hibernate框架的映射关系在我的blogs中也有详细的讲解),但是在Mybatis框架中我们又如何去实现 一对多的关系映射呢 ...

  6. 【MyBatis笔记】mapper文件的配置以及说明

    <!doctype html>[MyBatis笔记]mapper文件的配置以及说明 figure:last-child { margin-bottom: 0.5rem; } #write ...

  7. 用IntelliJ IDEA 开发Spring+SpringMVC+Mybatis框架 分步搭建二:配置MyBatis 并测试(2 配置spring-dao和测试)

    用IntelliJ IDEA 开发Spring+SpringMVC+Mybatis框架 分步搭建二:配置MyBatis 并测试(1 搭建目录环境和依赖) 四:在\resources\spring 下面 ...

  8. 用IntelliJ IDEA 开发Spring+SpringMVC+Mybatis框架 分步搭建二:配置MyBatis 并测试(1 构建目录环境和依赖)

    引言:在用IntelliJ IDEA 开发Spring+SpringMVC+Mybatis框架 分步搭建一   的基础上 继续进行项目搭建 该部分的主要目的是测通MyBatis 及Spring-dao ...

  9. 简单分析下mybatis中mapper文件中小知识

    <?xml version="1.0" encoding="UTF-8"?><!DOCTYPE mapper PUBLIC "-// ...

随机推荐

  1. Oracle XE安装具体解释

    一.原数据库的卸载       数据库的卸载就不多说了,讲一下过程:       1.运行Oracle Uninstall,卸载Oracle产品     2.删除regedit下的全部Oracle相关 ...

  2. android对话框,checkBox,同一时候在同一个页面上保存数据

    package com.example.selectonlyonle; import android.app.Activity; import android.app.AlertDialog; imp ...

  3. cocos2d-x 之 CCProgressTimer

    --绕圆心转动的进度动画 local function SpriteProgressToRadial() local leftProgress = CCProgressTimer:create(CCS ...

  4. 【亲测好用!】shell批量采集百度下拉框关键词

    [亲测好用!]shell批量采集百度下拉框关键词 SEO工具  方法  11个月前 (11-18)  2153浏览 3条评论 百度已收录 一直想写一篇用shell采集百度下拉框关键词的教程,个人感觉用 ...

  5. iOS 关于图片地理位置隐私信息的分析和读取

    今天突然想到微信朋友圈发照片,涉及个人隐私的地理位置是否外泄.因为iphone拍照的照片都会带有地理位置等信息,我们先来实现怎么读取里面的安全信息,然后再来分析 #import "ViewC ...

  6. Nginx设置expires设定页面缓存时间 不缓存或一直使用缓存

    配置expires expires起到控制页面缓存的作用,合理的配置expires可以减少很多服务器的请求 要配置expires,可以在http段中或者server段中或者location段中加入 l ...

  7. jquery衬衣产品内容详情页

    html代码: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www ...

  8. linux之backtrace

    backtrace用于打印函数调用堆栈 /******************************************************************************* ...

  9. cloudera-manager-installer.bin不生成repo文件

    [转] 运行cloudera-manager-installer.bin,并在后边增加参数使其不再在/etc/yum.repo.d/下生成cloudera-manager.repo文件 ./cloud ...

  10. insert,update和delete下的注入

    PS:刚开始看到这个注入方式的时候,问米哥:“为啥updatexml就足以报错了,为啥还要使用insert?”知道答案的我觉得问了一个傻蛋的问题.不过没关系.慢慢积累.答案很简单,并不是所有的注入点程 ...