gpt4 book ai didi

java - JDBI 的@BindBean 在 INSERT 期间没有在 bean 类中找到命名参数

转载 作者:行者123 更新时间:2023-11-29 01:09:48 25 4
gpt4 key购买 nike

在 Dropwizard 中使用 JDBI 的 @BindBean 将值插入我的 Mysql 数据库时,我一直在下面遇到以下异常。问题似乎是 JDBI 无法在 bean 中找到属性。我已经将问题隔离到一个单独的项目中,但无法弄清楚哪里出了问题。如果能提供一些建议,我将不胜感激。

org.skife.jdbi.v2.exceptions.UnableToExecuteStatementException: Unable to execute, no named parameter matches "title" and no positional param for place 0 (which is 1 in the JDBC 'start at 1' scheme) has been set. [statement:"INSERT INTO `car_tbl`(`title`, `teaser`, `ext_description`, `create_date`, `teaser_image_url`, `teaser_image_color`) VALUES ( :title, :teaser, :extDescription, unix_timestamp(), :teaserImageUrl, :teaserImageColor)", located:"INSERT INTO `car_tbl`(`title`, `teaser`, `ext_description`, `create_date`, `teaser_image_url`, `teaser_image_color`) VALUES ( :title, :teaser, :extDescription, unix_timestamp(), :teaserImageUrl, :teaserImageColor)", rewritten:"/* CarDAO.createCar2 */ INSERT INTO `car_tbl`(`title`, `teaser`, `ext_description`, `create_date`, `teaser_image_url`, `teaser_image_color`) VALUES ( ?, ?, ?, unix_timestamp(), ?, ?)", arguments:{ positional:{}, named:{class:class com.javaeeeee.dwstart.db.Car}, finder:[]}]

at org.skife.jdbi.v2.ColonPrefixNamedParamStatementRewriter$MyRewrittenStatement.bind(ColonPrefixNamedParamStatementRewriter.java:158)
at org.skife.jdbi.v2.SQLStatement.internalExecute(SQLStatement.java:1318)
at org.skife.jdbi.v2.Update.executeAndReturnGeneratedKeys(Update.java:78)
at org.skife.jdbi.v2.sqlobject.UpdateHandler$1.value(UpdateHandler.java:51)
at org.skife.jdbi.v2.sqlobject.UpdateHandler.invoke(UpdateHandler.java:75)
at org.skife.jdbi.v2.sqlobject.SqlObject.invoke(SqlObject.java:175)
at org.skife.jdbi.v2.sqlobject.SqlObject$1.intercept(SqlObject.java:75)
at org.skife.jdbi.v2.sqlobject.CloseInternalDoNotUseThisClass$$EnhancerByCGLIB$$8f365cd7.createCar2(<generated>)
at com.javaeeeee.dwstart.db.TestDatabase.testCreatecar2(TestDatabase.java:60)

这是我的值对象

public class Car {
public Long carId;
public String title;
public String teaser;
public String teaserImageUrl;
public String teaserImageColor;
public String extDescription;
public Long createDate;
}

并且我创建了以下 DAO 来使用 http://jdbi.org/sql_object_api_argument_binding/ 中描述的 @BindBean 功能- 两种插入方法均失败。

package com.javaeeeee.dwstart.db.dao;

import com.javaeeeee.dwstart.db.Car;
import org.skife.jdbi.v2.sqlobject.BindBean;
import org.skife.jdbi.v2.sqlobject.GetGeneratedKeys;
import org.skife.jdbi.v2.sqlobject.SqlUpdate;

public interface CarDAO {
@GetGeneratedKeys
@SqlUpdate("INSERT INTO `car_tbl`(`title`, `teaser`, `ext_description`, " +
"`create_date`, `teaser_image_url`, `teaser_image_color`) " +
"VALUES ( :c.title, :c.teaser, :c.extDescription, unix_timestamp(), " +
":c.teaserImageUrl, :c.teaserImageColor)")
Long createCar(@BindBean("c") Car car);

@GetGeneratedKeys
@SqlUpdate("INSERT INTO `car_tbl`(`title`, `teaser`, `ext_description`, " +
"`create_date`, `teaser_image_url`, `teaser_image_color`) " +
"VALUES ( :title, :teaser, :extDescription, unix_timestamp(), " +
":teaserImageUrl, :teaserImageColor)")
Long createCar2(@BindBean Car car);
}

这是我的表定义。

CREATE TABLE IF NOT EXISTS `cardb`.`car_tbl` (
`car_id` INT NOT NULL AUTO_INCREMENT COMMENT '',
`title` VARCHAR(255) NOT NULL COMMENT '',
`teaser` VARCHAR(2048) NULL COMMENT '',
`ext_description` VARCHAR(4096) NULL COMMENT '',
`create_date` BIGINT(20) NOT NULL DEFAULT 0 COMMENT '',
`teaser_image_url` VARCHAR(2048) NULL COMMENT '',
`teaser_image_color` VARCHAR(8) NULL COMMENT '',
PRIMARY KEY (`car_id`) COMMENT '',
INDEX `idx_create_date` USING BTREE (`create_date` ASC) COMMENT '')
ENGINE = InnoDB;

我正在使用以下 JUnit 集成测试来重现错误:

public class TestDatabase {
private DBI dbi;
private Handle handle;
private CarDAO carDAO;

@Before
public void setUpDatabase() throws Exception {
Environment environment = new Environment( "test-env", Jackson.newObjectMapper(), null, new MetricRegistry(), null );
dbi = new DBIFactory().build( environment, getDataSourceFactory(), "cardb" );
carDAO = dbi.onDemand(CarDAO.class);
handle = dbi.open();
}

protected DataSourceFactory getDataSourceFactory()
{
DataSourceFactory dataSourceFactory = new DataSourceFactory();
dataSourceFactory.setDriverClass( "com.mysql.jdbc.Driver" );
dataSourceFactory.setUrl( "jdbc:mysql://127.0.0.1:3306/cardb" );
dataSourceFactory.setUser( "root" );
dataSourceFactory.setPassword( "root" );
return dataSourceFactory;
}

@Test
public void testCreatecar(){
Car car = new Car("Test car","Test car teaser","http://localhost:8080/","#ff0000","car test description");
Long carId = carDAO.createCar(car);
assertNotNull(carId);
}

@Test
public void testCreatecar2(){
Car car = new Car("Test car","Test car teaser","http://localhost:8080/","#ff0000","car test description");
Long carId = carDAO.createCar2(car);
assertNotNull(carId);
}
}

我正在使用 Java 1.8.0_05、Dropwizard 0.9.2 和 MySQL 5.7.9。

最佳答案

BindBean 声称可以与 JavaBeans 一起工作。 JavaBeans 需要 getter 和 setter。

请为您的 Car 类添加 getter 和 setter。

关于java - JDBI 的@BindBean 在 INSERT 期间没有在 bean 类中找到命名参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35823812/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com