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

Spring操作JdbcTemplate數(shù)據(jù)庫的方法學(xué)習(xí)

 更新時(shí)間:2022年05月30日 10:39:57   作者:把蘋果咬哭的測試筆記  
這篇文章主要為大家介紹了Spring操作JdbcTemplate數(shù)據(jù)庫方法學(xué)習(xí),有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪

Spring操作JdbcTemplate

spring 對 jdbc 做了封裝,就是 JdbcTemplate,可以讓操作數(shù)據(jù)庫更加方便。

一、準(zhǔn)備工作

1. 引入依賴

在之前的基礎(chǔ)上,再引入這些依賴。

2. 配置文件中配置數(shù)據(jù)庫連接池

外部文件 jdbc.properties:

prop.driverClass=com.mysql.jdbc.Driver
prop.url=jdbc:mysql://localhost:3306/userDb
prop.username=root
prop.password=123456

配置文件引入:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:aop="http://www.springframework.org/schema/aop"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
                           http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
                           http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd">
    <context:component-scan base-package="com.pingguo.spring5"></context:component-scan>
    <!--引入外部屬性文件-->
    <context:property-placeholder location="classpath:jdbc.properties"/>
    <!--配置連接池-->
    <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">
        <property name="driverClassName" value="${prop.driverClass}"></property>
        <property name="url" value="${prop.url}"></property>
        <property name="username" value="${prop.username}"></property>
        <property name="password" value="${prop.password}"></property>
    </bean>
</beans>

3. 配置 JdbcTemplate 對象

注入 DataSource。

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

4. dao 中注入 JdbcTemplate 對象

創(chuàng)建 dao,在里面注入 JdbcTemplate 。

@Repository
public class BookDaoImpl implements BookDao {
    // 注入 jdbcTemplate
    @Autowired
    private JdbcTemplate jdbcTemplate;
}

創(chuàng)建 service 類,在里面注入 dao。

@Service
public class BookService {
    // 注入dao
    @Autowired
    private BookDao bookDao;
}

二、操作數(shù)據(jù)庫

以添加為例。

建一個(gè)很簡單的表,里面有 3 個(gè)字段。

1. 創(chuàng)建對應(yīng)實(shí)體類

創(chuàng)建數(shù)據(jù)表對應(yīng)的實(shí)體類,并且生成 3 個(gè)屬性的 get、set方法。

public class Book {
    private String userId;
    private String username;
    private String userStatus;
    public String getUserId() {
        return userId;
    }
    public void setUserId(String userId) {
        this.userId = userId;
    }
... ...

2. 編寫service 和 dao

service

@Service
public class BookService {
    // 注入dao
    @Autowired
    private BookDao bookDao;
    public void addBook(Book book) {
        bookDao.add(book);
    }
}

dao 的實(shí)現(xiàn)類。

@Repository
public class BookDaoImpl implements BookDao {
    // 注入 jdbcTemplate
    @Autowired
    private JdbcTemplate jdbcTemplate;
    @Override
    public void add(Book book) {
        String sql = "insert into t_book values (?, ?, ?)";
        int result = jdbcTemplate.update(sql, book.getUserId(), book.getUsername(), book.getUserStatus());
        System.out.println(result);
    }
}

使用 jdbcTemplate.update() 方法進(jìn)行添加,第一個(gè)參數(shù)是 sql,第二個(gè)不定長參數(shù),成功則返回 1。

3. 編寫測試

public class TestBook {
    @Test
    public void testJdbc() {
        ApplicationContext context =
                new ClassPathXmlApplicationContext("bean1.xml");
        BookService bookService = context.getBean("bookService", BookService.class);
        Book book = new Book();
        book.setUserId("1");
        book.setUsername("ceshi");
        book.setUserStatus("3");
        bookService.addBook(book);
    }
}

運(yùn)行結(jié)果:

八月 05, 2021 10:35:15 下午 com.alibaba.druid.pool.DruidDataSource info
信息: {dataSource-1} inited
1
Process finished with exit code 0

查看數(shù)據(jù)表

成功添加。

刪除跟修改操作跟上面類似了,不再演示。

以上就是Spring操作JdbcTemplate數(shù)據(jù)庫方法學(xué)習(xí)的詳細(xì)內(nèi)容,更多關(guān)于Spring操作JdbcTemplate的資料請關(guān)注腳本之家其它相關(guān)文章!

相關(guān)文章

  • 詳解Java多線程與并發(fā)

    詳解Java多線程與并發(fā)

    多線程是一個(gè)進(jìn)程在執(zhí)行過程中產(chǎn)生多個(gè)更小的程序單元,這些更小的單元稱為線程,這些線程可以同時(shí)存在,同時(shí)運(yùn)行,一個(gè)進(jìn)程可能包含多個(gè)同時(shí)執(zhí)行的線程。多線程是實(shí)現(xiàn)并發(fā)機(jī)制的一種有效手段。進(jìn)程和線程一樣,都是實(shí)現(xiàn)并發(fā)的一個(gè)基本單位。
    2021-06-06
  • 淺談Spring Boot 屬性配置和自定義屬性配置

    淺談Spring Boot 屬性配置和自定義屬性配置

    這篇文章主要介紹了淺談Spring Boot 屬性配置和自定義屬性配置,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧
    2018-03-03
  • RabbitMQ消息隊(duì)列之持久化機(jī)制詳解

    RabbitMQ消息隊(duì)列之持久化機(jī)制詳解

    這篇文章主要介紹了RabbitMQ消息隊(duì)列之持久化機(jī)制詳解,持久化,即將原本存在于內(nèi)存中的數(shù)據(jù)寫入到磁盤上永久保存數(shù)據(jù),防止服務(wù)宕機(jī)時(shí)內(nèi)存數(shù)據(jù)的丟失,Rabbitmq 的持久化分為隊(duì)列持久化、消息持久化和交換器持久化,需要的朋友可以參考下
    2023-08-08
  • 修改request的parameter的幾種方式總結(jié)

    修改request的parameter的幾種方式總結(jié)

    這篇文章主要介紹了修改request的parameter的幾種方式,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2021-08-08
  • Spring Boot加密配置文件方法介紹

    Spring Boot加密配置文件方法介紹

    這篇文章主要介紹了SpringBoot加密配置文件,近期在對開發(fā)框架安全策略方面進(jìn)行升級優(yōu)化,提供一些通用場景的解決方案,本文針對配置文件加密進(jìn)行簡單的分享
    2023-01-01
  • JPA @Basic單表查詢?nèi)绾螌?shí)現(xiàn)大字段懶加載

    JPA @Basic單表查詢?nèi)绾螌?shí)現(xiàn)大字段懶加載

    這篇文章主要介紹了JPA @Basic單表查詢?nèi)绾螌?shí)現(xiàn)大字段懶加載的操作,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2021-08-08
  • Spring?Security?中多個(gè)身份驗(yàn)證的示例代碼

    Spring?Security?中多個(gè)身份驗(yàn)證的示例代碼

    這篇文章主要介紹了Spring?Security?中多個(gè)身份驗(yàn)證的示例代碼,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2023-09-09
  • Java8 使用流抽取List<T>集合中T的某個(gè)屬性操作

    Java8 使用流抽取List<T>集合中T的某個(gè)屬性操作

    這篇文章主要介紹了Java8 使用流抽取List<T>集合中T的某個(gè)屬性操作,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2021-02-02
  • Java中的Spring循環(huán)依賴詳情

    Java中的Spring循環(huán)依賴詳情

    這篇文章主要介紹了Java中的Spring循環(huán)依賴詳情,文章基于Java的相關(guān)資料展開詳細(xì)介紹,具有一定的參考價(jià)值,需要的小伙伴可以參考一下
    2022-04-04
  • IntelliJ?IDEA教程之clean或者install?Maven項(xiàng)目的操作方法

    IntelliJ?IDEA教程之clean或者install?Maven項(xiàng)目的操作方法

    這篇文章主要介紹了IntelliJ?IDEA教程之clean或者install?Maven項(xiàng)目的操作方法,本文分步驟給大家介紹兩種方式講解如何調(diào)試出窗口,需要的朋友可以參考下
    2023-04-04

最新評論

黔东| 潮安县| 舒兰市| 洛南县| 青铜峡市| 太原市| 黄山市| 黔江区| 安义县| 应用必备| 察隅县| 兴安县| 庆城县| 丰县| 海原县| 娱乐| 武平县| 阿图什市| 金山区| 稷山县| 枝江市| 景东| 时尚| 广平县| 周至县| 九龙城区| 伊通| 琼海市| 许昌市| 宁晋县| 金塔县| 清丰县| 平凉市| 自治县| 大田县| 缙云县| 辛集市| 巧家县| 自治县| 潼关县| 五台县|