SSM项目转SpringBoot项目
2021-03-05 11:29
标签:driver name opp 标签 tip ann lin black erp //对于SpringBoot所推崇的去除xml化编程需求,将原始的spring-*.xml文件使用配置类的方式进行注入 //mybatis //service //springmvc controller // interceptor 拦截器 SSM项目转SpringBoot项目 标签:driver name opp 标签 tip ann lin black erp 原文地址:https://www.cnblogs.com/walxt/p/14322943.html// 该标签表示将这个类注入到Spring的IOC容器内
@Configuration
// 配置mybatis Mapper的扫描路径
@MapperScan("com.swust.emporium.dao")
public class DataSourceConfiguration {
// 定义需要注入的JDBC连接变量
@Value("${jdbc.url}")
private String jdbcUrl;
@Value("${jdbc.driver}")
private String jdbcDriver;
@Value("${jdbc.username}")
private String jdbcUserName;
@Value("${jdbc.password}")
private String jdbcPassword;
/**
* 生成与Spring-dao.xml对应的bean标签 dataSource
*/
@Bean(name = "dataSource")
public ComboPooledDataSource createDataSource(){
// 创建Bean对象
ComboPooledDataSource dataSource = new ComboPooledDataSource();
// 设置配置信息
dataSource.setJdbcUrl(jdbcUrl);
try {
dataSource.setDriverClass(jdbcDriver);
} catch (PropertyVetoException e) {
e.printStackTrace();
}
// 对用户名密码进行解密
String name = DESUtil.getDecryptString(jdbcUserName);
dataSource.setUser(name);
String password = DESUtil.getDecryptString(jdbcPassword);
dataSource.setPassword(password);
// 设置固有属性
dataSource.setMaxPoolSize(30);
dataSource.setMinPoolSize(10);
// 关闭连接后不自动提交
dataSource.setAutoCommitOnClose(false);
dataSource.setCheckoutTimeout(10000);
dataSource.setAcquireRetryAttempts(2);
return dataSource;
}
}
@Configuration
public class SessionFactoryConfiguration {
private static String mybatisConfigFile;
private static String mapperPath;
/**
* 静态属性需要通过setter方法注入 不可以直接注入
*/
// 基本属性
@Value("${mybatis_config_file}")
public void setMybatisConfigFile(String mybatisConfigFile) {
SessionFactoryConfiguration.mybatisConfigFile = mybatisConfigFile;
}
// mybatis mapper文件的所在路径
@Value("${mapper_path}")
public void setMapperPath(String mapperPath) {
SessionFactoryConfiguration.mapperPath = mapperPath;
}
// 实体类所在的package
@Value("${type_alias_package}")
private String typeAliasPackage;
@Autowired
private DataSource dataSource;
/**
* 创建SqlSessionFactoryBean实例对象,并设置配置属性
* @return
*/
@Bean(name = "sqlSessionFactory")
public SqlSessionFactoryBean sqlSessionFactoryBean() throws IOException {
SqlSessionFactoryBean sqlSessionFactoryBean = new SqlSessionFactoryBean();
sqlSessionFactoryBean.setDataSource(dataSource);
//mybatis 的配置文件
sqlSessionFactoryBean.setConfigLocation(new ClassPathResource(mybatisConfigFile));
PathMatchingResourcePatternResolver pathMatchingResourcePatternResolver
= new PathMatchingResourcePatternResolver();
String packageSearchPath = ResourcePatternResolver.CLASSPATH_ALL_URL_PREFIX + mapperPath;
sqlSessionFactoryBean.setMapperLocations(pathMatchingResourcePatternResolver.getResources(packageSearchPath));
sqlSessionFactoryBean.setTypeAliasesPackage(typeAliasPackage);
return sqlSessionFactoryBean;
}
}
// Spring事物配置
@Configuration
// 开启事务支持
@EnableTransactionManagement
public class TransactionManagementConfiguration implements TransactionManagementConfigurer {
@Autowired
private DataSource dataSource;
/***
* 关于事务管理,需要返回Platform里边的PlatformTransactionManager的实现
* @return
*/
@Override
public PlatformTransactionManager annotationDrivenTransactionManager() {
return new DataSourceTransactionManager(dataSource);
}
}
@Configuration
//将mvc交给我们自己管理
//等价于标签
@Configuration
public class InterceptorConfig implements WebMvcConfigurer {
@Override
public void addInterceptors(InterceptorRegistry registry) {
registry.addInterceptor(frontendLoginInterceptor())
.excludePathPatterns("/frontend/getoperationlogin")
.excludePathPatterns("/frontend/login")
.addPathPatterns("/frontend/**");
registry.addInterceptor(shopLoginInterceptor())
.addPathPatterns("/shopadmin/**");
registry.addInterceptor(shopPermissionInterceptor())
.excludePathPatterns("/shopadmin/shoplist","/shopadmin/getshoplist","/shopadmin/getshopinitinfo")
.excludePathPatterns("/shopadmin/registershop","/shopadmin/shopoperation")
.excludePathPatterns("/shopadmin/shopmanagement","/shopadmin/getshopmanagementinfo")
.addPathPatterns("/shopadmin/**");
}
@Bean
public ShopLoginInterceptor shopLoginInterceptor(){
return new ShopLoginInterceptor();
}
@Bean
public ShopPermissionInterceptor shopPermissionInterceptor(){
return new ShopPermissionInterceptor();
}
@Bean
public FrontendLoginInterceptor frontendLoginInterceptor(){
return new FrontendLoginInterceptor();
}
}