以下是我在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 = ?
参数未被替换。我无法弄清楚我在这里做错了什么。
答案 0 :(得分:3)
问号是预备语句中的占位符,记录的查询完全正常并且看起来像预期的那样。应将实际值作为单独的参数传递给数据库。