优势: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)); }