基于Spring Boot + MyBatis的用户管理系统配置

我来为您详细分析这两个配置文件的功能和含义。

一、文件整体概述

这是一个基于Spring Boot + MyBatis的用户管理系统配置:

  • UserMapper.xml:MyBatis的SQL映射文件,定义了用户表的增删改查操作
  • application.yml:Spring Boot的核心配置文件,配置了数据库连接、Redis、MyBatis等

二、UserMapper.xml 详细解析

1. XML文件头部声明

xml 复制代码
<?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">
  • 第1行:XML版本声明,指定使用UTF-8编码
  • 第2-3行:DOCTYPE声明,指定这是MyBatis 3.0版本的Mapper DTD文件格式

2. Mapper命名空间

xml 复制代码
<mapper namespace="com.example.usermanagement.mapper.UserMapper">
  • 定义了该XML文件对应的Java接口全限定名
  • MyBatis会将这个XML中的SQL语句与该接口的方法进行绑定

3. 结果映射配置

xml 复制代码
<resultMap id="BaseResultMap" type="com.example.usermanagement.entity.User">
    <id column="id" property="id"/>
    <result column="username" property="username"/>
    <result column="password" property="password"/>
    <result column="email" property="email"/>
    <result column="phone" property="phone"/>
    <result column="status" property="status"/>
    <result column="create_time" property="createTime"/>
    <result column="update_time" property="updateTime"/>
</resultMap>
  • resultMap:定义数据库字段与Java实体类属性的映射关系
  • id="BaseResultMap":映射的唯一标识,其他SQL语句可以引用
  • type:指定映射的目标Java类
  • <id>标签:映射主键字段,column是数据库字段名,property是Java属性名
  • <result>标签:映射普通字段
  • 注意:数据库使用下划线命名(create_time),Java使用驼峰命名(createTime)

4. SQL片段定义

xml 复制代码
<sql id="Base_Column_List">
    id, username, password, email, phone, status, score, create_time, update_time
</sql>
  • 定义可重用的SQL片段,避免重复编写字段列表
  • 注意这里包含了score字段,但resultMap中没有映射(可能是遗漏)

5. 插入操作

xml 复制代码
<insert id="insert" parameterType="com.example.usermanagement.entity.User"
        useGeneratedKeys="true" keyProperty="id">
    INSERT INTO user (username, password, email, phone, status, score)
    VALUES (#{username}, #{password}, #{email}, #{phone}, #{status}, #{score})
</insert>
  • id="insert":对应Java接口中的insert方法
  • parameterType:参数类型为User实体类
  • useGeneratedKeys="true":使用数据库自增主键
  • keyProperty="id":将生成的主键值回填到User对象的id属性
  • #{}:MyBatis的参数占位符,防止SQL注入

6. 删除操作

xml 复制代码
<delete id="deleteById" parameterType="Long">
    DELETE FROM user WHERE id = #{id}
</delete>
  • 根据ID删除用户记录
  • 参数类型为Long

7. 更新操作

xml 复制代码
<update id="update" parameterType="com.example.usermanagement.entity.User">
    UPDATE user
    <set>
        <if test="username != null">username = #{username},</if>
        <if test="password != null">password = #{password},</if>
        <if test="email != null">email = #{email},</if>
        <if test="phone != null">phone = #{phone},</if>
        <if test="status != null">status = #{status},</if>
        <if test="score != null">score = #{score},</if>
    </set>
    WHERE id = #{id}
</update>
  • 动态SQL更新:只更新非null字段
  • <set>标签:自动处理SET关键字和逗号
  • <if>标签:条件判断,test属性是OGNL表达式

8. 查询操作

xml 复制代码
<!-- 根据ID查询 -->
<select id="selectById" parameterType="Long" resultMap="BaseResultMap">
    SELECT <include refid="Base_Column_List"/>
    FROM user
    WHERE id = #{id}
</select>
  • resultMap="BaseResultMap":使用前面定义的结果映射
  • <include refid="Base_Column_List"/>:引用SQL片段

9. 分页查询

xml 复制代码
<select id="selectByPage" resultMap="BaseResultMap">
    SELECT <include refid="Base_Column_List"/>
    FROM user
    ORDER BY id DESC
    LIMIT #{offset}, #{limit}
</select>
  • 使用MySQL的LIMIT实现分页
  • offset:起始位置,limit:查询数量

10. 模糊查询

xml 复制代码
<select id="searchByUsername" resultMap="BaseResultMap">
    SELECT <include refid="Base_Column_List" />
    FROM user
    WHERE username LIKE CONCAT('%', #{username}, '%')
    ORDER BY id DESC
    LIMIT #{offset}, #{limit}
</select>
  • CONCAT函数:拼接字符串,实现模糊查询
  • 支持分页的用户名模糊搜索

三、application.yml 详细解析

1. 服务器配置

yaml 复制代码
server:
  port: 8080  # 应用端口号
  • 设置Spring Boot应用的启动端口为8080

2. 数据源配置

yaml 复制代码
spring:
  datasource:
    type: com.alibaba.druid.pool.DruidDataSource  # 使用Druid连接池
    driver-class-name: com.mysql.cj.jdbc.Driver   # MySQL 8.x驱动类
    url: jdbc:mysql://localhost:3306/user_db?useUnicode=true&characterEncoding=utf-8&serverTimezone=Asia/Shanghai
    username: root
    password: root
  • type:指定使用阿里巴巴的Druid数据库连接池
  • driver-class-name:MySQL 8.x版本的JDBC驱动类
  • url参数解析
    • localhost:3306:MySQL服务器地址和端口
    • user_db:数据库名称
    • useUnicode=true:支持Unicode字符集
    • characterEncoding=utf-8:使用UTF-8编码
    • serverTimezone=Asia/Shanghai:设置时区(MySQL 8.x必需)

3. Redis配置

yaml 复制代码
  data:
    redis:
      host: localhost      # Redis服务器地址
      port: 6379          # Redis端口(默认6379)
      password:           # Redis密码(空表示无密码)
      database: 0         # 使用的数据库编号(0-15)
      timeout: 5000       # 连接超时时间(毫秒)
  • Redis用于缓存、会话管理等功能
  • database: Redis支持16个逻辑数据库(0-15)

4. MyBatis配置

yaml 复制代码
mybatis:
  mapper-locations: classpath:mapper/*.xml  # Mapper XML文件位置
  type-aliases-package: com.example.usermanagement.entity  # 实体类包路径
  configuration:
    map-underscore-to-camel-case: true  # 开启驼峰命名转换
    log-impl: org.apache.ibatis.logging.stdout.StdOutImpl  # 打印SQL语句
  • mapper-locations:指定XML映射文件的位置(classpath:mapper/目录下所有xml文件)
  • type-aliases-package:指定实体类包,在XML中可以直接使用类名而不需要全限定名
  • map-underscore-to-camel-case:自动将数据库下划线命名转换为Java驼峰命名
  • log-impl:配置SQL日志输出到控制台,便于开发调试

5. 日志配置

yaml 复制代码
logging:
  level:
    com.example.userManagement.mapper: debug  # 打印MyBatis的SQL执行日志
  • 设置mapper包的日志级别为debug,会打印详细的SQL执行信息

四、存在的问题和建议

  1. resultMap与SQL片段不一致:SQL片段包含score字段,但resultMap中没有映射
  2. 包名不一致 :日志配置中是userManagement,其他地方是usermanagement
  3. 建议添加
    • 数据库连接池的详细配置(最大连接数、最小空闲等)
    • Redis连接池配置
    • 事务管理配置
    • 更完善的异常处理

这套配置实现了一个完整的用户管理系统的数据访问层,支持用户的增删改查、分页、模糊搜索等常用功能。

相关推荐
就是帅我不改4 小时前
敏感词过滤黑科技!SpringBoot+Vue3+TS强强联手,打造无懈可击的内容安全防线
前端·vue.js·后端
codeGoogle4 小时前
大厂研发之谜:千亿投入砸出利润大缩水
前端·人工智能·后端
Java水解4 小时前
PostgreSQL向量库pgvector实战指南
后端·postgresql
Dear.爬虫5 小时前
Golang中逃逸现象, 变量“何时栈?何时堆?”
开发语言·后端·golang
努力的小郑6 小时前
MySQL索引(三):字符串索引优化之前缀索引
后端·mysql·性能优化
IT_陈寒6 小时前
🔥3分钟掌握JavaScript性能优化:从V8引擎原理到5个实战提速技巧
前端·人工智能·后端
程序员清风7 小时前
贝壳一面:年轻代回收频率太高,如何定位?
java·后端·面试
考虑考虑7 小时前
Java实现字节转bcd编码
java·后端·java ee
AAA修煤气灶刘哥7 小时前
ES 聚合爽到飞起!从分桶到 Java 实操,再也不用翻烂文档
后端·elasticsearch·面试