SpringBoot--MybatisとRedisの統合
6289 ワード
1 SpringBootはMybatisを統合するツールパッケージを提供していません.1つ目はmybatisが公式に提供しているSpring Boot統合パッケージを使用して実現し、アドレスをダウンロードします.https://github.com/mybatis/spring-boot-starter2つ目は、Spring統合Mybatisに似た独自の統合です.
2 JDBCを構成し、コンテナにデータソースを追加します.
外部のプロファイルを読み込む必要がある:jdbc.properties
3 Mybatisの統合
Springの自動注入により、前のステップで設定したJDBCデータソースを注入します.
4宣言トランザクション管理を構成Spring Bootで@Transactional注釈を使用してトランザクションを明示することを推奨します.まず依存をインポートする必要があります.
jdbc依存が導入されるとSpring Bootは、Data SourceTransactionManagerまたはJpaTransactionManagerに自動的にデフォルトでインポートされるので、@Transactional注記を使用してトランザクションを使用するには、追加の構成は必要ありません.トランザクションが必要なサービスに@Transactionalコメントを追加すれば、Springの公式ドキュメント、宣言トランザクションについて参照できます.
5 Redisの統合
プロファイル:
JDBCと似ています.プロファイルを読み込み、変数にマッピングし、SpringBootコンテナにオブジェクトを初期化します.
統合redis方式2:.properties:
依存パッケージ:
ツールクラス:
参照先:https://blog.csdn.net/plei_yue/article/details/79362372
一部はある知的アナウンサーから抜粋した.
2 JDBCを構成し、コンテナにデータソースを追加します.
/**
* JDBC
* @author Tang
* 2017 11 18
*/
@Configuration
@PropertySource(value = { "jdbc.properties" })//
public class JdbcConfig {
//
@Value("${driverClassName}")
private String driverClassName;
@Value("${url}")
private String url;
@Value("${username}")
private String username;
@Value("${password}")
private String password;
@Bean(destroyMethod = "close")
public BasicDataSource dataSource() {
BasicDataSource basicDataSource = new BasicDataSource();
basicDataSource.setDriverClassName(driverClassName);
basicDataSource.setUrl(url);
basicDataSource.setUsername(username);
basicDataSource.setPassword(password);
return basicDataSource;
}
}
外部のプロファイルを読み込む必要がある:jdbc.properties
driverClassName=com.mysql.jdbc.Driver
url=jdbc:mysql://127.0.0.1:3306/test?characterEncoding=utf8&useSSL=true
username=root
password=123123
3 Mybatisの統合
/**
* Mybatis
* @author Tang
* 2017 11 18
*/
@Configuration
@AutoConfigureAfter(JdbcConfig.class)// JDBC
public class MybatisConfig {
@Autowired
private DataSource dataSource;
@Bean
@ConditionalOnMissingBean // Bean
public SqlSessionFactoryBean sqlSessionFactory() {
SqlSessionFactoryBean sqlSessionFactoryBean = new SqlSessionFactoryBean();
//
sqlSessionFactoryBean.setDataSource(dataSource);
// mybatis
ResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();
Resource mybatisConfigXml = resolver.getResource("classpath:mybatis-config.xml");
sqlSessionFactoryBean.setConfigLocation(mybatisConfigXml);
return sqlSessionFactoryBean;
}
/**
*
* @return
*/
@Bean
public MapperScannerConfigurer mapperScannerConfigurer() {
MapperScannerConfigurer mapperScannerConfigurer = new MapperScannerConfigurer();
mapperScannerConfigurer.setBasePackage("com.text.springboot.dao");
return mapperScannerConfigurer;
}
}
Springの自動注入により、前のステップで設定したJDBCデータソースを注入します.
4宣言トランザクション管理を構成Spring Bootで@Transactional注釈を使用してトランザクションを明示することを推奨します.まず依存をインポートする必要があります.
org.springframework.boot
spring-boot-starter-jdbc
jdbc依存が導入されるとSpring Bootは、Data SourceTransactionManagerまたはJpaTransactionManagerに自動的にデフォルトでインポートされるので、@Transactional注記を使用してトランザクションを使用するには、追加の構成は必要ありません.トランザクションが必要なサービスに@Transactionalコメントを追加すれば、Springの公式ドキュメント、宣言トランザクションについて参照できます.
5 Redisの統合
/**
* Redis
* @author Tang
* 2017 11 18
*/
@Configuration
@PropertySource(value="classpath:redis.properties")
public class RedisConfig {
@Value("${redis.url}")
private String url;
@Value("${redis.port}")
private Integer port;
@Value("${redis.maxTotal}")
private Integer maxTotal;
@Bean
public ShardedJedisPool shardedJedisPool() {
List jedisShardInfos = new ArrayList();
jedisShardInfos.add(new JedisShardInfo(url, port));
return new ShardedJedisPool(jedisPoolConfig(), jedisShardInfos);
}
/**
*
* @return
*/
private JedisPoolConfig jedisPoolConfig() {
JedisPoolConfig jedisPoolConfig = new JedisPoolConfig();
jedisPoolConfig.setMaxTotal(maxTotal);
return jedisPoolConfig;
}
}
プロファイル:
redis.url=127.0.0.1
redis.port=6379
redis.maxTotal=10
JDBCと似ています.プロファイルを読み込み、変数にマッピングし、SpringBootコンテナにオブジェクトを初期化します.
統合redis方式2:.properties:
# Redis
# Redis ( 0)
spring.redis.database=1
# Redis
spring.redis.host=192.168.41.101
# Redis
spring.redis.port=6379
# ( )
spring.redis.pool.max-active=8
# ( )
spring.redis.pool.max-wait=-1
#
spring.redis.pool.max-idle=8
#
spring.redis.pool.min-idle=1
# ( )
spring.redis.timeout=10000
@Configuration
public class RedisConfig {
/**
* retemplate
*
* @param factory
* @return
*/
@Bean
public RedisTemplate redisTemplate(RedisConnectionFactory factory) {
RedisTemplate template = new RedisTemplate<>();
//
template.setConnectionFactory(factory);
// Jackson2JsonRedisSerializer redis value ( JDK )
Jackson2JsonRedisSerializer jacksonSeial = new Jackson2JsonRedisSerializer(Object.class);
ObjectMapper om = new ObjectMapper();
// ,field,get set, ,ANY private public
om.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);
// , final ,final , String,Integer
om.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL);
jacksonSeial.setObjectMapper(om);
// json
template.setValueSerializer(jacksonSeial);
// StringRedisSerializer redis key
template.setKeySerializer(new StringRedisSerializer());
// hash key value
template.setHashKeySerializer(new StringRedisSerializer());
template.setHashValueSerializer(jacksonSeial);
template.afterPropertiesSet();
return template;
}
}
依存パッケージ:
org.springframework.boot
spring-boot-starter-redis
1.3.2.RELEASE
ツールクラス:
@Autowired
private RedisTemplate redisTemplate;
/**
*
* @param key
* @return
*/
public Object get(String key){
return key==null?null:redisTemplate.opsForValue().get(key);
}
参照先:https://blog.csdn.net/plei_yue/article/details/79362372
一部はある知的アナウンサーから抜粋した.