spring使用c3p0数据源

1.导入jar包(c3p0)

 

 

 

2.在spring配置文件中配置数据源和JdbcTemplate的bean

       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">

 

      

      

             

             

             

             

      

      

      

      

             

      

3.在单元测试测试

package com.zhiyou100.kfs.test;

 

import java.util.ArrayList;

import java.util.List;

 

import org.junit.jupiter.api.AfterAll;

import org.junit.jupiter.api.BeforeAll;

import org.junit.jupiter.api.Test;

import org.springframework.context.support.ClassPathXmlApplicationContext;

import org.springframework.jdbc.core.BeanPropertyRowMapper;

import org.springframework.jdbc.core.JdbcTemplate;

import org.springframework.jdbc.core.RowMapper;

 

import com.zhiyou100.kfs.bean.User;

 

class TestJdbcTemplate {

 

       private static JdbcTemplate jdbcTemplate;

       @BeforeAll

       static void setUpBeforeClass() throws Exception {

              jdbcTemplate =(JdbcTemplate)new ClassPathXmlApplicationContext("applicationContext.xml").getBean("jdbcTemplate");

       }

 

       @AfterAll

       static void tearDownAfterClass() throws Exception {

       }

 

       /**

        *   增删改

        */

       @Test

       void testUpdate() {

              String sql="insert into tb_user (uusername) value('张八');";

              jdbcTemplate.update(sql);

       }

       /**

        *   多条添加

        */

       @Test

       void testBatchUpdate() {

              String sql1="insert into tb_user (uusername) value(?);";

              String sql2="insert into tb_user (uusername) value('张十');";

              List list=new ArrayList<>();

              String[] uusername1=new String[] {new String("张九2")};

              String[] uusername2=new String[] {new String("张九3")};

              jdbcTemplate.batchUpdate(sql1, list);

       }

       /**

        *   查询一条记录

        */

       @Test

       void testQueryOne() {

              String sql="select * from tb_user where u_id=?;";

              RowMapper rowMapper=new BeanPropertyRowMapper<>(User.class);

              User user=jdbcTemplate.queryForObject(sql, rowMapper,1);

              System.out.println(user);

       }

       /**

        *   查询多条记录

        */

       @Test

       void testQueryList() {

              String sql="select * from tb_user;";

              RowMapper rowMapper=new BeanPropertyRowMapper<>(User.class);

              List list=jdbcTemplate.query(sql, rowMapper);

              System.out.println(list);

       }

       /**

        *   查询单值(有多少条记录)

        */

       @Test

       void testQuerySingtonColumn() {

              String sql="select count(u_id) c from tb_user;";

              int a=jdbcTemplate.queryForObject(sql, Integer.class);

              System.out.println(a);

       }

}

转载于:https://www.cnblogs.com/kfsrex/p/11494629.html

你可能感兴趣的:(spring使用c3p0数据源)