mybatis 使用tips - 使用多个参数

简介: 执行如下命令: mvn -Dmybatis.generator.overwrite=true mybatis-generator:generate 可以使用mybatis generator   mybatis 使用多个参数 自定义方法需要根据多个查询条件去查询: SELECT * FROM `db_demo`.

执行如下命令:

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

可以使用mybatis generator

 

mybatis 使用多个参数

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

SELECT * FROM `db_demo`.`hot_topic` WHERE lang='english' AND category='017' 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);}

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

 

虽千万人,吾往矣!
目录
相关文章
|
9月前
|
SQL Java 数据库连接
Mybatis之核心配置文件详解、默认类型别名、Mybatis获取参数值的两种方式
【1月更文挑战第3天】 一、核心配置文件详解 二、默认的类型别名 三、MyBatis的增删改查 四、MyBatis获取参数值的两种方式 1、单个字面量类型的参数 2、多个字面量类型的参数 3、map集合类型的参数 4、实体类类型的参数 5、使用@Param标识参数
174 2
Mybatis之核心配置文件详解、默认类型别名、Mybatis获取参数值的两种方式
|
4月前
|
SQL Java 数据库连接
mybatis使用四:dao接口参数与mapper 接口中SQL的对应和对应方式的总结,MyBatis的parameterType传入参数类型
这篇文章是关于MyBatis中DAO接口参数与Mapper接口中SQL的对应关系,以及如何使用parameterType传入参数类型的详细总结。
86 10
|
5月前
|
SQL XML Java
mybatis复习02,简单的增删改查,@Param注解多个参数,resultType与resultMap的区别,#{}预编译参数
文章介绍了MyBatis的简单增删改查操作,包括创建数据表、实体类、配置文件、Mapper接口及其XML文件,并解释了`#{}`预编译参数和`@Param`注解的使用。同时,还涵盖了resultType与resultMap的区别,并提供了完整的代码实例和测试用例。
mybatis复习02,简单的增删改查,@Param注解多个参数,resultType与resultMap的区别,#{}预编译参数
|
7月前
|
Java 数据库连接 mybatis
Mybatis查询传递单个参数和传递多个参数用法
Mybatis查询传递单个参数和传递多个参数用法
104 11
|
7月前
|
SQL
自定义SQL,可以利用MyBatisPlus的Wrapper来构建复杂的Where条件,如何自定义SQL呢?利用MyBatisPlus的Wrapper来构建Wh,在mapper方法参数中用Param注
自定义SQL,可以利用MyBatisPlus的Wrapper来构建复杂的Where条件,如何自定义SQL呢?利用MyBatisPlus的Wrapper来构建Wh,在mapper方法参数中用Param注
|
8月前
|
Java 数据库连接 mybatis
mybatis参数报错Parameter ‘docId‘ not found. Available parameters are [arg1, arg0, param1, param2]
mybatis参数报错Parameter ‘docId‘ not found. Available parameters are [arg1, arg0, param1, param2]
|
9月前
|
SQL Java 数据库连接
Mybatis如何通过泛型来动态调整返回参数
Mybatis如何通过泛型来动态调整返回参数
589 0
|
9月前
|
XML Java 数据库连接
MyBatis 解决上篇的参数绑定问题以及XML方式交互
MyBatis 解决上篇的参数绑定问题以及XML方式交互
91 0
|
9月前
|
SQL Java 数据库连接
Mybatis拦截器实现带参数SQL语句打印
Mybatis拦截器实现带参数SQL语句打印