执行如下命令:

mvn -Dmybatis.generator.overwrite=true mybatis-generator:generate

可以使用mybatis generator

mybatis 使用多个参数

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

SELECT * FROM `db_demo`.`hot_topic` WHERE lang='english' AND category='' AND  topic_type='video' ORDER BY score DESC;

推荐使用注解的方式:

需要自定义方法:

mapper文件中

  //  使用注解
JSONArray selectByLangAndCategoryAndTopicType(@Param("lang") String lang, @Param("category") String category, @Param("topicType") String topicType);

mapper.xml文件中:

<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层的函数方法

Public User selectUser(String name,String area);

对应的Mapper.xml

<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层的函数方法

Public User selectUser(Map paramMap);

对应的Mapper.xml

<select id=" selectUser" resultMap="BaseResultMap">
select * from user_user_t where user_name = #{userName,jdbcType=VARCHAR} and user_area=#{userArea,jdbcType=VARCHAR}
</select>

Service层调用

Private User xxxSelectUser(){
Map paramMap=new hashMap();
paramMap.put(“userName”,”对应具体的参数值”);
paramMap.put(“userArea”,”对应具体的参数值”);
User user=xxx. selectUser(paramMap);}

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

mybatis 使用tips - 使用多个参数的更多相关文章

  1. MyBatis传入集合 list 数组 map参数的写法

    foreach的主要用在构建in条件中,它可以在SQL语句中进行迭代一个集合.foreach元素的属性主要有item,index,collection,open,separator,close.ite ...

  2. Mybatis 实用篇(三)参数处理

    Mybatis 实用篇(三)参数处理 sql 语句中的参数 parameterType 可以省略不写. 一.参数封装 1.1 单个参数处理 public interface UserMapper { ...

  3. mybatis按datetime条件查询,参数为时间戳时

    mybatis按datetime条件查询,参数为时间戳时,如果数据库为2018-1-1 20:22:10, 你的时间戳也为2018-1-1 20:22:10,但却没找到数据.可能是时差导致的.百度修正 ...

  4. MyBatis映射文件中用#和$传递参数的特点

    在MyBatis映射文件中用#和$传递参数的特点, #是以占位符的形式来传递对应变量的参数值的,框架会对传入的参数做预编译的动作, 用$时会将传入的变量的参数值原样的传递过去,并且用$传递传递参数的时 ...

  5. MyBatis中抽象方法中多个参数的问题

    在使用MyBatis时,接口中的抽象方法只允许有1个参数,如果有多个参数,例如:      Integer updatePassword(             Integer id, String ...

  6. Mybatis接口中传递多个参数

    1.接口 public interface MemberMapper { public boolean insertMember(Members member); public Members sel ...

  7. Mybatis的mapper接口接受的参数类型

    最近项目用到了Mybatis,学一下记下来. Mybatis的Mapper文件中的select.insert.update.delete元素中有一个parameterType属性,用于对应的mappe ...

  8. 8.mybatis动态SQL模糊查询 (多参数查询,使用parameterType)

    多参数查询,使用parameterType.实例: 用户User[id, name, age] 1.mysql建表并插入数据 2.Java实体类 public class User { public ...

  9. Mybatis的parameterType传入多个参数

    如果查询的条件有多个的时候,mybatis有三种传入方式: 1.通过注解传入 例如: public interface Mapper(){ public User login(@Param(" ...

随机推荐

  1. Autofac框架详解

    一.组件 创建出来的对象需要从组件中来获取,组件的创建有如下4种(延续第一篇的Demo,仅仅变动所贴出的代码)方式: 1.类型创建RegisterType AutoFac能够通过反射检查一个类型,选择 ...

  2. Web前端应该从哪些方面来优化网站

    作者:斯迪链接:https://www.zhihu.com/question/21658448/answer/18903129来源:知乎著作权归作者所有,转载请联系作者获得授权. 不知道是哪位大牛的文 ...

  3. Java 目标

    Java 技术 其次掌握的技能树主要有三个方面:第一个是基础,比如对集合类,并发包,IO/NIO,JVM,内存模型,泛型,异常,反射,等有深入了解,最好是看过源码了解底层的设计.比如一般面试都会问Co ...

  4. hive 建表导入数据

    1. hive> create table wyp > (id int, name string, > age int, tel string) > ROW FORMAT DE ...

  5. win7局域网内共享文件夹及安全设置

    右键想要共享的文件夹,选择属性. 在文件夹属性对话框中选择共享标签,如图: 点击共享按钮,弹出文件共享对话框. 添加 Everyone ,并根据实际需要修改权限.权限可以是读取 或 读取/写入. 到此 ...

  6. kubernetes 示例 hello world

    本文所说的Hello world是一个web留言板应用,并且是基于PHP+Redis的两层分布式架构的web应用,前端PHP web网站通过访问后端Redis数据库完成用户留言的查询和添加功能,具备读 ...

  7. 使用heroku创建应用时报错 heroku does not appear to be a git repository

    在跟着heroku的官方教程创建python应用时,到deploy-the-app这一步,要上传代码到heroku 的git仓库时,报的这个错误: 网上一搜,相关的答案居然极少,首页只出现一篇(还好这 ...

  8. 通过@Configuratin配置Bean

    Spring的依赖注入可以基于xml配置,也可以基于注解配置,还可以基于java类配置. 普通的bean类,只要标注了@Configuration注解,就可以为Spring容器提供Bean定义的信息. ...

  9. python3菜鸟教程

    http://www.runoob.com/python3/python3-class.html

  10. 105. Construct Binary Tree from Preorder and Inorder Traversal (Tree; DFS)

    Given preorder and inorder traversal of a tree, construct the binary tree. Note: You may assume that ...