Mybatis系列番外篇之多参数

来源:互联网 发布:大数据 云存储 编辑:程序博客网 时间:2024/06/14 14:16

转载自Mybatis系列番外篇之多参数

Mybatis系列番外篇之多参数

引言

使用过Mybatis的小伙伴们都知道,在映射文件中只能使用parameterType属性指定一个传入参数,可是在实际的项目中,往往需要用到多个传入参数,那么应该如何实现呢?本文就以Mybatis接口式编程方式来分享一下我的实现方式。

分层设计

在实际的工作项目中,需要使用用户和角色的概念对系统权限进行管理,那么就引出了为用户分配角色的问题。

Service层

通常我们会在Service层定义一个方法用来为用户添加角色。这个方法需要传入两个参数,一个是用户id,另一个是角色id列表,方法签名如下。

public void grantRoles(int userId, List<Integer> roleIds) throws NotFoundException;  

Dao层

但是在Dao层却不可以使用这样的方法签名,因为Mybatis的映射文件中,只能使用parameterType指定一个传入参数,所以,方法签名就变成这个样子了。

public void grantRoles(Map<String, Object> parameter);  

这里使用了Map类型对Service层传入的两个参数进行了包装,将其转为一个参数。

当然,在编写映射文件时,也可以不使用parameterType属性来指定传入参数类型,这样就可以使用类似#{0},#{1}…方式来顺序获取传入的参数。
只是这种方式不适用于此场景中,因为在为用户分配角色时,角色的数量是不确定的,这样就没法确定传入参数的数量。所以使用Map类型对传入参数进行封装。
Mybatis映射文件

<insert id="grantRoles" parameterType="java.util.Map">      INSERT IGNORE sec_user_role(user_id, role_id) VALUES      <foreach collection="roleIds" item="item" index="index" separator=",">             (#{userId}, #{item})         </foreach>  </insert>  

在配置文件中出现的userIdroleIds是则是Map参数中的两个Key,#{userId}则是取出对应的value,由于roleIds对应的值是一个List类型,所以,这里使用了foreach标签遍历列表中的所有值。

Service层实现代码

@Override  public void grantRoles(int userId, List<Integer> roleIds) throws NotFoundException {      User user = this.getById(userId);      if (null != user) {          Map<String, Object> parameter = new HashMap<String, Object>();          parameter.put("userId", userId);          parameter.put("roleIds", roleIds);          dao.grantRoles(parameter);      } else {          String msg = "数据库中查找不到Id为[" + userId + "]的用户!";          logger.error(msg);          throw new NotFoundException(msg);      }  }  

其它方式

参考Java高并发秒杀API之业务分析与DAO层

如下的接口方法,方法参数加上了@Param()注解:

/** * 插入购买明细,可过滤重复 * @param seckillId * @param userPhone * @return 插入的行数 */int insertSuccessKilled(@Param("seckillId")long seckillId, @Param("userPhone") long userPhone);

对应的Mapper中select语句如下:

<insert id="insertSuccessKilled">    <!-- 主键冲突,报错 -->    insert ignore into success_killed(seckill_id, user_phone, state)    values(#{seckillId}, #{userPhone}, 0)</insert>

可以封装到一个对象中

public List<Message> queryMessageList(String command, String description){    DBAccess dbAccess =  new DBAccess();    SqlSession sqlSession = null;    List<Message> messageList = new ArrayList<>();    try {        sqlSession = dbAccess.getSqlSession();        //多个参数 封装到一个对象中        Message message = new Message();        message.setCommand(command);        message.setDescription(description);        //通过sqlSession执行SQL语句        //messageList = sqlSession.selectList("Message.queryMessageList", message);        //接口编程        IMessage iMessage = sqlSession.getMapper(IMessage.class);        messageList = iMessage.queryMessageList(message);    } catch (Exception e) {        e.printStackTrace();    } finally {        if (sqlSession != null) {            sqlSession.close();        }    }    return messageList;}
0 0