If the method in the er has only one parameter, You can reference this parameter in the corresponding SQL statement # {parameter name}. The previous examples mostly belong to this type. But this method does not apply to the need to pass multiple parameters, today to introduce how to use annotations to pass Multiple Parameter example source code: http://down.51cto.com/data/537051 ).
1. To use annotations to implement multi-parameter transfer, we should first introduce "org. apache. ibatis. annotations. param ", which we introduced in the interface TeacherMapper and added the Statement of the findTeacherByPage Method for instructor paging query. As follows:
Package com. abc. mapper;
Import com. abc. domain. Teacher;
Import org. springframework. stereotype. Component;
Import java. util. List;
// To use the @ Param annotation, you must first introduce Param
Import org. apache. ibatis. annotations. Param;
// @ Component specify the er name as myTeacherMapper
// For more information, see the author's blog:
// Http://legend2011.blog.51cto.com/3018495/980150
@ Component ("myTeacherMapper ")
Publicinterface TeacherMapper {
Public Teacher getById (int id );
// Query the instructor information by PAGE
Public List <Teacher> findTeacherByPage (
// Use the @ Param ("sort") annotation to go to the SQL statement
// Reference the sort parameter value of this method in the form of "# {sort.
// Of course, you can also use other names in @ Param,
// For example, @ Param ("mysort ")
@ Param ("sort") String sort, // sorting Field
// The following three annotations are the same
@ Param ("dir") String dir, // sort direction
@ Param ("start") int start, // start record
@ Param ("limit") int limit // number of records
);
}
The content of the corresponding ing file TeacherMapper. xml is as follows:
<? Xmlversion = "1.0" encoding = "utf8"?>
<! DOCTYPE mapper PUBLIC "-// mybatis.org//DTD Mapper 3.0 // EN"
Http://mybatis.org/dtd/mybatis-3-mapper.dtd>
<! -- As before, the namespace value is the full name of the corresponding ER interface -->
<Mappernamespace = "com. abc. mapper. TeacherMapper">
<! -- Instructor entity ing -->
<ResultMapid = "supervisorResultMap" type = "Teacher">
<Idproperty = "id"/>
<Resultproperty = "name"/>
<Resultproperty = "gender"/>
<Resultproperty = "researchArea" column = "research_area"/>
<Resultproperty = "title"/>
<! -- The collection element maps the attributes of the instructor's guidance student set. Here we use
The namespace name. select statement id is used to reference
Select statement getStudents. This collection element uses nested
For details about the select statement, refer to the author's blog:
Http://legend2011.blog.51cto.com/3018495/985907
-->
<Collectionproperty = "supStudents" column = "id" ofType = "Student"
Select = "com. abc. mapper. StudentMapper. getStudents"/>
</ResultMap>
<Selectid = "findTeacherByPage" resultMap = "supervisorResultMap">
Select * from teacher
Order by $ {sort }$ {dir} limit # {start}, # {limit}
</Select>
</Mapper>
Run the main program as follows:
Package com. demo;
Import org. springframework. context. ApplicationContext;
Import com. abc. mapper. StudentMapper;
Import com. abc. mapper. TeacherMapper;
Import com. abc. domain. Teacher;
Import com. abc. domain. Student;
Import org. springframework. context. support. ClassPathXmlApplicationContext;
Import java. util. List;
Publicclass CollectionDemo
{
Privatestatic ApplicationContext ctx;
Static
{
// Search for the resources/beans. xml file in the class path
Ctx = new ClassPathXmlApplicationContext ("resources/beans. xml ");
}
Publicstaticvoid main (String [] args)
{
// Request the er from the Spring container
TeacherMapper mapper =
(TeacherMapper) ctx. getBean ("myTeacherMapper ");
Teacher teacher = null;
// Query the instructor's paging information
List <Teacher> teachers =
// The name field is sorted in ascending order and queried from 0th records.
// Query 2 records
Mapper. findTeacherByPage ("name", "asc", 0, 2 );
If (teachers = null)
{
System. out. println ("related instructor information not found. ");
}
Else
{
Object [] t = teachers. toArray ();
System. out. println ("************************************* *********");
For (int I = 0; I <t. length; I ++)
{
Teacher = (Teacher) t [I];
System. out. println ("instructor name:" + "" + teacher. getName ());
System. out. println ("instructor title:" + "" + teacher. getTitle ());
System. out. println ("Guiding student information :");
// Traverse the guided students
For (Student s: teacher. getSupStudents ())
{
System. out. println (s. getName () + "" + s. getGender ()
+ "" + S. getGrade ()
+ "" + S. getMajor ());
}
System. out. println ("************************************* *********");
}
}
}
}
The running result is as follows:
650) this. width = 650; "src =" http://www.bkjia.com/uploads/allimg/131228/12132Q117-0.png "border =" 0 "/>
Ii. Possible Errors 1. About order by, in general, we will reference parameters in the method in the form of # {parameter name, however, this method is invalid for the order by clause or returns an error. For example, when TeacherMapper. when the order by clause in the select statement findTeacherByPage of xml references the value of the sort parameter in the method in the form of # {sort}, the reader can verify it by himself ); when the dir parameter value in the method is referenced in the form of # {dir}, MySQLSyntaxErrorException is reported, as shown in:
650) this. width = 650; "src =" http://www.bkjia.com/uploads/allimg/131228/12132UE4-1.png "border =" 0 "/>
Therefore, the corresponding parameter value is referenced in the form of $ {parameter name.
2. invalid XML character error. This is a strange error. This error is reported when the Chinese character "error" is followed by the Chinese character ending in the comment in the ing file, as shown in:
650) this. width = 650; "src =" http://www.bkjia.com/uploads/allimg/131228/12132Q614-2.png "border =" 0 "/>
The same is true in the Spring configuration file beans. xml. Similarly, this error is also reported when the Chinese character "error" is followed by a Chinese comma. If a Chinese character is added after the word "error", no error is reported. However, an error is still reported when the word "ah" is added. You can try it on your own. Maybe you can find other errors.
All the exceptions reported are org. xml. sax. SAXParseException, as shown on the left side of the red box in the error image above. This may mean that they all use the same xml parsing component. Is this component a bug?
MyBatis Study Notes series I: download and install ant
Prepare for MyBatis Study Notes Series II: ant getting started
MyBatis learning notes: MyBatis getting started
MyBatis Study Notes Series II: Example of adding, deleting, and modifying MyBatis
MyBatis Study Notes Series 3: association examples of MyBatis
MyBatis Study Notes Series 4: two forms of MyBatis association
MyBatis Study Notes Series 5: examples of integration between MyBatis and Spring
MyBatis Study Notes Series 6: examples of integration between MyBatis and Spring
MyBatis study notes 7: MyBatis one-to-multiple bidirectional Association
MyBatis Study Notes: MyBatis MapperScannerConfigurer Configuration
MyBatis Study Notes: two forms of MyBatis collection
MyBatis Study Notes Series 10: Log4j example of MyBatis logs
MyBatis Study Notes: example of how to annotate MyBatis with multiple parameters
MyBatis learning notes: Example of the default naming method for MyBatis multi-parameter transfer
MyBatis Study Notes: Example of Map mode for MyBatis multi-parameter transfer
MyBatis Study Notes: N + 1 in MyBatis
MyBatis Study Notes: a hybrid transfer of multiple parameters in MyBatis
MyBatis Study Notes:Spring declarative Transaction Management example
MyBatis Study Notes: Example of MyBatis multiple-to-multiple storage
This article is from the "Xiao fan's column" blog, please be sure to keep this source http://legend2011.blog.51cto.com/3018495/1015003