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

原文地址:https://www.cnblogs.com/yy3b2007com/p/6423489.html