前言:

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

【可参考 整合 mybatis-plus 和分页查询功能到 springboot_清泉影月-CSDN博客 整合 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);    //查

Logo

为开发者提供学习成长、分享交流、生态实践、资源工具等服务,帮助开发者快速成长。

更多推荐