1=1
where元素主要是用于简化查询语句中where部分的条件判断,where元素可以再
//测试类
@Test
public void getEmpByCondition() {
SqlSession sqlSession = SqlSessionUtils.getSqlSession();
DynamicSQLMapper mapper = sqlSession.getMapper(DynamicSQLMapper.class);
List emps= mapper.getEmpByCondition(new Emp(null, "张三", null, null, null, null));
System.out.println(emps);
}
结果如下:
choose、when、otherwise
相当于if...else if..else
@Test
public void getEmpByChoose() {
SqlSession sqlSession = SqlSessionUtils.getSqlSession();
DynamicSQLMapper mapper = sqlSession.getMapper(DynamicSQLMapper.class);
List emps = mapper.getEmpByChoose(new Emp(null, "张三", 23, "男", "[email protected]", null));
System.out.println(emps);
}
if a else if b else if c else d
,只会执行其中一个属性:
,
delete from t_emp where eid in
#{eid}
@Test
public void deleteMoreByArray() {
SqlSession sqlSession = SqlSessionUtils.getSqlSession();
DynamicSQLMapper mapper = sqlSession.getMapper(DynamicSQLMapper.class);
int result = mapper.deleteMoreByArray(new Integer[]{6, 7, 8, 9});
System.out.println(result);
}
结果如下:
其中的xml文件里的SQL语句也可以为:
insert into t_emp values
(null,#{emp.empName},#{emp.age},#{emp.sex},#{emp.email},null)
@Test
public void insertMoreByList() {
SqlSession sqlSession = SqlSessionUtils.getSqlSession();
DynamicSQLMapper mapper = sqlSession.getMapper(DynamicSQLMapper.class);
Emp emp1 = new Emp(null,"a",1,"男","[email protected]",null);
Emp emp2 = new Emp(null,"b",1,"男","[email protected]",null);
Emp emp3 = new Emp(null,"c",1,"男","[email protected]",null);
List emps = Arrays.asList(emp1, emp2, emp3);
int result = mapper.insertMoreByList(emps);
System.out.println(result);
}
标签eid,emp_name,age,sex,email
标签