[英]Spring jdbctemplate
I am new to Spring MVC and JDBCTemplate and badly need some help on this. 我是Spring MVC和JDBCTemplate的新手,因此急需一些帮助。 I've declared the following in applicationContext.xml:
我在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>
In my DAOImpl class, I've the following code: 在我的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;
}
}
Test class: 测试类别:
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));
}
}
Then I use jdbcTemplate to carry out an insert statement using PreparedStatementCreator. 然后,我使用jdbcTemplate使用PreparedStatementCreator进行插入语句。
Now, I am trying to test (without mocking, I am hardcoding the values just to test..) whether this code works or not? 现在,我正在尝试测试(不进行模拟,而是对值进行硬编码以进行测试..)此代码是否有效? When I run this test, it gives me NPE saying jdbcTemplate is null.
当我运行此测试时,它给了我NPE称jdbcTemplate为空。 Is there something wrong with my code or is there something with the way I am testing it?
我的代码有问题吗?还是我的测试方式有问题? Any help would be greatly appreciated.
任何帮助将不胜感激。
Happy New Year :) 新年快乐 :)
PS - I have annotated the Test class with @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration(locations = {"classpath:applicationContext.xml"}) only after comment from @Pradeep. PS-我仅在@Pradeep注释后,才使用@RunWith(SpringJUnit4ClassRunner.class)@ContextConfiguration(locations = {“ classpath:applicationContext.xml”})注释测试类。 And now I get another exception: "Failed to load ApplicationContext."
现在,我得到另一个异常:“无法加载ApplicationContext。”
The most obvious answer is that your test hasn't called the setDataSource()
method or done anything else that would cause the JdbcTemplate to be created. 最明显的答案是您的测试没有调用
setDataSource()
方法或进行了其他任何会导致创建JdbcTemplate的事情。 You haven't shown enough code for someone to point out where the problem is, though. 但是,您没有显示足够的代码来指出问题所在。
Update: In your test, you say abcDAOImpl = new ABCDAOImpl();
更新:在您的测试中,您说
abcDAOImpl = new ABCDAOImpl();
, and that's all. , 就这样。 How do you expect the JdbcTemplate/DataSource to be injected?
您如何期望JdbcTemplate / DataSource被注入? It won't happen by magic.
魔术不会发生。 Either you have to finish wiring it all up manually, or else let Spring inject the DAO into your test.
您要么必须手动完成所有布线,要么让Spring将DAO注入您的测试中。 To do that, just add a field like this:
为此,只需添加如下字段:
@Autowired
private ABCDAO abcDao;
也许您在test-applicationContext.xml
文件中缺少applicationContext.xml
文件的导入
<import resource="classpath:applicationContext.xml" />
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.