mybatis 使用tips - 使用多个参数
执行如下命令:
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 - 使用多个参数的更多相关文章
- MyBatis传入集合 list 数组 map参数的写法
foreach的主要用在构建in条件中,它可以在SQL语句中进行迭代一个集合.foreach元素的属性主要有item,index,collection,open,separator,close.ite ...
- Mybatis 实用篇(三)参数处理
Mybatis 实用篇(三)参数处理 sql 语句中的参数 parameterType 可以省略不写. 一.参数封装 1.1 单个参数处理 public interface UserMapper { ...
- mybatis按datetime条件查询,参数为时间戳时
mybatis按datetime条件查询,参数为时间戳时,如果数据库为2018-1-1 20:22:10, 你的时间戳也为2018-1-1 20:22:10,但却没找到数据.可能是时差导致的.百度修正 ...
- MyBatis映射文件中用#和$传递参数的特点
在MyBatis映射文件中用#和$传递参数的特点, #是以占位符的形式来传递对应变量的参数值的,框架会对传入的参数做预编译的动作, 用$时会将传入的变量的参数值原样的传递过去,并且用$传递传递参数的时 ...
- MyBatis中抽象方法中多个参数的问题
在使用MyBatis时,接口中的抽象方法只允许有1个参数,如果有多个参数,例如: Integer updatePassword( Integer id, String ...
- Mybatis接口中传递多个参数
1.接口 public interface MemberMapper { public boolean insertMember(Members member); public Members sel ...
- Mybatis的mapper接口接受的参数类型
最近项目用到了Mybatis,学一下记下来. Mybatis的Mapper文件中的select.insert.update.delete元素中有一个parameterType属性,用于对应的mappe ...
- 8.mybatis动态SQL模糊查询 (多参数查询,使用parameterType)
多参数查询,使用parameterType.实例: 用户User[id, name, age] 1.mysql建表并插入数据 2.Java实体类 public class User { public ...
- Mybatis的parameterType传入多个参数
如果查询的条件有多个的时候,mybatis有三种传入方式: 1.通过注解传入 例如: public interface Mapper(){ public User login(@Param(" ...
随机推荐
- 01:初识Redis
付磊和张益军两位大咖写的葵花宝典(Redis开发和运维)学习笔记. 一.初识Redis 1.redis简介 Redis是一种基于键值对(key-value)的NoSQL数据库,与很多键值对数据库不同的 ...
- 自动执行单元测试maven插件
<plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-suref ...
- uva-10815-字符串排序
又偷懒了,字符串排序,贱贱的用了std:map #include <iostream> #include <sstream> #include<algorithm> ...
- mybatis 1 - 获取自增ID
1.环境: mybatis : 3.2.3 spring-mybatis: 1.2.1 mysql:5.5.29 实体: public class sys_user { private int us ...
- C# 使用printDocument1.Print打印时不显示 正在打印对话框(里面还有一个讨厌的取消按钮)
C#使用printDocument1.Print打印时不显示正在打印对话框有两种方法 第一种,使用PrintController PrintController printController = n ...
- Spring Boot实践——Spring Boot 2.0 新特性和发展方向
出自:https://mp.weixin.qq.com/s/EWmuzsgHueHcSB0WH-3AQw 以Java 8 为基准 Spring Boot 2.0 要求Java 版本必须8以上, Jav ...
- eclipse双击变量高亮显示开关
在eclipse/myeclipse中如果不小心把变量的高亮显示弄丢了.可真是件愁人的事,不过看到这你就不用愁了 windows-> preferences-> java-> E ...
- Java常用的输出调试技巧
--------siwuxie095 Eclipse 开发中常用的输出调试技巧: 先在左侧的 Package Explorer,右键->New->J ...
- 使用mybatis提供的各种标签方法实现动态拼接Sql。这里演示where标签和if标签实现使用姓名的模糊查询和性别查询用户列表,当用户没有选择姓名以及性别时查询出所有的记录。
1.需求: 使用姓名的模糊查询和性别查询用户列表,当用户没有选择姓名以及性别时查询出所有的记录. 2.在UserMapper接口中定义方法: public List<User> findU ...
- A simple way to crack VBA password in Excel file
Unbelivibale, but I found a very simple way that really works! Do the follwoing: 1. Create a new sim ...