使用mybatis遇到的一些问题

本文探讨了在使用SpringBoot与Mybatis时遇到的SQL注入及OrderBy排序问题。通过调整mapper.xml文件中的SQL语句,引入参数化处理,有效避免了SQL注入风险。同时,针对OrderBy动态排序需求,提供了灵活的解决方案。

1.使用druid,具有防止sql注入的功能.

https://blog.youkuaiyun.com/vichou_fa/article/details/79285749  SpringBoot + Mybatis 在控制台输出sql语句(主要是配置)

问题一: 发现直接在mapper.xml中接直接写

select t1.constant_name as departmentName,
  CASE t2.pool_type
      WHEN '1' THEN '后勤线'
      WHEN '2' THEN '业务线'
      WHEN '3' THEN '物流线'
  end as line,
  t2.grant_amount as distributeAmount,t2.forfeit_amount as forfeitAmount,t2.result_amount as grantAmount,
  t2.surplus_amount as surplusAmount
   from
            t_system_constant t1,
            t_bonus_monthly_surplus t2
  where t1.constant_key=t2.department_id and t1.constant_type='pool_type' and 1=1

它会把它作为sql注入处理,就会报错.还有就是order by 排序问题

<select id="selectDeptBonusListByMap" parameterType="java.util.Map" resultType="com.deli.dom.bonus.form.BonusStatisticsForm">
  select t1.constant_name as departmentName,
    CASE t2.pool_type
        WHEN '1' THEN '后勤线'
        WHEN '2' THEN '业务线'
        WHEN '3' THEN '物流线'
    end as line,
    t2.grant_amount as distributeAmount,t2.forfeit_amount as forfeitAmount,t2.result_amount as grantAmount,
    t2.surplus_amount as surplusAmount
     from
              t_system_constant t1,
              t_bonus_monthly_surplus t2
    where t1.constant_key=t2.department_id and t1.constant_type=#{constantType,jdbcType=VARCHAR} and 1=1
    <if test="year !=null and year!=''">
      and t2.year = #{year,jdbcType=VARCHAR}
    </if>
    <if test="month !=null and month!=''">
      and t2.month = #{month,jdbcType=VARCHAR}
    </if>
    <if test="str !=null and str!=''">
      and FIND_IN_SET(t2.pool_type,#{str,jdbcType=VARCHAR})
    </if>
    <if test="deptName !=null and deptName!=''">
      and t1.constant_name like #{deptName,jdbcType=VARCHAR}
    </if>
    <if test="orderBy !=null and orderBy!=''">
      order by ${orderBy}
    </if>
</select>

参数如下:

Map map = MapUtil
    .convert2HashMap("deptName", deptName == null ? "" : "%" + deptName + "%", "str",
        s, "orderBy", "department_id", "year", year,
        "month", month, "constantType", "bonus_department_info");

https://www.jb51.net/article/125744.htm  find_in_set的用法参考

在微服务架构中使用 MyBatis-Plus 时,可能会遇到多个模块之间的依赖管理、配置冲突以及 Bean 加载问题。以下是常见问题及其解决方案。 ### 三级标题:多模块项目中 MyBatis-Plus 依赖管理 在多模块微服务项目中,若一个模块(如 `login-or-register`)调用另一个模块(如 `common`)中的 MyBatis-Plus 操作,需要确保所有涉及模块都正确引入 MyBatis-Plus 的依赖。例如,在根 `pom.xml` 中定义统一版本,并在子模块中引入: ```xml <!-- 根 pom.xml 中定义版本 --> <properties> <mybatis-plus.version>3.5.4</mybatis-plus.version> </properties> <!-- 子模块 pom.xml 中引用 --> <dependency> <groupId>com.baomidou</groupId> <artifactId>mybatis-plus-boot-starter</artifactId> <version>${mybatis-plus.version}</version> </dependency> ``` 若未统一版本或遗漏依赖,可能导致运行时找不到相关类或方法,从而引发 Bean 加载失败等问题[^4]。 ### 三级标题:MyBatisMyBatis-Plus 配置冲突 微服务项目中若原本使用 MyBatis,直接替换为 MyBatis-Plus 时需注意配置文件的兼容性。应将原有的 `mybatis` 配置替换为 `mybatis-plus`,并调整相关参数: ```yaml mybatis-plus: typeAliasesPackage: com.ruoyi.* mapperLocations: classpath:mapper/**/*.xml ``` 上述配置中,`typeAliasesPackage` 指定别名扫描包,`mapperLocations` 定义 XML 映射文件的路径。若保留原有 MyBatis 配置,可能与 MyBatis-Plus 冲突,导致 SQL 映射异常或 Mapper 接口无法注入[^3]。 ### 三级标题:Mapper 接口无法被 Spring 管理 在微服务中,若某模块的 Mapper 接口未被 Spring 容器识别,可能是由于 `@MapperScan` 注解未正确配置。应在主启动类或配置类上添加: ```java @SpringBootApplication @MapperScan("com.ljm.mapper") public class Application { public static void main(String[] args) { SpringApplication.run(Application.class, args); } } ``` 此注解确保 Spring Boot 启动时扫描指定包下的 Mapper 接口,并为其生成代理类。若未配置或包路径未包含实际 Mapper 接口位置,将导致注入失败或 Bean 创建异常[^1]。 ### 三级标题:Nacos 配置中心中 MyBatis-Plus 配置管理 在基于 Nacos 实现的微服务架构中,可将 MyBatis-Plus 的配置集中管理。例如,在 Nacos 配置文件中定义: ```yaml mybatis-plus: typeAliasesPackage: vip.ruoyi.system mapperLocations: classpath:mapper/**/*.xml ``` 通过配置中心统一管理,避免各模块重复配置,同时提升配置一致性与可维护性。若配置缺失或路径错误,可能导致启动失败或 SQL 文件加载异常[^2]。 ###
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值