如何检查是否创建了 h2 嵌入式数据库?
How to check that h2 embedded database is created?
我有 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 class看起来像:
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 嵌入式数据库?
您在 Spring 中配置您的数据库,然后使用构造函数创建 object。您不创建 Spring 上下文,这就是不创建数据库的原因。也因为你使用构造函数 Spring 无法将数据源注入你新创建的 object 因为 Spring IoC 容器不知道它。
因此您需要对代码和配置进行一些更改 XML。
首先,要使用 p:dataSource-ref
,您需要将此行添加到 XML 文件的标题中:xmlns:p="http://www.springframework.org/schema/p"
紧跟在 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());
}
}
}
我有 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 class看起来像:
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 嵌入式数据库?
您在 Spring 中配置您的数据库,然后使用构造函数创建 object。您不创建 Spring 上下文,这就是不创建数据库的原因。也因为你使用构造函数 Spring 无法将数据源注入你新创建的 object 因为 Spring IoC 容器不知道它。
因此您需要对代码和配置进行一些更改 XML。
首先,要使用 p:dataSource-ref
,您需要将此行添加到 XML 文件的标题中:xmlns:p="http://www.springframework.org/schema/p"
紧跟在 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());
}
}
}