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(" ...
随机推荐
- php 文件缓存类
//文件缓存类 class FileCache { private $cacheTime = 3600; //默认缓存时间 秒 private $cacheDir = './filecache'; / ...
- SPM——How to use github
In this semester, we take a class called 'Software Project Management'. And in this class, we have l ...
- cesium初始化参数
var viewer = new Cesium.Viewer('cesiumContainer',{ animation:false, //动画控制不显示 //baseLayerPicker:fals ...
- 一些linux知识和http知识
1 yum安装比源码编译安装 有的模块不能自定义安装 只能安装默认的模块进行安装 2 关于php的fastcgi 如果使用fastcgi 那么需要启动服务 如果不使用fastcgi 那么不需要启 ...
- leetcode400
public class Solution { public int FindNthDigit(int n) { //StringBuilder sb = new StringBuilder(); / ...
- Cordova+Vue构建Hybrid APP简易实操
当下APP市场,因为Native APP开发成本高,Web APP不稳定,混合开发APP大行其道,成为越来越多开发者的首选.Hybrid APP开发框架也比较多,Weex.Ionic.PhoneGap ...
- VB.Net条形码编程的方法
一.条形码的读取用过键盘口式的扫条码工具的朋友就知道,它就如同在鍵盘上按下数字鍵一样,基本不需任何编程和处理.但如果你使用的是其它接口的话,可能你就要为该设备编写通讯代码了.以下有一段简单的25针串口 ...
- js中元素结点的引用
<!DOCTYPE html><html lang="en"><head> <meta charset="UTF-8" ...
- Could not load TestContextBootstrapper [null]
在对SpringBoot进行单元测试时,报错“Could not load TestContextBootstrapper [null]” 错误原因: Maven的pom.xml中某些SpringBo ...
- 四种常见的 POST 提交数据方式,使用postman会用到
http://www.aikaiyuan.com/6324.html 用postman时候,选错了就出不来结果.用postman选择 application/x-www-form-urlencoded