MyBatis一多参数传递方式

作者: 一觉睡到丶小时候 | 来源:发表于2019-09-30 15:01 被阅读0次

    在mybatis映射器的接口中,一般在查询时需要传递一些参数作为查询条件,有时候是一个,有时候是多个。当只有一个参数时,我们只要在sql中使用接口中的参数名称即可,但是如果是多个呢,就不能直接用参数名称了,mybatis中有以下四种

    第一种:使用map传递

    1⃣️定义接口
    // 使用map传递多个参数进行查询
    public List<Product> getByMap(Map<String, Object> paramMap);
    
    2⃣️sql语句
    <!--第一种:通过map传递 -->
         <select id="getByMap" resultType="product" parameterType="map">
             SELECT * FROM product
             WHERE product_name LIKE concat('%',#{name},'%') AND
             CAST(product_price AS INT) > #{price}
         </select>
    

    需要注意的有:

    • parameterType参数类型为map(此处使用别名)

    • 参数名称是map中的key

    3⃣️查询
    /**
         * 通过map传递多个参数
         * 
         * @return
         */
        public void getProductsByMap() {
            System.out.println("使用map方式传递多个参数");
            List<Product> products = new ArrayList<>();
            Map<String, Object> paramMap = new HashMap<>();
            paramMap.put("name", "恤");
            paramMap.put("price", 200);
            sqlSession = MybatisTool.getSqlSession();
            productMapper = sqlSession.getMapper(ProductMapper.class);
            products = productMapper.getByMap(paramMap);
            printResult(products);
        }
    
    4⃣️查看结果
    使用map方式传递多个参数
    T恤2的价格是230元
    T恤3的价格是270元
    T恤4的价格是270元
    

    这种方式的缺点是:

    • map是一个键值对应的集合,使用者只有阅读了它的键才能知道其作用;

    • 使用map不能限定其传递的数据类型,可读性差

    所以一般不推荐使用这种方式。

    第二种:使用注解传递

    1⃣️创建接口
     // 使用注解传递多个参数进行查询
         public List<Product> getByAnnotation(@Param("name") String name, @Param("price") int price);
    
    2⃣️定义sql
    <!--第二种:通过注解传递 -->
        <select id="getByAnnotation" resultType="product">
            SELECT * FROM product
            WHERE product_name LIKE concat('%',#{name},'%') AND CAST(product_price
            AS INT) >
            #{price}
        </select>
    

    这种方式不需要设置参数类型 ,参数名称为注解定义的名称

    3⃣️查询
    /**
         * 通过注解传递多个参数
         */
        public void getProductByAnnotation() {
            System.out.println("使用注解方式传递多个参数");
            List<Product> products = new ArrayList<>();
            sqlSession = MybatisTool.getSqlSession();
            productMapper = sqlSession.getMapper(ProductMapper.class);
            products = productMapper.getByAnnotation("恤", 200);
            printResult(products);
        }
    
    4⃣️查看结果
     使用注解方式传递多个参数
     T恤2的价格是230元
     T恤3的价格是270元
     T恤4的价格是270元
    

    这种方式能够大大提高可读性,但是只适合参数较少的情况,一般是少于5个用此方法,5个以上九要用其他方式了。

    第三种:使用javabean传递

    此中方式需要将传递的参数封装成一个javabean,然后将此javabean当作参数传递即可,为了方便,我这里只有两个参数封装javabean。

    1⃣️参数封装成javabean
    /**
     * 定义一个Javabean用来传递参数
     */
    public class ParamBean {
        public String name;
        public int price;
    
        public ParamBean(String name, int price) {
            this.name = name;
            this.price = price;
        }
    
        public String getName() {
            return name;
        }
    
        public void setName(String name) {
            this.name = name;
        }
    
        public int getPrice() {
            return price;
        }
    
        public void setPrice(int price) {
            this.price = price;
        }
    }
    
    2⃣️创建接口
     // 使用JavaBean传递多个参数进行查询
         public List<Product> getByJavabean(ParamBean paramBean); 
    
    3⃣️定义sql
     <!--第三种:通过javabean传递 -->
        <select id="getByJavabean" resultType="product" parameterType="paramBean">
            SELECT * FROM product
            WHERE product_name LIKE concat('%',#{name},'%')
            AND CAST(product_price AS INT) > #{price}
         </select>
    

    需要注意的是:

    • 参数类型parameterType为前面定义的javabean的全限定名或别名;

    • sql中的参数名称是javabean中定义的属性;

    4⃣️查询
    /**
         * 通过javabean传递多个参数
         */
        public void getProductByJavabean() {
            System.out.println("使用javabean方式传递多个参数");
            List<Product> products = new ArrayList<>();
            sqlSession = MybatisTool.getSqlSession();
            productMapper = sqlSession.getMapper(ProductMapper.class);
            ParamBean paramBean = new ParamBean("恤", 200);
            products = productMapper.getByJavabean(paramBean);
            printResult(products);
        }
    
    5⃣️查看结果
      使用javabean方式传递多个参数
      T恤2的价格是230元
      T恤3的价格是270元
      T恤4的价格是270元 
    

    这种方式在参数多于5个的情况下比较实用。

    第四种:使用混合方式传递

    假设我要进行分页查询,那么我可以将分页参数单独封装成一个javabean进行传递,其他参数封装成上面的javabean,然后用注解传递这两个javabean,并在sql中获取。

    1⃣️封装分页参数javabean
    /*
     * 定义一个分页的javabean
     */
    public class PageParamBean {
        public int start;
        public int limit;
    
        public PageParamBean(int start, int limit) {
            super();
            this.start = start;
            this.limit = limit;
        }
    
        public int getStart() {
            return start;
        }
    
        public void setStart(int start) {
            this.start = start;
        }
    
        public int getLimit() {
            return limit;
        }
    
        public void setLimit(int limit) {
            this.limit = limit;
        }
    
    }
    
    2⃣️创建接口
     // 使用混合方式传递多个参数进行查询
     public List<Product> getByMix(@Param("param") ParamBean paramBean, @Param("page") PageParamBean pageBean);
    

    可以看出此处使用javabean+注解的方式传递参数

    3⃣️定义sql
    <!--第四种:混合方式传递 -->
        <select id="getByMix" resultType="product">
            SELECT * FROM product WHERE
            product_name LIKE concat('%',#{param.name},'%') AND CAST(product_price
            AS INT) >
            #{param.price} LIMIT #{page.limit} OFFSET #{page.start}
        </select>
    

    只要是注解方式,就不需要定义参数类型。

    4⃣️查询
    /**
         * 通过混合方式传递多个参数
         */
        public void getProductByMix() {
            System.out.println("使用混合方式传递多个参数");
            List<Product> products = new ArrayList<>();
            sqlSession = MybatisTool.getSqlSession();
            productMapper = sqlSession.getMapper(ProductMapper.class);
            ParamBean paramBean = new ParamBean("恤", 200);
            PageParamBean pageBean = new PageParamBean(0, 5);
            products = productMapper.getByMix(paramBean, pageBean);
            printResult(products);
    
    5⃣️查看结果
     使用混合方式传递多个参数
     T恤2的价格是230元
     T恤3的价格是270元
     T恤4的价格是270元
    

    相关文章

      网友评论

        本文标题:MyBatis一多参数传递方式

        本文链接:https://www.haomeiwen.com/subject/rnzcpctx.html