• 集成mybatis(六)


    config类配置综合:

    package com.ssm.chapter22.config;
    
    import java.util.Properties;
    
    import javax.sql.DataSource;
    
    import org.apache.commons.dbcp.BasicDataSourceFactory;
    import org.mybatis.spring.SqlSessionFactoryBean;
    import org.mybatis.spring.mapper.MapperScannerConfigurer;
    import org.springframework.context.annotation.Bean;
    import org.springframework.context.annotation.ComponentScan;
    import org.springframework.context.annotation.ComponentScan.Filter;
    import org.springframework.context.annotation.Configuration;
    import org.springframework.context.annotation.FilterType;
    import org.springframework.core.io.ClassPathResource;
    import org.springframework.core.io.Resource;
    import org.springframework.jdbc.datasource.DataSourceTransactionManager;
    import org.springframework.stereotype.Repository;
    import org.springframework.stereotype.Service;
    import org.springframework.transaction.PlatformTransactionManager;
    import org.springframework.transaction.annotation.EnableTransactionManagement;
    import org.springframework.transaction.annotation.TransactionManagementConfigurer;
    
    @Configuration
    //定义spring扫描的包
    @ComponentScan(value="com.*",includeFilters= {@Filter(type=FilterType.ANNOTATION,value= {Service.class})})
    //使用事务驱动管理器
    @EnableTransactionManagement 
    //实现TransactionManagementConfigurer,这样可以配置注解驱动事务
    public class RootConfig implements TransactionManagementConfigurer {
        private DataSource dataSource=null;
        
        /**
         * 配置数据库
         * @return 数据库连接池
         */
        @Bean(name="dataSource")
        public DataSource initDataSource() {
            if(dataSource!=null) {
                return dataSource;
            }
            Properties props=new Properties();
            props.setProperty("driverClassName", "com.mysql.cj.jdbc.Driver");
            props.setProperty("url","jdbc:mysql://localhost:3306/ssm?useUnicode=true&characterEncoding=utf8&serverTimezone=GMT%2B8&useSSL=false&allowMultiQueries=true&allowPublicKeyRetrieval=true");
            props.setProperty("userName","root");
            props.setProperty("password","123456abc");
            props.setProperty("maxActive","20");
            props.setProperty("maxIdle","20");
            props.setProperty("maxWait","30000");
            try {
                dataSource=BasicDataSourceFactory.createDataSource(props);
            }catch(Exception e) {
                e.printStackTrace();
            }
            return dataSource;
        }
        /**
         * 配置SqlSessionFactoryBean
         * @return SqlSessionFactoryBean
         */
        @Bean(name="sqlSessionFactory")
        public SqlSessionFactoryBean initSqlSessionFactory() {
            SqlSessionFactoryBean sqlSessionFactory=new SqlSessionFactoryBean();
            sqlSessionFactory.setDataSource(initDataSource());
            //配置MyBatis文件
            Resource resource=new ClassPathResource("mybatis/mybatis-config.xml");
            sqlSessionFactory.setConfigLocation(resource);
            return sqlSessionFactory;
        }
        /**
         * 通过自动扫描,发现MyBatis mapper接口
         * @return Mapper扫描器
         */
        @Bean
        public MapperScannerConfigurer initMapperScannerConfigurer() {
            MapperScannerConfigurer msc=new MapperScannerConfigurer();
            msc.setBasePackage("com.*");
            msc.setSqlSessionFactoryBeanName("sqlSessionFactory");
            msc.setAnnotationClass(Repository.class);
            return msc;
        }
        
        /**
         * 实现接口方法,注册注解事务,当@Transactional使用的时候产生数据库事务
         */
        @Bean(name="annotationDrivenTransactionManager")
        public PlatformTransactionManager annotationDrivenTransactionManager() {
            // TODO Auto-generated method stub
            DataSourceTransactionManager transactionManager=new DataSourceTransactionManager();
            transactionManager.setDataSource(initDataSource());
            return transactionManager;
        }
    
    }
  • 相关阅读:
    智能实验室-结构化存储浏览器(SSExplorer) 1.5.0.150
    智能实验室-杀马(Defendio) 3.1.0.681
    智能实验室-结构化存储浏览器(SSExplorer) 1.6.0.160
    IT餐馆—第八回 三十
    使用Silverlight Toolkit 绘制图表区域图和冒泡图
    IT餐馆—第十二回 软培
    IT餐馆—第四回 离职
    IT餐馆—第一回 前言
    IT餐馆—第十回 潜伏
    IT餐馆—第十三回 重构
  • 原文地址:https://www.cnblogs.com/xc-xinxue/p/12482239.html
Copyright © 2020-2023  润新知