Spring Boot 之JDBCTemplate
描述
Spring对数据库的操作在jdbc上面做了深层次的封装,使用spring的注入功能,可以把DataSource注册到JdbcTemplate之中。 JdbcTemplate 是在JDBC API基础上提供了更抽象的封装,并提供了基于方法注解的事务管理能力。 通过使用SpringBoot自动配置功能并代替我们自动配置beans. 在maven中,我们需要增加spring-boot-starter-jdbc模块
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
通过这个模块为我们做了以下几件事
1. tomcat-jdbc-{version}.jar为我们自动配置DataSource.
2. 如果你没有定义任何DataSource,SpringBoot将会自动配置一个内存的数据库资源设置
3. 如果没有设置任一个beans,SpringBoot会自动注册它
4. 初始化数据库
连接Datasource,关键几个参数
1. 数据库驱动:spring.datasource.driverClassName=com.mysql.jdbc.Driver
2. 连接数据库的URL:spring.datasource.url=jdbc:mysql://office2.cherrypicks.com:18266/hsbc_payme_uat?
characterEncoding=UTF8
3. 连接数据库userName:spring.datasource.username=root
4. 连接数据库Password:spring.datasource.pwd=577679F7D1DC6731FD4A38398FBF8F52
连接Datasource并访问数据库的流程
自定义数据源
先排除tomcat-jdbc的默认配置dataSource
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
<exclusions>
<exclusion>
<groupId>org.apache.tomcat</groupId>
<artifactId>tomcat-jdbc</artifactId>
</exclusion>
</exclusions>
</dependency>
Datasource配置类
package com.cherrypicks.hsbcpayme.cs.config;
import javax.sql.DataSource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.transaction.annotation.EnableTransactionManagement;
import com.cherrypicks.hsbcpayme.service.EncryptionService;
@Configuration
@EnableTransactionManagement
public class DataSourceConfig {
// 从properties中读取参数
@Value("${spring.datasource.driverClassName}")
private String driverClassName;
@Value("${spring.datasource.url}")
private String url;
@Value("${spring.datasource.username}")
private String userName;
@Value("${spring.datasource.pwd}")
private String pwd;
@Value("${srping.datasource.initialSize}")
private Integer initialSize;
@Value("${spring.datasource.maxActive}")
private Integer maxActive;
@Autowired
private EncryptionService encryptionService;
// system data source
@Bean
@Primary
public DataSource dataSource() throws Exception {
return tomcatPoolingDataSource();
}
@Bean
public JdbcTemplate jdbcTemplate() throws Exception {
return new JdbcTemplate(dataSource());
}
private DataSource tomcatPoolingDataSource() throws Exception {
final org.apache.tomcat.jdbc.pool.DataSource dataSource = new org.apache.tomcat.jdbc.pool.DataSource();
dataSource.setDriverClassName(driverClassName);
dataSource.setUrl(url);
dataSource.setUsername(userName);
dataSource.setPassword(encryptionService.cherrypicksDecryption(pwd));
dataSource.setInitialSize(initialSize); // 连接池启动时创建的初始化连接数量(默认值为0)
dataSource.setMaxActive(maxActive); // 连接池中可同时连接的最大的连接数
dataSource.setMaxIdle(5); // 连接池中最大的空闲的连接数,超过的空闲连接将被释放,如果设置为负数表示不限
dataSource.setMinIdle(0); // 连接池中最小的空闲的连接数,低于这个数量会被创建新的连接
dataSource.setMaxWait(60000); // 最大等待时间,当没有可用连接时,连接池等待连接释放的最大时间,超过该时间限制会抛出异常,如果设置-1表示无限等待
dataSource.setRemoveAbandonedTimeout(180); // 超过时间限制,回收没有用(废弃)的连接
dataSource.setRemoveAbandoned(true); // 超过removeAbandonedTimeout时间后,是否进 行没用连接(废弃)的回收
dataSource.setTestOnBorrow(true);
dataSource.setTestOnReturn(true);
dataSource.setTestWhileIdle(true);
dataSource.setValidationQuery("SELECT 1");
dataSource.setTimeBetweenEvictionRunsMillis(1000 * 60 * 30); // 检查无效连接的时间间隔 设为30分钟
return dataSource;
}
}
Spring Boot启动类
package com.cherrypicks.hsbcpayme.cs;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.context.web.SpringBootServletInitializer;
import org.springframework.context.annotation.Import;
import com.cherrypicks.hsbcpayme.cs.config.DataSourceConfig;
import com.cherrypicks.hsbcpayme.cs.config.WebConfig;
@SpringBootApplication
@Import({WebConfig.class, DataSourceConfig.class})
public class Application extends SpringBootServletInitializer {
@Override
protected SpringApplicationBuilder configure(final SpringApplicationBuilder application) {
return application.sources(Application.class);
}
public static void main(final String[] args) {
final SpringApplication app = new SpringApplication(Application.class);
// app.setBannerMode(Banner.Mode.OFF);
app.run(args);
}
}
@Import({WebConfig.class, DataSourceConfig.class})将Datasource配置加入进来
Model类
public class User
{
private Integer id;
private String name;
private String email;
public User()
{
}
public User(Integer id, String name, String email)
{
this.id = id;
this.name = name;
this.email = email;
}
public Integer getId()
{
return id;
}
public void setId(Integer id)
{
this.id = id;
}
public String getName()
{
return name;
}
public void setName(String name)
{
this.name = name;
}
public String getEmail()
{
return email;
}
public void setEmail(String email)
{
this.email = email;
}
@Override
public String toString() {
return "User{" +
"id=" + id +
", name='" + name + '\'' +
", email='" + email + '\'' +
'}';
}
}
持久层
package com.example.repositories;
import com.example.domain.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.PreparedStatementCreator;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.jdbc.support.GeneratedKeyHolder;
import org.springframework.jdbc.support.KeyHolder;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;
import java.sql.*;
import java.util.List;
/**
* Created by tom on 2016/5/21.
*/
@Repository
public class UserRepository {
@Autowired
private JdbcTemplate jdbcTemplate;
@Transactional(readOnly = true)
public List<User> findAll() {
return jdbcTemplate.query("select * from users", new UserRowMapper());
}
@Transactional(readOnly = true)
public User findUserById(int id) {
return jdbcTemplate.queryForObject("select * from users where id=?", new Object[]{id}, new UserRowMapper());
}
public User create(final User user) {
final String sql = "insert into users(name,email) values(?,?)";
KeyHolder holder = new GeneratedKeyHolder();
jdbcTemplate.update(new PreparedStatementCreator() {
@Override
public PreparedStatement createPreparedStatement(Connection connection)
throws SQLException {
PreparedStatement ps = connection.prepareStatement(sql, Statement.RETURN_GENERATED_KEYS);
ps.setString(1, user.getName());
ps.setString(2, user.getEmail());
return ps;
}
}, holder);
int newUserId = holder.getKey().intValue();
user.setId(newUserId);
return user;
}
public void delete(final Integer id) {
final String sql = "delete from users where id=?";
jdbcTemplate.update(sql,
new Object[]{id},
new int[]{java.sql.Types.INTEGER});
}
public void update(final User user) {
jdbcTemplate.update(
"update users set name=?,email=? where id=?",
new Object[]{user.getName(), user.getEmail(), user.getId()});
}
}
class UserRowMapper implements RowMapper<User> {
@Override
public User mapRow(ResultSet rs, int rowNum) throws SQLException {
User user = new User();
user.setId(rs.getInt("id"));
user.setName(rs.getString("name"));
user.setEmail(rs.getString("email"));
return user;
}
}
测试持久化类
import com.example.SpringBootJdbcDemoApplication;
import com.example.domain.User;
import com.example.repositories.UserRepository;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.SpringApplicationConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import java.util.List;
import static org.junit.Assert.*;
import static org.junit.Assert.assertEquals;
/**
* Created by tom on 2016/5/21.
*/
@RunWith(SpringJUnit4ClassRunner.class)
@SpringApplicationConfiguration(SpringBootJdbcDemoApplication.class)
public class SpringBootJdbcDemoApplicationTests
{
Logger logger= LoggerFactory.getLogger(SpringBootJdbcDemoApplicationTests.class);
@Autowired
private UserRepository userRepository;
@Test public void testAll(){
findAllUsers();
findUserById();
createUser();
}
@Test
public void findAllUsers() {
List<User> users = userRepository.findAll();
assertNotNull(users);
assertTrue(!users.isEmpty());
}
@Test
public void findUserById() {
User user = userRepository.findUserById(1);
assertNotNull(user);
}
private void updateById(Integer id) {
User newUser = new User(id, "JackChen", "[email protected]");
userRepository.update(newUser);
User newUser2 = userRepository.findUserById(newUser.getId());
assertEquals(newUser.getName(), newUser2.getName());
assertEquals(newUser.getEmail(), newUser2.getEmail());
}
@Test
public void createUser() {
User user = new User(0, "tom", "[email protected]");
User savedUser = userRepository.create(user);
logger.debug("{}",savedUser);
User newUser = userRepository.findUserById(savedUser.getId());
assertEquals("tom", newUser.getName());
assertEquals("[email protected]", newUser.getEmail());
updateById(newUser.getId());
userRepository.delete(newUser.getId());
}
}