優勢:html
缺點:java
使用內嵌入到應用程序中,由於它是一個jar包,因此放應用程序中就能夠了,可是它只能鏈接一個實例,也就是隻能在當前應用程序中鏈接,不能在其餘應用中操做(主要講解這種模式)mysql
接下來咱們就使用SpringJdbc鏈接數據庫進行操做,固然其餘orm框架也能夠,使用SpringJdbc是爲了簡化代碼git
JDK1.8+Spring4以上
<dependency> <groupId>com.h2database</groupId> <artifactId>h2</artifactId> <version>1.3.172</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-core</artifactId> <version>5.1.10.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-beans</artifactId> <version>5.1.10.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>5.1.10.RELEASE</version> </dependency> <!-- 2.Spring dao依賴 --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-jdbc</artifactId> <version>5.1.10.RELEASE</version> </dependency>
<?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.xsd http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc.xsd"> <!--固然是配置datasource了--> <jdbc:embedded-database id="dataSource" type="H2"> <!--必定要是先DDL,即數據庫定義語言--> <jdbc:script location="classpath:sql/h2-schema.sql"/> <!--而後纔是DML,數據庫操做語言--> <jdbc:script location="classpath:sql/h2-data.sql" encoding="UTF-8"/> </jdbc:embedded-database> <!--定義springjdbctemplate--> <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);
這裏的datasource是經過jdbc命名空間定義的,由於咱們選擇模式是內嵌式運行。一個最簡單的事情要明白,只有在這個應用運行中,纔會訪問到數據庫,其餘時間是不能使用外部工具鏈接的,好比idea的datasource工具github
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的信息
咱們給程序一個不退出的代碼,讓它一直運行(若是是一個web應用,只要啓動,就會一直運行),使用idea鏈接一下這個數據庫web
可是你經過這個工具是不能看見teahcer表的,一樣,你經過這個工具添加的表和數據也不會使用程序取到的,由於它自己就是鏈接實例之間是分開的,這樣作是很安全的。spring
運行環境:SpirngBoot+SpringJdbcsql
這裏並不建立一個新的SpringBoot項目,而是使用Java註解的方式來註冊bean(在SpirngBoot的環境就能夠這樣用)數據庫
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)); }