1、连接数据库
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
spring:
datasource:
# 数据源基本配置
username: root
password: 123456
driver-class-name: com.mysql.jdbc.Driver
url: jdbc:mysql://localhost:3306/jdbc
默认是用org.apache.tomcat.jdbc.pool.DataSource作为数据源;
数据源的相关配置都在DataSourceProperties类中;
自动配置原理SPI:
org.springframework.boot.autoconfigure.jdbc:
2、Springboot 事务:
@Component
public class TransactionConfig {
@Bean
public PlatformTransactionManager txManager(DataSource dataSource){
DataSourceTransactionManager manager = new DataSourceTransactionManager(dataSource);
manager.setNestedTransactionAllowed(true);
return manager;
}
}
上面代码对spring的数据库事务进行配置
springboot中专门用于配置事务的类是:TransactionAutoConfiguration 其依赖于JPA和DataSource,如下所示:
@Configuration
@ConditionalOnClass({PlatformTransactionManager.class})
@AutoConfigureAfter({JtaAutoConfiguration.class, HibernateJpaAutoConfiguration.class, DataSourceTransactionManagerAutoConfiguration.class,})
@EnableConfigurationProperties({TransactionProperties.class})
public class TransactionAutoConfiguration {
}
配置类:
可以看到transaction的配置类是TransactionProperties,其源码如下:
@ConfigurationProperties(prefix = "spring.transaction")
public class TransactionProperties{
}
可以看到我们可以在配置文件中使用spring.transaction前缀对事务进行配置
在springboot中我们无需显式显式使用注解@EnableTransactionManagement来支持事务
spring缓存:
spring定义了CacheManager和Cache来统一不同的缓存技术,CacheManager是各种缓存技术的抽象接口,Cache是缓存的各种操作
CacheManager的实现有:
AbstractTransactionSupportingCacheManager : spring提供的支持事务的缓存管理器实现
RedisCacheManager : redis的缓存管理器实现
Cache的实现有:
AbstractValueAdaptingCache : spring提供的支持事务的缓存管理器实现
RedisCache : redis的缓存操作实现,通过RedisCacheWriter 执行具体操作
springboot的缓存支持:
springboot的缓存配置放置在springframework.boot.autoconfigure.cache包下面,我们可以在配置文件中使用spring.cache前缀配置缓存相关配置
在springboot中启用缓存很简单,只需要在项目中导入缓存相关依赖包spring-boot-starter-cache,并使用注解@EnableCaching开启缓存支持就可以了
注意springboot程序使用Redis缓存功能,首先需要启动redis
springboot会默认连接127.0.0.1 端口号为6379的redis服务端应用
缓存使用的主要注解:
CachePut缓存新增的或者更新的数据到缓存,value是缓存名称,person.id是key值
@CachePut(value = "people",key = "#person.id")
public Person save(Person person) {
return personRepository.save(person);
}
CacheEvict从缓存people中删除key是id的缓存
@CacheEvict(value = "people")
public void remove(Long id) {
personRepository.deleteById(id);
}
Cacheable 从缓存person中查找key为ID的数据,如果缓存命中,则直接返回缓存数据,并且会将数据放入缓存
@Cacheable(value = "people" ,key = "#id")
public Person findOne(Long id) {
Optional<Person> op = personRepository.findById(id);
returnop.get();
}
jedis:
redis可以使用jedis
jedis连接工厂类:JedisConnectionFactory
错误信息:
返回对象数据给前端的时候报错:
Default Serializer requires a Serializable payload but received an object of type
因为返回的对象没有实现序列化接口Serializable
解决办法:给返回对象实现序列化接口
启动应用报错:
The Network Adapter could not establish the connection
原因是应用连接的数据库是docker启动的在虚拟机里面运行的oracle数据库,应用启动的时候连接不到自然就报错了
解决办法:启动docker,运行oracle数据库,如下所示:
docker run -d -p 9090:8080 -p 1521:1521 wnameless/oracle-xe-11g
并且确定在virtualBox中开启了端口转发,即可确保应用可以连接上数据库