【MyBatis】动态SQL
创始人
2024-02-11 22:43:20
0

if标签

CarMapper.java

/*** 多条件查询* @param brand 品牌* @param guidePrice 指导价* @param carType 汽车类型* @return*/List selectByMultiCondition(@Param("brand") String brand,@Param("guidePrice") Double guidePrice,@Param("carType") String carType);

CarMapper.xml

   

test.java

    @Testpublic void testSelectByMuItiCondition(){SqlSession sqlSession = SqlSessionUtil.openSession();CarMapper mapper = sqlSession.getMapper(CarMapper.class);//假设三个条件都不是空List cars = mapper.selectByMultiCondition("比亚泰迪",2.0,"新能源");//假设三个条件都是空//假设后两个条件不为空,第一个条件为空//假设第一个条件不为空,第二个条件为空cars.forEach(car -> System.out.println(car));sqlSession.close();}

where标签

作用:让where子句更加动态智能

  • 所有条件都为空时,where标签保证不会生成where子句
  • 自动去除某些条件前面多余的and或or
    mapper接口
 /*** 使用where标签* @param brand* @param guidePrice* @param carType* @return*/List selectByMultiConditionWithWhere(@Param("brand") String brand,@Param("guidePrice") Double guidePrice,@Param("carType") String carType);

xml

    

test

 @Testpublic void testselectByMultiConditionWithWhere(){SqlSession sqlSession = SqlSessionUtil.openSession();CarMapper mapper = sqlSession.getMapper(CarMapper.class);//三个条件都是空List cars = mapper.selectByMultiConditionWithWhere("",null,"");cars.forEach(car -> System.out.println(car));sqlSession.close();}

trim标签

接口

/*** 使用trim标签* @param brand* @param guidePrice* @param carType* @return*/List selectByMultiConditionWithTrim(@Param("brand") String brand,@Param("guidePrice") Double guidePrice,@Param("carType") String carType);

xml

 

test

@Testpublic void testselectByMultiConditionWithTrim(){SqlSession sqlSession = SqlSessionUtil.openSession();CarMapper mapper = sqlSession.getMapper(CarMapper.class);List cars = mapper.selectByMultiConditionWithTrim("",null,"");cars.forEach(car -> System.out.println(car));sqlSession.close();}

set标签

主要用在update语句中,用来生成set关键字,同时去除最后多余的“,”
比如我们只更新提交的不为空的字段,如果提交的数据是空或者“”,那么这个字段我们将不更新。
接口

    /*** 使用set标签* @param car* @return*/int updateBySet(Car car);

xml

update t_carcar_num=#{carNum},brand=#{brand},guide_price=#{guidePrice},produce_time=#{produceTime},car_type=#{carType},whereid=#{id}

test

@Testpublic void testUpdateBySet(){SqlSession sqlSession = SqlSessionUtil.openSession();CarMapper mapper = sqlSession.getMapper(CarMapper.class);Car car=new Car(2L,null,"丰田道",null,null,"燃油车");mapper.updateBySet(car);sqlSession.commit();sqlSession.close();}

choose when otherwise

这三个标签在一起使用
接口

 /*** 使用choose when otherwise标签* @param brand* @param guidePrice* @param carType* @return*/List selectByChoose(@Param("brand") String brand,@Param("guidePrice") Double guidePrice,@Param("carType") String carType);

xml

 

test

 @Testpublic void testselectByChoose(){SqlSession sqlSession=SqlSessionUtil.openSession();CarMapper mapper = sqlSession.getMapper(CarMapper.class);List cars = mapper.selectByChoose("宾利", null, null);cars.forEach(car -> System.out.println(car));sqlSession.close();}

foreach标签

批量删除

java

 /*** 批量删除,foreach标签* @param ids* @return*/int deleteByIds(@Param("ids") Long[] ids);

xml

 





id}-->



delete from t_car where id in#{id}

test

  @Testpublic void testDeleteByIds(){SqlSession sqlSession = SqlSessionUtil.openSession();CarMapper mapper = sqlSession.getMapper(CarMapper.class);Long[] ids={7L,8L,9L};int count=mapper.deleteByIds(ids);System.out.println(count);sqlSession.commit();sqlSession.close();}

第二种写法
xml

id=#{id}

批量添加

java

 /*** 批量插入,一次插入多条Car信息* @param cars* @return*/int insertBatch(@Param("cars") List cars);

xml

    insert into t_car values(null,#{car.carNum},#{car.brand},#{car.guidePrice},#{car.produceTime},#{car.carType})

test

   @Testpublic void testinsertBatch(){SqlSession sqlSession = SqlSessionUtil.openSession();CarMapper mapper = sqlSession.getMapper(CarMapper.class);Car car1=new Car(null,"1200","帕萨特",30.0,"2020-11-15","燃油车");Car car2=new Car(null,"1201","帕萨特CDS",50.0,"2021-11-15","燃油车");Car car3=new Car(null,"1202","奔驰",40.0,"2024-11-15","新能源");List cars=new ArrayList<>();cars.add(car1);cars.add(car2);cars.add(car3);mapper.insertBatch(cars);sqlSession.commit();sqlSession.close();}

sql标签 include标签

sql标签用来声明sql片段
include标签用来将声明的sql片段包含到某个sql语句中

在这里插入图片描述

相关内容

热门资讯

喜欢穿一身黑的男生性格(喜欢穿... 今天百科达人给各位分享喜欢穿一身黑的男生性格的知识,其中也会对喜欢穿一身黑衣服的男人人好相处吗进行解...
发春是什么意思(思春和发春是什... 本篇文章极速百科给大家谈谈发春是什么意思,以及思春和发春是什么意思对应的知识点,希望对各位有所帮助,...
网络用语zl是什么意思(zl是... 今天给各位分享网络用语zl是什么意思的知识,其中也会对zl是啥意思是什么网络用语进行解释,如果能碰巧...
为什么酷狗音乐自己唱的歌不能下... 本篇文章极速百科小编给大家谈谈为什么酷狗音乐自己唱的歌不能下载到本地?,以及为什么酷狗下载的歌曲不是...
华为下载未安装的文件去哪找(华... 今天百科达人给各位分享华为下载未安装的文件去哪找的知识,其中也会对华为下载未安装的文件去哪找到进行解...
家里可以做假山养金鱼吗(假山能... 今天百科达人给各位分享家里可以做假山养金鱼吗的知识,其中也会对假山能放鱼缸里吗进行解释,如果能碰巧解...
四分五裂是什么生肖什么动物(四... 本篇文章极速百科小编给大家谈谈四分五裂是什么生肖什么动物,以及四分五裂打一生肖是什么对应的知识点,希...
怎么往应用助手里添加应用(应用... 今天百科达人给各位分享怎么往应用助手里添加应用的知识,其中也会对应用助手怎么添加微信进行解释,如果能...
客厅放八骏马摆件可以吗(家里摆... 今天给各位分享客厅放八骏马摆件可以吗的知识,其中也会对家里摆八骏马摆件好吗进行解释,如果能碰巧解决你...
美团联名卡审核成功待激活(美团... 今天百科达人给各位分享美团联名卡审核成功待激活的知识,其中也会对美团联名卡审核未通过进行解释,如果能...