SpringCloud SpringBoot mybatis 分布式微服务(八)Spring Boot中使用JdbcTemplate访问数据库
2018-02-07 10:20
1101 查看
本文介绍在Spring Boot基础下配置数据源和通过JdbcTemplate编写数据访问的示例。
在我们访问数据库的时候,需要先配置一个数据源,下面分别介绍一下几种不同的数据库配置方式。
首先,为了连接数据库需要引入jdbc支持,在
嵌入式数据库通常用于开发和测试环境,不推荐用于生产环境。Spring Boot提供自动配置的嵌入式数据库有H2、HSQL、Derby,你不需要提供任何连接配置就能使用。
比如,我们可以在
以MySQL数据库为例,先引入MySQL连接的依赖包,在
在
当你将应用部署于应用服务器上的时候想让数据源由应用服务器管理,那么可以使用如下配置方式引入JNDI数据源。
Spring的JdbcTemplate是自动配置的,你可以直接使用
举例:我们在创建
定义包含有插入、删除、查询的抽象接口UserService
通过JdbcTemplate实现UserService中定义的数据访问操作
创建对UserService的单元测试用例,通过创建、删除和查询来验证数据库操作的正确性。
源码来源
上面介绍的
API
通过上面这个简单的例子,我们可以看到在Spring Boot下访问数据库的配置依然秉承了框架的初衷:简单。我们只需要在pom.xml中加入数据库依赖,再到application.properties中配置连接信息,不需要像Spring应用中创建JdbcTemplate的Bean,就可以直接在自己的对象中注入使用。
数据源配置
在我们访问数据库的时候,需要先配置一个数据源,下面分别介绍一下几种不同的数据库配置方式。首先,为了连接数据库需要引入jdbc支持,在
pom.xml中引入如下配置:
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-jdbc</artifactId> </dependency>
嵌入式数据库支持
嵌入式数据库通常用于开发和测试环境,不推荐用于生产环境。Spring Boot提供自动配置的嵌入式数据库有H2、HSQL、Derby,你不需要提供任何连接配置就能使用。比如,我们可以在
pom.xml中引入如下配置使用HSQL
<dependency> <groupId>org.hsqldb</groupId> <artifactId>hsqldb</artifactId> <scope>runtime</scope> </dependency>
连接生产数据源
以MySQL数据库为例,先引入MySQL连接的依赖包,在pom.xml中加入:
<dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>5.1.21</version> </dependency>
在
src/main/resources/application.properties中配置数据源信息
spring.datasource.url=jdbc:mysql://localhost:3306/test spring.datasource.username=dbuser spring.datasource.password=dbpass spring.datasource.driver-class-name=com.mysql.jdbc.Driver
连接JNDI数据源
当你将应用部署于应用服务器上的时候想让数据源由应用服务器管理,那么可以使用如下配置方式引入JNDI数据源。spring.datasource.jndi-name=java:jboss/datasources/customers
使用JdbcTemplate操作数据库
Spring的JdbcTemplate是自动配置的,你可以直接使用@Autowired来注入到你自己的bean中来使用。
举例:我们在创建
User表,包含属性
name、
age,下面来编写数据访问对象和单元测试用例。
定义包含有插入、删除、查询的抽象接口UserService
public interface UserService { /** * 新增一个用户 * @param name * @param age */ void create(String name, Integer age); /** * 根据name删除一个用户高 * @param name */ void deleteByName(String name); /** * 获取用户总量 */ Integer getAllUsers(); /** * 删除所有用户 */ void deleteAllUsers(); }
通过JdbcTemplate实现UserService中定义的数据访问操作
@Service public class UserServiceImpl implements UserService { @Autowired private JdbcTemplate jdbcTemplate; @Override public void create(String name, Integer age) { jdbcTemplate.update("insert into USER(NAME, AGE) values(?, ?)", name, age); } @Override public void deleteByName(String name) { jdbcTemplate.update("delete from USER where NAME = ?", name); } @Override public Integer getAllUsers() { return jdbcTemplate.queryForObject("select count(1) from USER", Integer.class); } @Override public void deleteAllUsers() { jdbcTemplate.update("delete from USER"); } }
创建对UserService的单元测试用例,通过创建、删除和查询来验证数据库操作的正确性。
@RunWith(SpringJUnit4ClassRunner.class) @SpringApplicationConfiguration(Application.class) public class ApplicationTests { @Autowired private UserService userSerivce; @Before public void setUp() { // 准备,清空user表 userSerivce.deleteAllUsers(); } @Test public void test() throws Exception { // 插入5个用户 userSerivce.create("a", 1); userSerivce.create("b", 2); userSerivce.create("c", 3); userSerivce.create("d", 4); userSerivce.create("e", 5); // 查数据库,应该有5个用户 Assert.assertEquals(5, userSerivce.getAllUsers().intValue()); // 删除两个用户 userSerivce.deleteByName("a"); userSerivce.deleteByName("e"); // 查数据库,应该有5个用户 Assert.assertEquals(3, userSerivce.getAllUsers().intValue()); } }
源码来源
上面介绍的
JdbcTemplate只是最基本的几个操作,更多其他数据访问操作的使用请参考:JdbcTemplate
API
通过上面这个简单的例子,我们可以看到在Spring Boot下访问数据库的配置依然秉承了框架的初衷:简单。我们只需要在pom.xml中加入数据库依赖,再到application.properties中配置连接信息,不需要像Spring应用中创建JdbcTemplate的Bean,就可以直接在自己的对象中注入使用。
相关文章推荐
- Spring Cloud Spring Boot mybatis分布式微服务云架构(十六)使用JdbcTemplate操作数据库
- Spring Cloud Spring Boot mybatis分布式微服务云架构(二十一)使用Redis数据库(1)
- SpringCloud SpringBoot mybatis 分布式微服务(十)Spring Boot多数据源配置与使用(JdbcTemplate支持)
- Spring Cloud Spring Boot mybatis分布式微服务云架构(二十二)使用Redis数据库(2)
- SpringCloud SpringBoot mybatis 分布式微服务(十二)Spring Boot中使用Redis数据库
- SpringCloud SpringBoot mybatis 分布式微服务(十六)Spring Boot中使用Flyway来管理数据库版本
- SpringCloud SpringBoot mybatis 分布式微服务(九)Spring Boot中使用Spring-data-jpa让数据访问更简单
- Spring Cloud Spring Boot mybatis分布式微服务云架构(二十八)使用Flyway来管理数据库版本
- 企业分布式微服务云SpringCloud SpringBoot mybatis (七)Spring Boot中使用JdbcTemplate访问数据库
- Spring Cloud Spring Boot mybatis分布式微服务云架构(二十四)使用MongoDB数据库(2)
- Spring Cloud Spring Boot mybatis分布式微服务云架构(十九)多数据源配置与使用(1)
- Spring Cloud Spring Boot mybatis分布式微服务云架构(九)使用Swagger2构建强大的RESTful API文档(1)
- Spring Cloud Spring Boot mybatis分布式微服务云架构(二十六)使用MyBatis注解配置详解(1)
- SpringCloud SpringBoot mybatis 分布式微服务(十三)Spring Boot中使用MongoDB数据库
- Spring Cloud Spring Boot mybatis分布式微服务云架构(二)使用Intellij中的Spring Initializr来快速构建Spring Boot/Cloud工程
- Spring Cloud Spring Boot mybatis分布式微服务云架构(十)使用Swagger2构建强大的RESTful API文档(2)
- Spring Cloud Spring Boot mybatis分布式微服务云架构(九)使用Swagger2构建强大的RESTful API文档(1)
- Spring Cloud Spring Boot mybatis分布式微服务云架构(二十三)使用MongoDB数据库(1)
- Spring Cloud Spring Boot mybatis分布式微服务云架构(十五)通过JdbcTemplate编写数据访问
- Spring Cloud Spring Boot mybatis分布式微服务云架构(十三)使用Spring Security安全控制