Mybatis中常用注解

1、@MapperKey

  xxxMapper.xml配置文件

<!--根据ID查询出一条记录,并且使用Map集合封装结果集-->
<select id="queryEmployeeById" parameterType="java.lang.Integer" resultType="java.util.Map">
	SELECT EMPLOYEE_ID,EMPLOYEE_NAME,EMPLOYEE_PASSWORD,EMPLOYEE_AGE,DEPARTMENT_ID FROM employee
	WHERE EMPLOYEE_ID=#{employeeId}
</select>
<!--查询出所有的记录,并且使用Map集合封装结果集-->
<select id="queryEmployees" resultType="java.util.Map">
	SELECT EMPLOYEE_ID,EMPLOYEE_NAME,EMPLOYEE_PASSWORD,EMPLOYEE_AGE,DEPARTMENT_ID FROM employee;
</select>

  xxxMapper接口

public interface EmployeeMapper {
    // @MapKey注解中的值对应的数据库中的列名,返回的Map集合会将EMPLOYEE_NAME作为Map集合的键
    @MapKey("EMPLOYEE_NAME")
    public abstract Map<String,Object> queryEmployeeById(Integer id);

    @MapKey("EMPLOYEE_NAME")
    public abstract Map<String,Employee> queryEmployees();
}

  测试类

public class TestMybatis {
    // 根据指定ID查询Employee
    @Test
    public void test01() throws IOException {
        SqlSession session = TestMybatis.openSession();
        EmployeeMapper mapper = session.getMapper(EmployeeMapper.class);
        Map<String, Object> map =  mapper.queryEmployeeById(3);
        System.out.println(map);
    }

    // 查询所有的Employee
    @Test
    public void test02() throws IOException {
        SqlSession session = TestMybatis.openSession();
        EmployeeMapper mapper = session.getMapper(EmployeeMapper.class);
        Map<String, Employee> maps = mapper.queryEmployees();
        System.out.println(maps);
}

  测试结果

// 根据ID查询出一条记录 
{王五={EMPLOYEE_ID=3, EMPLOYEE_AGE=25, DEPARTMENT_ID=2, EMPLOYEE_NAME=王五, EMPLOYEE_PASSWORD=123}}
// 查询出所有记录
{
	李四={EMPLOYEE_ID=2, EMPLOYEE_AGE=24, DEPARTMENT_ID=1, EMPLOYEE_NAME=李四, EMPLOYEE_PASSWORD=123}, 
	张三={EMPLOYEE_ID=1, EMPLOYEE_AGE=23, DEPARTMENT_ID=1, EMPLOYEE_NAME=张三, EMPLOYEE_PASSWORD=123}, 
	王五={EMPLOYEE_ID=3, EMPLOYEE_AGE=25, DEPARTMENT_ID=2, EMPLOYEE_NAME=王五, EMPLOYEE_PASSWORD=123}, 
	陈九={EMPLOYEE_ID=7, EMPLOYEE_AGE=41, DEPARTMENT_ID=1, EMPLOYEE_NAME=陈九, EMPLOYEE_PASSWORD=123456789}, 
	沈六={EMPLOYEE_ID=4, EMPLOYEE_AGE=26, DEPARTMENT_ID=2, EMPLOYEE_NAME=沈六, EMPLOYEE_PASSWORD=123}, 
	林八={EMPLOYEE_ID=6, EMPLOYEE_AGE=28, DEPARTMENT_ID=3, EMPLOYEE_NAME=林八, EMPLOYEE_PASSWORD=456}, 
	汪十={EMPLOYEE_ID=9, EMPLOYEE_AGE=30, DEPARTMENT_ID=30, EMPLOYEE_NAME=汪十, EMPLOYEE_PASSWORD=321}, 
	田七={EMPLOYEE_ID=5, EMPLOYEE_AGE=27, DEPARTMENT_ID=3, EMPLOYEE_NAME=田七, EMPLOYEE_PASSWORD=456}
}

  

2、@Param

  例如:当传入多个参数的时候我们可以通过以下方式获取值

public abstract User queryUserByThreeConditions(Integer id, String userName, String password);
<select id="queryUserByThreeConditions" resultType="com.mybatis.entity.User">
	SELECT ID,USER_NAME,PASSWORD,USER_INFORMATION FROM user
	WHERE ID=#{param1} AND USER_NAME=#{param2} AND PASSWORD=#{param3}
</select>  
  
<select id="queryUserByThreeConditions" resultType="com.mybatis.entity.User">
	SELECT ID,USER_NAME,PASSWORD,USER_INFORMATION FROM user
	WHERE ID=#{arg0} AND USER_NAME=#{arg1} AND PASSWORD=#{arg2}
</select>

  但是如果在mapper.xml文件中写上arg0、arg、arg2或者是param1、param2、param3等会影响阅读性,所以我们改进为下面的方式:使用@param("key")注解,显示的指出键的值,在mapper.xml文件中通过键找值取出值即可,这样就可以很明显的表达出出入的参数的作用

public abstract User queryUserByThreeConditions(@Param("userId") Integer id, @Param("userName") String name, @Param("password") String pwd);
<select id="queryUserByThreeConditions" resultType="com.mybatis.entity.User">
	SELECT ID,USER_NAME,PASSWORD,USER_INFORMATION FROM user
	WHERE ID=#{userId} AND USER_NAME=#{userName} AND PASSWORD=#{password}
</select> 

  

 

posted @ 2020-09-13 17:05  变体精灵  阅读(528)  评论(0编辑  收藏  举报