Re: Can mybatis execute SQL directly?

classic Classic list List threaded Threaded
1 message Options
Reply | Threaded
Open this post in threaded view
|

Re: Can mybatis execute SQL directly?

Ray Sprinkle

Bad idea. Really bad idea.  You might do this for dynamic SQL, but not for your example.  Use annotations in the interface instead.

On May 4, 2015 10:29 AM, "Digeng Tianhan" <[hidden email]> wrote:
I'm using the MyBatis 3 Java API for my mappers i.e. no XML. This generally works great and saves a lot of code, but breaks down when the annotations can't be used to support the needed return type. Or when the return type needs to be enriched or transformed in some way.

Is there a reason MyBatis couldn't support Abstract Class Mappers as opposed to just Interfaces? I'm thinking that an Abstract Class mapper could be used as a sort of ResultHandler for the java API. Two contrived examples below.

public abstract class EmployeeMapper {

@Select("Select * from Employee")
abstract List<Employee> getAllEmployees();

// As far as I know this isn't possible with annotations
public Map<Integer, String> getEmployeeIdToNameMap(){
List<Employee> employees = getAllEmployees();
Map<Integer, String> idToNameMap = new HashMap<>();
for (Employee employee : employees) {
idToNameMap.put(employee.getId(), employee.getName());
}
return getEmployeeIdToNameMap();
}
}

public abstract class EmployeeMapper {

@Select("Select * from Employee where name = ${name}")
abstract List<Employee> getEmployees(String name);

public List<EnrichedEmployee> getEnrichedEmployee(){
//code to transform Employees to EnrichedEmployees
}
}





--
You received this message because you are subscribed to the Google Groups "mybatis-user" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
For more options, visit https://groups.google.com/d/optout.

--
You received this message because you are subscribed to the Google Groups "mybatis-user" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
For more options, visit https://groups.google.com/d/optout.