wordpress双栏,罗湖网站建设优化,医疗网站建设 飞沐,wordpress 后台登录文章目录 if标签where标签trim标签set标签choose when otherwise批量删除批量插入sql标签与include标签 if标签
1、if标签中的test属性是必须的 2、if标签中test属性的值是false或者true 3、如果test是true#xff0c;则if标签中的sql语句就会拼接#xff0c;反之#xff0… 文章目录 if标签where标签trim标签set标签choose when otherwise批量删除批量插入sql标签与include标签 if标签
1、if标签中的test属性是必须的 2、if标签中test属性的值是false或者true 3、如果test是true则if标签中的sql语句就会拼接反之则不会拼接 4、test属性中可以使用的是 当使用了Param注解那么test中要出现的是Param注解指定的参数名Param“brand”那么这里只能使用brand 当没有使用Param注解那么test中要出现的是param1param2arg0arg1… 当使用了POJO那么test中出现的是POJO类的属性名。 5、在mybatis的动态SQL当中不能使用只能使用and。
//CarMapper.javaListCar selectByMultiCondition(Param(brand)String brand,Param(guidePrice) Double guidePrice,Param(carType) String carType);!-- CarMapper.xml --select idselectByMultiCondition resultTypeCarselect * from t_car whereif testbrand ! null and brand ! brand like %#{brand}%/ifif testguidePrice ! null and guidePrice ! and guide_price #{guidePrice}/ifif testcarType ! null and carType ! and car_type #{carType}/if/select//CarMapperTest.javaTestpublic void testSelectById(){SqlSession sqlSession SqlSessionUtil.openSession();CarMapper mapper sqlSession.getMapper(CarMapper.class);ListCar cars mapper.selectByMultiCondition(比亚迪,2.0,新能源);cars.forEach(car - System.out.println(car));sqlSession.close();}↑但是上面写的SQL语句明显存在着逻辑的问题如果第一个判断语句为false里面的SQL语句没有执行而后面的加上了就存在SQL语句的语法错误 所以改为下面这种语句 select idselectByMultiCondition resultTypeCarselect * from t_car where 1 1if testbrand ! null and brand ! and brand like %#{brand}%/ifif testguidePrice ! null and guidePrice ! and guide_price #{guidePrice}/ifif testcarType ! null and carType ! and car_type #{carType}/if/selectwhere标签
where标签的作用让where子句更加多态智能
所有条件都为空时where标签保证不会生成where子句。自动去除某些条件前面多余的and或or wher标签是专门负责where子句动态生成的 select idselectByMultiConditionWhere resultTypeCarselect * from t_carwhereif testbrand ! null and brand ! and brand like %#{brand}%/ifif testguidePrice ! null and guidePrice ! and guide_price #{guidePrice}/ifif testcarType ! null and carType ! and car_type #{carType}/if/where/selecttrim标签
trim标签的属性
prefix在trim标签中的语句前添加内容suffix在trim标签中的语句后添加内容prefixOverrides前缀覆盖掉去掉suffixOverrides后缀覆盖掉去掉 select idselectByMultiConditionTrim resultTypeCarselect * from t_cartrim prefixwhere suffixOverridesand|orif testbrand ! null and brand ! brand like %#{brand}% and/ifif testguidePrice ! null and guidePrice ! guide_price #{guidePrice} and/ifif testcarType ! null and carType ! car_type #{carType}/if/trim/selectprefixwhere是在trim标签所有内容的前面添加wheresuffixOverridesand|or把trim标签中内容的后缀and或or去掉
set标签
主要使用在update语句当中用来生成set关键字同时去掉最后多余的“” 比如我们只更新提交的不为空的字段如果提交的数据是空或者“ ”那么这个字段我们将不更新
int updateWithSet(Car car)update idupdateWithSetupdate t_carsetif testcarNum ! null and carNum ! car_num #{carNum},/ifif testbrand ! null and brand ! brand #{brand},/ifif testguidePrice ! null and guidePrice ! guide_price #{guidePrice},/ifif testproduceTime ! null and produceTime ! produce_time #{produceTime},/ifif testcarType ! null and carType ! car_type #{carType}/if/setwhere id #{id}/updateTestpublic void testBySet(){SqlSession sqlSession SqlSessionUtil.openSession();CarMapper mapper sqlSession.getMapper(CarMapper.class);Car car new Car(5L,null,null,丰田sucker,null,null);mapper.updateWithSet(car);sqlSession.commit();sqlSession.close();}choose when otherwise
这三个标签是在一起使用的 相当于if、elseif、else 只有一个分支会被选择 ListCar selectByChoose(Param(brand)String brand,Param(guidePrice)Double guidePrice,Param(carType)String carType);select idselectByChoose resultTypeCarselect * from t_carwherechoosewhen testbrand ! null and brand ! brand like %#{brand}%/whenwhen testguidePrice ! null and guidePrice ! guide_price #{guidePrice}/whenotherwisecar_type #{carType}/otherwise/choose/where/selectTestpublic void testchoose(){SqlSession sqlSession SqlSessionUtil.openSession();CarMapper mapper sqlSession.getMapper(CarMapper.class);ListCar cars mapper.selectByChoose(, 5.0,null);cars.forEach(car - System.out.println(car));sqlSession.close();}批量删除 int deleteByIds(String ids[]);foreach标签的属性
collection指定数组或者集合item代表数组或集合中的元素separator循环之间的分隔符 delete iddeleteByIdsdelete from t_car where id in(foreach collectionids itembaga separator,#{baga}/foreach)/deleteTestpublic void testDelteAll(){SqlSession sqlSession SqlSessionUtil.openSession();CarMapper mapper sqlSession.getMapper(CarMapper.class);Long[] ids {7L, 8L, 9L};int i mapper.deleteByIds(ids);System.out.println(i);sqlSession.commit();sqlSession.close();}批量插入
int insertBatch(Param(cars)ListCar cars);insert idinsertBatchinsert into t_car valuesforeach collectioncars itemcar separator, (null,#{car.carNum},#{car.brand},#{car.guidePrice},#{car.carType},#{car.produceTime})/foreach/insertTestpublic void testInsertBatch(){SqlSession sqlSession SqlSessionUtil.openSession();CarMapper mapper sqlSession.getMapper(CarMapper.class);Car car1 new Car(null,八嘎亚路1,30.8,燃油第,null,111);Car car2 new Car(null,八嘎亚路2,30.8,燃油第,null,111);Car car3 new Car(null,八嘎亚路3,30.8,燃油第,null,111);ListCar 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语句当中 作用代码复用易维护
sql idcarColumnNameSqlid,car_num as carNum,brnd,guide_price as guidePrice,produce_time as produceTime,car_type as carType
/sql
select id.... resultTypecarselectinclude refidcarColumnNameSqlfrom t_car where id #{id}
/select