MyBatis! Passing multiple parameters to Mapper DAO

I try to send several parameters to the Mapper function defined in the DAO implementation, but I cannot send more than one parameter if the parameter does not own any class. I mean, how can I change the following code -

obj.getName(int a, int b);

In the implementation of DAO

public void getAllName() throws PersistenceException {
        SqlSession session = sf.openSession();
        try {
            session.selectList("getNames");         
        } finally {
            session.close();
        }
    }

I want to send a and b to request getNames.

Thanks in advance.

+3
source share
2 answers

If you are using a DAO implementation, you can do this using HashMap. Just add a key-value pair to HashMapand add it to the function call, and you can access it mapper.xmlwith the "key".

+4
source

Use a mapper.

interface Mapper
{
     @Select( " select names from names_table where a = #{fieldA} and b = #{fieldB}" )
     List<String> getNames(  @Param("fieldA") String fieldA, @Param("fieldB") String fieldB)
}

@Param , sql. @Select, , xml.

,

public List<String> getAllName() throws PersistenceException {
        SqlSession session = sf.openSession();
        try 
        {
             Mapper mapper = session.getMapper(Mapper.class);
            return mapper.getNames("a","b");
        } finally {
            session.close();
        }
    }

.

+13

All Articles