第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

Spring 集成 JdbcTemplate

1. 前言

各位同學(xué)大家好,我們又見面了。在前面的一些小節(jié)中,我們已經(jīng)學(xué)習(xí)了 Spring 的基本使用,包括概念解釋,IOC 容器使用,AOP 面向切面編程。那么本小節(jié),我們要學(xué)習(xí)哪些新知識呢?是有關(guān) Spring 對于事務(wù)的操作相關(guān)知識。

慕課解釋
在我們之前的案例中,我們實(shí)現(xiàn)了對數(shù)據(jù)庫的數(shù)據(jù)做增刪改查,但是使用的類是 QueryRunner ,它是屬于 DbUtils 中的一個操作數(shù)據(jù)庫的工具類,是 Jdbc 技術(shù)中的范圍。而 Jdbc 屬于最底層的接口,是一個規(guī)范,定義了 Java 操作數(shù)據(jù)庫必須實(shí)現(xiàn)的硬性要求。它雖然能滿足項(xiàng)目的開發(fā)需求,但是它有以下弊端:

  1. Jdbc 的工具類需要手動獲取連接,釋放資源,頻繁地開啟和關(guān)閉連接,造成資源的浪費(fèi);
  2. 代碼和 SQL 語句硬性編碼在一起,耦合性太高,不利于維護(hù)。因?yàn)?SQL 語句的修改,涉及到 Java 代碼的修改;
  3. 數(shù)據(jù)操作繁瑣,參數(shù)的位置和類型匹配容錯率極低;
  4. 最重要的,使用 Jdbc 的過程中,事務(wù)的控制,需要自己手動的回滾和提交,開發(fā)成本高。

那么如何優(yōu)雅地解決呢?有請我們的主角登場…JdbcTemplate ,也是本小節(jié)我們重點(diǎn)學(xué)習(xí)的內(nèi)容。

2. 實(shí)例演示

2.1 JdbcTemplate 使用介紹

概念解釋
JdbcTemplate 是 Spring 框架提供的一個類,對 Spring Jdbc 接口做了實(shí)現(xiàn),負(fù)責(zé)處理資源的建立和釋放,對于開發(fā)人員來說只需要提供 SQL 語句,使 JDBC 更加易于使用。
使用方式

  1. 引入使用的依賴 jar 包文件: 既然是個封裝的框架類,那么一定也需要引入框架中的依賴 jar 包;
  2. 在 Spring 框架的配置文件中配置 JdbcTemplate 類: 使用的類實(shí)例化的動作都交給 Spring ,它當(dāng)然也不例外;
  3. 在 Dao 的實(shí)現(xiàn)類中,引入 JdbTemplate 的類屬性: 既然作用是操作數(shù)據(jù)的工具類,它作用就是替換掉了之前使用的 Jdbc 中的 QueryRunner 。

2.2 工程搭建

1. 創(chuàng)建工程
圖片描述
2. 引入依賴

<dependencies>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>5.0.2.RELEASE</version>
        </dependency>
		<!-- Spring jdbc 使用的依賴-->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-jdbc</artifactId>
            <version>5.0.2.RELEASE</version>
        </dependency>

        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-tx</artifactId>
            <version>5.0.2.RELEASE</version>
        </dependency>

        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>5.1.6</version>
        </dependency>
    </dependencies>

3. 準(zhǔn)備代碼
實(shí)體類代碼

/**
 * 賬戶的實(shí)體類
 */
public class Account implements Serializable {
    //數(shù)據(jù)id
    private Integer id;
    //賬號編碼
    private String accountNum;
    //賬號金額
    private Float money;
}

接口代碼

/**
 * 賬戶的持久層接口
 */
public interface IAccountDao {

    /**
     * 根據(jù)Id查詢賬戶
     * @param accountId
     * @return
     */
    Account findAccountById(Integer accountId);

    /**
     * 保存賬戶
     * @param account
     */
    void saveAccount(Account account);

    /**
     * 更新賬戶
     * @param account
     */
    void updateAccount(Account account);


}

實(shí)現(xiàn)類代碼

/**
 * 賬戶的持久層實(shí)現(xiàn)類
 */
@Repository
public class AccountDaoImpl implements IAccountDao {
    //jdbc模板類屬性
    @Autowired
    private JdbcTemplate jdbcTemplate;

    //根據(jù)id查找
    public Account findAccountById(Integer accountId) {
        List<Account> accounts = jdbcTemplate.query("select * from account where id = ?",new BeanPropertyRowMapper<Account>(Account.class),accountId);
        return accounts.isEmpty()?null:accounts.get(0);
    }

    public void saveAccount(Account account) {
        jdbcTemplate.update("insert into account  values(?,?,?)",
                account.getId(),account.getAccountNum(),account.getMoney());
    }

    public void updateAccount(Account account) {
        jdbcTemplate.update("update account set accountnum=?,money=? where id=?",account.getAccountNum(),account.getMoney(),account.getId());
    }
}

4. 配置文件

	<!--配置JdbcTemplate-->
    <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
        <property name="dataSource" ref="dataSource"></property>
    </bean>

    <!-- 配置數(shù)據(jù)源-->
    <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
        <property name="driverClassName" value="com.mysql.jdbc.Driver"></property>
        <property name="url" value="jdbc:mysql:///transmoney"></property>
        <property name="username" value="root"></property>
        <property name="password" value="root"></property>
    </bean>
	<!--路徑掃描-->
    <context:component-scan base-package="com.offcn.dao"></context:component-scan>

5. 測試代碼

/**
 * 測試數(shù)據(jù)的查詢和修改
 */
public class JdbcTemplateTest {

    public static void main(String[] args) {
        //1.獲取容器
        ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");
        //2.獲取對象
        IAccountDao accountDao = ac.getBean(IAccountDao.class);
        Account account = accountDao.findAccountById(1);
        System.out.println(account);
        account.setMoney(2000f);
        accountDao.updateAccount(account);
        System.out.println("賬號更改成功");
    }
}

6. 測試結(jié)果:
圖片描述

3. 總結(jié)

好了,大家。通過上面的案例,我們已經(jīng)看到了通過 JdbcTemplate 操作數(shù)據(jù)庫的效果了。我們來做一個小的總結(jié)哈。

  1. 使用 JdbcTemplate 替換掉原來 Jdbc 的實(shí)現(xiàn)方式,代碼更為整潔;
  2. Spring 的配置文件中,只配置 JdbcTemplate 模板類即可,配置更為簡單;
  3. 數(shù)據(jù)庫連接的獲取與釋放,無需手動調(diào)用方法,因?yàn)橛?JdbcTemplate 幫助我們實(shí)現(xiàn)了,我們只需關(guān)注業(yè)務(wù)實(shí)現(xiàn),無需考慮資源的使用,提升開發(fā)效率。

當(dāng)然:對于事務(wù)的控制,本小節(jié)并沒有體現(xiàn),只是最為基礎(chǔ)的使用。在下個小節(jié)的講解中,我會帶你體驗(yàn) Spring 對事務(wù)的控制。本小節(jié)先到這里,謝謝大家的關(guān)注。

沒有比人更高的山… 沒有比腳更長的路… 繼續(xù)加油哦!