最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Java?超詳細講解核心類Spring?JdbcTemplate

 更新時間:2022年04月06日 11:36:50   作者:求不脫發(fā)  
JdbcTemplate?JdbcTemplate是Spring?JDBC核心包(core)中的核心類,它可以通過配置文件、注解、Java?配置類等形式獲取數(shù)據(jù)庫的相關(guān)信息,實現(xiàn)了對JDBC開發(fā)過程中的驅(qū)動加載、連接的開啟和關(guān)閉、SQL語句的創(chuàng)建與執(zhí)行、異常處理、事務(wù)處理、數(shù)據(jù)類型轉(zhuǎn)換等操作的封裝

JdbcTemplate概述

它是spring框架中提供的一個對象,是對原始繁瑣的Jdbc API對象的簡單封裝。spring框架為我們提供了很多的操作 模板類。例如:操作關(guān)系型數(shù)據(jù)的JdbcTemplate和HibernateTemplate,操作nosql數(shù)據(jù)庫的RedisTemplate,操 作消息隊列的JmsTemplate等等。

JdbcTemplate開發(fā)步驟

① 導(dǎo)入spring-jdbc和spring-tx坐標

② 創(chuàng)建數(shù)據(jù)庫表和實體

③ 創(chuàng)建JdbcTemplate對象

④ 執(zhí)行數(shù)據(jù)庫操作

JdbcTemplate快速入門

① 在pom.xml文件中導(dǎo)入spring-jdbc和spring-tx坐標

<!--導(dǎo)入spring的jdbc坐標--> 
<dependency>
 <groupId>org.springframework</groupId>
 <artifactId>spring-jdbc</artifactId>
 <version>5.0.5.RELEASE</version>
</dependency>
<!--導(dǎo)入spring的tx坐標--> 
<dependency>
 <groupId>org.springframework</groupId>
 <artifactId>spring-tx</artifactId>
 <version>5.0.5.RELEASE</version>
</dependency>

② 創(chuàng)建數(shù)據(jù)庫表和實體

表名稱account
namevarchar(20)
moneyvarchar(20)

?

public class Account {
    private String name;
    private double money;
    //為方便展示省略get和set方法,具體開發(fā)中不能省。
}

③ 創(chuàng)建JdbcTemplate對象,執(zhí)行數(shù)據(jù)庫操作

//1、創(chuàng)建數(shù)據(jù)源對象
ComboPooledDataSource dataSource = new ComboPooledDataSource();
dataSource.setDriverClass("com.mysql.jdbc.Driver");
dataSource.setJdbcUrl("jdbc:mysql://localhost:3306/test");
dataSource.setUser("root");
dataSource.setPassword("root");
//2、創(chuàng)建JdbcTemplate對象
JdbcTemplate jdbcTemplate = new JdbcTemplate();
//3、設(shè)置數(shù)據(jù)源給JdbcTemplate
jdbcTemplate.setDataSource(dataSource);
//4、執(zhí)行操作
jdbcTemplate.update("insert into account values(?,?)","tom",5000);

Spring產(chǎn)生JdbcTemplate對象

我們可以將JdbcTemplate的創(chuàng)建權(quán)交給Spring,將數(shù)據(jù)源DataSource的創(chuàng)建權(quán)也交給Spring,在Spring容器內(nèi)部將數(shù)據(jù)源DataSource注入到JdbcTemplate模版對象中,applicationContext.xml配置如下:

<!--數(shù)據(jù)源DataSource--> 
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
 <property name="driverClass" value="com.mysql.jdbc.Driver"></property>
 <property name="jdbcUrl" value="jdbc:mysql:///test"></property>
 <property name="user" value="root"></property>
 <property name="password" value="root"></property>
</bean>
<!--JdbcTemplate--> 
<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
 <property name="dataSource" ref="dataSource"></property>
</bean>

 從容器中獲得JdbcTemplate進行添加操作。

@Test
public void testSpringJdbcTemplate() throws PropertyVetoException {
    ApplicationContext applicationContext = new 
        ClassPathXmlApplicationContext("applicationContext.xml");
    JdbcTemplate jdbcTemplate = applicationContext.getBean(JdbcTemplate.class);
    jdbcTemplate.update("insert into account values(?,?)","lucy",5000);
}

JdbcTemplate的常用操作

修改操作

@RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration("classpath:applicationContext.xml")
public class JdbcTemplateCRUDTest { 
    @Autowired
    private JdbcTemplate jdbcTemplate;
    @Test
    //測試修改操作
    public void testUpdate(){
    jdbcTemplate.update("update account set money=? where name=?",1000,"tom");
    }
}

刪除和查詢?nèi)坎僮?/h3>
@Test
public void testDelete(){
    jdbcTemplate.update("delete from account where name=?","tom");
}
@Test
public void testQueryAll(){
    List<Account> accounts = jdbcTemplate.query("select * from account", new 
 BeanPropertyRowMapper<Account>(Account.class));
    for (Account account : accounts) {
        System.out.println(account.getName());
    }
}

查詢單個數(shù)據(jù)操作

@Test
//測試查詢單個對象操作
public void testQueryOne(){
    Account account = jdbcTemplate.queryForObject("select * from account where name=?", new BeanPropertyRowMapper<Account>(Account.class), "tom");
    System.out.println(account.getName());
}
@Test
//測試查詢單個簡單數(shù)據(jù)操作(聚合查詢)
public void testQueryCount(){
    Long aLong = jdbcTemplate.queryForObject("select count(*) from account", Long.class);
    System.out.println(aLong);
}

本章小結(jié)

Spring JdbcTemplate開發(fā)步驟:

① 導(dǎo)入spring-jdbc和spring-tx坐標

② 創(chuàng)建數(shù)據(jù)庫表和實體

③ 創(chuàng)建JdbcTemplate對象

JdbcTemplate jdbcTemplate = new JdbcTemplate();

jdbcTemplate.setDataSource(dataSource);

④ 執(zhí)行數(shù)據(jù)庫操作

更新操作:

dbcTemplate.update(sql,params)

查詢操作:

jdbcTemplate.query(sql,Mapper,params)

jdbcTemplate.queryForObject(sql,Mapper,params)

到此這篇關(guān)于Java 超詳細講解核心類Spring JdbcTemplate的文章就介紹到這了,更多相關(guān)Java Spring JdbcTemplate內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • Python3 列表list合并的4種方法

    Python3 列表list合并的4種方法

    這篇文章主要介紹了Python3 列表list合并的4種方法,需要的朋友可以參考下
    2021-04-04
  • django模型中的字段和model名顯示為中文小技巧分享

    django模型中的字段和model名顯示為中文小技巧分享

    這里給大家分享2個可以讓django模型中的字段和model名顯示為中文的小技巧,非常的簡單實用,給需要的小伙伴參考下。
    2014-11-11
  • 使用PyCharm在Github上保存代碼并在服務(wù)器上運行方式

    使用PyCharm在Github上保存代碼并在服務(wù)器上運行方式

    這篇文章主要介紹了使用PyCharm在Github上保存代碼并在服務(wù)器上運行方式,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教
    2024-02-02
  • Python實現(xiàn)對PPT文件進行截圖操作的方法

    Python實現(xiàn)對PPT文件進行截圖操作的方法

    這篇文章主要介紹了Python實現(xiàn)對PPT文件進行截圖操作的方法,涉及Python操作幻燈片的相關(guān)技巧,非常具有實用價值,需要的朋友可以參考下
    2015-04-04
  • Python3 模塊、包調(diào)用&路徑詳解

    Python3 模塊、包調(diào)用&路徑詳解

    下面小編就為大家?guī)硪黄狿ython3 模塊、包調(diào)用&路徑詳解。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2017-10-10
  • 詳解python使用canvas實現(xiàn)移動并綁定鍵盤

    詳解python使用canvas實現(xiàn)移動并綁定鍵盤

    這篇文章主要為大家介紹了python使用canvas實現(xiàn)移動并綁定鍵盤,具有一定的參考價值,感興趣的小伙伴們可以參考一下,希望能夠給你帶來幫助
    2021-12-12
  • python正向最大匹配分詞和逆向最大匹配分詞的實例

    python正向最大匹配分詞和逆向最大匹配分詞的實例

    今天小編就為大家分享一篇python正向最大匹配分詞和逆向最大匹配分詞的實例,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2018-11-11
  • Python驗證的50個常見正則表達式

    Python驗證的50個常見正則表達式

    這篇文章主要給大家介紹了關(guān)于利用Python驗證的50個常見正則表達式的相關(guān)資料,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2021-03-03
  • python對文檔中元素刪除,替換操作

    python對文檔中元素刪除,替換操作

    這篇文章主要介紹了python對文檔中元素刪除,替換操作,pthon更換文檔中某元素、python改變或者刪除txt文檔中某一列元素,下文具體代碼實現(xiàn)需要的小伙伴可以參考一下
    2022-04-04
  • Python返回數(shù)組/List長度的實例

    Python返回數(shù)組/List長度的實例

    今天小編就為大家分享一篇Python返回數(shù)組/List長度的實例,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2018-06-06

最新評論

建宁县| 仁布县| 慈利县| 岫岩| 玉田县| 资兴市| 浠水县| 商南县| 涞水县| 马公市| 文昌市| 新余市| 翼城县| 宝兴县| 垦利县| 本溪市| 福海县| 泗水县| 阿城市| 镇平县| 达拉特旗| 台北县| 三门峡市| 通化市| 清镇市| 临邑县| 宜兰市| 成武县| 丹寨县| 蛟河市| 天峨县| 兰西县| 平果县| 东城区| 淅川县| 剑川县| 西盟| 余干县| 宁明县| 托里县| 定襄县|