码迷,mamicode.com
首页 > 编程语言 > 详细

SSM项目转SpringBoot项目

时间:2021-01-26 12:19:56      阅读:0      评论:0      收藏:0      [点我收藏+]

标签:driver   name   opp   标签   tip   ann   lin   black   erp   

//对于SpringBoot所推崇的去除xml化编程需求,将原始的spring-*.xml文件使用配置类的方式进行注入

//mybatis

// 该标签表示将这个类注入到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;

    }

}

//service

// Spring事物配置
@Configuration
// 开启事务支持
@EnableTransactionManagement
public class TransactionManagementConfiguration implements TransactionManagementConfigurer {


    @Autowired
    private DataSource dataSource;

    /***
     * 关于事务管理,需要返回Platform里边的PlatformTransactionManager的实现
     * @return
     */
    @Override
    public PlatformTransactionManager annotationDrivenTransactionManager() {
        return new DataSourceTransactionManager(dataSource);
    }


}

//springmvc controller

@Configuration
//将mvc交给我们自己管理
//等价于标签 <mvc:annotation-driven>
@EnableWebMvc
public class MvcConfiguration implements WebMvcConfigurer,ApplicationContextAware {


    //Spring 容器
    private ApplicationContext applicationContext;

    @Override
    public void setApplicationContext(ApplicationContext applicationContext) {
        this.applicationContext = applicationContext;
    }


    /**
    * 定义默认的请求处理器
    */

    @Override
    public void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer) {

        configurer.enable();
    }


    /**
     * 静态资源配置
     * 将/upload 映射为 D:/projectdev/image/upload
     * 需要自己更改
     * @param registry
     */
//    @Override
//    public void addResourceHandlers(ResourceHandlerRegistry registry) {
//        registry.addResourceHandler("/upload/**")
//                .addResourceLocations("file:D:\\projectdev\\image\\upload\\");
//    }
    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/root/imgdatas/upload/images/**")
                .addResourceLocations("file:/root/imgdatas/upload/images/");
        registry.addResourceHandler("/root/imgdatas/project/**")
                .addResourceLocations("file:/root/imgdatas/project");
    }


    /*
    * 创建viewResolver
    * */

    @Bean(name = "viewResolver")
    public ViewResolver viewResolver(){

        InternalResourceViewResolver viewResolver = new InternalResourceViewResolver();
        viewResolver.setApplicationContext(applicationContext);
        viewResolver.setCache(false);
        viewResolver.setPrefix("/WEB-INF/html/");
        viewResolver.setSuffix(".html");

        return viewResolver;

    }

    /*
    * 文件上传解析器
    * */
    @Bean(name = "multipartResolver")
    public CommonsMultipartResolver multipartResolver(){

        CommonsMultipartResolver multipartResolver = new CommonsMultipartResolver();

        multipartResolver.setDefaultEncoding("utf-8");

        multipartResolver.setMaxInMemorySize(20971520);

        multipartResolver.setMaxUploadSize(20971520);

        return multipartResolver;
    }


    //注册servlet 验证码
    @Bean
    public ServletRegistrationBean servletRegistrationBean(){
        ServletRegistrationBean servlet = new ServletRegistrationBean(new KaptchaServlet(),"/kaptcha");
        servlet.addInitParameter("kaptcha.border","no");
        servlet.addInitParameter("kaptcha.textproducer.font.color","red");
        servlet.addInitParameter("kaptcha.image.width","135");
        servlet.addInitParameter("kaptcha.textproducer.char.string","ACDEFHKGPRSTWX23456789");
        servlet.addInitParameter("kaptcha.image.height","50");
        servlet.addInitParameter("kaptcha.textproducer.font.size","43");
        servlet.addInitParameter("kaptcha.noise.color","black");
        servlet.addInitParameter("kaptcha.textproducer.char.length","4");
        servlet.addInitParameter("kaptcha.textproducer.font.names","Arial");
        return servlet;
    }


//    @Bean
//    public FilterRegistrationBean filterRegistrationBean(){
//
//        CodingFilter codingFilter = new CodingFilter();
//
//        FilterRegistrationBean filterRegistrationBean = new FilterRegistrationBean(codingFilter);
//        filterRegistrationBean.addUrlPatterns("/*");
//        return filterRegistrationBean;
//
//    }


//    @Bean
//    public ServletListenerRegistrationBean servletListenerRegistrationBean(){
//        PathListener pathListener = new PathListener();
//        ServletListenerRegistrationBean servletListenerRegistrationBean = new ServletListenerRegistrationBean(pathListener);
//        return servletListenerRegistrationBean;
//    }
    
}

// interceptor 拦截器

@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();
    }

}

  

 

SSM项目转SpringBoot项目

标签:driver   name   opp   标签   tip   ann   lin   black   erp   

原文地址:https://www.cnblogs.com/walxt/p/14322943.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!