mybatis动态sql之修改(学习set标签)

mybatis动态sql之修改(学习set标签),第1张

概述EmployeeMapperDynamicSql.java package com.gong.mybatis.mapper; import java.util.List; import java.ut

EmployeeMapperDynamicsql.java

package com.gong.mybatis.mapper;import java.util.List; java.util.Map; org.apache.ibatis.annotations.MapKey; com.gong.mybatis.bean.Employee;public interface EmployeeMapperDynamicsql {    public List<Employee> getEmpByConditionIf(Employee employee);        voID updateEmp(Employee employee);}

EmployeeMapperDynamicsql.xml

<?xml version="1.0" enCoding="UTF-8" ?><!DOCTYPE mapper PUBliC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd"> <mapper namespace="com.gong.mybatis.mapper.EmployeeMapperDynamicsql">    <!-- 查询,要查那个就带上那个条件 -->    select ID="getEmpByConditionIf" resultType="com.gong.mybatis.bean.Employee">        select * from tbl_employee         where>            choose>                when test="ID!=null"                    ID=#{ID}                </when="lastname!=null"                    last_name like #{lastname}                ="email!=null"                    email=#{email}                otherwise>        select>        update ="updateEmp"        update tbl_employee         set        if             last_name=#{lastname},if="gender!=null and gender == 0 or gender == 1"            gender=#{gender},1)">            email=#{email}                where ID=#{ID}    update>mapper>

结合之前的知识,我们在这里不使用set标签

进行测试:

 com.gong.mybatis.test; java.io.IOException; java.io.inputStream; org.apache.ibatis.io.Resources; org.apache.ibatis.session.sqlSession; org.apache.ibatis.session.sqlSessionFactory; org.apache.ibatis.session.sqlSessionFactoryBuilder; org.junit.Test; com.gong.mybatis.bean.Employee; com.gong.mybatis.mapper.EmployeeMapperDynamicsql;class TestMybatis3 {        public sqlSessionFactory getsqlSessionFactory() throws IOException {        String resource = "mybatis-config.xml";        inputStream is = Resources.getResourceAsstream(resource);        return new sqlSessionFactoryBuilder().build(is);    }    @Test    voID test()  IOException {        sqlSessionFactory sqlSessionFactory = getsqlSessionFactory();        sqlSession openSession = sqlSessionFactory.openSession();        try {            EmployeeMapperDynamicsql mapper = openSession.getMapper(EmployeeMapperDynamicsql.);            Employee employee =  Employee();            employee.setID(1);            employee.setLastname("hanbin");//            employee.setGender("0");            employee.setEmail("dema@qq.com");            mapper.updateEmp(employee);            List<Employee> es = mapper.getEmpByConditionIf( Employee());            for(Employee e:es) {                System.err.println(e);            }            openSession.commit();        } finally {            openSession.close();        }            }    }

我们先对ID=1的记录修改last_name=hanbin,结果:会报错,因为sql最后面多了个逗号

DEBUG 01-21 14:31:18,323 ==>  Preparing: update tbl_employee set last_name=?, where ID=?   (BaseJdbcLogger.java:145) DEBUG 01-21 14:31:18,366 ==> Parameters: hanbin(String),1(Integer)  (BaseJdbcLogger.java:145) 

解决方法有两种:

第一种:使用set标签,会自动过滤掉多余的逗号

            update tbl_employee         set                last_name=#{lastname},1)">                gender=#{gender},1)">                email=#{email}            >

结果:

DEBUG 01-21 14:46:11,726 ==>  Preparing: select * from tbl_employee   (BaseJdbcLogger.java:145) DEBUG 01-21 14:46:11,727 ==> Parameters:   (BaseJdbcLogger.java:145) DEBUG 01-21 14:46:11,756 ==      Total: 4  (BaseJdbcLogger.java:145) Employee [ID=1,lastname=hanbin,1)">gender=0,1)">email=hanbin@qq.com,1)">dept=null]

第二种:使用之前学过的trim标签

    trim prefix="set" suffixOverrIDes=","trim>

并修改ID=1的last_name为dema,gender为1,email为dema@qq.com

            employee.setID(1);            employee.setLastname("dema");            employee.setGender("1");            employee.setEmail("dema@qq.com");

结果:

DEBUG 01-21 14:53:38,851 ==>  Preparing: select * from tbl_employee   (BaseJdbcLogger.java:145) DEBUG 01-21 14:53:38,852 ==> Parameters:   (BaseJdbcLogger.java:145) DEBUG 01-21 14:53:38,885 =dema,1)">=dema@qq.com,1)">=null]Employee [ID=2,1)">=jack,1)">=675544321@qq.com,1)">=3,1)">=小红,1)">=xiaohong@qq.com,1)">=4,1)">=小明,1)">=xiaoming@qq.com,1)">=null]
总结

以上是内存溢出为你收集整理的mybatis动态sql之修改(学习set标签)全部内容,希望文章能够帮你解决mybatis动态sql之修改(学习set标签)所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/langs/1218993.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-06-05
下一篇 2022-06-05

发表评论

登录后才能评论

评论列表(0条)

保存