前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
社区首页 >专栏 >mybatis 使用tips - 使用多个参数

mybatis 使用tips - 使用多个参数

作者头像
千往
发布2018-01-24 11:23:15
发布2018-01-24 11:23:15
1.7K00
代码可运行
举报
运行总次数:0
代码可运行

执行如下命令:

代码语言:javascript
代码运行次数:0
运行
复制
mvn -Dmybatis.generator.overwrite=true mybatis-generator:generate

可以使用mybatis generator

mybatis 使用多个参数

自定义方法需要根据多个查询条件去查询:

代码语言:javascript
代码运行次数:0
运行
复制
SELECT * FROM `db_demo`.`hot_topic` WHERE lang='english' AND category='017' AND  topic_type='video' ORDER BY score DESC;

推荐使用注解的方式:

需要自定义方法:

mapper文件中

代码语言:javascript
代码运行次数:0
运行
复制
  //  使用注解
    JSONArray selectByLangAndCategoryAndTopicType(@Param("lang") String lang, @Param("category") String category, @Param("topicType") String topicType);

mapper.xml文件中:

代码语言:javascript
代码运行次数:0
运行
复制
<select id="selectByLangAndCategoryAndTopicType" resultMap="ResultMapWithBLOBs" >
    <!--
      WARNING - @mbggenerated
      This element is automatically generated by MyBatis Generator, do not modify.
      This element was generated on Thu Oct 12 14:51:34 CST 2017.
    -->
    select
    <include refid="Base_Column_List" />
    ,
    <include refid="Blob_Column_List" />
    from hot_topic
    where lang= #{lang,jdbcType=VARCHAR} AND category = #{category,jdbcType=VARCHAR} AND topic_type=#{topicType,jdbcType=VARCHAR}
  </select>

其他方法

DAO层的函数方法 

代码语言:javascript
代码运行次数:0
运行
复制
Public User selectUser(String name,String area);

对应的Mapper.xml  

代码语言:javascript
代码运行次数:0
运行
复制
<select id="selectUser" resultMap="BaseResultMap">
    select  *  from user_user_t   where user_name = #{0} and user_area=#{1}
</select>

其中,#{0}代表接收的是dao层中的第一个参数,#{1}代表dao层中第二参数,更多参数一致往后加即可。

其他方法

此方法采用Map传多参数.

Dao层的函数方法

代码语言:javascript
代码运行次数:0
运行
复制
Public User selectUser(Map paramMap);

对应的Mapper.xml

代码语言:javascript
代码运行次数:0
运行
复制
<select id=" selectUser" resultMap="BaseResultMap">
   select  *  from user_user_t   where user_name = #{userName,jdbcType=VARCHAR} and user_area=#{userArea,jdbcType=VARCHAR}
</select>

Service层调用

代码语言:javascript
代码运行次数:0
运行
复制
Private User xxxSelectUser(){
Map paramMap=new hashMap();
paramMap.put(“userName”,”对应具体的参数值”);
paramMap.put(“userArea”,”对应具体的参数值”);
User user=xxx. selectUser(paramMap);}

此方法不够直观,见到接口方法不能直接的知道要传的参数是什么。

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2017-10-12 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档