03-mybatis

mybatis的Mapper文件中的大于小于号,为什么要转成“&lt ;”、“&gt ;”,转义后的lt、gt又代表什么?open in new window

1. deleteByPrimaryKey

1. 没有@Id

DELETE
FROM t_application
WHERE ID = 'lisongtao'
  AND APP_CODE = 'songsong'
  AND APP_NAME = null
  AND APP_URL = null
  AND REMARK = null
  AND STATUS = null
  AND CREATE_BY = null
  AND CREATE_TIME = null
  AND LAST_UPDATE_BY = null
  AND LAST_UPDATE_TIME = null
  AND DEL_FLAG = null;

2. 有@Id

DELETE
FROM t_application
WHERE ID = 'lisongtao';

2. selectByPrimaryKey

User one = userMapper.selectByPrimaryKey("songsong");
User one = userMapper.selectByPrimaryKey("");
User one = userMapper.selectByPrimaryKey(null);

3. config

1. 返回map

  • resultType=“map" 当查询的字段为null时,map中不会有该属性
<!--grayLevelSelPageUser-->
<select id="grayLevelSelPageUser" resultType="map">
    select u.ID         as userId,
           u.LOGIN_NAME as userName,
           u.USER_NAME  as nickName
    from t_user u
            where u.APP_ID = #{appId}
    <if test="loginName != null and loginName != ''">
        and u.LOGIN_NAME like CONCAT('%', #{loginName}, '%')
    </if>
    order by SORT_NO, CREATE_TIME DESC
</select>

2. 字段返回null

1. xml里不生效的

解决MyBatis查询的结果为Map时,Map的值为null,键就不存在的问题open in new window

Um4zUXU5aDFKN0NSYmF2WDQzZTE4Q2pSUGlnUTNKMGl2Ykk0MW1iZ1RnPT0=

2. yml生效

springboot下配置mybatis的call-setters-on-nulls属性open in new window

YzVxRjgybk9QemFvQTdkZUFZb0tLU2pSUGlnUTNKMGl2Ykk0MW1iZ1RnPT0=

4. 批量insert

insert into t_user_role_change (id, user_id, role_id, change_way, expire_time, create_by, create_time,
                                     last_update_by, last_update_time)
VALUES ('1394983922990977025', '1390636402349314048', '786987152541093888', 1, null, 'songsong',
        '2021-05-19 19:50:59.446', 'songsong', '2021-05-19 19:50:59.446'),
       ('1394983922990977027', '1390636402349314048', '786612517806936064', 1, null, 'songsong',
        '2021-05-19 19:50:59.446', 'songsong', '2021-05-19 19:50:59.446'),
       ('1394983922990977029', '1390636402349314048', '786550636585029632', 1, null, 'songsong',
        '2021-05-19 19:50:59.446', 'songsong', '2021-05-19 19:50:59.446');
  • 列名可以取消
insert into t_user_role_change
VALUES ('1394983922990977025', '1390636402349314048', '786987152541093888', 1, null, 'songsong',
        '2021-05-19 19:50:59.446', 'songsong', '2021-05-19 19:50:59.446'),
       ('1394983922990977027', '1390636402349314048', '786612517806936064', 1, null, 'songsong',
        '2021-05-19 19:50:59.446', 'songsong', '2021-05-19 19:50:59.446'),
       ('1394983922990977029', '1390636402349314048', '786550636585029632', 1, null, 'songsong',
        '2021-05-19 19:50:59.446', 'songsong', '2021-05-19 19:50:59.446');