天天看點

Spring中的JdbcTemplateJdbcTemplate快速入門JdbcTemplate實作完整的CRUD

JdbcTemplate快速入門

JdbcTemplate是Spring的一款用于簡化Dao代碼的工具包,它底層封裝了JDBC技術,特點是簡單、友善

Spring中的JdbcTemplateJdbcTemplate快速入門JdbcTemplate實作完整的CRUD

核心對象

public class JdbcTemplateTest {
    public static void main(String[] args) {
        //建立資料源,即内置的資料庫連接配接池
        DriverManagerDataSource dataSource = new DriverManagerDataSource();
        dataSource.setDriverClassName("com.mysql.jdbc.Driver");
        dataSource.setPassword("root");
        dataSource.setUsername("root");
        dataSource.setUrl("jdbc:mysql://localhost:3306/spring2");
        //建立jdbcTemplate對象,将資料源交給jdbcTemplate
        JdbcTemplate jdbcTemplate = new JdbcTemplate(dataSource);
        //通過jdbcTemplate操作sql語句
        jdbcTemplate.update("insert into account values(null,'張三',2000)");
    }
}
           
Spring中的JdbcTemplateJdbcTemplate快速入門JdbcTemplate實作完整的CRUD

Spring的IOC管理JdbcTemplate

public class JdbcTemplateTest {
    public static void main(String[] args) {
        建立資料源,即内置的資料庫連接配接池
        //DriverManagerDataSource dataSource = new DriverManagerDataSource();
        //dataSource.setDriverClassName("com.mysql.jdbc.Driver");
        //dataSource.setPassword("root");
        //dataSource.setUsername("root");
        //dataSource.setUrl("jdbc:mysql://localhost:3306/spring2");
        建立jdbcTemplate對象,将資料源交給jdbcTemplate
        //JdbcTemplate jdbcTemplate = new JdbcTemplate(dataSource);

        //通過spring的IOC容器來擷取JdbcTemplate對象
        ApplicationContext ac = new ClassPathXmlApplicationContext("classpath:applicationContext.xml");
        JdbcTemplate jdbcTemplate = ac.getBean(JdbcTemplate.class);
        //通過jdbcTemplate操作sql語句
        jdbcTemplate.update("insert into account values(null,'李四',2000)");
    }
}
           
Spring中的JdbcTemplateJdbcTemplate快速入門JdbcTemplate實作完整的CRUD
Spring中的JdbcTemplateJdbcTemplate快速入門JdbcTemplate實作完整的CRUD

JdbcTemplate實作完整的CRUD

核心方法

int update(); 執行增、删、改語句

List<T> query(); 查詢多個
T queryForObject(); 查詢一個
     RowMapper<>(); ORM映射接口
	 new BeanPropertyRowMapper<>(); 實作ORM映射封裝子類
           

代碼實作

public class JdbcTemplateTest {
    public static void main(String[] args) {
        建立資料源,即内置的資料庫連接配接池
        //DriverManagerDataSource dataSource = new DriverManagerDataSource();
        //dataSource.setDriverClassName("com.mysql.jdbc.Driver");
        //dataSource.setPassword("root");
        //dataSource.setUsername("root");
        //dataSource.setUrl("jdbc:mysql://localhost:3306/spring2");
        建立jdbcTemplate對象,将資料源交給jdbcTemplate
        //JdbcTemplate jdbcTemplate = new JdbcTemplate(dataSource);

        //通過spring的IOC容器來擷取JdbcTemplate對象
        ApplicationContext ac = new ClassPathXmlApplicationContext("classpath:applicationContext.xml");
        JdbcTemplate jdbcTemplate = ac.getBean(JdbcTemplate.class);
        //通過jdbcTemplate操作sql語句
        //jdbcTemplate.update("insert into account values(null,'李四',2000)");
        //JdbcTemplate實作完整的CRUD
        //需求:新增一個使用者
        //jdbcTemplate.update("insert into account value(null,?,?)", "亞索", "2500");
        //根據id修改使用者
        jdbcTemplate.update("update account set money=money-? where id=?", 500, 6);
        //根據使用者id删除一個使用者
        //jdbcTemplate.update("delete from account where id =?", 6);

        //查詢表中所有的使用者
        //編寫sql語句
        String sql = "select * from account";
        List<Account> accountList = jdbcTemplate.query(sql, new BeanPropertyRowMapper<>(Account.class));
        for (Account account : accountList) {
            System.out.println(account);
        //Account{id=1, name='小明', money=1000.0}
        //Account{id=2, name='小花', money=1000.0}
        //Account{id=3, name='小王', money=1000.0}
        //Account{id=4, name='張三', money=2000.0}
        //Account{id=5, name='李四', money=2000.0}
        }

        //查詢一條資料
        //編寫sql語句
        String sql1 = "select * from account where id = ?";
        //執行sql語句
        Account account = jdbcTemplate.queryForObject(sql1, new BeanPropertyRowMapper<>(Account.class), 4);
        System.out.println(account);
        //Account{id=4, name='張三', money=2000.0}
    }
}

           

JdbcTemplate整合資料源

pom.xml:

<!--c3p0-->
<dependency>
    <groupId>c3p0</groupId>
    <artifactId>c3p0</artifactId>
    <version>0.9.1.2</version>
</dependency>
<!-- 德魯伊連接配接池-->
<dependency>
    <groupId>com.alibaba</groupId>
    <artifactId>druid</artifactId>
    <version>1.0.9</version>
</dependency>

<!--dbcp連接配接池-->
<dependency>
    <groupId>commons-dbcp</groupId>
    <artifactId>commons-dbcp</artifactId>
    <version>1.4</version>
</dependency>
           

applicationContext.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       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">

    <!--spring的内置資料源-->
    <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
        <!-- 依賴注入資料源的連接配接屬性-->
        <property name="driverClassName" value="com.mysql.jdbc.Driver"></property>
        <property name="url" value="jdbc:mysql://localhost:3306/spring2"></property>
        <property name="password" value="root"></property>
        <property name="username" value="root"></property>
    </bean>

    <!--c3p0連接配接池-->
    <bean id="c3p0DataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
        <property name="password" value="root"></property>
        <property name="driverClass" value="com.mysql.jdbc.Driver"></property>
        <property name="jdbcUrl" value="jdbc:mysql://localhost:3306/spring2"></property>
        <property name="user" value="root"></property>
    </bean>

    <!--德魯伊-->
    <bean id="druidDataSource" class="com.alibaba.druid.pool.DruidDataSource">
        <property name="password" value="root"></property>
        <property name="username" value="root"></property>
        <property name="url" value="jdbc:mysql://localhost:3306/spring2"></property>
        <property name="driverClassName" value="com.mysql.jdbc.Driver"></property>
    </bean>


    <!--dbcp連接配接池-->
    <bean id="dbcpDataSource" class="org.apache.commons.dbcp.BasicDataSource">
        <property name="driverClassName" value="com.mysql.jdbc.Driver"></property>
        <property name="url" value="jdbc:mysql://localhost:3306/spring2"></property>
        <property name="username" value="root"></property>
        <property name="password" value="root"></property>
    </bean>

    <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
        <!-- 注入spring内置的資料源-->
        <!--<property name="dataSource" ref="dataSource"></property>-->
        
        <!-- 注入c3p0的資料源-->
       <!-- <property name="dataSource" ref="c3p0DataSource"></property>-->

        <!-- 注入德魯伊連接配接池-->
        <!--<property name="dataSource" ref="druidDataSource"></property>-->

        <!-- 注入dbcp連接配接池-->
        <property name="dataSource" ref="dbcpDataSource"></property>
    </bean>
</beans>