10, mybatis learning --sqlmapper configured to return a result set list and map

A return to list

  mapper interface methods:

  sqlmapper configuration file:

    <! - according to name fuzzy query returns a list 
            resultType written list of which types of multiple results mybatis automatically added to the list in -> 
    < the SELECT the above mentioned id = "selectEmpByNameLike" resultType = "the Employee" > 
        the SELECT * from the WHERE name like the Employee {name} # 
    </ SELECT >

  Test Methods:

    @Test
    public void testSelectEmpByNameLike() throws IOException {
        String source = "mybatis-config.xml";
        InputStream inputStream = Resources.getResourceAsStream(source);
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
        SqlSession sqlSession = sqlSessionFactory.openSession();
        EmployeeMapper employeeMapper = sqlSession.getMapper(EmployeeMapper.class);
        System.out.println(employeeMapper.selectEmpByNameLike("%o%"));
        sqlSession.close();
    }

 

 

Second, a record query returns a map

  mapper interface methods:

 

   sqlmapper file configuration

    <! - returns a map record; key is the column name, a column value is the value corresponding to 
        the query result is only a map value when resultType, MyBatis automatically take many alias java types -> 
    < SELECT ID = "selectEmpByIdReturnMap " the resultType =" Map " > 
        SELECT * WHERE ID from Employee ID = # {} 
    </ SELECT >

  Test Methods:

    @Test
    public void testSelectEmpByIdReturnMap() throws IOException {
        String source = "mybatis-config.xml";
        InputStream inputStream = Resources.getResourceAsStream(source);
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
        
        SqlSession sqlSession = sqlSessionFactory.openSession();
        
        EmployeeMapper employeeMapper = sqlSession.getMapper(EmployeeMapper.class);
        System.out.println(employeeMapper.selectEmpByIdReturnMap(1));
        sqlSession.close();
    }

 

Third, the query returns multiple records a map

  mapper interface methods:

 

   sqlmapper configuration file:

    <! - returns multiple records map; key map corresponding to the value of @MapKey interface method, the value is the key object corresponding to 
    the query result is returned object type when a plurality of resultType -> 
    < SELECT ID = "selectEmpByNameLikeReturnMap" the resultType = "Employee" > 
        SELECT * WHERE name like from Employee # {name} 
    </ SELECT >

  Test Methods:

    @Test
    public void testSelectEmpByNameLikeReturnMap() throws IOException {
        String source = "mybatis-config.xml";
        InputStream inputStream = Resources.getResourceAsStream(source);
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
        
        SqlSession sqlSession = sqlSessionFactory.openSession();
        
        EmployeeMapper employeeMapper = sqlSession.getMapper(EmployeeMapper.class);
        System.out.println(employeeMapper.selectEmpByNameLikeReturnMap("%o%"));
        sqlSession.close();
    }

 

Guess you like

Origin www.cnblogs.com/lyh233/p/12346354.html