springboot项目中基于mybatis组件的升级解决

在SpringBoot项目中使用Mybatis进行数据库操作时,为提高效率,进行了Mybatis到MybatisPlus的升级。在升级过程中,遇到了字段映射问题,由于历史mapper使用了AS映射,但在Mybatis-plus中未正确配置resultMap,导致部分字段返回null。解决方案是规范mapper书写,避免控制台映射问题。同时,文章提到了元数据类字段配置的繁琐,建议通过全局驼峰命名配置简化操作,并讨论了在支持多种数据库环境下,主键生成策略的选择。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

在平日里代码的开发工作中,重复最多的还是对表的增删改查,很耗时,这就提起了目前项目的组件选型,用的是springboot+mybatis,写逻辑之前还要花几刻钟写增删改查的mapper,leader说就写几个xml吧不多,但是我想哭,看着这么多需求过来,宝宝哭着要下班呢。
以下代码为 伪代码 结构:

@Repository
public interface BasAuditNoticeMapper{
   

   /**
     *
     * @param auditNoticeId
     * @return
     */
    BasAuditNotice selectById(@Param("auditNoticeId") Long auditNoticeId);
...
<mapper namespace="com.dtwave.dipper.dubhe.server.database.dao.table.BasAuditNoticeMapper">
    <resultMap id="BaseResultMap" type="com.dtwave.dipper.dubhe.server.database.entity.table.BasAuditNotice">
        <id column="audit_notice_id" property="auditNoticeId"/>
        <result column="ctime" property="ctime"/>
        <result column="mtime" property="mtime"/>
        <result column="table_id" property="tableId"/>
        <result column="field_id" property="fieldId"/>
    </resultMap>

    <sql id="BaseColumnList">
        <trim suffix="" suffixOverrides=",">
 			audit_notice_id as auditNoticeId,
            ctime as ctime,
            mtime as mtime,
            table_id as tableId,
            field_id as fieldId
        </trim>
    </sql>
    
    <!-- 根据ID查询 -->
    <select id="selectById" resultMap="BaseResultMap">
        select
        <include refid="BaseColumnList"/>
        from bas_audit_notice
        where audit_notice_id = #{auditNoticeId}
        and invalid=0
    </select>

@Data
public class BasAuditNotice {
   
    /**
     * 告警记录自增id
     */
    private Long auditNoticeId;
    /**
     * 创建时间
     */
    private Date ctime;
    /**
     * 修改时间
     */
    private Date mtime;
    /**
     * 任务ID
     */
    private Long taskId;
    ...
}

mybatis配置文件

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
            http://www.springframework.org/schema/beans/spring-beans.xsd">

    <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource" init-method="init" destroy-method="close">
        <property name="driverClassName" value="com.mysql.jdbc.Driver"/>

        <property name="url" value="jdbc:mysql://${spring.datasource.url}"/>
        <property name="username" value="${spring.datasource.username}"/>
        <property name="password" value="${spring.datasource.password}"/>

        <property name="filters" value="config,stat"/>
        <property name="connectionProperties" value="config.decrypt=false"/>
        <property name="initialSize" value="10"/>
        <property name="minIdle" value="1"/>
        <property name="maxActive" value="50"/>
        <property name="maxWait" value="60000"/>
        <property name="timeBetweenEvi
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值