当前位置: 首页 > news >正文

做SEO公司多给网站ip或域名查询网

做SEO公司多给网站,ip或域名查询网,全站仪建站流程,网站对接app一、前言 本人在学习mybatis的过程中遇到的一个让人不爽的bug,在查找了些相关的资料后得以解决,遂记录。 二、报错及解决 mapper中有一方法: Select("select * from emp " "where name like concat(%, #{name}, %) "…

一、前言

本人在学习mybatis的过程中遇到的一个让人不爽的bug,在查找了些相关的资料后得以解决,遂记录。

二、报错及解决

mapper中有一方法:

@Select("select * from emp " +"where name like concat('%', #{name}, '%') " +"and gender = #{gender} " +"and entrydate between #{begin} and #{end} " +"order by update_time desc")public List<Emp> list(String name,Short gender, LocalDate begin, LocalDate end);

测试方法:

 @Testpublic void testList() {String name = "张";Short gender = 1; // 假设1代表男性,0代表女性LocalDate begin = LocalDate.parse("2001-01-01");LocalDate end = LocalDate.parse("2023-12-31");List<Emp> empList = empMapper.list(name, gender, begin, end);// 遍历打印查询结果for (Emp emp : empList) {System.out.println(emp);}}

报错信息:
org.mybatis.spring.MyBatisSystemException: nested exception is org.apache.ibatis.binding.BindingException: Parameter 'name' not found. Available parameters are [arg3, arg2, arg1, arg0, param3, param4, param1, param2]

说是name参数找不到,靠。

原因:
在 MyBatis 中,当方法参数只有一个时,会使用基于位置的参数映射。这意味着 MyBatis 会将第一个参数与 SQL 查询语句中的第一个占位符进行匹配,以此类推。

然而,当方法参数超过一个时,MyBatis 就无法准确地确定每个参数应该映射到 SQL 查询语句中的哪个占位符。

大白话就是mybatis中方法参数为一个(可以是一个基本类型的参数或者是一个JavaBean)的时候,mybatis可以自动映射。有多个参数时需要一些措施让他成功映射。

解决方法:

1、使用 @Param 注解为方法参数指定名称:

@Select("select * from emp " +"where name like concat('%', #{name}, '%') " +"and gender = #{gender} " +"and entrydate between #{begin} and #{end} " +"order by update_time desc")
public List<Emp> list(@Param("name") String name, @Param("gender") Short gender, @Param("begin") LocalDate begin, @Param("end") LocalDate end);

在接口方法中使用 @Param 注解为每个参数指定名称,确保名称与 SQL 查询语句中的占位符名称匹配。

2、将参数包装在一个对象中:

创建一个包含所有查询参数的对象,例如 EmpQuery,并将该对象作为方法的参数:

@Select("select * from emp " +"where name like concat('%', #{name}, '%') " +"and gender = #{gender} " +"and entrydate between #{begin} and #{end} " +"order by update_time desc")
public List<Emp> list(EmpQuery query);

其中 EmpQuery 类包括 namegenderbeginend 等字段。

三、@Param

1、概述

首先明确这个注解是为SQL语句中参数赋值而服务的。

@Param的作用就是给参数命名,比如在mapper里面某方法A(int id),当添加注解后A(@Param("userId") int id),也就是说外部想要取出传入的id值,只需要取它的参数名userId就可以了。将参数值传如SQL语句中,通过#{userId}进行取值给SQL的参数赋值。

2、实例:

实例一:@Param注解基本类型的参数

public User selectUser(@Param("userName") String name,@Param("password") String pwd);

映射到xml中的标签

<select id="selectUser" resultMap="User">  select * from user  where user_name = #{userName} and user_password=#{password}  
</select>

实例二:@Param注解JavaBean对象

SQL语句通过@Param注解中的别名把对象中的属性取出来然后复制

mapper中的方法:

public List<User> getAllUser(@Param("user") User u);

映射到xml中的标签

<select id="getAllUser" parameterType="com.vo.User" resultMap="userMapper">  select   from user t where 1=1  and   t.user_name = #{user.userName}  and   t.user_age = #{user.userAge}  </select>  <!-- 不加@Param的话 -->
<select id="getAllUser" parameterType="com.vo.User" resultMap="userMapper">  select   from user t where 1=1  and   t.user_name = #{userName}  and   t.user_age = #{userAge}  </select>  

3、注意点
使用了@Param注解来声明参数的时候,SQL语句取值使用#{}${}取值都可以。

不使用@Param注解声明参数的时候,必须使用的是#{}来取参数。使用${}方式取值会报错。

不使用@Param注解时,参数只能有一个,并且是Javabean。在SQL语句里可以引用JavaBean的属性,而且只能引用JavaBean的属性。

参考文章:【详解】@Param注解的用法

http://www.tj-hxxt.cn/news/70124.html

相关文章:

  • wordpress 4.5.2 漏洞长沙官网seo收费
  • 长春几个火车站啊美国今天刚刚发生的新闻
  • 软件开发专业名词seo产品是什么意思
  • 获取页面内容wordpress长春seo外包
  • aspx php哪个做门户网站好自己做网站如何赚钱
  • 湘潭网站建设湘潭振企专业重庆 seo
  • 网站网络广告如何建设浙江百度推广
  • 微信引流被加软件上海有什么seo公司
  • 网站怎么添加栏目自媒体怎么入门
  • 国外做的好看的网站有哪些推广平台和渠道
  • 网站安全维护怎么做网站链接
  • 网站开发提问seo优化网站的手段
  • 网站在线客服代码下载做游戏推广一个月能拿多少钱
  • 建设网站的目的是什么站长工具权重查询
  • 不花钱自己可以做网站吗移动惠生活app下载网址
  • 专业模板网站制作多少钱网站注册流程
  • 三站一体网站制作二级域名免费分发
  • 福田祥菱双排座小货车深圳百度seo怎么做
  • 自己做的创意的网站软件外包
  • 惠州做网站哪家公司好中国网络营销网
  • 成都旅游必去推荐优化的含义
  • 太原网站推广排名nba
  • 深圳市中心房价seo搜索引擎优化招聘
  • 瑞昌市建设局网站qq群推广平台
  • 石家庄 做网站百度开放云平台
  • 如何靠做网站赚钱吗全媒体广告代理
  • 个人网站空间购买谷歌收录提交入口
  • 大连今日刚刚发生的重大新闻seo公司多少钱
  • 图片网站制作谷歌搜索引擎镜像入口
  • 网站怎么做sem网站seo查询