mybatis不加@Parm注解报错的解决方案

我的idea版本2017.3.4,低版本貌似不会加上这个配置,idea高版本会

补充知识:Mybatis传多个参数的问题 及MyBatis报错 Parameter '0' not found. Available parameters are [arg1, arg0, param1 问题

对于使用Mybatis ,传多个参数,我们可以使用对象封装外,还可以直接传递参数

对象的封装,例如查询对象条件basequery对象

<select id="getProductByProductQuery" parameterType="com.niulande.product.query.BaseQuery" resultMap="BaseResultMap">
 select
 <include refid="Base_Column_List" />
 from pd_product
 <include refid="whereSql"/>
 </select>
 <sql id= "whereSql" >
 <where>
  <if test="gameCode != null and gameCode != ''" >
  and game_type_coding = #{gameCode}
  </if>
  <if test="goodsTypeId != null">
  and goods_type_id = #{goodsTypeId}
  </if>
  <if test="accId != null">
  and account_id = #{accId}
  </if>
  <if test="delFlag != null">
  and del_flag = #{delFlag}
  </if>
 </where>
 limit #{start},#{rows}
 </sql>
</mapper>

直接传递参数

例如:

mapper方法

selectByGameIdAndGoodsTypeId(Long gameTypeId, Long goodsTypeId);

对应的xml文件方法:

<select id="selectByGameIdAndGoodsTypeId" resultMap="BaseResultMap">
 select
 <include refid="Base_Column_List" />
 from pd_game_goods_type_mid
 where game_type_id = #{gameTypeId} AND goods_type_id = #{goodsTypeId}
</select>

第一:在select标签后就不再使用parameterType,因为这个标签只能指定一个参数,而两个参数及以上的,则不用再使用

第二:在sql语句里面以上的写法是错误的(为了演示执行报错)

会报错

Parameter '0' not found. Available parameters are [arg1, arg0, param1, param2]

注意这里使用的mybatis的版本号

在MyBatis3.4.4版不能直接使用#{0}要使用 #{arg0}

0是指参数的索引,从0开始。第一个参数是0,第二个参数是1,依次类推

以下正确的写法:

<select id="selectByGameIdAndGoodsTypeId" resultMap="BaseResultMap">
 select
 <include refid="Base_Column_List" />
 from pd_game_goods_type_mid
 where game_type_id = #{arg0} AND goods_type_id = #{arg1}
</select>

第三种:

<select id="selectByGameIdAndGoodsTypeId" resultMap="BaseResultMap">
 select
 <include refid="Base_Column_List" />
 from pd_game_goods_type_mid
 where game_type_id = #{gameTypeId} AND goods_type_id = #{goodsTypeId}
</select>

刚刚说这样的会报错。解决办法,更改mapper方法

加上@Param注解

selectByGameIdAndGoodsTypeId(@Param("gameTypeId")Long gameTypeId, @Param("goodsTypeId") Long goodsTypeId)

以上这篇mybatis不加@Parm注解报错的解决方案就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持我们。

(0)

相关推荐

  • Mybatis如何使用注解优化代码

    entity层代码不变,因为是优化,所以在dao层新增了一个通过id查询用户的功能,现在来演示一下优化前后的代码对比,希望你们喜欢 entity层代码不变,dao层中的UserMapper中添加一个方法 /** * 根据id查询用户 */ User findUserById(int id); 在UserMapper.xml映射文件中添加 <select id="findUserById" parameterType="int" resultType="

  • mybatis不加@Parm注解报错的解决方案

    我的idea版本2017.3.4,低版本貌似不会加上这个配置,idea高版本会 补充知识:Mybatis传多个参数的问题 及MyBatis报错 Parameter '0' not found. Available parameters are [arg1, arg0, param1 问题 对于使用Mybatis ,传多个参数,我们可以使用对象封装外,还可以直接传递参数 对象的封装,例如查询对象条件basequery对象 <select id="getProductByProductQuer

  • SpringBoot整合Mybatis注解开发的实现代码

    官方文档: https://mybatis.org/mybatis-3/zh/getting-started.html SpringBoot整合Mybatis 引入maven依赖 (IDEA建项目的时候直接选就可以了) <dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> <ve

  • Spring AOP如何实现注解式的Mybatis多数据源切换详解

    一.为什么要使用多数据源切换? 多数据源切换是为了满足什么业务场景?正常情况下,一个微服务或者说一个WEB项目,在使用Mybatis作为数据库链接和操作框架的情况下通常只需要构建一个系统库,在该系统库创建业务表来满足需求,当然也有分为测试库和正式库dev/prod,不过这俩库的切换是使用配置文件进行切分的,在项目启动时或者打成maven JAR包指定environment-dev.properties或者environment-prod.properties. 那么当程序运行过程中,比如一个co

  • mybatis省略@Param注解操作

    项目是Springboot+mybatis,每次写一堆@Param注解感觉挺麻烦,就找方法想把这个注解给省了,最后确实找到一个方法 1.在mybatis的配置里有个属性useActualParamName,允许使用方法签名中的名称作为语句参数名称 我用的mybatis:3.4.2版本Configuration中useActualParamName的默认值为true 源码简单分析: MapperMethod的execute方法中获取参数的方法convertArgsToSqlCommandParam

  • @Accessors(chain = true)注解报错的解决方案

    如下所示: Cannot invoke setItemTitle(String) on the primitive type void 定义的实体类如下: @Data public static class RefundOrderItem implements Serializable { /** * 商品标题 */ @JsonProperty("item_title") private String itemTitle; /** * 数量 */ private BigDecimal

  • Mybatis Mapper中多参数方法不使用@param注解报错的解决

    目录 问题描述 寻求解决方案 寻找原因 拓展延伸 在使用低版本的Mybatis的时候,Mapper中的方法如果有多个参数时需要使用@param注解,才能在对应xml的sql语句中使用参数名称获取传入方法的参数值,否则就会报错.本文结合自身在真实开发环境中使用IDEA开发时遇到的问题来共同探讨一下不使用@Param注解报错背后的原因以及解决方案. 问题描述 最近使用IDEA进行开发,项目使用SpringBoot+Mybatis3.4.6,同样的代码检出到本地IDEA后运行,在一个业务查询模块报错,

  • mybatis中使用InsertProvider注解报错解决全过程

    目录 使用InsertProvider注解报错解决 mybatis注解开发@InsertProvider Userprovider类 mapper的书写 使用InsertProvider注解报错解决 目前项目在使用mybatis,并且是使用注解的方式. 在使用InsertProvider注解的时候报了一下的错误: org.apache.ibatis.builder.BuilderException: Could not find value method on SQL annotation.  

  • SpringBoot集成MybatisPlus报错的解决方案

    这篇文章主要介绍了SpringBoot集成MybatisPlus报错的解决方案,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下 问题 启动的时候总是报如下错误: java.lang.annotation.AnnotationFormatError: Invalid default: public abstract java.lang.Class 解决方案 需要一个mybatis-spring-boot-starter的包,在pom文件加上之后,完

  • Django migrate报错的解决方案

    前言 在讲解如何解决migrate报错原因前,我们先要了解migrate做了什么事情,migrate:将新生成的迁移脚本.映射到数据库中.创建新的表或者修改表的结构. 问题1:migrate怎么判断哪些迁移脚本需要执行? 它会将代码中的迁移脚本和数据库中django_migrations中的迁移脚本进行对比,如果发现数据库中,没有这个迁移脚本,那么就会执行这个迁移脚本. 问题2:migrate做了什么事情 将相关的迁移脚本翻译成SQL语句,在数据库中执行这个SQL语句. 如果这个SQL语句执行没

  • 启动springboot应用因未配置数据库报错的解决方案

    目录 启动springboot应用因未配置数据库报错 描述 解决方案 springboot 1.5.8.RELEASE 版本启动报错 起因 错误排查 解决方法 启动springboot应用因未配置数据库报错 描述 创建一个全新的springboot项目,第一次启动时报错,具体错误信息如下所示: Error starting ApplicationContext. To display the conditions report re-run your application with 'debu

  • SpringDataJpa的@Query注解报错的解决

    目录 SpringDataJpa @Query注解报错 SpringDataJpa @query注解使用原生代码报错 SpringDataJpa @Query注解报错 public interface TimeContentRepository extends JpaRepository<TimeContent,String> { @Query(value = "select id,user_id as userId,create_time as createTime "

  • Maven依赖junit @Test报错的解决方案

    目录 Maven依赖junit@Test报错 现象 解决方案 idea添加junit的maven依赖后,使用@Test.@Before.@After仍报错 maven中的依赖配置如下 Maven依赖junit @Test报错 现象 解决方案 测试文件夹标记使用错啦,test 表示junit的jar包只能在标记为 Test Sources Root 的文件夹下被调用,调整一下就OK了 如下图: idea添加junit的maven依赖后,使用@Test.@Before.@After仍报错 一般该问题

  • npm i报错以及解决方案实战案例

    目录 报错案例1 报错案例2 报错案例3 报错案例4 报错案例5 总结 报错案例1 npm ERR! Cannot read properties of null (reading 'pickAlgorithm') 解决方案:清理缓存后再次安装 npm cache clear --force 报错案例2 npm ERR! gyp info it worked if it ends with ok ... npm ERR! gyp ERR! cwd C:\...\node_modules\node

随机推荐