Home > Java > javaTutorial > body text

Detailed introduction to the incoming parameters of MyBatis

黄舟
Release: 2017-03-02 11:14:38
Original
2264 people have browsed it

The parameterType attribute is mentioned in the select, insert, update, and delete elements of MyBatis. The parameterTypes that MyBatis can now use include basic data types and Java complex data types

  • Basic data types: including int, String, Date, etc. Basic data types are used as parameters, and only one can be passed in. The incoming value can be obtained through #{parameter name}

  • Complex data types: including JAVA entity classes and Maps. You can get the incoming value through #{Attribute name} or #{KeyName of map}

  • Basic data type parameter example:

  • Query the teacher list based on class ID

xml file

<select id="selectTeacher" parameterType="int" resultType="com.myapp.domain.Teacher">  
    select * from Teacher where c_id=#{id}  
</select>
Copy after login

Java Code

List<Teacher> tList = teacherMapper.selectTeacher(2);    
for (Teacher entityTemp : tList) {    
    System.out.println(entityTemp.toString());    
}
Copy after login

JAVA entity type parameter example:

<select id="selectTeacher" parameterType="com.myapp.domain.Teacher" resultType="com.myapp.domain.Teacher">  
    select * from Teacher where c_id=#{id}  
</select>
Copy after login
java代码  
  
Teacher queryTeacher=new Teacher();  
queryTeacher.setId(2);  
List<Teacher> tList = teacherMapper.selectTeacher(queryTeacher);    
for (Teacher entityTemp : tList) {    
    System.out.println(entityTemp.toString()); }
Copy after login

Map parameter example:

<select id="selectTeacher" parameterType="Map" resultType="com.myapp.domain.Teacher">  
    select * from Teacher where c_id=#{id} and sex=#{sex}  
</select>
Copy after login
java代码  
  
Map<String,String> map=new HasMap<String,String>();  
map.put("id","2");  
map.put("sex","男");  
List<Teacher> tList = teacherMapper.selectTeacher(map);    
for (Teacher entityTemp : tList) {    
    System.out.println(entityTemp.toString()); }
Copy after login

In addition, MyBatis also provides a way to use annotations to participate in multiple parameters. This method requires adding the @Param annotation to the parameters of the interface

Example:

Interface method

public List<Teacher> selectTeacher(@Param(value="id") String id,@Param(value="sex") String sex);
Copy after login

XML file

<select id="selectTeacher"  resultType="com.myapp.domain.Teacher">  
    select * from Teacher where c_id=#{id} and sex=#{sex}  
</select>
Copy after login

Test code

List<Teacher> tList = teacherMapper.selectTeacher("2","男");    
for (Teacher entityTemp : tList) {    
    System.out.println(entityTemp.toString());
Copy after login

The above is a detailed introduction to the incoming parameters of MyBatis, more related Please pay attention to the PHP Chinese website (www.php.cn) for content!


Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!