Brief mybatis four kinds of parameters of a plurality of incoming Method

Use mybatis in CRUD when often need to pass parameters, in grammar, mybatis only allow incoming one argument, but through some skills, we can pass in multiple parameters, the following I briefly passed more than four parameters methods.

The first method of logic expansion

Our most common method is to use logic expansion method.
Simulation scenario: we need to add student database into three attribute values, respectively studentId, studentName, studentAge, three properties at this time may be packaged as Student object, three values logically combined into one object, then the following code :

   <!--
        parameterType   填入封装对象的那个类的完整路径
        id              接口的方法名
        #{}             用于获取对象对应的属性值
    
    -->
  <insert id="addStudent" parameterType="com.example.model.Student">
        insert into student(studentId,studentName,studentAge) values (#{studentId},#{studentName},#{studentAge})
  </insert>

Passing a plurality of second process parameters on interface methods

We only need to pass a plurality of parameters in the interface method, but we need to pay attention in the mapper:
1. Properties omitted parameterType
2. param1, param2 ...... or arg0, arg1 ...... parameters used in place of
the specific use to see the code below :

Interface StudentMapper.java
/**
	这里建议
		1.虽然是添加学生,照理来说不需要返回值,但是我们还是写了一个Integer型的返回值,
		  此时返回的是影响的行数。用于检测插入数据到数据库是否成功
		2.参数内使用Integer而非int是因为防止int型默认为0
*/
public interface StudentMapper {
    public Integer addStudent(Integer studentId,String studentName,Integer studentAge);
}
Configuration StudentMapper.xml
   <!--
        id              接口的方法名
        #{}             用于获取对象对应的属性值
    -->
  <insert id="addStudent">
        insert into student(studentId,studentName,studentAge) values (#{param1},#{param2},#{param3})
  </insert>

  <!--或者这么写-->
    <insert id="addStudent">
        insert into student(studentId,studentName,studentAge) values (#{arg0},#{arg1},#{arg2})
  </insert>

The third method upgraded version

The second method is obvious, very inconvenient. Because light from the sql statement seen, do not know their incoming (agr0, arg1 ......) what it is. The third is an upgraded version of the second approach to solve the problem of unclear parameters.
The third method employed in the interface @Param added () Note alias parameter setting mode, the following is the code:

Interface StudentMapper.java
public interface StudentMapper {
    public Integer addStudent(@Param("sId") Integer studentId, @Param("sName")String studentName, @Param("sAge")Integer studentAge);
}
Configuration StudentMapper.xml
  <insert id="addStudent">
        insert into student(studentId,studentName,studentAge) values (#{sId},#{sName},#{sAge})
  </insert>

The fourth method Mixed Mode

Scenario: When you need to pass not only the basic parameter type data, as well as objects, a first third methods may be combined, using the following

Interface StudentMapper.java
public interface StudentMapper {
	/**
		这里Course是一个类,封装了Course这个对象,有courseId,courseName两个属性
	*/
    public Integer addStudent(@Param("sCourse") Course course,@Param("sId") Integer studentId, @Param("sName")String studentName, @Param("sAge")Integer studentAge);
}
Configuration StudentMapper.xml
  <insert id="addStudent">
        insert into student(studentId,studentName,studentAge,studentCourseId,studentCourseName) values (#{sId},#{sName},#{sAge},#{sCourse.courseId},#{sCourse.courseName})
  </insert>

A little tired today, if the process of writing or clerical error, please contact private letter

Published 34 original articles · won praise 65 · views 3709

Guess you like

Origin blog.csdn.net/baidu_41860619/article/details/104679011