我的 app-context 文件包含嵌入式数据库:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:jdbc="http://www.springframework.org/schema/jdbc"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/jdbc
http://www.springframework.org/schema/jdbc/spring-jdbc.xsd">
<jdbc:embedded-database id="dataSource" type="H2">
<jdbc:script location="classpath:META-INF/schema.sql" />
<jdbc:script location="classpath:META-INF/data.sql" />
</jdbc:embedded-database>
<bean id="reportDao"
class="jdbc.dao.ImplReportDao"
p:dataSource-ref="dataSource" />
</beans>
我有以下 src / META-INF / schema.sql 文件:
USE `reports`;
/*Table structure for table `reports` */
DROP TABLE IF EXISTS `reports`;
CREATE TABLE `reports` (
`id` int(9) unsigned zerofill NOT NULL,
`parent_id` int(9) unsigned zerofill NOT NULL DEFAULT '0000',
`name` varchar(99) NOT NULL,
`is_category` tinyint(1) NOT NULL DEFAULT '1',
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=cp1251;
和 src / META-INF / data.sql 文件:
insert into `reports`.`reports`(`id`,`parent_id`,`name`,`is_category`)
values
(0000,0000,'Reports',1),
(0001,0000,'Card emission',1),
(0002,0000,'Technical reports',1);
ImplReportDao
看起来像这样:
public class ImplReportDao implements ReportDao {
private DataSource dataSource;
public void setDataSource(DataSource dataSource) {
this.dataSource = dataSource;
}
@Override
public List<Report> findAll() {
System.out.println("find all");
List<Report> result = new ArrayList<Report>();
Connection conn = null;
try {
if (dataSource != null) {
System.out.println("dataSource != null");
} else {
System.out.println("dataSource == null");
}
conn = dataSource.getConnection();
PreparedStatement statement = conn.prepareStatement("select * from reports");
ResultSet resultSet = statement.executeQuery();
while (resultSet.next()) {
Report report = new Report();
report.setId(resultSet.getInt("id"));
report.setParentId(resultSet.getInt("parent_id"));
report.setName(resultSet.getString("name"));
result.add(report);
}
return result;
} catch (Exception ex) {
return null;
}
}
}
Main 类看起来像:
public class Main {
private static ReportDao reportDao = new ImplReportDao();
public static void main(String[] args) {
List<Report> reports = reportDao.findAll();
for (Report report : reports) {
System.out.println("report: " + report.getName());
}
}
}
但是在发布后我发现错误NullPointerException
,因为ImplReportDao.findAll()
dataSource
中的错误为空。如何检查是否创建了h2嵌入式数据库?
答案 0 :(得分:1)
您在Spring中配置数据库,然后使用构造函数创建对象。您不创建Spring上下文,这就是未创建数据库的原因。另外因为你使用构造函数Spring不能将数据源注入到你新创建的对象中,因为Spring IoC容器并不知道它。
因此,您需要对代码进行一些更改并配置XML。
首先,要使用p:dataSource-ref
,您需要在xmlns:p="http://www.springframework.org/schema/p"
之后立即将此行添加到XML文件的标题中:xmlns:jdbc="http://www.springframework.org/schema/jdbc"
。
其次,您需要像这样实例化Spring上下文
public class Main {
public static void main(String[] args) {
ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext("app-context.xml"); //assuming app-context.xml is in classpath of project
ReportDao reportDao = ctx.getBean(ReportDao.class); //Ask Spring to gice you DAO object with injected dataSource
List<Report> reports = reportDao.findAll();
for (Report report : reports) {
System.out.println("report: " + report.getName());
}
}
}