我试图使用Spring Jdbctemplate从数据库中获取记录..在JdbcTemplate中我试图通过使用类BeanPropertyRowMapper来自动化数据绑定...我的数据库表列名称类型&模型对象字段名称类型相同...我实现了这个API,但我没有得到预期的结果..对于int&双字段我得0& 0.0值,即使这些是数据库中的不同值...
请帮助解决这个问题...我的代码如下,
@Override
public DiscountDetail getDiscountDetailById(int ID) throws ServiceException
{
String sql = "SELECT * FROM TBL_DISCOUNT where Id="+ID;
logger.info("sql :"+sql);
List<DiscountDetail> discounts = jdbcTemplate.query(sql, new BeanPropertyRowMapper(DiscountDetail.class));
if (discounts.size() == 1) {
logger.info("discount :"+discounts.get(0).toString());
return discounts.get(0);
}
return null;
}
DescountDetail.java
public class DiscountDetail扩展了GenericEntity {
private int discountId,higherLimit,lowerLimit;
private double discountPercentage;
public int getDiscountId() {
return discountId;
}
public void setDiscountId(int discountId) {
this.discountId = discountId;
}
public int getHigherLimit() {
return higherLimit;
}
public void setHigherLimit(int higherLimit) {
this.higherLimit = higherLimit;
}
public int getLowerLimit() {
return lowerLimit;
}
public void setLowerLimit(int lowerLimit) {
this.lowerLimit = lowerLimit;
}
public double getDiscountPercentage() {
return discountPercentage;
}
public void setDiscountPercentage(double discountPercentage) {
this.discountPercentage = discountPercentage;
}
public String toString()
{
StringBuffer sb = new StringBuffer("");
sb.append("discountId: " + discountId);
sb.append(", higherLimit:" + higherLimit);
sb.append(", lowerLimit:" + lowerLimit);
sb.append(", discountPercentage:" + discountPercentage);
sb.append(super.toString());
return sb.toString();
}
}
--
-- Table structure for table `TBL_DISCOUNT_DETAIL`
--
CREATE TABLE `TBL_DISCOUNT_DETAIL` (
`Id` int(11) NOT NULL,
`DiscountId` int(11) NOT NULL,
`HigherLimit` int(11) NOT NULL,
`LowerLimit` int(11) NOT NULL,
`DiscountPercentage` double NOT NULL,
`CreatedOn` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
`CreatedBy` varchar(100) NOT NULL,
`UpdatedOn` timestamp NULL DEFAULT NULL,
`UpdatedBy` varchar(100) DEFAULT NULL,
`VersionId` int(11) NOT NULL DEFAULT '1'
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
实际输出:
2018-05-08 01:28:15 INFO InventoryDAO:1052 - sql:SELECT * FROM TBL_DISCOUNT其中Id = 30 2018-05-08 01:28:15 INFO InventoryDAO:1056 - 折扣: discountId:0 , higherLimit:0 , lowerLimit:0 , discountPercentage:0.0 :ID [30]:CreatedBy SYSTEM on 2018-05-07 19:06:42.0,UpdatedBy Rahul on 2018-05-07 19:06:42.0:V [2]
预期产出:
2018-05-08 01:28:15 INFO InventoryDAO:1052 - sql:SELECT * FROM TBL_DISCOUNT其中Id = 30 2018-05-08 01:28:15 INFO InventoryDAO:1056 - 折扣: discountId:1 , higherLimit:5 , lowerLimit:20 , discountPercentage:5.0 :ID [30]:CreatedBy SYSTEM on 2018-05-07 19:06:42.0,UpdatedBy Rahul on 2018-05-07 19:06:42.0:V [2]
答案 0 :(得分:0)
请记住,如果您使用的是BeanPropertyRowMapper,那么您也同意允许它根据内部逻辑将数据库字段名称实际映射到您的对象。
特别重要的是this bit from the JavaDoc:
根据从结果集元数据获取的列名与相应属性的公共setter匹配来映射列值。这些名称可以直接匹配,也可以使用“camel”case将名称与下划线分隔成相同名称。
所以,在这种情况下,你正在使用
new BeanPropertyRowMapper(DiscountDetail.class)
并且基于DiscountDetail类,您还应该将目标数据库表中的列名定义为:
discountId
higherLimit
lowerLimit
discountPercentage
或者作为:
discount_id
higher_limit
lower_limit
discount_percentage
最有可能的是,DiscountDetail中的公共setter方法的名称与基础表的字段名称不匹配。