• 8、JdbcTemplate



    【尚硅谷】SSM框架全套教程-讲师:杨博超

    但行好事,莫问前程

    8、JdbcTemplate

    8.1、简介

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

    8.2、准备工作

    1 依赖坐标

    
    <dependency>
        <groupId>org.springframeworkgroupId>
        <artifactId>spring-contextartifactId>
        <version>5.3.22version>
    dependency>
    
    
    
    <dependency>
        <groupId>org.springframeworkgroupId>
        <artifactId>spring-ormartifactId>
        <version>5.3.22version>
    dependency>
    
    <dependency>
        <groupId>org.springframeworkgroupId>
        <artifactId>spring-testartifactId>
        <version>5.3.22version>
    dependency>
    
    <dependency>
        <groupId>junitgroupId>
        <artifactId>junitartifactId>
        <version>4.12version>
        <scope>testscope>
    dependency>
    
    <dependency>
        <groupId>mysqlgroupId>
        <artifactId>mysql-connector-javaartifactId>
        <version>8.0.29version>
    dependency>
    
    <dependency>
        <groupId>com.alibabagroupId>
        <artifactId>druidartifactId>
        <version>1.2.11version>
    dependency>
    
    • 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
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40

    2 创建jdbc.properties文件

    druid.driverClassName=com.mysql.cj.jdbc.Driver
    druid.url=jdbc:mysql://localhost:3306/ssmbuild?userUnicode=true&characterEncoding=utf8&useSSL=true&serverTimezone=GMT%2B8
    druid.username=root
    druid.password=root
    
    • 1
    • 2
    • 3
    • 4

    3 Spring配置文件

    
    
    
    <context:property-placeholder location="classpath:jdbc.properties"/>
    
    
    <bean id="druidDataSource" class="com.alibaba.druid.pool.DruidDataSource">
        <property name="driverClassName" value="${druid.driverClassName}">property>
        <property name="url" value="${druid.url}">property>
        <property name="username" value="${druid.username}">property>
        <property name="password" value="${druid.password}">property>
    bean>
    
    
    <bean class="org.springframework.jdbc.core.JdbcTemplate">
        
        <property name="dataSource" ref="druidDataSource">property>
    bean>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18

    8.3、测试

    1 在测试类装配 JdbcTemplate

    引入坐标的时候,引入一个spring-test依赖,它的作用是spring整合junit。

    当前junit用的是4的版本,如果是5版本,需要的jar包和注解都不相同,有兴趣自行百度。

    spring-test可以让测试类在spring的测试环境中执行,不必每一次有获取IOC容器,直接通过依赖注入就可以使用某一个类。

    // 指定当前测试类在Spring的测试环境中执行,此时就可以通过注入的方式直接获取IOC容器中bean
    @RunWith(SpringJUnit4ClassRunner.class)
    // 设置Spring测试环境的配置文件 classpath:类路径
    @ContextConfiguration("classpath:spring-jdbc.xml")
    public class TestUser {
    
        private JdbcTemplate jdbcTemplate;
        
        @Autowired
        public void setJdbcTemplate(JdbcTemplate jdbcTemplate){
        	this.jdbcTemplate = jdbcTemplate;
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13

    2 测试增删改功能

    // 添加
    @Test
    public void testInsert() {
        String sql = "insert into t_user values (null,?,?,?,?,?)";
        jdbcTemplate.update(sql, "root", "123", 23, "女", "123@qq.com");
    }
    
    // 删除
    @Test
    public void testDelete() {
        String sql = "delete from t_user where id = ?";
        jdbcTemplate.update(sql, 2);
    }
    
    // 修改
    @Test
    public void testUpdate() {
        String sql = "update t_user set username = ?, password = ?, age = ?, gender = ?, email = ? where id = ?";
        jdbcTemplate.update(sql, "tt", "234", 24, "男", "345@qq.com",2);
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20

    3 查询一条数据为实体类对象

    @Test
    public void testGetUserById(){
        String sql = "select * from t_user where id = ?";
        User user = jdbcTemplate.queryForObject(sql, new BeanPropertyRowMapper<>(User.class), 3);
        System.out.println(user);
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6

    4 查询多条数据为一个list集合

    @Test
    public void testGetAllUser(){
        String sql = "select * from t_user";
        List<User> list = jdbcTemplate.query(sql, new BeanPropertyRowMapper<>(User.class));
        list.forEach(System.out::println);
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6

    5 查询单行单列的值

    @Test
    public void testGetUserCount(){
        String sql = "select count(*) from t_user";
        Integer count = jdbcTemplate.queryForObject(sql, Integer.class);
        System.out.println(count);
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
  • 相关阅读:
    阿里云服务器u1和经济型e系列性能差异?哪个比较好?
    【代码随想录】算法训练营 第二十天 第六章 二叉树 Part 6
    前端工程化
    【蓝桥杯选拔赛真题28】python字符串包含字符 青少年组蓝桥杯python 选拔赛STEMA比赛真题解析
    使用CEF(六)— 解读CEF的cmake工程配置
    apifox的使用以及和idea集成
    JDBC的使用
    css初入门:BFC(格式化上下文)
    高性能日志脱敏组件:已支持 log4j2 和 logback 插件
    SpringBoot整合Redis
  • 原文地址:https://blog.csdn.net/zhao854116434/article/details/126805568