Mybatis- study notes (3) mapper profile

1, mapper configuration file common elements

   

     parameterMap obsolete, old-fashioned style parameter mapping.

2, select the elements

   Mapping query. {...} # statement parameters for the pretreatment by the JDBC, such a parameter will SQL "?" Is identified by one and transferred to a new preprocessing statement.

   

   Attribute describes the select element as follows:

    

    

3、insert、update、delete

   Mapping DML statements. And select the configuration is very similar.

   

   In addition to the configuration properties and select the most consistent, unique are the following:

   

    If some database supports generates primary key values, and the driving support acquired primary key returned after the insert, may be provided useGeneratedKeys = "to true", the keyProperty = "ID" . (Id is optional, is mainly used to receive the primary key value is returned).

    Do not support automatic generation of database types (e.g. oracle) or JDBC driver does not support, Mybatis may also generate the primary key in the following ways:

     

    In the example above, the selectKey element will be running, by querying SEQUENCE sequence, the sequence obtained is set to the value of the id.

     

4、sql

  SQL code segment for defining reusable. May be included in other statements, it may be statically (at the time of loading parameters) parameterization.

  

5、Parameters

  That parameterType.

 or

 

6、ResultMaps

  Results taken from the centralized data into objects developer needed.

  

  1 "or into our POJO.

<select id="selectAllUser" resultType="com.lfy.bean.User">
    select * from t_user
</select>

  When POJO, namely the field and property fields of our Java Bean database is inconsistent, you need to convert (to the field can play an alias in sql statement):

< The resultMap id = "userResultMap" type = "com.lfy.bean.User" > 
     <-! Id attribute to specify the primary key field -> 
    < id Property = "id" column = "user_id" /> 
     <! - using the result attribute matches the normal field -> 
     < result property = "name" column = "USER_NAME" /> 
     < result property = "Sex" column = "user_sex" /> 
</ The resultMap >

    

  2 "occurs when the association between tables, Java Bean this other nest when a Java Bean.

      The following is a 1-to-1 relationship, a student has only one class, namely Student is only one Clazz property.

<!-- 映射学生对象的resultMap -->
  <resultMap id="studentResultMap" type="com.lfy.bean.Student">
      <id property="id" column="id" />
      <result property="name" column="name"/>
      <result property="sex" column="sex"/>
      <result property="age" column="age"/>
      <!- = "clazz"PropertyAssociation<->association map
       column="clazz_id" 
         javaType="com.lfy.bean.Clazz"
         select="selectClazzWithId"/>
    </resultMap>
    
  <!-- 根据班级id查询班级 -->
  <select id="selectClazzWithId" resultType="com.lfy.bean.Clazz">
      SELECT * FROM TB_CLAZZ where id = #{id}
  </select>
  
  <!-- 查询所有学生信息 -->
  <select id="selectStudent" resultMap="studentResultMap">
      SELECT * FROM TB_STUDENT
  </select>

    

    (PS: If the query is associated, may be directly mapped resultMap the association table to the fields "within the first object" embedded the corresponding field, such as multi-table queries associated with the Java bean and other compositions the bean )

<association property="teacher" javaType="com.lfy.bean.Teacher">
     <id property="id" column="t_id"/>
     <result property="name" column="t_name"/>
</association>

     In turn, check all classes, and check out all the students in each class, there will be more than one relationship, that Clazz have a List <Student> Properties.

<! - mapping the object class The resultMap -> 
  < The resultMap ID = "clazzResultMap" type = "com.lfy.bean.Clazz" > 
      < ID Property = "ID" column = "ID"  /> 
      < Result Property = " code " column =" code " /> 
      <-! student attributes of the class, because there are a plurality of a class of students, so that the property is a collection -> 
      < collection property =" students. " the javaType =" the ArrayList " 
      column =" the above mentioned id " ofType =" com.lfy.bean.Student " 
      select="selectStudentWithId"/>
    </ The resultMap > 
    
    <-! Query students according to the class ID -> 
    < SELECT ID = "selectStudentWithId" the resultType = "com.lfy.bean.Student" > 
          the SELECT * WHERE clazz_id the FROM TB_STUDENT ID = # {} 
    </ SELECT > 
      
    <! - queries all class information -> 
  < the SELECT the above mentioned id = "selectClazz" resultMap = "clazzResultMap" > 
      the SELECT * the FROM TB_CLAZZ 
  </ the SELECT >

   

 

Guess you like

Origin www.cnblogs.com/ZeroMZ/p/11415373.html