MyBatis (four) MyBatis sql statement and the difference between dynamic and ResultMap ResultType ResultMap distinction in use and in use ResultType

1.  dynamic sql statement .

1.1 if.

<if test=”name!=null”>

    

</if>

 

1.2  the Where: In sql statement at the beginning of plus where. Removal as head and or or at the beginning.

 <where>

     <if test=””></if>

     <if test=””></if>

 </where>

 

1.3  the Set: In sql added before the statement set and will sql statement last comma removed.

 <set>

<if test=””></if>

<if test=””></if>

 

</set>

 

1.4 Trim

 <Trim prefix = "prefix" prefixoverrides = "prefix removal" suffix = "suffix" suffixoverrides = "remove suffix">

      <if test=””></if>

<if test=””></if>

 

 </trim>

 

1.5 choose+when+otherwise

1.6  SQL fragments.

 

Quote:

 <select id="selectAll"  resultMap="BaseResultMap">
    select 
    <include refid="usercolumns" />
    from users
  </select>

 

1.7 foreach tag traversal.

Delete id = [1,2,3,5,7,9]

 

 Specifically View: https://www.cnblogs.com/ysocean/p/7289529.html#_label0 (Details)

 Fuzzy query like concat ( '%', 'a word', '%')

 

 

 

ResultMap and ResultType difference in use

 

Processing returns results when using mybatis database connection operations for SQL statements are usually two ways, one is resultType Another is resultMap, I say this under the following knowledge and understanding of both

resultType:当使用resultType做SQL语句返回结果类型处理时,对于SQL语句查询出的字段在相应的pojo中必须有和它相同的字段对应,而resultType中的内容就是pojo在本项目中的位置。

因此对于单表查询的话用resultType是最合适的。但是,如果在写pojo时,不想用数据库表中定义的字段名称,也是可以使用resultMap进行处理对应的。多表连接查询时,若是一对一的连接查询,那么需要新建一个pojo,pojo中包括两个表中需要查询出的所有的字段,这个地方的处理方式通常为创建一个继承一个表字段的pojo,再在里面添加另外一个表内需要查询出的字段即可。若是一对多查询时,若是使用内连接查询,则很可能出现查询出的字段有重复。使用双重for循环嵌套处理即可。

 

resultMap:当使用resultMap做SQL语句返回结果类型处理时,通常需要在mapper.xml中定义resultMap进行pojo和相应表字段的对应。

 

在使用mybatis进行数据库连接操作时对于SQL语句返回结果的处理通常有两种方式,一种就是resultType另一种就是resultMap,下面说下我对这两者的认识和理解

resultType:当使用resultType做SQL语句返回结果类型处理时,对于SQL语句查询出的字段在相应的pojo中必须有和它相同的字段对应,而resultType中的内容就是pojo在本项目中的位置。

因此对于单表查询的话用resultType是最合适的。但是,如果在写pojo时,不想用数据库表中定义的字段名称,也是可以使用resultMap进行处理对应的。多表连接查询时,若是一对一的连接查询,那么需要新建一个pojo,pojo中包括两个表中需要查询出的所有的字段,这个地方的处理方式通常为创建一个继承一个表字段的pojo,再在里面添加另外一个表内需要查询出的字段即可。若是一对多查询时,若是使用内连接查询,则很可能出现查询出的字段有重复。使用双重for循环嵌套处理即可。

 

resultMap:当使用resultMap做SQL语句返回结果类型处理时,通常需要在mapper.xml中定义resultMap进行pojo和相应表字段的对应。

Guess you like

Origin www.cnblogs.com/yufengwang/p/11443100.html