3.4 set标签
👉功能
主要用于解决set关键字及多出一个【,】问题
👉用法案例
修改员工的信息
代码示例如下:
①在EmployeeMapper接口中定义修改员工的信息的方法
//修改员工的信息 public void updateEmp(Employee employee);
②在EmployeeMapper接口对应的映射文件中书写相应的sql
问题版(会出现多一个【,】问题
)
<update id="updateEmp"> update tbl_employee set <if test="lastName != null"> last_name=#{lastName} , </if> <if test="email != null"> email=#{email} , </if> <if test="salary != null"> salary=#{salary} , </if> where id=#{id} </update>
set标签解决问题版
<update id="updateEmp"> update tbl_employee <set> <if test="lastName != null"> last_name=#{lastName} , </if> <if test="email != null"> email=#{email} , </if> <if test="salary != null"> salary=#{salary} , </if> </set> where id=#{id} </update>
③测试
@Test public void test03(){ try { String resource = "mybatis-config.xml"; InputStream inputStream = Resources.getResourceAsStream(resource); SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream); //通过SqlSessionFactory对象调用openSession(); SqlSession sqlSession = sqlSessionFactory.openSession(); //获取EmployeeMapper的代理对象 EmployeeMapper employeeMapper = sqlSession.getMapper(EmployeeMapper.class); //动态参数 Employee employee=new Employee(); employee.setId(1); employee.setLastName("tom"); employee.setSalary(16800.0); employeeMapper.updateEmp(employee); xml } catch (IOException e) { e.printStackTrace(); } }
使用问题版的sql进行测试
使用set标签解决问题版的sql进行测试
3.5 choose标签
👉功能
类似java中if-else【switch-case】结构
👉应用场景
应用于单个条件不确定的业务场景
👉用法案例
不指定查询条件,查询对应的员工信息(单个条件不确定的)
代码示例如下:
①在EmployeeMapper接口书写相应的方法
//不指定查询条件,查询对应员工信息(单个条件不确定的) public List<Employee> selectEmpByOneOpr(int empId);
②在EmployeeMapper接口对应的映射文件中书写相应的sql
<!-- 根据查询条件查找对应的员工信息(条件不确定) 动态的sql(trim标签优化版) --> <select id="selectEmpByOneOpr" resultType="employee"> SELECT `id`, `last_name`, `email`, `salary`, `dept_id` FROM `tbl_employee` <where> <choose> <when test="id !=null"> id=#{id} </when> <when test="lastName != null"> last_name=#{lastName} </when> <when test="email != null"> email=#{email} </when> <otherwise> salary=#{salary} </otherwise> </choose> </where> </select>
③测试
@Test public void test04(){ try { String resource = "mybatis-config.xml"; InputStream inputStream = Resources.getResourceAsStream(resource); SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream); //通过SqlSessionFactory对象调用openSession(); SqlSession sqlSession = sqlSessionFactory.openSession(); //获取EmployeeMapper的代理对象 EmployeeMapper employeeMapper = sqlSession.getMapper(EmployeeMapper.class); List<Employee> employees = employeeMapper.selectEmpByOneOpr(1); System.out.println(employees); } catch (IOException e) { e.printStackTrace(); } }
3.6 foreach标签
👉功能
类似java中for循环
👉标签属性
collection
:要迭代的集合item
:当前从集合中迭代出的元素separator
:元素与元素之间的分隔符open
:开始字符close
:结束字符
👉应用场景
🚩 ①遍历迭代
用法案例
通过多个id获取员工的信息 【EmpIds:员工id的集合】
代码示例如下:
a.在EmployeeMapper接口定义相应的方法
/** * 通过多个id获取员工的信息 【EmpIds:员工id的集合】 * @param EmpIds * @return */ public List<Employee> selectEmpByIds(@Param("ids") List<Integer> EmpIds);
b.在EmployeeMapper接口对应的映射文件中定义相应的sql
<select id="selectEmpByIds" resultType="employee"> SELECT `id`, `last_name`, `email`, `salary`, `dept_id` FROM `tbl_employee` <where> `id` in ( <foreach collection="ids" item="id" separator=","> #{id} </foreach> ) </where> </select>
c.测试
@Test public void test04(){ try { String resource = "mybatis-config.xml"; InputStream inputStream = Resources.getResourceAsStream(resource); SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream); //通过SqlSessionFactory对象调用openSession(); SqlSession sqlSession = sqlSessionFactory.openSession(); //获取EmployeeMapper的代理对象 EmployeeMapper employeeMapper = sqlSession.getMapper(EmployeeMapper.class); List<Employee> employees = employeeMapper.selectEmpByOneOpr(1); System.out.println(employees); } catch (IOException e) { e.printStackTrace(); } }
🚩②批量导入
用法案例
批量添加员工数据
代码示例如下:
a.在EmployeeMapper接口定义相应的方法
//批量添加员工数据 public void batchInsertEmp(@Param("emps") List<Employee> employees);
b.在EmployeeMapper接口对应的映射文件中定义相应的sql
// 批量添加员工数据,使用insert标签书写相应的sql <insert id="batchInsertEmp"> insert into tbl_employee(last_name,email,salary) values <foreach collection="emps" item="emp" separator=","> (#{emp.lastName},#{emp.email},#{emp.salary}) </foreach> </insert>
c.测试
@Test public void test06(){ try { String resource = "mybatis-config.xml"; InputStream inputStream = Resources.getResourceAsStream(resource); SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream); //通过SqlSessionFactory对象调用openSession(); SqlSession sqlSession = sqlSessionFactory.openSession(); //获取EmployeeMapper的代理对象 EmployeeMapper employeeMapper = sqlSession.getMapper(EmployeeMapper.class); //定义要添加的员工集合 List<Employee> list=new ArrayList<>(); list.add(new Employee("zhangsan","sdhjsd@qq.com",6700.0)); list.add(new Employee("wangwu","dddhjsd@123.com",9700.0)); employeeMapper.batchInsertEmp(list); sqlSession.commit(); } catch (IOException e) { e.printStackTrace(); } }
3.7 sql标签
👉功能
提取可重用SQL片段
❗注意
该SQL片段可以是一个完整的sql语句,也可以是一个sql语句中的某个片段)
👉用法案例
使用sql标签对3.6小节中的应用场景1的案例里映射文件里的的”select xxx,xxxx,xxx from
xxxx”部分提取出来,作为一个可重用的sql片段,在select>标签内引入该sql片段
代码示例如下:
①使用sql标签抽取映射文件中”select xxx,xxxx,xxx from xxxx”部分片段作为可重用的sql片段
<!-- 抽取映射文件中”select xxx,xxxx,xxx from xxxx”部分片段作为可重用的sql片段 --> <sql id="select_employee"> SELECT `id`, `last_name`, `email`, `salary`, `dept_id` FROM `tbl_employee` </sql> <select id="selectEmpByIds" resultType="employee"> <!-- 将刚才抽取的sql片段select_employee引入进来 --> <include refid="select_employee"></include> <where> `id` in ( <foreach collection="ids" item="id" separator=","> #{id} </foreach> ) </where> </select>
②测试运行