【CarMapper.xml样例】
【测试样例】:
@Test
public void testSelectByMultiCondition() {
SqlSession sqlSession = SqlSessionUtil.openSession();
com.mapper.CarMapper mapper = sqlSession.getMapper(com.mapper.CarMapper.class);
List cars = mapper.selectByMultiCondition("比亚迪", 2.0, "新能源");
cars.forEach(car -> System.out.println(car));
sqlSession.close();
}
【作用】:让where标签更加动态智能。
【CarMapper.xml样例】:
【测试样例】:
@Test
public void testSelectByMultiConditionWithWhere() {
SqlSession sqlSession = SqlSessionUtil.openSession();
com.mapper.CarMapper mapper = sqlSession.getMapper(com.mapper.CarMapper.class);
//List cars = mapper.selectByMultiConditionWithWhere("比亚迪", 2.0, "新能源");
List cars = mapper.selectByMultiConditionWithWhere("", null, "");
cars.forEach(car -> System.out.println(car));
sqlSession.close();
}
【CarMapper.xml样例】:
【测试样例】:
@Test
public void testselectByMultiConditionWithTrim() {
SqlSession sqlSession = SqlSessionUtil.openSession();
com.mapper.CarMapper mapper = sqlSession.getMapper(com.mapper.CarMapper.class);
//List cars = mapper.selectByMultiConditionWithWhere("比亚迪", 2.0, "新能源");
List cars = mapper.selectByMultiConditionWithTrim("比亚迪", null, "");
cars.forEach(car -> System.out.println(car));
sqlSession.close();
}
【作用】:主要使用在update语句当中,用来生成set关键字,同时去掉最后多余的" ,",就是说只更新我们提交的不为空的数据
【CarMapper.xml样例】
update t_car
car_num=#{carNum},
brand=#{brand},
guide_price=#{guidePrice},
produce_time=#{produceTime},
car_type=#{carType},
where id=#{id}
【测试样例】:
public void testUpdateBySet() {
SqlSession sqlSession = SqlSessionUtil.openSession();
com.mapper.CarMapper mapper = sqlSession.getMapper(com.mapper.CarMapper.class);
Car car = new Car(19L, null, "丰田霸道", null, null, "燃油车");
int i = mapper.updateBySet(car);
System.out.println(i);
sqlSession.commit();
sqlSession.close();
}
【注意】:choose when otherwise三个标签是在一起的
if () {
} else if () {
} else if () {
} else if () {
} else {
}
只有一个条件会被选择
【需求示范】:先根据品牌查询,如果没有提供品牌,再根据指导价格查询,如果没有提供指导价格,就根据知道日期查询。
【CarMapper.xml样例】:
【测试样例】:
@Test
public void testSelectByChoose() {
SqlSession sqlSession = SqlSessionUtil.openSession();
com.mapper.CarMapper mapper = sqlSession.getMapper(com.mapper.CarMapper.class);
List cars = mapper.selectByChoose("东风", null, null);
cars.forEach(car -> System.out.println(car));
sqlSession.close();
}
foreach标签属性:
【CarMapper.xml样例】:
delete from t_car where id in(
#{id}
)
【测试样例】:
@Test
public void testDeleteById() {
SqlSession sqlSession = SqlSessionUtil.openSession();
com.mapper.CarMapper mapper = sqlSession.getMapper(com.mapper.CarMapper.class);
Long[] ids={12L,9L};
int i = mapper.deleteByIds(ids);
System.out.println("i = " + i);
}
【CarMapper.xml样例】:
insert into t_car values
(NULL,
#{car.carNum},
#{car.brand},
#{car.guidePrice},
#{car.produceTime},
#{car.carType})
【测试样例】:
@Test
public void testInsertBatch() {
SqlSession sqlSession = SqlSessionUtil.openSession();
com.mapper.CarMapper mapper = sqlSession.getMapper(com.mapper.CarMapper.class);
List cars=new ArrayList<>();
Car car1 = new Car(null,"1211","帕萨特",23.0,"2022-10-22","燃油车");
Car car2 = new Car(null,"1212","梅赛德斯",21.0,"2022-11-23","燃油车");
cars.add(car1);
cars.add(car2);
int i = mapper.insertBatch(cars);
System.out.println("i = " + i);
}