天天看点

JdbcTemplate1       JdbcTemplate

1       JdbcTemplate

l  spring 提供用于操作JDBC工具类,类似:DBUtils。

l  依赖 连接池DataSource (数据源)

1.1  环境搭建

1.1.1   创建表

create database ee19_spring_day02;

use ee19_spring_day02;

create table t_user(

  id int primary key auto_increment,

  username varchar(50),

  password varchar(32)

);

insert into t_user(username,password) values('jack','1234');

insert into t_user(username,password) values('rose','5678');

1.1.2   导入jar包

JdbcTemplate1       JdbcTemplate

1.1.3   javabean

package com.itheima.domain;

publicclass User {

   private Integer id;

   private String username;

   private String password;

1.2  使用api(了解)

public staticvoid main(String[] args) {

        //1 创建数据源(连接池) dbcp

        BasicDataSource dataSource = new BasicDataSource();

        // * 基本4项

        dataSource.setDriverClassName("com.mysql.jdbc.Driver");

        dataSource.setUrl("jdbc:mysql://localhost:3306/ee19_spring_day02");

        dataSource.setUsername("root");

        dataSource.setPassword("1234");

        //2  创建模板

        JdbcTemplate jdbcTemplate = new JdbcTemplate();

        jdbcTemplate.setDataSource(dataSource);

        //3 通过api操作

        jdbcTemplate.update("insert into t_user(username,password) values(?,?);", "tom","998");

    }

1.3  配置DBCP

<!-- 创建数据源 -->

    <bean id="dataSourceId" class="org.apache.commons.dbcp.BasicDataSource">

        <property name="driverClassName" value="com.mysql.jdbc.Driver"></property>

        <property name="url" value="jdbc:mysql://localhost:3306/ee19_spring_day02"></property>

        <property name="username" value="root"></property>

        <property name="password" value="1234"></property>

    </bean>

    <!-- 创建模板 ,需要注入数据源-->

    <bean id="jdbcTemplateId" class="org.springframework.jdbc.core.JdbcTemplate">

        <property name="dataSource" ref="dataSourceId"></property>

    </bean>

    <!-- 配置dao -->

    <bean id="userDaoId" class="com.itheima.c_dbcp.UserDao">

        <property name="jdbcTemplate" ref="jdbcTemplateId"></property>

    </bean>

1.4  配置C3P0

<!-- 创建数据源 c3p0-->

    <bean id="dataSourceId" class="com.mchange.v2.c3p0.ComboPooledDataSource">

        <property name="driverClass" value="com.mysql.jdbc.Driver"></property>

        <property name="jdbcUrl" value="jdbc:mysql://localhost:3306/ee19_spring_day02"></property>

        <property name="user" value="root"></property>

        <property name="password" value="1234"></property>

    </bean>

1.5  使用JdbcDaoSupport

1.5.1   dao层

JdbcTemplate1       JdbcTemplate

1.5.2   spring配置文件

    <!-- 配置dao

        * dao 继承 JdbcDaoSupport,之后只需要注入数据源,底层将自动创建模板

    -->

    <bean id="userDaoId" class="com.itheima.e_jdbcdaosupport.UserDao">

        <property name="dataSource" ref="dataSourceId"></property>

    </bean>

1.5.3   源码分析

JdbcTemplate1       JdbcTemplate

1.6  配置properties

1.6.1   properties文件

jdbc.driverClass=com.mysql.jdbc.Driver

jdbc.jdbcUrl=jdbc:mysql://localhost:3306/ee19_spring_day02

jdbc.user=root

jdbc.password=1234

1.6.2   spring配置

<!-- 加载配置文件

      "classpath:"前缀表示 src下

      在配置文件之后通过  ${key} 获得内容

   -->

   <context:property-placeholder location="classpath:com/itheima/f_properties/jdbcInfo.properties"/>

   <!-- 创建数据源 c3p0-->

   <bean id="dataSourceId" class="com.mchange.v2.c3p0.ComboPooledDataSource">

      <property name="driverClass" value="${jdbc.driverClass}"></property>

      <property name="jdbcUrl" value="${jdbc.jdbcUrl}"></property>

      <property name="user" value="${jdbc.user}"></property>

      <property name="password"  value="${jdbc.password}"></property>

   </bean>

继续阅读