第22天 连接池和JdbcTemplate

连接池

概念

  • 思想:程序启动的时候先创建一个容器,容器初始化一些连接对象,用的时候从容器中取,用完以后归还到容器中。
  • 优点:效率高,节省资源

C3P0

使用步骤

  • 导包: 2个jar包
  • 拷贝配置文件:名字和目录的位置都不能改变 (放在src目录下)
  • C3P0实现连接池的工具类ComboPooledDataSource(联合池数据源,new出来的)
public class C3P0Utils {
    private C3P0Utils(){}
   private static ComboPooledDataSource cpd;
    static {
        cpd  = new ComboPooledDataSource();
    }
    public static DataSource getDataSource(){
        return  cpd;
    }
}

Druid(德鲁伊,阿里巴巴旗下的产品,高效)

使用步骤

  • 导包,复制配置文件,因为是通过类加载器加载的,所以文件的位置没有要求,一般也放在src目录下
  • 建立连接池(DruidDataSourceFactory.createDatasource(properties配置文件),德鲁伊连接池工厂创建连接池,池子里放配置文件)
public class DruidUtils {
    private static DataSource dataSource;
    static{
        try {
            Properties properties = new Properties();
            properties.load(DruidUtils.class.getClassLoader().getResourceAsStream("druid.properties"));
            dataSource = DruidDataSourceFactory.createDataSource(properties);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    public static DataSource getDataSource(){
        return dataSource;
    }
}

JdbcTemplate

  • 个人理解:template,模板的意思,jdbc接口的实现类(模板类,简化了jdbc的操作)
  • 创建:new JdbcTemplate(dataSource) /DruidUtils.getDataSource

查询

  • queryformap:sql 语句可以有?,后面跟多个参数(也可以不用带?的SQL语句)
    查询的结果只能是一条map封装的记录
  • queryforlist: 语句也可以有?后面跟多个参数,返回的是封装了多个map的list集 合
  • query: Template.query(sql,new BeanPropertyRowMapper<>(Person.class)),返回的的 是list
    记忆:查询实例的时候,jdbc的模板(盾牌)不怕人马(罗马)
  • queryforObject 聚合函数作为查询结果,返回数字类型:template.queryforobject(sql,long.class)
queryformap
JdbcTemplate template = new JdbcTemplate(DruidUtils.getDataSource());
@Test
public void func(){
    Map<String, Object> map = template.queryForMap("select * from student where id=1");
    for (Map.Entry<String, Object> entry : map.entrySet()) {
        System.out.print(entry.getKey()+"--"+entry.getValue()+"\t\t");
    }
}

queryforlist
@Test
public void func1(){
    List<Map<String, Object>> lists = template.queryForList("select * from student ");
    for (Map<String, Object> maps : lists) {
        for (Map.Entry<String, Object> entry : maps.entrySet()) {
            System.out.print(entry.getKey()+"--"+entry.getValue()+"\t\t");
        }
        System.out.println();
    }
}

@Test
public void func2(){
    List<Person> persons = template.query("select * from student", new BeanPropertyRowMapper<>(Person.class));
    for (Person person : persons) {
        System.out.println(person);
    }
}

@Test
public void func3(){
    Long l = template.queryForObject("select avg(id) from student", Long.class);
    System.out.println(l);
}

增删改

@Test
public void func4(){
    int num1 = template.update("insert into student values (null,?,?)","nana",23);
    int num2 = template.update("update student set name='sara',age=18 where id=3");
    int num3 = template.update("DROP INDEX abc ON student");
    int num4 = template.update("alter table student add index a(name)");
    System.out.println(num1);
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值