mybatis 常用

1、新增时获得主键
<insert ...>
  <selectKey resultType="java.lang.Integer" keyProperty="id" order="AFTER">
  SELECT LAST_INSERT_ID()
  </selectKey>
  INSERT INTO ...
</insert>

<insert id="insert" parameterType="com.test.User" keyProperty="id" useGeneratedKeys="true" >
  INSERT INTO ...
</insert>

2、Boolean判断
<if test="isReturnReport !=null and isReturnReport.toString() == 'true'.toString()">
...
</if>

3、大于、小于符号转义
      & &amp;
      " &quot;
      ' &apos;
大于号: >  &gt;   或 <![CDATA[ > ]]>
小于号: <     &lt;   或 <![CDATA[ < ]]>
eg.
  AND DATE_FORMAT(create_date,'%Y-%m-%d') &gt;= #{beginDate, jdbcType=VARCHAR}
  AND DATE_FORMAT(create_date,'%Y-%m-%d') <![CDATA[ >= ]]> #{beginDate, jdbcType=VARCHAR}

4、参数为逗号隔开的字符串
  FIND_IN_SET(field,param)函数:
  AND FIND_IN_SET(user_id,#{_parameter})
  _parameter:
  获取传入的原参数值,不作预编译处理

5、模糊查询
AND settle_name LIKE CONCAT('%',#{settleName,jdbcType=VARCHAR},'%')

6、级联查询
一对多查询:
一中的xml:
<collection property="optionsList"
  column="id"
  ofType="com.hs.qmct.app.web.model.question.QuestionOptions"
  select="com.hs.qmct.app.web.dao.question.QuestionOptionsMapper.selectByQuestionId">
</collection>
多中的dao:
List<QuestionOptions> selectByQuestionId(Integer questionId);


传递多个参数时:
一中的xml:
  column= "{prop1=col1,prop2=col2}"
多中的xml:
  <select parameterType="java.util.Map" ....>
  select .... where val1=prop1 and val2=prop2
  </select>

多对一查询:
一中的xml:
<association property="district"
  column="receive_district_id"
  javaType="com.hs.qmct.app.web.model.district.District"
  select="com.hs.qmct.app.web.dao.district.DistrictMapper.selectByPrimaryKey">
</association>
另一个一中的dao:
  District selectByPrimaryKey(Integer id);

7、传参占位符#{}和${}的区别
#{} 具有预编译效果,一般作为条件传参时使用,可防止sql注入
${} 不具有预编译效果,一般在排序中会用到,偶尔也会作为条件传参
eg1.
  SELECT id from tables where id=#{id} 解析为:SELECT id from tables where id=?
  SELECT id from tables where id='${id}' 解析为:SELECT id from tables where id=原参数值
eg2.
  ORDER BY ${param} 此时param是一个列名
  ORDER BY #{_parameter} 此时传入的参数有且仅为一个列名

8、批量新增使用forEach
int batchInsert(List<Event> records);

<!--批量新增事件-->
<insert id="batchInsert">
  insert into
    hserp_sys_event(id, event_name_parent, event_name,event_desc, class_path, interface_path,create_by, create_date)
  values
    <foreach item="item" index="index" collection="list" separator=",">
    (#{item.id}, #{item.eventNameParent}, #{item.eventName},
    #{item.eventDesc}, #{item.classPath}, #{item.interfacePath},
    #{item.createBy}, sysdate())
  </foreach>
  on DUPLICATE key update last_update_date = sysdate()
</insert>

传参主要有一下3种情况:
1. 如果传入的是单参数且参数类型是一个List的时候,collection属性值为list
2. 如果传入的是单参数且参数类型是一个array数组的时候,collection的属性值为array
3. 如果传入的参数是多个的时候,我们就需要把它们封装成一个Map

9、解决int类型返回报错
  IFNULL(MAX(SORT),0)

10、一次执行多条sql
  需要在xml中加入
  <property name="url" value="jdbc:mysql://localhost:3306/how2java?characterEncoding=UTF-8&allowMultiQueries=true"/>
  或在配置文件中加入
  hikariConfig.security.jdbcUrl=jdbc:mysql://xx.xx.xx:3306/xxxxx?characterEncoding=utf-8&autoReconnect=true&failOverReadOnly=false&allowMultiQueries=true

原文地址:https://www.cnblogs.com/wlxslsb/p/10718360.html