• Spring5 学习笔记 五 、JdbcTemplate


    1、概念和准备

    <1> 什么是 JdbcTemplate

    Spring 框架对 JDBC 进行封装,使用 JdbcTemplate 方便实现对数据库操作

    <2> 准备工作

    【1】引入相关 jar 包

    在这里插入图片描述
    在这里插入图片描述
    在这里插入图片描述
    在这里插入图片描述

    在这里插入图片描述
    在这里插入图片描述
    在这里插入图片描述

    【2】新建数据库

    在这里插入图片描述

    在这里插入图片描述
    在这里插入图片描述

    在这里插入图片描述

    【3】在 spring 配置文件配置数据库连接池

    bean.xml:

    
        <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource"
              destroy-method="close">
            <property name="url" value="jdbc:mysql:///user_db" />
            <property name="username" value="admin" />
            <property name="password" value="123456" />
            <property name="driverClassName" value="com.mysql.jdbc.Driver" />
        bean>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8

    【4】配置 JdbcTemplate 对象,注入 DataSource

        
        <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
            
            <property name="dataSource" ref="dataSource"/>
        bean>
    
    • 1
    • 2
    • 3
    • 4
    • 5

    在这里插入图片描述

    【5】创建 service 类,创建 dao 类,在 dao 注入 jdbcTemplate 对象

    bean.xml:

     
    <context:component-scan base-package="com.company.JdbcTemplate">context:component-scan>   
    
    • 1
    • 2

    在这里插入图片描述

    Service:BookService

    package com.company.JdbcTemplate.service;
    
    import com.company.JdbcTemplate.dao.BookDao;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.stereotype.Service;
    
    @Service
    public class BookService {
        //注入 dao
        @Autowired
        private BookDao bookDao;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12

    Dao:BookDaoImpl

    package com.company.JdbcTemplate.dao;
    
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.jdbc.core.JdbcTemplate;
    import org.springframework.stereotype.Repository;
    
    @Repository
    public class BookDaoImpl implements BookDao {
        //注入 JdbcTemplate
        @Autowired
        private JdbcTemplate jdbcTemplate;
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13

    2、JdbcTemplate 操作数据库

    <1> 操作数据库进行(添加)

    在这里插入图片描述

    【1】对应数据库创建实体类

    package com.company.JdbcTemplate.entity;
    
    public class Book {
        private String userId;
        private String userName;
        private String userStatus;
    
        public void setUserId(String userId) {
            this.userId = userId;
        }
    
        public void setUserName(String userName) {
            this.userName = userName;
        }
    
        public void setUserStatus(String userStatus) {
            this.userStatus = userStatus;
        }
    
        public String getUserId() {
            return userId;
        }
    
        public String getUserName() {
            return userName;
        }
    
        public String getUserStatus() {
            return userStatus;
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31

    【2】编写 service 和 dao

    (1)在 dao 进行数据库添加操作
    dao :

    service:

    package com.company.JdbcTemplate.service;
    
    import com.company.JdbcTemplate.dao.BookDao;
    import com.company.JdbcTemplate.entity.Book;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.stereotype.Service;
    
    @Service
    public class BookService {
        //注入 dao
        @Autowired
        private BookDao bookDao;
    
        //添加的方法
        public void addBook(Book book){
            bookDao.add(book);
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18

    【3】调用 JdbcTemplate 对象里面 update 方法实现添加操作

    package com.company.JdbcTemplate.dao;
    
    
    import com.company.JdbcTemplate.entity.Book;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.jdbc.core.JdbcTemplate;
    import org.springframework.stereotype.Repository;
    
    @Repository
    public class BookDaoImpl implements BookDao {
        //注入 JdbcTemplate
        @Autowired
        private JdbcTemplate jdbcTemplate;
    
        @Override
        public void add(Book book) {
            //1 创建 sql 语句
            String sql = "insert into t_book values(?,?,?)";
            //2 调用方法实现
            Object[] args = {book.getUserId(), book.getUserName(),
                    book.getUserStatus()};
            int update = jdbcTemplate.update(sql, args);
            System.out.println(update);
    
        }
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27

    【4】测试

    package com.company.JdbcTemplate;
    
    import com.company.JdbcTemplate.entity.Book;
    import com.company.JdbcTemplate.service.BookService;
    import org.junit.Test;
    import org.springframework.context.ApplicationContext;
    import org.springframework.context.support.ClassPathXmlApplicationContext;
    
    public class testDao {
        @Test
        public void testJdbcTemplate() {
            ApplicationContext context =
                    new ClassPathXmlApplicationContext("bean.xml");
            BookService bookService = context.getBean("bookService",
                    BookService.class);
            Book book = new Book();
            book.setUserId("1");
            book.setUserName("java");
            book.setUserStatus("a");
            bookService.addBook(book);
        }
    
    
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
  • 相关阅读:
    Spark GC日志分析
    一文详解 Okio 输入输出流
    【设计模式】32.结构型模式-组合模式(Composite)
    GBASE 8C——SQL参考6 sql语法(5)
    网络安全笔记-文件包含
    【蓝桥杯-单片机】基础模块:数码管
    BAT大厂Java面试,如何抓住面试重点知识?收割大厂offer
    LeetCode220730_62、位1的个数
    Win11怎么修改关机界面颜色?Win11修改关机界面颜色的方法
    「专升本信息技术」计算机基础知识单选题集(02)
  • 原文地址:https://blog.csdn.net/weixin_46703850/article/details/121441796