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标签* @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标签* @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();}
主要用在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标签* @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();}
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标签用来声明sql片段
include标签用来将声明的sql片段包含到某个sql语句中