0

I have the following table

class table

id  name     hours
1   science     3
2   math        5

teachers table

id  first_name  last_name
1   Ronald      Manlapao
2   hello       goodbye
3   elise       elise
4   me          me

class_teachers table

class_ref   teachers_ref
2                   2
1                   1

I would like to query in class_teachers table where in the results shows teachers names and class names. select statement is like this

select class.name as classname, 
       class.hours as hours, 
       teachers.first_name as teacherFirstName, 
       teachers.last_name as teacherLastName 
from class_teachers cl 
left join class on class.id = cl.class_ref  
left join teachers on teachers.id = cl.teachers_ref

how can I do this in myBatis? I can't declare classname and teachersnames in class_teacher table result because they are not part of it

5
  • What kind of relation you are having with these tables. I mean in table class_teachers you have relation between classes and teachers as one-to-one or many-to-one? Aug 8, 2014 at 6:19
  • Refer to link: this might help you. stackoverflow.com/questions/12491536/… Aug 8, 2014 at 6:20
  • a class_teacher can have one class. a class_teacher can have many teachers Aug 8, 2014 at 6:27
  • Great. So you have one-to-many relation between class and teachers. Here is perfect example from MyBatis site: mybatis.co.uk/index.php/2010/10/… Aug 8, 2014 at 6:56
  • I would like to see an example wherein the intermediary table (class_teacher) is being mapped Aug 11, 2014 at 4:48

1 Answer 1

0
<select id="****" parameterType="map" resultType = "java.util.LinkedHashMap">

    your select query as listed

</select>

This will give you a List of linkedHashMap object, from which, you can get values as needed.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.