set标签
存放修改方法,我们之前写的更新方法是全字段的更新,或者是指定字段的更新,现在我想实现一个新功能,传入的Employee包含什么字段,数据库就更新相对应的列值:
如果我们啥也不做直接上<if>标签看看会发生什么
- <update id="updateEmp">
- update tb_employee set
- <if test="lastName!=null">
- last_name = #{lastName},
- </if>
- <if test="email!=null">
- email=#{email},
- </if>
- <if test="gender!=null">
- gender=#{gender}
- </if>
- where id=#{id}
- </update>
- @Test
- public void testGetEmpsByConditionIf() throws IOException {
- SqlSessionFactory sqlSessionFactory = getSqlSessionFactory();
- SqlSession openSession = sqlSessionFactory.openSession(true);
- try {
- EmployeeMapperDynamic mapper = openSession.getMapper(EmployeeMapperDynamic.class);
- Employee e = new Employee();
- e.setId(5);
- e.setLastName("王大锤");
- mapper.updateEmp(e);
- }finally {
- openSession.close();
- }
- }
DEBUG [main] - ==> Preparing: update tb_employee set last_name = ?, where id=?
出错原因很明显,多出了个逗号,接下来将这些<if>标签套在<set>再做尝试
- <update id="updateEmp">
- update tb_employee
- <set>
- <if test="lastName!=null">
- last_name = #{lastName},
- </if>
- <if test="email!=null">
- email=#{email},
- </if>
- <if test="gender!=null">
- gender=#{gender}
- </if>
- </set>
- where id=#{id}
- </update>
发现成功,同样的我们也可以用<trim>标签实现
- <update id="updateEmp">
- update tb_employee
- <trim prefix="set" suffixOverrides=",">
- <if test="lastName!=null">
- last_name = #{lastName},
- </if>
- <if test="email!=null">
- email=#{email},
- </if>
- <if test="gender!=null">
- gender=#{gender}
- </if>
- </trim>
- where id=#{id}
- </update>
foreach标签
<foreach>可以应用于SQL语句中where id in(…….)这种类型的查询。
<foreach>含有两个属性,一个是collection,指定要遍历的集合,另外一个是item,即将当前遍历的元素赋给指定的变量,接下来我们使用#{变量名}就可以取出变量的值,也就是当前遍历出的元素,
- <select id="getEmpsByConditionForeach" resultType="com.figsprite.bean.Employee">
- select * from tb_employee where id in (
- <foreach collection="ids" item="item_id" separator="," >
- #{item_id}
- </foreach>
- )
- </select>
属性separate指定元素之间的分隔符,当然上面的写法有些不美观()中套着很多东西,不方便查看,因此,我们再使用两个其他属性,让编码更加美观:
- <select id="getEmpsByConditionForeach" resultType="com.figsprite.bean.Employee">
- select * from tb_employee
- <foreach collection="ids" item="item_id" separator="," open=" where id in (" close=")">
- #{item_id}
- </foreach>
- </select>
注意括号不要写成中文的,不然这里的括号很难看出中英文差别。
还有一个属性index ,当遍历list时,它代表索引,当遍历map是,它就成了key
List<Employee> getEmpsByConditionForeach(@Param("ids") List<Integer> ids);
- @Test
- public void testGetEmpsByCondition() throws IOException {
- SqlSessionFactory sqlSessionFactory = getSqlSessionFactory();
- SqlSession openSession = sqlSessionFactory.openSession(true);
- try {
- EmployeeMapperDynamic mapper = openSession.getMapper(EmployeeMapperDynamic.class);
- List<Employee> list = mapper.getEmpsByConditionForeach(Arrays.asList(6,7));
- for (Employee emp : list) {
- System.out.println(emp);
- }
- }finally {
- openSession.close();
- }
- }
DEBUG [main] - ==> Preparing: select * from tb_employee where id in ( ? , ? )
DEBUG [main] - ==> Parameters: 6(Integer), 7(Integer)
DEBUG [main] - <== Total: 2
Employee{id=6, lastName='吴花琴', gender='0', email='qweqwe', department=null}
Employee{id=7, lastName='吴小明', gender='1', email='wewqewq', department=null}
<foreach>标签 批量保存
在MYSQL中,我们如果想批量插入就要用到以下方式的SQL语句:
- INSERT INTO tb_employee(last_name,email,gender,d_id) VALUES('tom','sdasdasd@gmail.com','0',1),
- ('jerry','asdasd@qq.com','0',1);
同样我们可以通过<foreach>来实现这个功能
void addEmps(@Param("emps") List<Employee> emps);
- <insert id="addEmps">
- INSERT INTO tb_employee(last_name,email,gender,d_id) VALUES
- <foreach collection="emps" item="emp" separator=",">
- (#{emp.lastName},#{emp.email},#{emp.gender},#{emp.department.id})
- </foreach>
- </insert>
- @Test
- public void TestSaveBach() throws IOException {
- SqlSessionFactory sqlSessionFactory = getSqlSessionFactory();
- SqlSession openSession = sqlSessionFactory.openSession(true);
- Department department = new Department(2);
- Employee e1 = new Employee("Wendy","0","adcxs@qq.com",department);
- Employee e2 = new Employee("Diyago","1","aaasaadccjdjj@qq.con",department);
- List<Employee> employeeList = new ArrayList<>();
- employeeList.add(e1);
- employeeList.add(e2);
- try{
- EmployeeMapperDynamic mapper = openSession.getMapper(EmployeeMapperDynamic.class);
- mapper.addEmps(employeeList);
- }finally {
- openSession.close();
- }
- }
DEBUG [main] - ==> Preparing: INSERT INTO tb_employee(last_name,email,gender,d_id) VALUES (?,?,?,?) , (?,?,?,?)
MySQl支持这种VALUES ( ),( ),( ),还可以这么写:
每次取出一个员工,就插入
- <insert id="addEmps">
- <foreach collection="emps" item="emp" separator=";">
- INSERT INTO tb_employee(last_name,email,gender,d_id) VALUES
- (#{emp.lastName},#{emp.email},#{emp.gender},#{emp.department.id})
- </foreach>
- </insert>
发现出错了,主要原因JDBC链接属性不能支持多SQL语句用;分开的形式,我们需要将这个选项开启,其中有一个allowMultiQueries,我们只要把它的值改成true即可,
jdbc.url=jdbc:mysql://localhost:3306/mybatis_learn?allowMultiQueries=true
,再去尝试,发现就可以了,这种方式也可以用于批量修改和删除,十分方便