Spring3
以后开始支持嵌入式数据库,嵌入式数据库目前在市面上有好多种,HSQL
,DERBY
,H2
…
今天就主要讲一下h2
的使用 对于一个数据库产品来说,主要就是如何存储数据和读取数据了。
所谓嵌入式就是直接运行在项目中,不需要安装额外的产品。说白了就是一个jar包,可以随项目启动和结束而结束, 比如使用IDEA直接连接:
它主要有以下特点:
优点:
缺点:
MySql
很相似的服务器模式,运行起来后,可以连接多个实例,下载地址http://www.h2database.com/html/main.html接下来我们就使用SpringJdbc
连接数据库进行操作,当然其他orm
框架也可以,使用SpringJdbc
是为了简化代码
<dependency>
<groupId>com.h2databasegroupId>
<artifactId>h2artifactId>
<version>1.3.172version>
dependency>
<dependency>
<groupId>org.springframeworkgroupId>
<artifactId>spring-coreartifactId>
<version>5.1.10.RELEASEversion>
dependency>
<dependency>
<groupId>org.springframeworkgroupId>
<artifactId>spring-beansartifactId>
<version>5.1.10.RELEASEversion>
dependency>
<dependency>
<groupId>org.springframeworkgroupId>
<artifactId>spring-contextartifactId>
<version>5.1.10.RELEASEversion>
dependency>
<dependency>
<groupId>org.springframeworkgroupId>
<artifactId>spring-jdbcartifactId>
<version>5.1.10.RELEASEversion>
dependency>
<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.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:sql/h2-schema.sql"/>
<jdbc:script location="classpath:sql/h2-data.sql" encoding="UTF-8"/>
jdbc:embedded-database>
<bean class="org.springframework.jdbc.core.JdbcTemplate" id="jdbcTemplate">
<property name="dataSource" ref="dataSource"/>
bean>
beans>
-- h2-schame.sql
drop table if exists teacher ;
-- 创建表
create table teacher(
id int primary key auto_increment,
name varchar(20),
age int
);
-- 插入表数据 h2-data.sql
insert into teacher(name,age) values('张老师',23);
insert into teacher(name,age) values('李老师',24);
public class Teacher {
private int id;
private String name;
private int age;
//省略set和get
}
public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext("classpath:application.xml");
JdbcTemplate jdbcTemplate = context.getBean("jdbcTemplate", JdbcTemplate.class);
String selectSql = "select * from teacher";
List query = jdbcTemplate.query(selectSql, new RowMapper() {
@Nullable
@Override
public Object mapRow(ResultSet resultSet, int i) throws SQLException {
Teacher teacher = new Teacher();
teacher.setId(resultSet.getInt(1));
teacher.setName(resultSet.getString(2));
teacher.setAge(resultSet.getInt(3));
return teacher;
}
});
query.forEach(o -> System.out.println(o));
}
这里的datasource是通过jdbc命名空间定义的,因为我们选择模式是内嵌式运行。一个最简单的事情要明白,只有在这个应用运行中,才会访问到数据库,其他时间是不能使用外部工具连接的,比如idea的datasource工具, 即使从日志中找到连接地址也无法成功访问表
此时teacher表是无法访问和操作的(即使代码断点处表是存在的)
JavaConfig
方式的话:package cn.lyn4ever.bean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseBuilder;
import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseType;
import javax.sql.DataSource;
@Configuration
public class BeanConfig {
@Bean
public DataSource dataSource() {
try {
EmbeddedDatabaseBuilder dbBuilder = new EmbeddedDatabaseBuilder();
return dbBuilder.setType(EmbeddedDatabaseType.H2)
.addScripts("classpath:sql/h2-schema.sql", "classpath:sql/h2-data.sql")
.build();
} catch (Exception e) {
System.out.println("创建数据库连接失败");
return null;
}
}
@Bean
public JdbcTemplate jdbcTemplate(){
JdbcTemplate jdbcTemplate = new JdbcTemplate();
jdbcTemplate.setDataSource(dataSource());
return jdbcTemplate;
}
}
public static void main(String[] args) {
ApplicationContext context = new AnnotationConfigApplicationContext(BeanConfig.class);
JdbcTemplate jdbcTemplate = context.getBean("jdbcTemplate", JdbcTemplate.class);
String selectSql = "select * from teacher";
List query = jdbcTemplate.query(selectSql, new RowMapper() {
@Nullable
@Override
public Object mapRow(ResultSet resultSet, int i) throws SQLException {
Teacher teacher = new Teacher();
teacher.setId(resultSet.getInt(1));
teacher.setName(resultSet.getString(2));
teacher.setAge(resultSet.getInt(3));
return teacher;
}
});
query.forEach(o -> System.out.println(o));
}
您可以在Gitee上找到本文的源代码。