Mybatis框架中Mapper文件传值参数获取。【Mybatis】
1、参数个数为1个(string或者int)
dao层方法为以下两种:
- /**
- * 单个int型
- */
- public List<UserComment> findByDepartmentId(int dapartmentId);
- /**
- * 单个string型
- */
- public Source findByTitle(String title);
对应的Mapper取值:
取值时应当注意,参数名字应该与dao层传入的参数名字相同。
- /*单个int型*/
- <select id="findByDepartmentId" resultType="com.bonc.wechat.entity.publicserver.UserComment">
- select * from wx_user_comment where
- department_id=#{departmentId}
- order by createtime desc;
- </select>
- /*单个string型*/
- <select id="findByTitle" parameterType="java.lang.String" resultType="com.bonc.wechat.entity.publicserver.Source">
- select * from wx_source where
- title=#{title};
- </select>
- /*****或者直接使用通用方法获取参数*****/
- <select id="findByDepartmentId" resultType="com.bonc.wechat.entity.publicserver.UserComment">
- select * from wx_user_comment where
- department_id=#{_parameter}
- order by createtime desc;
- </select>
- <select id="findByTitle" parameterType="java.lang.String" resultType="com.bonc.wechat.entity.publicserver.Source">
- select * from wx_source where
- title=#{_parameter};
- </select>
2、参数个数为多个。
dao层方法:
- /*****1.正常传参*****/
- public Dailyuserinfo findStutaByUserAndDaily(String username,String dailyid);
- /*****2.注解传参*****/
- public List<UserTab> selectUserListExceptUserId
- (@Param("USER_ID")String USER_ID,
- @Param("LIMIT_POS")int LIMIT_POS,
- @Param("LIMIT_SIZE")int LIMIT_SIZE);
对应的Mapper取值:
取值时应当注意,参数名字应该与dao层传入的参数名字相同。
- /****正常传参方式参数获取****/
- <select id="findStutaByUserAndDaily"
- parameterType="java.lang.String"
- resultType="com.thinkgem.jeesite.modules.dailynews.entity.Dailyuserinfo">
- select * from daily_user_info
- where login_name=#{username}
- And daily_id=#{dailyid};
- </select>
- /****注解传参方式参数获取****/
- <select id="selectUserListExceptUserId"
- resultMap="userResMap">
- select * from MH_USER
- where USER_ID!=#{USER_ID}
- and USER_STATE>9
- order by NICK_NAME
- limit #{LIMIT_POS},#{LIMIT_SIZE}
- </select>
3、参数为map的形式。
mapper中使用map的key取值。
dao中的方法。
- /*****1.参数为map*****/
- public List<Source> search(Map<String,Object> param);
- /***2.map的内部封装***/
- Map<String,Object> param=new HashMap<String,Object>();
- param.put("page", (page-1)*pageSize);
- param.put("pageSize",pageSize);
- param.put("keyword","%"+keyword+"%");
- param.put("type",type);
- List<Source> sources=sourceDao.search(param);
对应的Mapper取值:
- <select id="search"
- parameterType="java.util.Map"
- resultType="com.bonc.wechat.entity.publicserver.Source">
- select * from wx_source
- where
- <if test="keyword != null and keyword != ''">
- (title like #{keyword} or content like #{keyword}) and
- </if>
- type=#{type}
- order by ordernum asc
- limit #{page},#{pageSize};
- </select>
4、参数为对象。
mapper中使用对象中的属性直接取值,或者【对象.属性】取值。
dao中的方法。
- /*****使用对象传参*****/
- public int addUserComment(UserComment UC);
- /*****对象中的属性*****/
- private int id;
- private int department_id;
- private String telphone;
- private String content;
- private String createtime;
对应的Mapper取值:
- <insert id="addUserComment"
- parameterType="com.bonc.wechat.entity.publicserver.UserComment">
- insert into wx_user_comment
- (department_id,
- telphone,
- content,
- createtime)
- values(#{department_id},
- #{telphone},
- #{content},
- #{createtime});
- </insert>
*使用【对象.属性】取值。
dao层:
- /******此示例中直接省去dao层,service直接绑定mapper层******/
- public PageResult findPanoramaPage(Page page) throws Exception{
- List<PageData> panoramaList = new ArrayList<PageData>();
- try {
- panoramaList = (List<PageData>)dao.findForList("PanoramaMapper.findPagePanorama", page);
- } catch (Exception e) {
- e.printStackTrace();
- }
- PageResult pageResult = new PageResult(page.getTotalResult(),panoramaList);
- return pageResult;
- }
对应的Mapper取值:
- <select id="findPagePanorama"
- parameterType="page"
- resultType="pd">
- SELECT
- a.id as id,
- a.project_code as projectCode,
- a.project_name as projectName,
- a.project_addr as projectAddr,
- a.project_type as projectType
- FROM
- calm_project a
- WHERE
- a.is_valid=1
- <if test="page.projectType != null and page.projectType != ''">
- AND a.project_type = #{page.projectType}
- </if>
- <if test="page.keyword != null and page.keyword != ''">
- AND (a.project_name LIKE '%${page.keyword}%' OR a.project_code LIKE '%${page.keyword}%')
- </if>
- ORDER BY
- a.sort
- </select>
推荐使用第三和第四种,将所传的数据在controller层或者service层封装起来,传入mapper文件中取值。
Mybatis框架中Mapper文件传值参数获取。【Mybatis】的更多相关文章
- mybatis框架中XxxxMaper.xml的文件
我们知道在mybatis框架中,config.xml中会关联到许多的XxxxMapper的xml文件,这些文件又对应着一个个的接口,来观察下这些xml文件 从以下这个文件为例子: <?xml v ...
- 逆向工程生成的mybatis中mapper文件。mapper接口,实例化成对象
逆向工程生成的mybatis中mapper文件中,*mapper文件只是接口,而不是类文件.但是却可以通过spring的容器获得实例. 例如: //1.获得mapper代理对象,从spring容器获得 ...
- idea插件(mybatis框架下mapper接口快速跳转对应xml文件)亲测好用!
我相信目前在绝大部分公司里,主要使用的框架是S(spring)S(spring MVC)M(mybatis),其中mybatis总体架构是编写mapper接口,框架扫描其对应的mapper.xml文件 ...
- 详解Java的MyBatis框架中SQL语句映射部分的编写
这篇文章主要介绍了Java的MyBatis框架中SQL语句映射部分的编写,文中分为resultMap和增删查改实现两个部分来讲解,需要的朋友可以参考下 1.resultMap SQL 映射XML 文件 ...
- Mybatis框架中实现双向一对多关系映射
学习过Hibernate框架的伙伴们很容易就能简单的配置各种映射关系(Hibernate框架的映射关系在我的blogs中也有详细的讲解),但是在Mybatis框架中我们又如何去实现 一对多的关系映射呢 ...
- 【MyBatis笔记】mapper文件的配置以及说明
<!doctype html>[MyBatis笔记]mapper文件的配置以及说明 figure:last-child { margin-bottom: 0.5rem; } #write ...
- 用IntelliJ IDEA 开发Spring+SpringMVC+Mybatis框架 分步搭建二:配置MyBatis 并测试(2 配置spring-dao和测试)
用IntelliJ IDEA 开发Spring+SpringMVC+Mybatis框架 分步搭建二:配置MyBatis 并测试(1 搭建目录环境和依赖) 四:在\resources\spring 下面 ...
- 用IntelliJ IDEA 开发Spring+SpringMVC+Mybatis框架 分步搭建二:配置MyBatis 并测试(1 构建目录环境和依赖)
引言:在用IntelliJ IDEA 开发Spring+SpringMVC+Mybatis框架 分步搭建一 的基础上 继续进行项目搭建 该部分的主要目的是测通MyBatis 及Spring-dao ...
- 简单分析下mybatis中mapper文件中小知识
<?xml version="1.0" encoding="UTF-8"?><!DOCTYPE mapper PUBLIC "-// ...
随机推荐
- C#USB录像视频拍照-代码
论坛帖:http://bbs.csdn.net/topics/390536016 using System; using System.Collections.Generic; using Syste ...
- 详解C#泛型(二) 获取C#中方法的执行时间及其代码注入 详解C#泛型(一) 详解C#委托和事件(二) 详解C#特性和反射(四) 记一次.net core调用SOAP接口遇到的问题 C# WebRequest.Create 锚点“#”字符问题 根据内容来产生一个二维码
详解C#泛型(二) 一.自定义泛型方法(Generic Method),将类型参数用作参数列表或返回值的类型: void MyFunc<T>() //声明具有一个类型参数的泛型方法 { ...
- Ubuntu 14.10 -- 异次元软件世界
Ubuntu 14.10 中文桌面版/服务器正式版下载 - 华丽免费易于入门的 Linux 操作系统 [ 系统工具 - Linux // 2014-10-25 ] 一说到 Linux,就不得不 ...
- 使用Open Live Writer写博客
1. 下载安装软件 安装包路径http://openlivewriter.org/ 2.配置 打开软件后会提示你配置博客账号地址 3.安装代码高亮插件 下载插件源代码https://pan.baidu ...
- hdu 2066 一个人的旅行(dijkstra)
一个人的旅行 Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Sub ...
- C数据结构-栈和队列,括号匹配举例---ShinePans
1.栈和队列是两种特殊的线性表 运算操作被限定仅仅能在表的一端或两端插入,删除元素,故也称它们为限定的线性表结构 2.栈的基本运算 1).Stackinit(&s) 构 ...
- Linux系统(Ubuntu/Debian/RedHat/CentOS)超级简单的samba配置文件smb.conf
1.超简单的smb.conf 该配置文件对Ubuntu和CentOS都好用. #============== Global Settings ============== [global] ## Br ...
- 0070 过滤器调用Spring的bean操作数据库
假设有这样的需求:将用户每次请求的ip.时间.请求.user-agent存入数据库,很明显可以用过滤器实现,在过滤器中获取到这些数据调用mybatis的mapper存入数据库,但问题来了:mybati ...
- windows测试模式
测试模式通常意义就是让windows 操作系统在测试状态下运行,windows操作系统在这种模式下可以运行非官方或无数字签名的驱动程序 . 目录 1 定义 2 进入/退出windows测试模式方法 ...
- Extjs,实现树形结构的总结
工作总结,用extjs.mybatis.springMVC实现树形显示班级 前台extjs实现树形代码如下: xtype : 'combotree', fieldLabel : '部门名称', nam ...