logo 范 · 拾光录
网址收集 关于作者 Github Gitee
杂文随笔5
Hexo博客:基础使用Hexo博客:Next主题Hexo博客:Next进阶使用Hexo博客:Next高级配置基于Node的WIKI管理
前端知识16
HTML常用知识CSS常用知识CSS美化checkbox复选框JavaScript常用知识JavaScript格式化时间戳JavaScript窗口宽高处理JavaScript黑夜主题切换实现方案JavaScript数字转大写简易图片查看器TypeScript基础知识Threejs基础三要素Threejs网格辅助和轨道控制器Threejs物体绘制Electron基础使用Nodejs基础知识animate.css页面动画
Vue框架19
Vite的使用及扩展Vue3父子组件Vue3使用Marked解析MarkdownMermaid图表生成库初始化页面加载动画Axios表单提交二维码解决方案NProgress加载进度条Vue3动态菜单实现Vue3使用ECharts图表Vue3处理Excel导入导出keep-alive页面缓存及setup问题Element:文件上传Element:结合Pinia实现动态菜单Element:图片上传组件Element:自定义统一弹窗组件Element:表格自定义指令控制按钮显示(鉴权)可视化大屏使用缩放适配分辨率
UniApp15
UniApp的基础使用封装网络请求工具及文件上传uni-app的开发记录微信小程序分享原生文件上传Pinia取消滚动条(兼容小程序)tabbar消息数量显示scroll-view上滑到底部加载数据状态栏高度动态设配数据共享与传递uview-plus导航栏实现背景融合Wot UIWot UI实现顶部背景图融合uni-app x
Java基础知识10
基础知识面向对象Lambda表达式常用API常用知识积累try-with-resource注解反射多线程经纬度距离计算
SpringBoot31
application配置Maven创建聚合项目全局异常处理锁机制项目启动初始化数据方式邮件功能集成原生定时任务异步集成阿里云OSS阿里OSS预签名上传基于hutool读excelJSR303WebSocketWebSocket版AI接口流式调用Smart-Doc接口文档生成器application配置信息加密雪花算法工具AOP实现请求参数脱敏思路JWT生成Token及工具类SpringBoot默认JSON与对象转换若依框架:安装使用若依框架:优化和调整文件上传若依框架:管理后台页面优化若依框架:后端接口代码优化SpringAISpringBoot实现AI接口流式调用服务启动时创建MySQL连接自建项目工程树形结构处理工具微信支付代码微信手机号登录
SpringMVC14
跨域处理拦截器RESTful风格伪前后端分离Jackson转换器调整Thymeleaf基于拦截器做权限校验AOP打印接口请求响应日志AOP打印接口请求响应耗时文件上传和回显POST请求加解密实现(AES)POST请求加解密实现(RSA+AES)参数动态校验实现方案真实IP和归属地
MyBatis8
MyBatis基本使用与配置Mapper使用相关MaBatis多数据源配置MyBatisPlus数据统计类处理方案MyBatisPlus条件查询正向工程的实现(H2)mybatis-plus-join
SpringCloud15
Netflix:微服务与搭建Netflix:服务的消费与提供Netflix:EurekaNetflix:ActuatorNetflix:RibbonNetflix:FeignNetflix:HystrixNetflix:ZuulAlibaba:简介与搭建Alibaba:Nacos注册中心Alibaba:RibbonAlibaba:OpenFeignAlibaba:Nacos配置中心Alibaba:GetewayAlibaba:Sentinel
MySQL6
MySQL基础知识MySQL多表查询与事务MySQL常用函数及解决方案MySQL视图MySQL索引安装MySQL
Redis7
Redis介绍和安装Redis配置文件Redis持久化Redis集群Redis语法基础Redis相关问题及解决方案SpringBoot集成Redis使用记录
MongoDB10
Linux安装MongoDBMongoDB基础语法MongoTemplate及SpringBoot配置MongoTemplate中Update操作MongoTemplate中聚合查询MongoTemplate日期归档示例项目使用相关知识归纳地理位置存储与距离查询MongoDB副本集与事务获取类名和属性名工具类
其他数据库1
H2数据库
Python编程6
Python基础知识Python语法yolo目标检测OpenCV的使用及树莓派平台condauv
工具集合13
IDEAMavenGradleGitNginx安装Nginx配置VSCodeJMeter压测DockerOllamaRustFSPicGoObs录制
Linux知识11
Linux常用命令Jar启动脚本VirtualBox安装CentOSVirtualBox安装Ubuntu树莓派安装及使用frp内网穿透ArchLinux:基础系统安装ArchLInux:图形化界面安装ArchLinux:常用软件ArchLinux:深度优化ArchLinux:Niri
创意设计2
Blender:入门知识UI设计基础知识
AI相关9
Claude CodeHermes AgentOpenAI基本使用OpenAI工具调用OpenAI记忆管理OpenAI推理执行OpenAI开发框架Langchainllama.cpp

mapper文件模板

<?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.fan.mapper.ModuleMapper">

</mapper>

关于mapper和xml

首先会定义一个mapper接口,这里操作简单也可以使用增删改查的注解完成操作,但是通常是不推荐的,在有些情况下(如表字段和实体字段不对应的情况),就不好处理

正常情况下都会对应一个mapper文件(不好对应的可以安装MybatisX插件,就能直接跳转),然后定义好表和实体类字段的对应关系

注意:这步最好不要省略,在以后的迭代中不保证出现字段不一致的情况,在这里修改就很方便

提醒:

  1. desckeyread是关键字,不在出现在表中
  2. MybatisX插件可以生成该文件
<?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.fan.mapper.ReportRecordMapper">
    <resultMap id="BaseResultMap" type="com.lhxn.model.course.ReportRecord">
        <id property="id" column="id" jdbcType="BIGINT"/>
        <result property="uid" column="uid" jdbcType="BIGINT"/>
        <result property="reportId" column="report_id" jdbcType="BIGINT"/>
        <result property="reportTypeName" column="report_type_name" jdbcType="VARCHAR"/>
        <result property="operationId" column="operation_id" jdbcType="BIGINT"/>
        <result property="type" column="type" jdbcType="INTEGER"/>
        <result property="desc" column="content" jdbcType="VARCHAR"/>
        <result property="status" column="status" jdbcType="INTEGER"/>
        <result property="handleDesc" column="handle_desc" jdbcType="VARCHAR"/>
        <result property="handleResult" column="handle_result" jdbcType="INTEGER"/>
        <result property="picUrl" column="pic_url" jdbcType="VARCHAR"/>
        <result property="createDate" column="create_date" jdbcType="TIMESTAMP"/>
        <result property="updateDate" column="update_date" jdbcType="TIMESTAMP"/>
        <result property="processorReportResult" column="processor_report_result" jdbcType="VARCHAR"/>
    </resultMap>


</mapper>

对象返回(一对一)

有些场景需要返回对象中还有对象的情况,这样的逻辑确实比全部字段放在一个对象中清晰,能更方便的知道哪个字段属于哪个对象

<!--这里的id与查询的resultMap对应,type与接收的实体类对象-->
<resultMap id="getMessageDynamicListMap" type="com.model.MessageDynamicInfo">
    <id column="id" property="id"/>
    <result column="user_id" property="userId"/>
    <result column="text" property="text"/>
    <result column="photos" property="photos"/>
    <!--这是映射对象中的对象,property为对象中的对象的字段,javaType为实体类-->
    <association property="user" javaType="com.model.User">
        <id column="uid" property="id"/>
        <result column="username" property="username"/>
        <result column="real_name" property="realName"/>
        <result column="avatar" property="avatar"/>
    </association>
</resultMap>

<select id="selectMessageList" resultMap="getMessageDynamicListMap">
    <!--SQL语句-->
    SELECT xxx
</select>

对象返回(一对多)

推荐子查询实现

<!--对象映射-->
<resultMap id="selectHostelInfoDetailsResultMap" type="com.springmvc.model.HostelInfoDetailsVo">
    <id column="id" property="id"/>
    <result column="user_id" property="userId"/>
    <result column="hostel_name" property="hostelName"/>
    <result column="username" property="linkUser"/>
    <result column="province" property="province"/>
    <result column="city" property="city"/>
    <result column="county" property="county"/>
    <result column="city_code" property="cityCode"/>
    <result column="address" property="address"/>
    <result column="authenticate" property="authenticate"/>
    <result column="create_time" property="createTime"/>
    <result column="update_time" property="updateTime"/>
    <!--将主表的字段传给子查询-->
    <collection property="imgList" ofType="com.springmvc.model.HostelInfoImgVo" column="{hostelId=id}" select="selectHostelInfoDetailsImg">
        <id column="id" property="id"/>
        <result column="url" property="url"/>
    </collection>
</resultMap>
<!--主表查询-->
<select id="selectHostelInfoDetails" resultMap="selectHostelInfoDetailsResultMap">
    select *
    from sw_hostel_info
    where sw_hostel_info.id = #{hostelId}
</select>
<!--子查询-->
<select id="selectHostelInfoDetailsImg" resultType="com.springmvc.model.HostelInfoImgVo">
    select *
    from sw_hostel_info_img
    where hostel_id = #{hostelId} and status = 1
</select>

关于查询别名

刚开始的时候不觉得,后来发现关联表查询时,使用别名能更好的区分获取了哪张表的哪个字段

SELECT t1.id,
       t1.user_id,
       t1.text,
       t1.photos,
       t1.from_oid,
       t1.share_num,
       t1.viewing_count,
       t1.file_type,
       t2.id as uid,
       t2.username,
       t2.real_name,
       t2.avatar,
       t2.type
FROM message_dynamic t1,
     `user` t2
WHERE t1.user_id = t2.id
  AND t1.`status` = 0
  AND t1.from_oid = #{circleId}
ORDER BY t1.id DESC LIMIT #{skip}, #{limit}

关于点赞和总数

SELECT t1.id,
       t1.user_id,
       t1.text,
       t2.id as uid,
       t2.username,
       <!--查询点赞数-->
       (SELECT count(1) FROM dynamic_like WHERE dynamic_like.m_id = t1.id)                         as likeNum,
       <!--查询自己是否点赞-->
       (SELECT count(1) FROM dynamic_like WHERE dynamic_like.m_id = t1.id AND user_id = #{userId} AND dynamic_like.m_type = 1) as `like`
FROM message_dynamic t1,
     `user` t2
WHERE t1.user_id = t2.id
  AND t1.`status` = 0
  AND t1.from_oid = #{circleId}
ORDER BY t1.id DESC LIMIT #{skip}, #{limit}

查询存在优化:SELECT 1 FROM table WHERE a = 1 AND b = 2 LIMIT 1

xml中使用对象参数

mapper

/**
 * 添加管理员信息
 */
Integer insertAdminInfo(@Param("admin") Admin admin);

这里注解不可少,同时注意该注解在import org.apache.ibatis.annotations.Param;包下

xml

<!--添加管理员信息-->
<insert id="insertAdminInfo">
    insert into admin(username, password, nick)
    values (#{admin.username}, #{admin.password}, #{admin.nick})
</insert>

if标签

<select id="selectList" resultType="com.springmvc.model.HostelGuestRegisterVo">
    select *
    from sw_hostel_guest_user t1
    where t1.hostel_id = #{hostelId}
    <if test="userId != null and userId != ''">
        and user_id = #{userId}
    </if>
</select>

like拼接

<if test="keyword != null and keyword != ''">
    and keyword like CONCAT('%',#{keyword},'%')
</if>

日期大小

不要加!='',会报错。

<if test="hostelListDto.registerTimeStart != null">
    and t1.last_operate_time &gt; #{hostelListDto.registerTimeStart}
</if>

列表查询示例(直接计算分页)

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.lky.entity.manage.SysRole;
import com.lky.vo.system.SysRoleListReqVo;
import com.lky.vo.system.SysRoleListRespVo;
import org.apache.ibatis.annotations.Param;

import java.util.List;

/**
 * 系统角色
 */
public interface SysRoleMapper extends BaseMapper<SysRole> {

    /**
     * 列表总数
     */
    Long listCount(@Param("params") SysRoleListReqVo params);

    /**
     * 列表
     */
    List<SysRoleListRespVo> listSelect(@Param("params") SysRoleListReqVo params);
    
}

// 注意@Param的包不要引错



<?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.lky.mapper.SysRoleMapper">

    <select id="listCount" resultType="java.lang.Long">
        select count(id) from sys_role
        where status = 1
        <if test="params.keyword != null and params.keyword != ''">
            and name like CONCAT('%',#{params.keyword},'%')
        </if>
    </select>

    <select id="listSelect" resultType="com.lky.vo.system.SysRoleListRespVo">
        select * from sys_role
        where status = 1
        <if test="params.keyword != null and params.keyword != ''">
            and name like CONCAT('%',#{params.keyword},'%')
        </if>
        limit ${(params.pageIndex-1)*params.pageSize}, #{params.pageSize}
    </select>

</mapper>

when标签

<select id="selectOrderList" parameterType="Order" resultMap="OrderResult">
    <include refid="selectOrderVo"/>
    <where>
        <if test="id != null  and id != ''">and id = #{id}</if>
        <if test="unitName != null  and unitName != ''">and unit_name like concat('%', #{unitName}, '%')</if>
        <if test="unitUser != null  and unitUser != ''">and unit_user = #{unitUser}</if>
        <if test="unitPhone != null  and unitPhone != ''">and unit_phone = #{unitPhone}</if>
        <choose>
            <when test="status == -1">
                and status != 1
            </when>
            <when test="status == null">

            </when>
            <otherwise>
                and status = #{status}
            </otherwise>
        </choose>
        <if test="payStatus != null ">and pay_status = #{payStatus}</if>
    </where>
</select>
mapper文件模板
关于mapper和xml
对象返回(一对一)
对象返回(一对多)
关于查询别名
关于点赞和总数
xml中使用对象参数
if标签
like拼接
日期大小
列表查询示例(直接计算分页)
when标签