[mybatis]映射文件_select_返回集合(List,Map)

Posted 唐火

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了[mybatis]映射文件_select_返回集合(List,Map)相关的知识,希望对你有一定的参考价值。

select返回List

public interface EmployeeMapper 

    public List<Employee> getEmpsByLastNameLike(String lastName);
    
    <!--    public List<Employee> getEmpsByLastNameLike(String lastName);-->
    <!--resultType;如果返回的是一个集合,要写集合中元素的类型-->


    <select id = "getEmpsByLastNameLike" resultType="com.atguigu.mybatis.bean.Employee">

select * from tb1_employee where last_name like #lastName
</select>
   @Test
    public void test03() throws IOException 
        SqlSessionFactory sqlSessionFactory = getSqlSessionFactory();

        SqlSession sqlSession = sqlSessionFactory.openSession();

        try
        
            EmployeeMapper mapper = sqlSession.getMapper(EmployeeMapper.class);

         List<Employee> like = mapper.getEmpsByLastNameLike("%e%");

         for (Employee employee : like)
         
             System.out.println(employee);
         

        finally 

            sqlSession.close();

        
    

select返回map

public interface EmployeeMapper 

    //返回一条记录的map;key就是列名,值就是对应的值
    public Map<String,Object> getEmpByIdReturnMap(Integer id);
    
    <!--    public Map<String,Object> getEmpByIdReturnMap(Integer id);-->
    <select id="getEmpByIdReturnMap" resultType="map">
    select * from tb1_employee where id = #id

</select>

  @Test
    public void test03() throws IOException 
        SqlSessionFactory sqlSessionFactory = getSqlSessionFactory();

        SqlSession sqlSession = sqlSessionFactory.openSession();

        try
        
            EmployeeMapper mapper = sqlSession.getMapper(EmployeeMapper.class);

        Map<String,Object> map = mapper.getEmpByIdReturnMap(1);

            System.out.println(map);

        finally 

            sqlSession.close();

        
    

select返回多条记录封装的map

public interface EmployeeMapper 
    //多条记录封装一个map:Map<String, Employee>:键是这条记录的主键,值是这条记录的主键,值是记录封装后的javaBean
    //告诉mybatis封装这个map的时候,使用哪个属性作为map的key
    
    @MapKey("id")
    public Map<Integer,Employee> getEmpByLastNameLikeReturnMap(String lastName);
    
    <!--resultType;如果返回的是一个集合,要写集合中元素的类型-->
  <!--    public Map<Integer,Employee> getEmpByLastNameLikeReturnMap(String lastName);-->
<select id="getEmpByLastNameLikeReturnMap" resultType="com.atguigu.mybatis.bean.Employee">
    select * from tb1_employee where last_name like #lastName

</select>
    @Test
    public void test03() throws IOException 
        SqlSessionFactory sqlSessionFactory = getSqlSessionFactory();

        SqlSession sqlSession = sqlSessionFactory.openSession();

        try
        
            EmployeeMapper mapper = sqlSession.getMapper(EmployeeMapper.class);

            Map<Integer, Employee> map = mapper.getEmpByLastNameLikeReturnMap("%j%");

            System.out.println(map);

        finally 

            sqlSession.close();

        
    

以上是关于[mybatis]映射文件_select_返回集合(List,Map)的主要内容,如果未能解决你的问题,请参考以下文章

[刘阳Java]_MyBatis_映射文件的常用标签总结_第5讲

[mybatis]映射文件_select_resultMap_关联查询_association分步查询&延迟加载

[mybatis]映射文件_select_resultMap_discriminator鉴别器

MyBatis__配置多对一映射返回

Mybatis-select-返回值类型错误理解

[mybatis]select_resultMap_自定义结果集映射规则