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

62 篇文章 0 订阅
60 篇文章 0 订阅

前言:

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语句符号

转义符号

>

>

>=

>=

<

<

<=

<=

方式二:

使用 @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 很人性化的处理了增删改查,该方法适合不想做任何配置的人。

【可参考整合 mybatis-plus 和分页查询功能到 springboot_-CSDN博客整合 mybatis-plus】。

各种 Wrapper 用于构造条件:

Wrapper

说明

Wrapper

条件构造抽象类,最顶端父类

AbstractWrapper

用于查询条件封装,生成 sql 的 where 条件

QueryWrapper

查询条件封装,不是用lambda语法

UpdateWrapper

更新条件封装,用于对象更新操作

AbstractLambdaWrapper

Lambda 语法使用 Wrapper统一处理解析

LambdaQueryWrapper

Lambda语法使用的查询Wrapper

LambdaUpdateWrapper

Lambda 更新封装Wrapper

条件语句:

查询方式

说明

setSqlSelect

设置 SELECT 查询字段

where

WHERE 语句,拼接 +WHERE 条件

and

AND 语句,拼接 +AND 字段=值

andNew

AND 语句,拼接 +AND (字段=值)

or

OR 语句,拼接 +OR 字段=值

orNew

OR 语句,拼接 +OR (字段=值)

eq

等于=

allEq

基于 map 内容等于=

ne

不等于<>

gt

大于>

ge

大于等于>=

lt

小于<

le

小于等于<=

like

模糊查询 LIKE

notLike

模糊查询 NOT LIKE

in

IN 查询

notIn

NOT IN 查询

isNull

NULL 值查询

isNotNull

IS NOT NULL

groupBy

分组 GROUP BY

having

HAVING 关键词

orderBy

排序 ORDER BY

orderAsc

ASC 排序 ORDER BY

orderDesc

DESC 排序 ORDER BY

exists

EXISTS 条件语句

notExists

NOT EXISTS 条件语句

between

BETWEEN 条件语句

notBetween

NOT 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);    //查

最后

深知大多数初中级Java工程师,想要提升技能,往往是自己摸索成长或者是报班学习,但对于培训机构动则近万的学费,着实压力不小。自己不成体系的自学效果低效又漫长,而且极易碰到天花板技术停滞不前!

因此收集整理了一份《Java开发全套学习资料》送给大家,初衷也很简单,就是希望能够帮助到想自学提升又不知道该从何学起的朋友,同时减轻大家的负担。

小编已加密:aHR0cHM6Ly9kb2NzLnFxLmNvbS9kb2MvRFVrVm9aSGxQZUVsTlkwUnc==出于安全原因,我们把网站通过base64编码了,大家可以通过base64解码把网址获取下来。

  • 2
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值