Detailed use of XML – parameters, return result processing

Parameter value mode

There are two ways to value SQL statements in an XML file: #{} and ${}. Here are the differences:

<! #{} ==> JDBC String SQL =" SELECT id,user_name FROM EMP WHERE id=?" 1. PreparedStatement is precompiled in JDBC and compiled into data for the corresponding database based on different data types. 2. Can effectively prevent SQL injection. Recommended use!! Special uses: Attributes that come with many built-in arguments: usually not used. Understand javaType, jdbcType, mode, numericScale, resultMap, typeHandler. If you want to change the default NULL===>OTHER:#{id,javaType=NULL} #{id,numericScale=2} 2.${} ==> jdbc String sql=" SELECT id,user_name FROM EMP WHERE id="+id 1. It does not precompile and concatenates the incoming data directly into SQL. 2. SQL injection risks exist. Not recommended. Special usage: 1. Can be used temporarily under debugging situation. 2. To achieve some special functions: Data security must be guaranteed. For example: dynamic table, dynamic column. Dynamic SQL. -- -- >
<select id="SelectEmp"  resultType="Emp"  resultMap="emp_map"  >
SELECT id,user_name,create_date FROM EMP where id=#{id}
</select>
Copy the code

Select parameter passing

  <! 1. Single parameter :SelectEmp(Integer ID); Mybatis does not make any special request to get the method: #:{enter any character to get the parameter} 2. Multiple parameters :Emp SelectEmp(Integer ID,String username); Mybatis encapsulates the parameters passed in as a map: Each value corresponds to two map items: Id ===> {key:arg0,value:id value},{key:param1,value: ID value} username===> {key:arg1,value:id value},{key:param2,value: ID value} @param: id=====> #{arg0} or #{param1} username=====> #{arg1} or #{param2} @param ("") : SelectEmp(@param (" ID ") Integer ID, @param ("username") String username); @param: id=====> #{id} or #{param1} username=====> #{param2} 3. Single parameter: Emp SelectEmp(Emp Emp); Emp. Id =====>#{id} emp.username=====>#{username} Emp SelectEmp(Integer num, emp emp); Num ===> #{param1} or @param emp===> Emp. Id = = = > # {param2. Id} or @ Param (" emp ") emp emp = = = = > # {emp. Id} emp. The username = = = > # {param2. Username} or @ Param emp (" emp ") emp ====>#{emp.username} 4. Emp SelectEmp(List<String> usernames); MyBatis automatically encapsulates a list as a map: {key: "a list" : value: usernames} no @ Param (" ") to get: usernames. Get (0) = = = = = > # [0]} {list: usernames. Get (0) = = = = = > # {agr0 [0]} @ Param (" usernames ") to get: usernames. Get (0) = = = = = > # {usernames [0]} : usernames. Get (0) = = = = = > # {param1 [0]} MyBatis will automatically encapsulate an array as a map: {key: "array" : value: usernames} no @ Param (" ") to get: usernames. Get (0) = = = = = > # {array [0]} : usernames. Get (0) = = = = = > # {agr0 [0]} @ Param (" usernames ") to get: usernames. Get (0) = = = = = > # {usernames [0]} : usernames. Get (0) = = = = = > # {param1 [0]} 5. The map parameter The same is true for javabeans. In general: If you're asking for parameters that correspond to poJO, if you're asking for parameters that don't correspond to POJO, if you're asking for parameters that don't correspond to POJO, if you're asking for parameters that correspond to POJO, if you're asking for parameters that correspond to POJO, if you're asking for parameters that correspond to POJO, if you're asking for parameters that correspond to POJO, Use TO, DTO (that is, create a corresponding javaBean for these parameters, TO make the parameter pass more standard, more reuse) -->

  <! SelectEmp(String username, @param ("id") Integer ID); The username = = = = > # # {arg0} {param1} id = = = = > # # {id} {param2} interface: SelectEmp(@Param("beginDate") String beginDate, String endDate, Emp emp); BeginDate = = = = > # # {beginDate} {param1} endDate = = = = > # # {arg1} {param2} emp. The id = = = = > # # {arg2. Id} {param2. Id} interface: SelectEmp(List<Integer> ids, String[] usernames, @Param("beginDate") String beginDate, String endDate,); ids.get(0)=====> #{list[0]} #{param1[0]} usernames[0]=====> #{array[0]} #{param2[0]} beginDate====> #{beginDate} #{param3} end====> #{arg3} #{param4} -->
Copy the code

The processing collection returns the result

  • EmpMapper.xml
        <! When the result of a return value is a collection, the type of the return value is still the specific type of the collection.
<select id="selectAllEmp" resultType="com.mybatis.bean.Emp">
select  * from emp
</select>
        <! Mybatis will convert the name of the column as the value of the key column into the map after the query is completed.
<select id="selectEmpByEmpReturnMap" resultType="map">
select * from emp where empno = #{empno}
</select>

        <! Note that when the return result is a collection object, the return value type must be the set value type and the annotation @mapKey must be added to the dao method. @mapKey ("empno") Map<Integer,Emp> getAllEmpReturnMap(); -->
<select id="getAllEmpReturnMap" resultType="com.mybatis.bean.Emp">
select * from emp
</select>
Copy the code
  • UserMapper.java
public interface EmpMapper {

    public Emp findEmpByEmpno(Integer empno);

    public int updateEmp(Emp emp);

    public int deleteEmp(Integer empno);

    public int insertEmp(Emp emp);

    Emp selectEmpByNoAndName(@Param("empno") Integer empno, @Param("ename") String ename, @Param("t") String tablename);
    Emp selectEmpByNoAndName2(Map<String,Object> map);

    List<Emp> selectAllEmp(a);

    Map<String,Object> selectEmpByEmpReturnMap(Integer empno);

    @MapKey("empno")
    Map<Integer,Emp> getAllEmpReturnMap(a);
}
Copy the code

Customize the result set. -resultMap

     <! --1. Declare resultMap Only one resultType or resultMap can be used. Id uniquely identifies poJO objects that need to be mapped to resultMap on <select. You can set the alias autoMapping for automatic mapping. (Default =true) Field and attribute names can be automatically mapped as long as they follow the mapping rules, but it is not recommended. Extends If multiple ResultMaps have duplicate mappings, you can declare a parent resultMap to extract the common mappings. This reduces the mapping redundancy of the child resultMap.
<resultMap id="emp_map" type="emp" autoMapping="false" extends="common_map">
<result column="create_date" property="cjsj"></result>
</resultMap>

<resultMap id="common_map" type="emp" autoMapping="false" >
<! -- <id> Primary key must use the name of the database column that needs to be mapped.
<id column="id" property="id"></id>
<result column="user_name" property="username"></result>
</resultMap>

        <! --2. Associate the id of a custom result set with resultMap
<select id="SelectEmp"  resultType="Emp"  resultMap="emp_map"  >
SELECT id,user_name,create_date FROM EMP where id=#{id}
</select>
Copy the code