配置dbcp連接池

使用MySQL
配置dbcp連接池

依賴有

    compile "org.springframework:spring-context:$springVersion"
    compile "org.springframework:spring-jdbc:$springVersion"
    compile "com.h2database:h2:$h2Version"
    compile "javax.inject:javax.inject:1"
    compile group: 'mysql', name: 'mysql-connector-java', version: '6.0.6'
    compile group: 'org.apache.commons', name: 'commons-dbcp2', version: '2.1.1'

    testCompile "junit:junit-dep:$junitVersion"
    testCompile "org.springframework:spring-test:$springVersion"

邏輯

//這個(gè)是實(shí)現(xiàn)類中的方法  使用spring  jdbcTemplate操作數(shù)據(jù)庫
    private JdbcTemplate jdbcTemplate;

    public PoolRepository(JdbcTemplate jdbcTemplate) {
        this.jdbcTemplate = jdbcTemplate;
    }
    @Override
    public boolean update(Student student) {
        //PrepareStatement
        String sql = "update students set name = ? where id = 1";
//        Map<String, Object> args = new HashMap<>();
//        args.put("name", "f");
        jdbcTemplate.update(sql, "f");
        return false;
    }

使用JavaConfig注入

@Configuration
public class PoolConfig {
    @Bean
    public BasicDataSource dataSource(){
//獲取basicDataSource
        BasicDataSource dataSource = new BasicDataSource();
//高版本的sql驅(qū)動(dòng)需要按以下兩個(gè)寫法配置
        dataSource.setDriverClassName("com.mysql.cj.jdbc.Driver");
//加上useSSL和Timezone
        dataSource.setUrl("jdbc:mysql://127.0.0.1:3306/blogs?useUnicode=true&characterEncoding=utf-8&useSSL=false&serverTimezone=UTC");
        dataSource.setUsername("root");
        dataSource.setPassword("123456");
        dataSource.setInitialSize(5);
        dataSource.setMaxIdle(10);
        return dataSource;
    }
    @Bean
    public JdbcTemplate jdbcTemplate(BasicDataSource dataSource){
//給jdbctemplate注入DataSource
        return new JdbcTemplate(dataSource);
    }
    @Bean
    public IPoolRepository poolRepository(JdbcTemplate jdbcTemplate){
//PoolRepository注入到spring上下文  使用Autowire獲取
        return new PoolRepository(jdbcTemplate);
    }
}

測(cè)試

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = PoolConfig.class)
public class PoolTest {
    @Autowired
    private PoolRepository poolRepository;

    @Test
    public void test(){
        Student student = new Student();
        student.setAge(1);
        student.setName("g");
        student.setScore(3);
        poolRepository.add(student);
    }
    @Test
    public void testUpdate(){
        Student student = new Student();
        student.setName("g");
        poolRepository.update(student);
    }
}

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • Spring Cloud為開發(fā)人員提供了快速構(gòu)建分布式系統(tǒng)中一些常見模式的工具(例如配置管理,服務(wù)發(fā)現(xiàn),斷路器,智...
    卡卡羅2017閱讀 136,568評(píng)論 19 139
  • Spring Boot 參考指南 介紹 轉(zhuǎn)載自:https://www.gitbook.com/book/qbgb...
    毛宇鵬閱讀 47,273評(píng)論 6 342
  • 本文包括傳統(tǒng)JDBC的缺點(diǎn)連接池原理自定義連接池開源數(shù)據(jù)庫連接池DBCP連接池C3P0連接池Tomcat內(nèi)置連接池...
    廖少少閱讀 16,940評(píng)論 0 37
  • 黃鳥的左翅上 曾有過閃電與印紋 羽翼拍打,抖落滿天星辰 也曾在夕嵐升起時(shí) 看到眾神的前生 夜里放下桂冠 做牧星人,...
    南風(fēng)認(rèn)識(shí)我閱讀 452評(píng)論 0 3
  • CNBLUEone閱讀 250評(píng)論 0 0

友情鏈接更多精彩內(nèi)容