MyBatis映射器(一)--多参数传递方式
在mybatis映射器的接口中,一般在查询时需要传递一些参数作为查询条件,有时候是一个,有时候是多个。当只有一个参数时,我们只要在sql中使用接口中的参数名称即可,但是如果是多个呢,就不能直接用参数名称了,mybatis中有以下四种 第一种:使用map传递 1⃣️定义接口 1//使用map传递多个参数进行查询 2publicListgetByMap(MapparamMap); 2⃣️sql语句 1 23SELECT*FROMproduct 4WHEREproduct_nameLIKEconcat('%',#{name},'%')AND 5CAST(product_priceASINT)>#{price} 6 需要注意的有: 1、parameterType参数类型为map(此处使用别名) 2、参数名称是map中的key 3⃣️查询 1/** 2*通过map传递多个参数 3* 4*@return 5*/ 6publicvoidgetProductsByMap(){ 7System.out.println("使用map方式传递多个参数"); 8Listproducts=newArrayList<>(); 9MapparamMap=newHashMap<>(); 10paramMap.put("name","恤"); 11paramMap.put("price",200); 12sqlSession=MybatisTool.getSqlSession(); 13productMapper=sqlSession.getMapper(ProductMapper.class); 14products=productMapper.getByMap(paramMap); 15printResult(products); 16} 4⃣️查看结果 1使用map方式传递多个参数 2T恤2的价格是230元 3T恤3的价格是270元 4T恤4的价格是270元 这种方式的缺点是: 1、map是一个键值对应的集合,使用者只有阅读了它的键才能知道其作用; 2、使用map不能限定其传递的数据类型,可读性差 所以一般不推荐使用这种方式。 第二种:使用注解传递 1⃣️创建接口 1//使用注解传递多个参数进行查询 2publicListgetByAnnotation(@Param("name")Stringname,@Param("price")intprice); 2⃣️定义sql 1 23SELECT*FROMproduct 4WHEREproduct_nameLIKEconcat('%',#{name},'%')ANDCAST(product_price 5ASINT)> 6#{price} 7 这种方式不需要设置参数类型 ,参数名称为注解定义的名称 3⃣️查询 1/** 2*通过注解传递多个参数 3*/ 4publicvoidgetProductByAnnotation(){ 5System.out.println("使用注解方式传递多个参数"); 6Listproducts=newArrayList<>(); 7sqlSession=MybatisTool.getSqlSession(); 8productMapper=sqlSession.getMapper(ProductMapper.class); 9products=productMapper.getByAnnotation("恤",200); 10printResult(products); 11} 4⃣️查看结果 1使用注解方式传递多个参数 2T恤2的价格是230元 3T恤3的价格是270元 4T恤4的价格是270元 这种方式能够大大提高可读性,但是只适合参数较少的情况,一般是少于5个用此方法,5个以上九要用其他方式了。 第三种:使用javabean传递 此中方式需要将传递的参数封装成一个javabean,然后将此javabean当作参数传递即可,为了方便,我这里只有两个参数封装javabean。 1⃣️参数封装成javabean 1/** 2*定义一个Javabean用来传递参数 3*/ 4publicclassParamBean{ 5publicStringname; 6publicintprice; 7 8publicParamBean(Stringname,intprice){ 9this.name=name; 10this.price=price; 11} 12 13publicStringgetName(){ 14returnname; 15} 16 17publicvoidsetName(Stringname){ 18this.name=name; 19} 20 21publicintgetPrice(){ 22returnprice; 23} 24 25publicvoidsetPrice(intprice){ 26this.price=price; 27} 28} 2⃣️创建接口 1//使用JavaBean传递多个参数进行查询 2publicListgetByJavabean(ParamBeanparamBean); 3⃣️定义sql 1 23SELECT*FROMproduct 4WHEREproduct_nameLIKEconcat('%',#{name},'%') 5ANDCAST(product_priceASINT)>#{price} 6 需要注意的是: 1、参数类型parameterType为前面定义的javabean的全限定名或别名; 2、sql中的参数名称是javabean中定义的属性; 4⃣️查询 1/** 2*通过javabean传递多个参数 3*/ 4publicvoidgetProductByJavabean(){ 5System.out.println("使用javabean方式传递多个参数"); 6Listproducts=newArrayList<>(); 7sqlSession=MybatisTool.getSqlSession(); 8productMapper=sqlSession.getMapper(ProductMapper.class); 9ParamBeanparamBean=newParamBean("恤",200); 10products=productMapper.getByJavabean(paramBean); 11printResult(products); 12} 5⃣️查看结果 1使用javabean方式传递多个参数 2T恤2的价格是230元 3T恤3的价格是270元 4T恤4的价格是270元 这种方式在参数多于5个的情况下比较实用。 第四种:使用混合方式传递 假设我要进行分页查询,那么我可以将分页参数单独封装成一个javabean进行传递,其他参数封装成上面的javabean,然后用注解传递这两个javabean,并在sql中获取。 1⃣️封装分页参数javabean 1/* 2*定义一个分页的javabean 3*/ 4publicclassPageParamBean{ 5publicintstart; 6publicintlimit; 7 8publicPageParamBean(intstart,intlimit){ 9super(); 10this.start=start; 11this.limit=limit; 12} 13 14publicintgetStart(){ 15returnstart; 16} 17 18publicvoidsetStart(intstart){ 19this.start=start; 20} 21 22publicintgetLimit(){ 23returnlimit; 24} 25 26publicvoidsetLimit(intlimit){ 27this.limit=limit; 28} 29 30} 2⃣️创建接口 1//使用混合方式传递多个参数进行查询 2publicListgetByMix(@Param("param")ParamBeanparamBean,@Param("page")PageParamBeanpageBean); 可以看出此处使用javabean+注解的方式传递参数 3⃣️定义sql 1 23SELECT*FROMproductWHERE 4product_nameLIKEconcat('%',#{param.name},'%')ANDCAST(product_price 5ASINT)> 6#{param.price}LIMIT#{page.limit}OFFSET#{page.start} 7 只要是注解方式,就不需要定义参数类型。 4⃣️查询 1/** 2*通过混合方式传递多个参数 3*/ 4publicvoidgetProductByMix(){ 5System.out.println("使用混合方式传递多个参数"); 6Listproducts=newArrayList<>(); 7sqlSession=MybatisTool.getSqlSession(); 8productMapper=sqlSession.getMapper(ProductMapper.class); 9ParamBeanparamBean=newParamBean("恤",200); 10PageParamBeanpageBean=newPageParamBean(0,5); 11products=productMapper.getByMix(paramBean,pageBean); 12printResult(products); 5⃣️查看结果 1使用混合方式传递多个参数 2T恤2的价格是230元 3T恤3的价格是270元 4T恤4的价格是270元 以上就是四种方式传递多个参数的实例。