微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

mybatis 或 mybatis-plus 执行 sql 的三种方式

前言:

mybatis 是目前非常流行的数据库框架,mybatis-plus 是 mybatis 的增强版(只做增强,不做改变),有兴趣的可以研究下。

方式一:

配置 xml 文件,该方式是比较通用的方法,适合任何 sql 语句(尤其是复杂 sql)。

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
<mapper namespace="com.gtja.ibcenter.wechat.moudle.mapper.WeChatPushRecordMapper">

    <select id="getPushRecord"  resultType="com.gtja.ibcenter.wechat.dto.WeChatPushRecordDto">
        select job_id jobId,pusher,type,app_key appKey,app_name appName,content,cancel,pr.create_time createTime,pr.update_time updateTime
        from wechat_push_record pr join wechat_app_info ai on pr.create_app=ai.app_key
        where job_id is not null

        <if test="pusher != null and pusher != ''">
            and pusher=#{pusher}
        </if>
        <if test="type != null and type != ''">
            and type=#{type}
        </if>
        <if test="createApp != null and createApp != ''">
            and create_app=#{createApp}
        </if>
        <if test="content != null and content != ''">
            and content like concat("%",#{content},"%")
        </if>
        <if test="cancel != null and cancel != ''">
            and cancel=#{cancel}
        </if>
        <if test="startTime != null and startTime != ''">
            and pr.create_time &gt;= #{startTime}
        </if>
        <if test="endTime != null and endTime != ''">
            and pr.create_time &lt;= #{endTime}
        </if>

        order by pr.create_time desc
    </select>

</mapper>

注:大于号、小于号的写法:

sql语句符号转义符号
>&gt;
>=&gt;=
<&lt;
<=&lt;=

方式二:

使用 @Select 注解,该方式适合比较简单的 sql 语句,使用起来比较简单。

    @Select("select dept_code,dept_name from dept_info where source = #{source}")
    List<DeptPo> getDeptBySource(@Param("source") Integer source);

方式三:

sqlSession 执行 sql,稍微复杂,不到万不得已不建议使用。mybatis-plus 很人性化的处理了增删改查,该方法适合不想做任何配置的人。

各种 Wrapper 用于构造条件:

Wrapper说明
Wrapper条件构造抽象类,最顶端父类
AbstractWrapper用于查询条件封装,生成 sql 的 where 条件
QueryWrapper查询条件封装,不是用lambda语法
UpdateWrapper更新条件封装,用于对象更新操作
AbstractLambdaWrapperLambda 语法使用 Wrapper统一处理解析
LambdaQueryWrapperLambda语法使用的查询Wrapper
LambdaUpdateWrapperLambda 更新封装Wrapper

条件语句:

查询方式说明
setsqlSelect设置 SELECT 查询字段
whereWHERE 语句,拼接 + WHERE 条件
andAND 语句,拼接 + AND 字段=值
andNewAND 语句,拼接 + AND (字段=值)
orOR 语句,拼接 + OR 字段=值
orNewOR 语句,拼接 + OR (字段=值)
eq等于=
allEq基于 map 内容等于=
ne不等于<>
gt大于>
ge大于等于>=
lt小于<
le小于等于<=
like模糊查询 LIKE
notLike模糊查询 NOT LIKE
inIN 查询
notinNOT IN 查询
isNullNULL 值查询
isNotNullIS NOT NULL
groupBy分组 GROUP BY
havingHAVING 关键词
orderBy排序 ORDER BY
orderAscASC 排序 ORDER BY
orderDescDESC 排序 ORDER BY
existsEXISTS 条件语句
notExistsNOT EXISTS 条件语句
betweenBETWEEN 条件语句
notBetweenNOT BETWEEN 条件语句
addFilter自由拼接 sql
last拼接在最后,例如:last(“LIMIT 1”)

示例(BaseMapper 里面有所有的方法):

int result = userMapper.insert(UserPo);    // 增
QueryWrapper<UserPo> queryWrapper= new QueryWrapper<>();
queryWrapper.eq("uid", uid);
int result = userMapper.delete(queryWrapper);    // 删
UpdateWrapper<UserPo> updateWrapper = new UpdateWrapper<>();
updateWrapper.eq("uid", uid);
int result = userMapper.update(userPo, updateWrapper);    //改
QueryWrapper<UserPo> queryWrapper= new QueryWrapper<>();
queryWrapper.eq("uid", uid);
List<UserPo> list = userMapper.selectList(queryWrapper);    //查

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐