参数未在MyBatis查询中替换

时间:2012-02-22 09:10:04

标签: java sql spring jdbc mybatis

以下是我在BrandMapper.xml中的选择查询。

<select id="getBrand" parameterType="String" resultMap="brandResult">
    SELECT 
        B.bid as bid, 
        B.bname as bname, 
        B.avg_price as avg_price, 
        B.total_number as total_number, 
        P.pid as pid, 
        P.pname as pname, 
        P.bid as pbid, 
        P.bname as pbname, 
        P.specs as pspecs, 
        P.price as price 
    from Brands B left outer join Products P on P.bid = B.bid 
    where B.bname = #{bname, jdbcType=VARCHAR}
</select>

这是界面

public interface BrandMapper {
    BrandDAO getBrand(String bname);
}

这是服务类

@Service
public class BrandService {
@Autowired
private BrandMapper brandMapper;
public BrandDAO getBrand(String bname) {
    System.out.println("Inside DBService getBrand");
    return brandMapper.getBrand(bname);
}
}

我的问题是getBrand中的BrandService函数返回null值。如果我将#{bname}中的参数BrandMapper.xml替换为硬编码字符串,则它会起作用并返回正确的类。我在这做错了什么?是否有任何日志或任何可用的地方,我可以看到正在构建的实际查询?任何帮助表示赞赏。

我设法使用log4j启用loggin,这是正在执行的查询

SELECT B.bid as bid, B.bname as bname, B.avg_price as avg_price, B.total_number as total_number, P.pid as pid, P.pname as pname, P.bid as pbid, P.bname as pbname, P.specs as pspecs, P.price as price from Brands B left outer join Products P on P.bid = B.bid where B.bname = ?

参数未被替换。我无法弄清楚我在这里做错了什么。

1 个答案:

答案 0 :(得分:3)

问号是预备语句中的占位符,记录的查询完全正常并且看起来像预期的那样。应将实际值作为单独的参数传递给数据库。