MyBatis(二):Select語句傳遞參數的集中方案


從別人說的方案中看出,傳遞參數方案還挺多,不如自己整理下,以便以后使用過程中有個筆記回憶錄。

  • 1、傳遞一個參數的用法:

配置文件

<select id="getById" resultType="TaskAutoExecutePlan" resultMap="TaskAutoExecutePlanResult" parameterType="java.lang.String">
        select * from TaskAutoExecutePlan where id=#{id}
    </select>

注意:這里邊需要在dao層傳遞定義的接口類對應getById函數的參數名最好也是id,類型為String,否則也不會出現異常(我測試的結果是通過了,比如:函數寫成這樣getById(String name))。

Mapper.java

public TaskAutoExecutePlan getById(String id);

Server.java

    @Override
    public TaskAutoExecutePlan getById(String id) {
        SqlSession sqlSession = this.factory.openSession();
        TaskAutoExecutePlan result = sqlSession.selectOne("com.xxx.mapper.TaskAutoExecutePlanMapper.getById",id);
        sqlSession.commit();
        sqlSession.close();

        return result;
    }
  • 2、傳遞多個參數的用法,使用Index來替換參數名稱:

配置文件

 <select id="getByTaskStatusAndAutoExecutePlanType" resultType="TaskAutoExecutePlan" resultMap="TaskAutoExecutePlanResult">
        select * from TaskAutoExecutePlan where taskStatus=#{0} and autoExecutePlanType=#{1}
    </select>

需要注意:

1、#{index}這個更像是用在有IOC的框架中使用,但是目前我並沒有使用IOC。

2、如果在傳遞參數是不能使用List<Object>這種集合來存儲參數。

3、目前我使用Map<String,Object>這種方式,雖然實現了Mapper的實現類Server,但不代表他是正宗的用法。

4、目前我這種通過Map參數實現傳遞Index參數的方案,Map元素中的值不能是Enum類型,即使在配置文件參數中指定類型也不正確。

Mapper.java

  public List<TaskAutoExecutePlan> getByTaskStatusAndAutoExecutePlanType(TaskStatus taskStatus, AutoExecutePlanType autoExecutePlanType);

Server.java

    @Override
    public List<TaskAutoExecutePlan> getByTaskStatusAndAutoExecutePlanType(TaskStatus taskStatus, AutoExecutePlanType autoExecutePlanType) {
        SqlSession sqlSession = this.factory.openSession();

        Map<String,Object> parameters=new java.util.HashMap<>();
        parameters.put("0",taskStatus.getValue());
        parameters.put("1",autoExecutePlanType.getValue());

        List<TaskAutoExecutePlan> result = sqlSession.selectList("com.xxx.mapper.TaskAutoExecutePlanMapper.getByTaskStatusAndAutoExecutePlanType",parameters);
        sqlSession.commit();
        sqlSession.close();

        return result;
    }
  • 3、Map封裝多參數:

配置文件:

  <select id="getByTaskStatusAndAutoExecutePlanType" resultType="TaskAutoExecutePlan" resultMap="TaskAutoExecutePlanResult" parameterType="java.util.Map">
        select * from TaskAutoExecutePlan where taskStatus=#{taskStatus,jdbcType=INTEGER} and autoExecutePlanType=#{autoExecutePlanType,jdbcType=INTEGER}
    </select>

需要注意:這里不能直接把Enum存儲到Map的元素中,第一:方面enum不是object對象,第二:默認TaskStatus.Doing會被轉化為“Doing”字符串,參數不是整數意義發生了變換。

Mapper.java

    public List<TaskAutoExecutePlan> getByTaskStatusAndAutoExecutePlanType(Map<String,Object> parameters);

Server.java

    @Override
    public List<TaskAutoExecutePlan> getByTaskStatusAndAutoExecutePlanType(Map<String,Object> parameters) {
        SqlSession sqlSession = this.factory.openSession();
        List<TaskAutoExecutePlan> result = sqlSession.selectList("com.xxx.mapper.TaskAutoExecutePlanMapper.getByTaskStatusAndAutoExecutePlanType",parameters);
        sqlSession.commit();
        sqlSession.close();

        return result;
    }

調用代碼:

        Map<String,java.lang.Object> parameters=new java.util.HashMap<>();
        parameters.put("taskStatus",TaskStatus.Doing.getValue());
        parameters.put("autoExecutePlanType",AutoExecutePlanType.MrRasterization.getValue());

        List<TaskAutoExecutePlan> items1 = taskAutoExecutePlanServer.getByTaskStatusAndAutoExecutePlanType(parameters);
        for (TaskAutoExecutePlan item : items1)
            System.out.println(item.getId() + "," + item.getAutoExecutePlanType() + "," + item.getTaskStatus() + "," + item.getCreateDate() + "," + item.getModifyDate());
  • 4、List封裝in

配置文件:

    <select id="getByTaskStatusAndAutoExecutePlanType" resultType="TaskAutoExecutePlan" resultMap="TaskAutoExecutePlanResult">
        select * from TaskAutoExecutePlan where id in
        <foreach item="item" index="index" collection="list" open="(" separator="," close=")">#{item,jdbcType=VARCHAR}</foreach>
    </select>

Mapper.java

    public List<TaskAutoExecutePlan> getByTaskStatusAndAutoExecutePlanType(List<String> list);

Server.java

    @Override
    public List<TaskAutoExecutePlan> getByTaskStatusAndAutoExecutePlanType(List<String> list) {
        SqlSession sqlSession = this.factory.openSession();
        List<TaskAutoExecutePlan> result = sqlSession.selectList("com.xxx.mapper.TaskAutoExecutePlanMapper.getByTaskStatusAndAutoExecutePlanType",list);
        sqlSession.commit();
        sqlSession.close();

        return result;
    }

調用:

 List<String> list=new java.util.ArrayList<>();
        list.add("6a5987a6f78c11e69fb300fffdc16f2e");
        list.add("0729bf7ef78d11e69fb300fffdc16f2e");

        List<TaskAutoExecutePlan> items1 = taskAutoExecutePlanServer.getByTaskStatusAndAutoExecutePlanType(list);
        for (TaskAutoExecutePlan item : items1)
            System.out.println(item.getId() + "," + item.getAutoExecutePlanType() + "," + item.getTaskStatus() + "," + item.getCreateDate() + "," + item.getModifyDate());

 

其他方式還有混合調用、注解等方式,這些需要學習了Spring+MyBatis后在使用。

更多用法請參考:http://www.cnblogs.com/mingyue1818/p/3714162.html


免責聲明!

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



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