栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java > SpringBoot

SpringBoot---数据源和SessionFactory配置

SpringBoot 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

SpringBoot---数据源和SessionFactory配置


DataSourceConfiguration.java


@Configuration
// 配置mybatis mapper的扫描路径
@MapperScan("ink.xlr.demo.dao")
public class DataSourceConfiguration {
    @Value("${jdbc.driver}")
    private String jdbcDriver;
    @Value("${jdbc.url}")
    private String jdbcUrl;
    @Value("${jdbc.user}")
    private String jdbcUsername;
    @Value("${jdbc.password}")
    private String jdbcPassword;

    @Bean(name = "dataSource")
    public ComboPooledDataSource createDataSource() throws PropertyVetoException {
 ComboPooledDataSource dataSource = new ComboPooledDataSource();
 dataSource.setDriverClass(jdbcDriver);
 dataSource.setJdbcUrl(jdbcUrl);
 dataSource.setUser(jdbcUsername);
 dataSource.setPassword(jdbcPassword);
 // 关闭连接后 不自动commit
 dataSource.setAutoCommitOnClose(false);
 return dataSource;
    }
}

application.properties

# tomcat运行端口
server.port=8082
# 项目根路径
server.servlet.context-path=/demo

# c3p0数据源配置
jdbc.driver=com.mysql.cj.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/springboot_test?characterEncoding=UTF-8&serverTimezone=UTC&useSSL=false
jdbc.user=root
jdbc.password=123456789

# Mybatis配置文件
mybatis_config_file=mybatis-config.xml
# mapper文件路径
mapper_path=/mapper/**.xml
# 实体类路径
entity_package=ink.xlr.demo.entity

SessionFactoryConfiguration.java



@Configuration
public class SessionFactoryConfiguration {

    // mybatis-config.xml配置文件的路径
    @Value("${mybatis_config_file}")
    private String mybatisConfigFilePath;

    // mybatis mapper文件所在路径
    @Value("${mapper_path}")
    private String mapperPath;

    // 实体类所在的package
    @Value("${entity_package}")
    private String entityPackage;

    @Autowired
    @Qualifier("dataSource")
    private DataSource dataSource;

    @Bean(name="sqlSessionFactory")
    public SqlSessionFactoryBean createSqlSessionFactoryBean() throws IOException {
 SqlSessionFactoryBean sqlSessionFactoryBean = new SqlSessionFactoryBean();
 // 扫描mybatis配置文件
 sqlSessionFactoryBean.setConfigLocation(new ClassPathResource(mybatisConfigFilePath));
 // 扫描相关mapper文件
 PathMatchingResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();
 String packageSearchPath = PathMatchingResourcePatternResolver.CLASSPATH_ALL_URL_PREFIX + mapperPath;
 sqlSessionFactoryBean.setMapperLocations(resolver.getResources(packageSearchPath));
 // 调用dataSource
 sqlSessionFactoryBean.setDataSource(dataSource);
 // 映射实体类
 sqlSessionFactoryBean.setTypeAliasesPackage(entityPackage);
 return sqlSessionFactoryBean;
    }
}

pom.xml

   
     org.springframework.boot
     spring-boot-starter-web
 
 
     org.mybatis.spring.boot
     mybatis-spring-boot-starter
     2.0.1
 
 
     org.springframework.boot
     spring-boot-starter-test
     test
 
 
     mysql
     mysql-connector-java
 
 
 
     com.mchange
     c3p0
     0.9.5.2
 
 
转载请注明:文章转载自 www.mshxw.com
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号