1. 程式人生 > >5.通過參數註解多個接口參數的用法

5.通過參數註解多個接口參數的用法

names lena get Coding simple extend .org 通過 face

一、參數註解

遇到多個參數,因為不能因為兩三個參數去創建新的JavaBean類,對於參數比較少的情況下可以使用@Param註解。

UserMapper.java
package tk.mybatis.simple.mapper;

import org.apache.ibatis.annotations.Param;
import tk.mybatis.simple.model.SysRole;
import tk.mybatis.simple.model.SysUser;

import java.util.List;

/**
 * @author weihu
 * @date 2018/8/3/003 0:01
 
*/ public interface UserMapper { /** * 根據用戶id和角色的enabled狀態獲取用戶的角色 */ List<SysRole> selectRolesByUserIdAndRoleEnabled(@Param("userId") Long userId,@Param("enabled") Integer enabled); }

UserMapper.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"> <!--sql語句映射文件--> <!--因為已經配置了別名,所以resultType可以直接寫類名,沒有配置的話就要寫類全名--> <mapper namespace="tk.mybatis.simple.mapper.UserMapper"> <select id="selectRolesByUserIdAndRoleEnabled" resultType="tk.mybatis.simple.model.SysRole
"> select r.id, r.role_name roleName, r.enabled, r.create_by createBy, r.create_time createTime from sys_user u inner join sys_user_role ur on u.id=ur.user_id inner join sys_role r on ur.role_id=r.id where u.id=#{userId} and r.enabled=#{enabled} </select> </mapper>
UserMapperTest.java
package tk.mybatis.simple.mapper;

import org.apache.ibatis.session.SqlSession;
import org.junit.Assert;
import org.junit.Test;
import tk.mybatis.simple.model.SysRole;
import tk.mybatis.simple.model.SysUser;

import java.util.Date;
import java.util.List;

/**
 * @author weihu
 * @date 2018/8/5/005 9:34
 * @desc
 */
public class UserMapperTest extends BaseMapperTest {

 

    public void testParam(){
        SqlSession sqlSession = getSqlSession();
        try {
            UserMapper userMapper = sqlSession.getMapper(UserMapper.class);
            List<SysRole> sysRoles = userMapper.selectRolesByUserIdAndRoleEnabled(1L, 1);
            System.out.println(sysRoles);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            sqlSession.close();
        }

    }
}

5.通過參數註解多個接口參數的用法