Springboot 集成Mybatis 增刪改查 使用XML 動態SQL


依據https://www.cnblogs.com/suphowe/p/13157474.html 進行修改

一、mapper\TestMapper.xml

1、if語句

    <!-- 使用if進行條件判斷,where 元素只會在子元素返回任何內容的情況下才插入 "WHERE" 子句。-->
    <!-- 而且,若子句的開頭為 “AND” 或 “OR”,where 元素也會將它們去除 -->
    <select id="findByNameUseIf" resultType="com.soft.entity.Test">
        select * from sys_test
        <where>
            <if test="name != null">
            and name=#{name}
            </if>
        </where>
    </select>

2、like查詢

    <!-- like 查詢 -->
    <select id="findByNameUseLike" resultType="com.soft.entity.Test">
        select * from sys_test
        <where>
            <if test="name != null">
                and name like concat('%',#{name},'%')
            </if>
        </where>
    </select>

3、Choose、When、Otherwise

    <!-- 傳入了 “user” 就按 “user” 查找,傳入了 “name” 就按 “name” 查找,傳入了 “tel” 就按 “tel” 查找的情形。-->
    <!-- 若三者都沒有傳入,就返回標記為 1=1 的 BLOG -->
    <select id="findUseChooseWhenOtherwise" resultType="com.soft.entity.Test">
        select * from sys_test where 1=1
        <choose>
            <when test="user != null">
                and user=#{title}
            </when>
            <when test="name != null">
                and name=#{name}
            </when>
            <when test="tel != null">
                and tel like concat('%',#{tel},'%')
            </when>
            <otherwise>
                and 1 = 1
            </otherwise>
        </choose>
    </select>

4、Set

    <!-- set 元素可以用於動態包含需要更新的列,忽略其它不更新的列 -->
    <update id="updateSysTestUseSet" parameterType="com.soft.entity.Test">
        update sys_test
        <set>
            <if test="user != null">user=#{user},</if>
            <if test="name != null">name=#{name},</if>
            <if test="tel != null">tel=#{tel}</if>
        </set>
        where id=#{id}
    </update>

二、Java代碼

com\soft\dao\MybatisTestMapper.java

新增:

    List<Test> findByNameUseIf(Test test);

    List<Test> findByNameUseLike(Test test);

    List<Test> findUseChooseWhenOtherwise(Test test);

    int updateSysTestUseSet(Test test);

com\soft\service\impl\MybatisTestServiceImpl.java

新增:

    @Override
    public List<Test> findByNameUseIf(String name) {
        Test test = new Test();
        test.setName(name);
        return mybatisTestMapper.findByNameUseIf(test);
    }

    @Override
    public List<Test> findByNameUseLike(String name) {
        Test test = new Test();
        test.setName(name);
        return mybatisTestMapper.findByNameUseLike(test);
    }

    @Override
    public List<Test> findUseChooseWhenOtherwise(String user, String name, String tel) {
        Test test = new Test();
        test.setUser(user);
        test.setName(name);
        test.setTel(tel);
        return mybatisTestMapper.findUseChooseWhenOtherwise(test);
    }

    @Override
    public HashMap<String, Object> updateSysTestUseSet(int id, String user, String name, String tel) {
        Test test = new Test();
        test.setId(id);
        test.setUser(user);
        test.setName(name);
        test.setTel(tel);
        int result = mybatisTestMapper.updateSysTestUseSet(test);
        HashMap<String, Object> returnMap = new HashMap<>(1);
        returnMap.put("count", result);
        return returnMap;
    }

com\soft\service\IMybatisServiceTest.java

新增:

    List<Test> findByNameUseIf(String name);

    List<Test> findByNameUseLike(String name);

    List<Test> findUseChooseWhenOtherwise(String user, String name, String tel);

    HashMap<String, Object> updateSysTestUseSet(int id, String user, String name, String tel);

com\soft\controller\MybatisTestController.java

新增:

    @RequestMapping(value = "/findByNameUseIf", method = RequestMethod.POST)
    @ApiOperation(value = "Mybatis查詢測試,動態Sql,if語句", notes = "按名稱查詢")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "name", value = "name", dataType = "String")
    })
    public String findByNameUseIf(String name) {
        List<Test> list = mybatisServiceTest.findByNameUseIf(name);
        HashMap<String, Object> result = new HashMap<>();
        bsUtil.createReturnMsg(result, 200, list);
        return new Gson().toJson(result);
    }

    @RequestMapping(value = "/findByNameUseLike", method = RequestMethod.POST)
    @ApiOperation(value = "Mybatis查詢測試,動態Sql,Like寫法", notes = "按名稱查詢")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "name", value = "name", dataType = "String")
    })
    public String findByNameUseLike(String name) {
        List<Test> list = mybatisServiceTest.findByNameUseLike(name);
        HashMap<String, Object> result = new HashMap<>();
        bsUtil.createReturnMsg(result, 200, list);
        return new Gson().toJson(result);
    }

    @RequestMapping(value = "/findUseChooseWhenOtherwise", method = RequestMethod.POST)
    @ApiOperation(value = "Mybatis查詢測試,動態Sql,choose、when、otherwise", notes = "查詢")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "user", value = "user", dataType = "String"),
            @ApiImplicitParam(paramType = "query", name = "name", value = "name", dataType = "String"),
            @ApiImplicitParam(paramType = "query", name = "tel", value = "tel", dataType = "String")
    })
    public String findUseChooseWhenOtherwise(String user, String name, String tel) {
        List<Test> list = mybatisServiceTest.findUseChooseWhenOtherwise(user, name, tel);
        HashMap<String, Object> result = new HashMap<>();
        bsUtil.createReturnMsg(result, 200, list);
        return new Gson().toJson(result);
    }

    @RequestMapping(value = "/updateSysTestUseSet", method = RequestMethod.POST)
    @ApiOperation(value = "Mybatis測試,動態Sql,使用Set", notes = "修改")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "id", value = "id", dataType = "int"),
            @ApiImplicitParam(paramType = "query", name = "user", value = "user", dataType = "String"),
            @ApiImplicitParam(paramType = "query", name = "name", value = "name", dataType = "String"),
            @ApiImplicitParam(paramType = "query", name = "tel", value = "tel", dataType = "String")
    })
    public String updateSysTestUseSet(int id, String user, String name, String tel) {
        HashMap<String, Object> updateSysTest = mybatisServiceTest.updateSysTestUseSet(id, user, name, tel);
        HashMap<String, Object> result = new HashMap<>();
        bsUtil.createReturnMsg(result, 200, updateSysTest);
        return new Gson().toJson(result);
    }

三、測試

1、if測試

 

2、like測試

 

 

3、choose、when、otherwise

 

 

 

 4、set

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM