代码位置:
https://github.com/viakiba/springboot/tree/master/springbootredis
引入依赖查看pom文件不再贴出来。
首先,配置application.properties:
#server.port = 8080spring.debug = true# DATASOURCEspring.datasource.driver-class-name=com.mysql.jdbc.Driverspring.datasource.url=jdbc:mysql://127.0.0.1:3306/springbootspring.datasource.username = rootspring.datasource.password = 951357qazspring.datasource.type = com.alibaba.druid.pool.DruidDataSource# mybatismybatis.configLocation: classpath:mybatis-config.xml# REDIS (RedisProperties) spring.redis.host=127.0.0.1 spring.redis.port=6379#thymeleafspring.thymeleaf.suffix=.html spring.thymeleaf.content-type=text/html#spring.thymeleaf.prefix=classpath:/templates/#spring.thymeleaf.mode=HTML5# ;charset=is added#spring.thymeleaf.encoding=UTF-8# set to false for hot refreshspring.thymeleaf.cache=false
然后设置mybatis配置文件
最后设置logback:
配置文件配置完毕,代码方面:
第一个设置key的生成原则,在config包下:
package com.example.demo.config;import org.springframework.cache.annotation.CachingConfigurerSupport;import org.springframework.cache.annotation.EnableCaching;import org.springframework.cache.interceptor.KeyGenerator;import org.springframework.cache.interceptor.SimpleKey;import org.springframework.context.annotation.Bean;import org.springframework.context.annotation.Configuration;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.data.redis.cache.RedisCacheManager; import org.springframework.data.redis.connection.jedis.JedisConnectionFactory;import org.springframework.data.redis.core.RedisTemplate; import org.springframework.data.redis.serializer.StringRedisSerializer;import java.lang.reflect.Method; @Configuration @EnableCaching public class CacheRedisConfig extends CachingConfigurerSupport{ @Autowired private JedisConnectionFactory jedisConnectionFactory; @Bean(name = "SimpleKey") public KeyGenerator getKeyGenerator(){ return new KeyGenerator() { @Override public Object generate(Object target, Method method, Object... params) { StringBuilder sb = new StringBuilder(); sb.append(target.getClass().getName()); sb.append(method.getName()); for (Object obj : params) { sb.append(obj.toString()); } System.out.println(sb.toString()); return sb.toString(); } }; } @Bean public RedisCacheManager cacheManager() { RedisCacheManager redisCacheManager = new RedisCacheManager(redisTemplate()); return redisCacheManager; } @Bean public RedisTemplate
设置数据库连接池,也是在config包下:
package com.example.demo.config;import javax.sql.DataSource;import org.apache.ibatis.session.SqlSessionFactory;import org.mybatis.spring.SqlSessionFactoryBean;import org.mybatis.spring.annotation.MapperScan;import org.springframework.beans.factory.annotation.Qualifier;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.core.io.support.PathMatchingResourcePatternResolver;import org.springframework.jdbc.datasource.DataSourceTransactionManager;import com.alibaba.druid.pool.DruidDataSource;/** * @description: * @author viakiba * @date 2017年7月12日 */@Configuration@MapperScan(basePackages = DruidDataSourceConfig.PACKAGE,sqlSessionFactoryRef = "sqlSessionFactory")public class DruidDataSourceConfig { static final String PACKAGE = "com.example.demo.dao"; static final String MAPPER_LOCATION = "classpath:mapper/*.xml"; //接下来使用的value注解获取application.properties拿到值进行初始化 spring-EL表达式 @Value("${spring.datasource.url}") private String url ; @Value("${spring.datasource.username}") private String user ; @Value("${spring.datasource.password}") private String password ; @Value("${spring.datasource.driver-class-name}") private String driverClass ; //数据源 //Bean明确地指示产生一个bean的方法,并且交给Spring容器管理; @Bean(name="dataSource") //标志这个 Bean 如果在多个同类 Bean 候选时,该 Bean 优先被考虑。「多数据源配置的时候注意,必须要有一个主数据源,用 @Primary 标志该 Bean @Primary public DataSource dataSource(){ DruidDataSource druidDataSource = new DruidDataSource(); druidDataSource.setUrl(url); druidDataSource.setUsername(user); druidDataSource.setPassword(password); druidDataSource.setDriverClassName(driverClass); return druidDataSource; } //主库事务 @Bean(name = "transactionManager") @Primary public DataSourceTransactionManager masterTransactionManager() { return new DataSourceTransactionManager(dataSource()); } //主库sqlSessionFactory @Qualifier("XXX") 中的 XX是 Bean 的名称,所以 @Autowired 和 @Qualifier 结合使用时,自动注入的策略就从 byType 转变成 byName 了。 @Bean(name = "sqlSessionFactory") @Primary public SqlSessionFactory sqlSessionFactory(@Qualifier("dataSource") DataSource dataSource) throws Exception{ final SqlSessionFactoryBean sqlSessionFactoryBean = new SqlSessionFactoryBean(); sqlSessionFactoryBean.setDataSource(dataSource); sqlSessionFactoryBean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources(DruidDataSourceConfig.MAPPER_LOCATION)); return sqlSessionFactoryBean.getObject(); }}
其中的注解作用,不在详细解释。
然后,在dao包下,如下写法:
package com.example.demo.dao;import org.springframework.cache.annotation.Cacheable;/** * @description: * @author viakiba * @date 2017年7月13日 */public interface UserinfoDao { @Cacheable(value = "usercache",keyGenerator="SimpleKey") public String selectUser();}
注意simplekey就是config包下,CacheRedisConfig 里面通过@Bean指定名字载入的。
其他关于mybatis的mapper查看application.properties即可。