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

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

 更新時間: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ù)庫

以添加為例。

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

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

創(chuàng)建數(shù)據(jù)表對應(yīng)的實體類,并且生成 3 個屬性的 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 的實現(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)行添加,第一個參數(shù)是 sql,第二個不定長參數(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ā)

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

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

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

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

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

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

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

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

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

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

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

    Spring?Security?中多個身份驗證的示例代碼

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

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

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

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

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

    IntelliJ?IDEA教程之clean或者install?Maven項目的操作方法

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

最新評論

瓮安县| 安远县| 东阳市| 韶山市| 马尔康县| 铜山县| 长海县| 类乌齐县| 辽中县| 通山县| 镇安县| 乌兰察布市| 公安县| 盐津县| 奇台县| 调兵山市| 临沂市| 龙井市| 盐源县| 花垣县| 化州市| 深圳市| 寿宁县| 三明市| 仙游县| 图们市| 广西| 读书| 定西市| 墨脱县| 鄄城县| 桃园县| 尉犁县| 六枝特区| 遂昌县| 赤峰市| 额尔古纳市| 辛集市| 两当县| 民县| 满洲里市|