表:
实体类:
public class Emp { private Integer empId; private String empName; private Integer age; private String gender; public Emp() { } public Emp(Integer empId, String empName, Integer age, String gender) { this.empId = empId; this.empName = empName; this.age = age; this.gender = gender; } public Integer getEmpId() { return empId; } public void setEmpId(Integer empId) { this.empId = empId; } public String getEmpName() { return empName; } public void setEmpName(String empName) { this.empName = empName; } public Integer getAge() { return age; } public void setAge(Integer age) { this.age = age; } public String getGender() { return gender; } public void setGender(String gender) { this.gender = gender; } @Override public String toString() { return "Emp{" + "empId=" + empId + ", empName='" + empName + '\'' + ", age=" + age + ", gender='" + gender + '\'' + '}'; } }
1.if
if 标签可通过 test 属性的表达式进行判断,若表达式的结果为 true ,则标签中的内容会执行;反之标签中的内容不会执行
<select id="getEmpByyCondition" resultType="com.atguigu.mybatis.pojo.Emp"> select * from t_emp where 1=1 <if test="empName!=null and empName!=''"> and emp_name=#{empName} </if> <if test="age!=null and age!=''"> and age=#{age} </if> <if test="gender!=null and gender!=''"> and gender=#{gender} </if> </select>
测试:
public void test6(){ SqlSessionUtils sqlSessionUtils = new SqlSessionUtils(); SqlSession sqlSession = sqlSessionUtils.getSqlSession(); EmpMapper mapper = sqlSession.getMapper(EmpMapper.class); Emp emp = new Emp(null,"张三",null,null); List<Emp> list = mapper.getEmpByyCondition(emp); System.out.println(list); }
2.where
where和if一般结合使用:
若where标签中的if条件都不满足,则where标签没有任何功能,即不会添加where关键字
若where标签中的if条件满足,则where标签会自动添加where关键字,并将条件最前方多余的and去掉
注意:where标签不能去掉条件最后多余的and
<select id="getEmpByyCondition" resultType="com.atguigu.mybatis.pojo.Emp"> select * from t_emp <where> <if test="empName!=null and empName!=''"> and emp_name=#{empName} </if> <if test="age!=null and age!=''"> and age=#{age} </if> <if test="gender!=null and gender!=''"> and gender=#{gender} </if> </where> </select>
1.若where标签中的if条件都不满足,则where标签没有任何功能
public void test6(){ SqlSessionUtils sqlSessionUtils = new SqlSessionUtils(); SqlSession sqlSession = sqlSessionUtils.getSqlSession(); EmpMapper mapper = sqlSession.getMapper(EmpMapper.class); Emp emp = new Emp(null,null,null,null); List<Emp> list = mapper.getEmpByyCondition(emp); System.out.println(list); }
2.若where标签中的if条件满足,则where标签会自动添加where关键字,并将条件最前方多余的and去掉
public void test7(){ SqlSessionUtils sqlSessionUtils = new SqlSessionUtils(); SqlSession sqlSession = sqlSessionUtils.getSqlSession(); EmpMapper mapper = sqlSession.getMapper(EmpMapper.class); Emp emp = new Emp(null,null,20,null); List<Emp> list = mapper.getEmpByyCondition(emp); System.out.println(list); }
3.trim
trim用于去掉或添加标签中的内容
常用属性:
prefix:在trim标签中的内容的前面添加某些内容
prefixOverrides:在trim标签中的内容的前面去掉某些内容
suffix:在trim标签中的内容的后面添加某些内容
suffixOverrides:在trim标签中的内容的后面去掉某些内容
<select id="getEmpByyCondition" resultType="com.atguigu.mybatis.pojo.Emp"> select * from t_emp <trim prefix="where" suffixOverrides="and"> <if test="empName!=null and empName!=''"> emp_name=#{empName} and </if> <if test="age!=null and age!=''"> age=#{age}and </if> <if test="gender!=null and gender!=''"> gender=#{gender} </if> </trim> </select>
4.choose、when、otherwise
choose、when、 otherwise相当于if...else if..else
<select id="getEmpByChoose" resultType="com.atguigu.mybatis.pojo.Emp"> select * from t_emp <where> <choose> <when test="empName!=null and empName!=''">emp_name=#{empName} </when> <when test="age!=null and age!=''">ge=#{age} </when> <when test="gender!=null and gender!=''"> gender=#{gender}</when> </choose> </where> </select>
public void test7(){ SqlSessionUtils sqlSessionUtils = new SqlSessionUtils(); SqlSession sqlSession = sqlSessionUtils.getSqlSession(); EmpMapper mapper = sqlSession.getMapper(EmpMapper.class); Emp emp = new Emp(null,"张三",21,null); List<Emp> list = mapper.getEmpByChoose(emp); System.out.println(list); }
因为when相当于else if 所以当前面有条件满足时,不会执行后面的when语句
5.foreach
collection: 需做foreach(遍历)的对象,作为入参时,list、array对象时,collection属性值分别默认用"list"、"array"代替,Map对象没有默认的属性值。但是,在作为入参时可以使用@Param(“keyName”)注解来设置自定义collection属性值,设置keyName后,list、array会失效;
item: 集合元素迭代时的别名称,该参数为必选项;
index: 在list、array中,index为元素的序号索引。但是在Map中,index为遍历元素的key值,该参数为可选项;
open: 遍历集合时的开始符号,通常与close=")"搭配使用。使用场景IN(),values()时,该参数为可选项;
separator: 元素之间的分隔符,类比在IN()的时候,separator=",",最终所有遍历的元素将会以设定的(,)逗号符号隔开,该参数为可选项;
close: 遍历集合时的结束符号,通常与open="("搭配使用,该参数为可选项;
1.批量添加数据
void insertEmps(@Param("emps") List<Emp> emps);
<insert id="insertEmps"> insert into t_emp value <foreach collection="emps" item="emp" separator=","> (null,#{emp.empName},#{emp.age},#{emp.gender},null) </foreach> </insert>
public void test8(){ SqlSessionUtils sqlSessionUtils = new SqlSessionUtils(); SqlSession sqlSession = sqlSessionUtils.getSqlSession(); EmpMapper mapper = sqlSession.getMapper(EmpMapper.class); Emp emp1 = new Emp(null,"数据1",21,"女"); Emp emp2 = new Emp(null,"数据2",24,"男"); Emp emp3 = new Emp(null,"数据3",31,"女"); List<Emp> emps = Arrays.asList(emp1, emp2, emp3); mapper.insertEmps(emps); }
运行结果:
添加数据前:
添加数据后:
2.批量删除数据
void deleteByEmpIds(@Param("empIds") Integer[] empIds);
<delete id="deleteByEmpIds"> delete from t_emp where emp_id in <foreach collection="empIds" item="empId" separator="," open="(" close=")"> #{empId} </foreach> </delete>
public void test9(){ SqlSessionUtils sqlSessionUtils = new SqlSessionUtils(); SqlSession sqlSession = sqlSessionUtils.getSqlSession(); EmpMapper mapper = sqlSession.getMapper(EmpMapper.class); Integer empIds[]={6,7}; mapper.deleteByEmpIds(empIds); }
方法2:
<delete id="deleteByEmpIds"> delete from t_emp where <foreach collection="empIds" item="empId" separator="or" > emp_id=#{empId} </foreach> </delete>
6.SQL片段
sql片段,可以记录一段公共sql片段,在使用的地方通过include标签进行引
List<Emp> selectAll();
<sql id="empColumns"> emp_id,emp_name,age,gender</sql> <select id="selectAll" resultType="com.atguigu.mybatis.pojo.Emp"> select <include refid="empColumns"></include> from t_emp </select>
public void test10(){ SqlSessionUtils sqlSessionUtils = new SqlSessionUtils(); SqlSession sqlSession = sqlSessionUtils.getSqlSession(); EmpMapper mapper = sqlSession.getMapper(EmpMapper.class); List<Emp> emps = mapper.selectAll(); System.out.println(emps); }