Mybatis parameter (Parameters) pass
1.. Single parameter
Values of basic type, object type, collection type can be accepted.In this case, MyBatis can use this parameter directly without any processing.
<!-- according to id Query a record in a data table and encapsulate it User object --> <select id="selectById" resultType="com.softjx.model.User"> select t_id as id,t_username as username,t_password password from t_user where t_id=#{id}; </select> <!-- according to id Query a record in a data table usingparameterType Limit the type of parameter,Parameter variable names can be defined arbitrarily and are not javabean Attribute control in--> <select id="selectById1" parameterType="java.lang.Integer" resultType="com.softjx.model.User"> select t_id as id,t_username as username,t_password password from t_user where t_id=#{id1}; </select>
2.POJO
When these parameters belong to our business POJO, we pass the POJO directly.
<!-- Add User --> <insert id="insertUser"> insert into t_user (t_username,t_password) values (#{username},#{password}) </insert> <!-- Add User --> <!-- Parameters can be omitted,In any case, the variable name is javabean Medium Property Name --> <insert id="insertUser1" parameterType="com.softjx.model.User"> insert into t_user (t_username,t_password) values (#{username},#{password}) </insert>
3.Map
We can also encapsulate multiple parameters as map s and pass them directly.
public int insertUser2(Map<String ,Object> map); <!-- Add User --> <!-- The parameter is map,The variable name is arbitrary, when called, map Of key Consistent with variable name ,Parameters can also be omitted--> <insert id="insertUser2" parameterType="java.util.Map"> insert into t_user (t_username,t_password) values (#{a},#{b}) </insert> @Test public void TestInserUser2() { System.out.println("Add a user"); try { UserMapper mapper=session.getMapper(UserMapper.class); System.out.println(mapper); Map<String,Object> map=new HashMap<String,Object>(); map.put("a", "lisi"); map.put("b", "222222"); int count=mapper.insertUser2(map); session.commit();//Be sure to submit here, or the data won't go into the database //session.rollback(); System.out.println(count); } finally { session.close(); } }
4. Multiple parameters
Any number of parameters will be repackaged by MyBatis as a Map and passed in.The key of a Map is param1, param2, or 0, 1, and the value is the value of the parameter.
//Add User public int insertUser3(String name,String pass);
<!-- Add User --> <!-- With multiple parameters,Parameter Name #{param1},#{param2},perhaps#{0},#{1},Cannot use parameter name--> <insert id="insertUser3" parameterType="java.util.Map"> insert into t_user (t_username,t_password) values (#{param1},#{param2}) </insert> <insert id="insertUser3" parameterType="java.util.Map"> insert into t_user (t_username,t_password) values (#{0},#{1}) </insert>
@Test public void TestInserUser3() { System.out.println("Add a user"); try { UserMapper mapper=session.getMapper(UserMapper.class); System.out.println(mapper); int count=mapper.insertUser3("aaa","bbb"); session.commit();//Be sure to submit here, or the data won't go into the database //session.rollback(); System.out.println(count); } finally { session.close(); } }
5. Named parameters
Give the parameters a name of @Param, and MyBatis encapsulates them in a map with the key being our own name.
//Add User public int insertUser4(@Param("name")String name,@Param("pass")String pass);
<!-- Add User --> <!-- With multiple parameters,Use@Param annotation,Parameter names are defined directly. #{name},#{pass}Or,#{param1},#{param2},Out-of-service#{0},#{1}--> <insert id="insertUser4" parameterType="java.util.Map"> insert into t_user (t_username,t_password) values (#{name},#{pass}) </insert>
@Test public void TestInserUser4() { System.out.println("Add a user"); try { UserMapper mapper=session.getMapper(UserMapper.class); System.out.println(mapper); int count=mapper.insertUser4("song","555"); session.commit();//Be sure to submit here, or the data won't go into the database //session.rollback(); System.out.println(count); } finally { session.close(); } }