import org.mybatis.spring.mapper.MapperScannerConfigurer;
import org.springframework.boot.autoconfigure.AutoConfigureAfter;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
@AutoConfigureAfter(MyBatisConfig.class) //保证在MyBatisConfig实例化之后再实例化该类
public class MapperScannerConfig {
// mapper接口的扫描器
@Bean
public MapperScannerConfigurer mapperScannerConfigurer() {
MapperScannerConfigurer mapperScannerConfigurer = new MapperScannerConfigurer();
mapperScannerConfigurer.setBasePackage("com.taotao.cart.mapper");
return mapperScannerConfigurer;
}
}
import javax.sql.DataSource;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.Resource;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.core.io.support.ResourcePatternResolver;
@Configuration
public class MyBatisConfig {
@Autowired
private DataSource dataSource;
@Bean
@ConditionalOnMissingBean //当容器里没有指定的Bean的情况下创建该对象
public SqlSessionFactoryBean sqlSessionFactoryBean() {
SqlSessionFactoryBean sqlSessionFactoryBean = new SqlSessionFactoryBean();
// 设置数据源
sqlSessionFactoryBean.setDataSource(dataSource);
// 设置mybatis的主配置文件
ResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();
Resource mybatisConfigXml = resolver.getResource("classpath:mybatis/mybatis-config.xml");
sqlSessionFactoryBean.setConfigLocation(mybatisConfigXml);
// 设置别名包
sqlSessionFactoryBean.setTypeAliasesPackage("com.taotao.cart.pojo");
return sqlSessionFactoryBean;
}
}
import java.util.ArrayList;
import java.util.List;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
import redis.clients.jedis.JedisPoolConfig;
import redis.clients.jedis.JedisShardInfo;
import redis.clients.jedis.ShardedJedisPool;
@Configuration
@PropertySource(value = "classpath:redis.properties")
public class RedisSpringConfig {
@Value("${redis.maxTotal}")
private Integer redisMaxTotal;
@Value("${redis.node1.host}")
private String redisNode1Host;
@Value("${redis.node1.port}")
private Integer redisNode1Port;
private JedisPoolConfig jedisPoolConfig() {
JedisPoolConfig jedisPoolConfig = new JedisPoolConfig();
jedisPoolConfig.setMaxTotal(redisMaxTotal);
return jedisPoolConfig;
}
@Bean
public ShardedJedisPool shardedJedisPool() {
List<JedisShardInfo> jedisShardInfos = new ArrayList<JedisShardInfo>();
jedisShardInfos.add(new JedisShardInfo(redisNode1Host, redisNode1Port));
return new ShardedJedisPool(jedisPoolConfig(), jedisShardInfos);
}
}
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
import com.taotao.cart.interceptors.UserLoginHandlerInterceptor;
@Configuration
public class SpringMVCConfig extends WebMvcConfigurerAdapter {
@Autowired
private UserLoginHandlerInterceptor userLoginHandlerInterceptor;
@Override
public void addInterceptors(InterceptorRegistry registry) {
// 判断用户是否登录的拦截器
registry.addInterceptor(userLoginHandlerInterceptor).addPathPatterns("/cart/**");
}
}
import javax.sql.DataSource;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.web.support.SpringBootServletInitializer;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.ImportResource;
import org.springframework.context.annotation.PropertySource;
import com.jolbox.bonecp.BoneCPDataSource;
@Configuration
@PropertySource(value = { "classpath:jdbc.properties", "classpath:env.properties",
"classpath:httpclient.properties" })
@ComponentScan(basePackages = "com.taotao")
@ImportResource(value = "classpath:dubbo/dubbo-consumer.xml")
@SpringBootApplication
public class TaotaoApplication extends SpringBootServletInitializer{
@Value("${jdbc.url}")
private String jdbcUrl;
@Value("${jdbc.driverClassName}")
private String jdbcDriverClassName;
@Value("${jdbc.username}")
private String jdbcUsername;
@Value("${jdbc.password}")
private String jdbcPassword;
@Bean(destroyMethod = "close")
public DataSource dataSource() {
BoneCPDataSource boneCPDataSource = new BoneCPDataSource();
// 数据库驱动
boneCPDataSource.setDriverClass(jdbcDriverClassName);
// 相应驱动的jdbcUrl
boneCPDataSource.setJdbcUrl(jdbcUrl);
// 数据库的用户名
boneCPDataSource.setUsername(jdbcUsername);
// 数据库的密码
boneCPDataSource.setPassword(jdbcUsername);
// 检查数据库连接池中空闲连接的间隔时间,单位是分,默认值:240,如果要取消则设置为0
boneCPDataSource.setIdleConnectionTestPeriodInMinutes(60);
// 连接池中未使用的链接最大存活时间,单位是分,默认值:60,如果要永远存活设置为0
boneCPDataSource.setIdleMaxAgeInMinutes(30);
// 每个分区最大的连接数
boneCPDataSource.setMaxConnectionsPerPartition(100);
// 每个分区最小的连接数
boneCPDataSource.setMinConnectionsPerPartition(5);
return boneCPDataSource;
}
@Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder builder) {
return builder.sources(TaotaoApplication.class);
}
}