在我的application.properties中,设置了:
datasource.test.driverClass=org.postgresql.Driver
datasource.test.url=jdbc:postgresql://localhost:5433/test
datasource.test.username=admin
datasource.test.password=admin
logging.level.com.eternity = DEBUG
在我的控制器中,我试图执行一些SQL查询,形式像这样的字符串:
String selectQueryPartOne = "SELECT name, ("+ StringUtils.join(sumString, " + ")+") AS 'Price' FROM house WHERE NOT (" +StringUtils.join(sumString, " IS NULL OR ")+" IS NULL);";
JdbcTemplate statement = new JdbcTemplate();
statement.queryForList(selectQueryPartOne);
它将正常工作,但是,我收到以下错误:
java.lang.IllegalArgumentException: No DataSource specified
我发现,在我的statement
对象中,我需要首先设置setDataSource。但是,我不知道在哪里可以获取此dataSource对象。你能帮忙吗?
自己创建JdbcTemplate
实例时,您正在Spring依赖项注入之外工作,因此将不会DataSource
注入。您需要通过自动装配来使用Spring提供的实例,例如:
@Controller
public class MyController {
@Autowired private JdbcTemplate jdbcTemplate;
@RequestMapping("/")
public String myAction(){
// do stuff with the jdbc template
}
}
另外,Spring和Spring-boot文档是进一步研究Spring的重要资源。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句