【问题标题】:Spring jdbctemplateSpring jdbc模板
【发布时间】:2012-12-15 23:17:28
【问题描述】:

我是 Spring MVC 和 JDBCTemplate 的新手,非常需要一些帮助。我在 applicationContext.xml 中声明了以下内容:

<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
    <property name="driverClassName" value="com.mysql.jdbc.Driver" />
    <property name="url" value="jdbc:mysql://" />
    <property name="username" value="user" />
    <property name="password" value="pwd" />
</bean>

<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
    <property name="dataSource" ref="dataSource"/>
</bean>

在我的 DAOImpl 类中,我有以下代码:

@Repository
public class ABCDAOImpl implements ABCDAO
{

    private String INSERT_SQL = null;
    private JdbcTemplate jdbcTemplate;

    @Autowired
    public void setDataSource(DataSource dataSource)
    {
        this.jdbcTemplate = new JdbcTemplate( dataSource );
    }

    @Override
    public boolean insertDataInDataBase(final Object obj)
    {
        boolean insertSuccessful = false;

        INSERT_SQL = "INSERT INTO XXX " +
                "(AA, BB, CC, DD, EE, " +
                "FF, GG, HH, II) VALUES (?,?,?,?,?,?,?,?,?)";

    KeyHolder keyHolder = new GeneratedKeyHolder();
    jdbcTemplate.update(new PreparedStatementCreator() 
            {public PreparedStatement createPreparedStatement(Connection connection) 
                           throws SQLException
                {
                  PreparedStatement ps = null;
                  ps = connection.prepareStatement(INSERT_SQL);
                  ps.setString(1, xx);
                  ps.setString(2, xx);
                  ps.setString(3, xx);
                      ps.setString(4, xx);
                  ps.setString(5, xx);
                  ps.setString(6, xx;
                  ps.setString(7, xx);
                  ps.setString(8, xx);
                  ps.setString(9, xx);
                  return ps;
                  }}, keyHolder);

        return insertSuccessful;    
    }
}

测试类:

  import static org.junit.Assert.assertTrue;
    import org.junit.Before;
    import org.junit.Test;
    import org.junit.runner.RunWith;
    import org.springframework.test.context.ContextConfiguration;
    import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

    @RunWith(SpringJUnit4ClassRunner.class)
    @ContextConfiguration(locations = {"classpath:applicationContext.xml"})
    public class ABCDAOImplTest
    {
    private Object object = new Object();
    private ABCDAOImpl abcDAOImpl;

    @Before
    public void setup()
    {
        object.setAllVaribles();
        abcDAOImpl = new ABCDAOImpl();
    }

    @Test
    public void testRepositoryInsert()
    {
        System.out.println(abcDAOImpl.insertDataInDataBase(object));
        assertTrue(abcDAOImpl.insertDataInDataBase(object));
    }

}

然后我使用 jdbcTemplate 通过 PreparedStatementCreator 执行插入语句。
现在,我正在尝试测试(没有模拟,我硬编码值只是为了测试..)这段代码是否有效?当我运行这个测试时,它给了我 NPE 说 jdbcTemplate 为空。我的代码有问题还是我测试它的方式有问题?任何帮助将不胜感激。

新年快乐:)

PS - 我已经用 @RunWith(SpringJUnit4ClassRunner.class) 注释了 Test 类 @ContextConfiguration(locations = {"classpath:applicationContext.xml"}) 仅在来自@Pradeep 的评论之后。现在我得到另一个异常:“无法加载 ApplicationContext。”

【问题讨论】:

  • 您的DAOImpl 课程是否包含在您的component-scan 中?
  • 是的。
  • 1.尝试 2. 尝试基于实例变量 jdbcTemplate 注入,而不是在 setter 上
  • 在 DAOImpl.java 上使用 @Repository
  • @swamy 我已经做到了。

标签: java spring-mvc jdbctemplate


【解决方案1】:

最明显的答案是您的测试没有调用 setDataSource() 方法或执行任何其他会导致创建 JdbcTemplate 的操作。不过,您还没有显示足够的代码让别人指出问题所在。

更新:在你的测试中,你说abcDAOImpl = new ABCDAOImpl();,仅此而已。您希望如何注入 JdbcTemplate/DataSource?它不会通过魔法发生。要么你必须手动完成所有的连接,要么让 Spring 将 DAO 注入到你的测试中。为此,只需添加如下字段:

@Autowired
private ABCDAO abcDao;

【讨论】:

  • 不幸的是,您的代码很难阅读。使用 IDE 对其进行正确格式化,然后将其粘贴到您的问题中,全选,然后单击编辑器中的“代码”按钮使其有用。
  • 听起来你最初的问题是你甚至没有为你的测试启动 Spring,但你期望它会为你自动装配东西。现在您正在尝试启动它,但您的配置存在其他问题。
  • 是的,你是对的。我曾尝试使用“代码”按钮。请让我知道我是否可以做其他事情。
  • 是的,这样好多了。更新了我的答案。
  • 感谢 Ryan 和其他所有人的帮助。
【解决方案2】:

也许您在test-applicationContext.xml 文件中缺少applicationContext.xml 文件的导入

<import resource="classpath:applicationContext.xml" />

【讨论】:

  • 感谢您的回复,在您的帮助下我进步了。我还没有创建单独的 test-applicationContext.xml,只是用 applicationContext 注释了我的测试类(我已将整个代码放在帖子中)。我用@RunWith(SpringJunit4Runner.class) 和@ContextConfiguration(locations = {"classpath:applicationContext.xml"}) 注释了我的类。现在,当我运行 JUnit 测试时,它显示“加载应用程序上下文失败”。
  • 测试类找不到 applicationContext 文件。
猜你喜欢
  • 1970-01-01
  • 2015-04-13
  • 2012-04-23
  • 2019-01-25
  • 2017-08-08
  • 1970-01-01
  • 1970-01-01
  • 2018-08-10
  • 1970-01-01
相关资源
最近更新 更多