我们一直使用ssm框架都是编写访问数据库类型的网站,app,web网站都是需要有数据库来存储数据.
下面我们来看一下springboot使用JdbcTemplate操作数据库;
配置文件中的数据源配置
在我们访问数据库的时候,需要先配置一个数据源,下面分别介绍一下几种不同的数据库配置方式。
嵌入式数据库支持
嵌入式数据库通常用于开发和测试环境,不推荐用于生产环境。Spring Boot提供自动配置的嵌入式数据库有H2、HSQL、Derby,你不需要提供任何连接配置就能使用。
比如,我们可以在pom.xml中引入如下配置使用HSQL;
连接生产数据源
以MySQL数据库为例,先引入MySQL连接的依赖包,在pom.xml中加入:
首先,为了连接数据库需要引入jdbc支持,在pom.xml中引入如下配置:
这里我们使用的是 1.3.2,他跟1.5.8的tests注解区别前面博客有过说明这里就不多说了;来看pom文件
org.springframework.boot spring-boot-starter-parent 1.3.2.RELEASE UTF-8 1.8 org.springframework.boot spring-boot-starter org.springframework.boot spring-boot-starter-test test mysql mysql-connector-java 5.1.21 org.springframework.boot spring-boot-starter-jdbc org.springframework.boot spring-boot-maven-plugin
在src/main/resources/application.properties中配置数据源信息
spring.datasource.url=jdbc:mysql://localhost:3306/test spring.datasource.username=root spring.datasource.password=root 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,下面来编写数据访问对象和单元测试用例。
/**
* 筱进GG
*/
public interface UserService {
/**
* 新增一个用户
* @param name
* @param age
*/
void create(String name, Integer age);
/**
* 根据name删除一个用户高
* @param name
*/
void deleteByName(String name);
/**
* 获取用户总量
*/
Integer getAllUsers();
/**
* 删除所有用户
*/
void deleteAllUsers();
}
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;
/**
* 筱进GG
*/
@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");
}
}
import com.didispace.service.UserService;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.SpringApplicationConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
/**
* 筱进GG
*/
@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
这里只是介绍的简单的例子;使用的时候我们只需要在pom.xml中加入数据库依赖,再到application.properties中配置连接信息,不需要像Spring应用中创建JdbcTemplate的Bean,需要在自己的对象中注入使用。
欢迎大家一起交流学习SpringBoot,java等领域的技术,交流群 : 587674051 博客的源码也在里面