本篇博客主要内容是:(1)SQL语句需要传参的解决办法;(2)只传一个参数时候parameterType设置成对应的包装类型;(3)传多个参数时候parameterType设置Map接口类型;

目录

1.只传一个参数:以根据id查商品信息为例;

(1)在goods.xml中新增一个select标签:

(2)外侧实际调用,演示:

2.传递多个参数:以查询某个价格范围内的商品信息为例

(1)在goods.xml中新增一个select标签:

(2)外侧实际调用,演示:


SQL查询有时是需要外部传入参数的。


1.只传一个参数:以根据id查商品信息为例;

(1)在goods.xml中新增一个select标签:

goods.xml:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper
        PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="goods">
    <select id="selectAll" resultType="com.imooc.mybatis.entity.Goods">
        select * from t_goods order by goods_id desc limit 10;
    </select>

    <select id="selectById" parameterType="Integer" resultType="com.imooc.mybatis.entity.Goods">
        select * from t_goods where goods_id = #{value}
    </select>
</mapper>

说明:

(1) 只传一个参数的时候,parameterType设置成基础的包装类类型就可以了。

(2)外侧实际调用,演示:

package com.imooc.mybatis;

import com.imooc.mybatis.entity.Goods;
import com.imooc.mybatis.utils.MyBatisUtils;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.junit.Test;

import java.io.IOException;
import java.io.Reader;
import java.sql.Connection;
import java.util.List;

/**
 * JUnit单元测试类
 */
public class MyBatisTestor {

    @Test
    public void testSelectById() throws Exception {
        SqlSession session = null;
        try {
            session = MyBatisUtils.openSession();
            Goods goods = session.selectOne("goods.selectById", 937);
            System.out.println(goods.getTitle());
        } catch (Exception e) {
            throw e;
        }finally {
            MyBatisUtils.closeSession(session);
        }
    }
}

说明:

(1)

(2)

运行结果:

但是,单参数有局限性,无法解决复杂的查询。


2.传递多个参数:以查询某个价格范围内的商品信息为例

mybatis的只支持设置一个paremeterType;;;如果要传递多个数值,单靠一个基础的数据类型是无法承载的。遇到需要传递多个参数的时候,可以使用mybatis一个变通的办法:parameterType设置成Map接口类型

(1)在goods.xml中新增一个select标签:

goods.xml:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper
        PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="goods">
    <select id="selectAll" resultType="com.imooc.mybatis.entity.Goods">
        select * from t_goods order by goods_id desc limit 10;
    </select>

    <select id="selectById" parameterType="Integer" resultType="com.imooc.mybatis.entity.Goods">
        select * from t_goods where goods_id = #{value}
    </select>

    <select id="selectByPriceRange" parameterType="java.util.Map" resultType="com.imooc.mybatis.entity.Goods">
        select * from t_goods
        where
            current_price between #{min} and #{max}
            order by current_price
            limit 0,#{limt};
    </select>
</mapper>

说明:

(1)

(2)外侧实际调用,演示:

外侧调用,传参:在测试类中编写测试代码

package com.imooc.mybatis;

import com.imooc.mybatis.entity.Goods;
import com.imooc.mybatis.utils.MyBatisUtils;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.junit.Test;

import java.io.IOException;
import java.io.Reader;
import java.sql.Connection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * JUnit单元测试类
 */
public class MyBatisTestor {

    @Test
    public void testSelectByPriceRange() throws Exception {
        SqlSession session = null;
        try {
            session = MyBatisUtils.openSession();
            Map parm = new HashMap();
            parm.put("min",100); parm.put("max", 500); parm.put("limt", 10);
            List<Goods> list = session.selectList("selectByPriceRange", parm);
            for (Goods g : list) {
                System.out.println(g.getTitle());
            }
        } catch (Exception e) {
            throw e;
        }finally {
            MyBatisUtils.closeSession(session);
        }
    }
}

说明:

(1)

运行结果: