Spring框架---JDBC模板技术

 

目录

Spring框架的JDBC模板技术

JDBC模板技术概述

JDBC的模板类的使用

使用Spring框架来管理模板类

Spring框架管理开源的连接池

Spring框架的JDBC模板的简单操作

模拟转账开发

Dao编写的方式(第二种方式)

Spring框架的事务管理

Spring框架的事务管理相关的类和API

Spring框架声明式事务管理


Spring框架的JDBC模板技术

JDBC模板技术概述

Spring框架中提供了很多持久层的模板类来简化编程,使用模板类编写程序会变的简单

  •  template 模板
  • 都是Spring框架提供XxxTemplate

提供了JDBC模板,Spring框架提供的

  •  JdbcTemplate类,Connection 表示连接,管理事务 Statement ResultSet

如果不使用JdbcTemplate,想要实现增删改查需要以下步骤

  1. 加载驱动
  2. 通过Conection接口获取连接
  3. 编写sql语句
  4. 获取执行sql对象,Statment或者PreparedStatment(更推荐,是一种预编译SQL语句的方法,可以防止SQL注入问题,使用?作为占位符)
  5. 执行sql语句,返回查询结果,将结果封装在Result结果集中
  6. 释放资源

JDBC的模板类的使用

引入JdbcTemplate,JdbcTemplate接口会帮我们完成获取连接,执行sql语句并封装结果,释放资源

创建maven项目,引入坐标依赖


    
        org.springframework
        spring-context
        5.0.2.RELEASE
    
    
        commons-logging
        commons-logging
        1.2
    
    
        log4j
        log4j
        1.2.12
    
    
        org.springframework
        spring-test
        5.0.2.RELEASE
    
    
        junit
        junit
        4.12
    

    
    
        aopalliance
        aopalliance
        1.0
    
    
    
        org.springframework
        spring-aspects
        5.0.2.RELEASE
    
    
    
        org.aspectj
        aspectjweaver
        1.8.3
    

    

    
        mysql
        mysql-connector-java
        5.1.6
    
    
    
    
        org.springframework
        spring-jdbc
        5.0.2.RELEASE
    
    
    
        org.springframework
        spring-tx
        5.0.2.RELEASE
    

    
        com.alibaba
        druid
        1.1.10
    

创建spring_db数据库

create database spring_db;
use spring_db;
create table account(
    id int primary key auto_increment,
    name varchar(40),
    money double
)character set utf8 collate utf8_general_ci;

insert into account(name,money) values('aaa',1000);
insert into account(name,money) values('bbb',1000);
insert into account(name,money) values('ccc',1000);

数据库实体类

package com.qcby.demo4;

public class Account {
    private Integer id;
    private String name;
    private Double money;

    public Account() {
    }

    public Account(Integer id, String name, Double money) {
        this.id = id;
        this.name = name;
        this.money = money;
    }

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Double getMoney() {
        return money;
    }

    public void setMoney(Double money) {
        this.money = money;
    }

    @Override
    public String toString() {
        return "Account{" +
                "id=" + id +
                ", name='" + name + '\'' +
                ", money=" + money +
                '}';
    }
}

测试类

package com.qcby.demo0401Test;

import org.junit.Test;
import org.springframework.jdbc.datasource.DriverManagerDataSource;
import org.springframework.jdbc.core.JdbcTemplate;

public class demo1 {

    /**
     * 使用new对象方式完成
     */
    @Test
    public void run1(){
        //创建连接池对象,spring框架内置了连接池对象
        DriverManagerDataSource dataSource = new DriverManagerDataSource();
        //设置了4个参数
        dataSource.setDriverClassName("com.mysql.jdbc.Driver");
        dataSource.setUrl("jdbc:mysql://localhost:3306/spring_db");
        dataSource.setUsername("root");
        dataSource.setPassword("2020");

        //提供模板,创建对象
        JdbcTemplate template = new JdbcTemplate(dataSource);
        //完成数据的增删改查
        template.update("insert into account values (null,?,?)","熊大",1000);
    }
}

将连接池对象传入到JdbcTemplate中,直接调用JdbcTemplate中的方法即可完成操作

使用Spring框架来管理模板类

上面代码的JdbcTemplate是自己手动new出来的,我们可以把这些类交给Spring框架去管理。

在配置文件中使用Spring管理内置的连接池



                
                
            
            
                
                
                
                
            
            
            
            
                
            



        

编写测试方法

package com.qcby.demo0401Test;

import com.qcby.demo4.Account;
import com.qcby.demo4.BeanMapper;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

import java.util.List;

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:applicationContext_jdbc.xml")
public class demo1_1 {

    @Autowired
    private JdbcTemplate jdbcTemplate;

    /**
     * 测试的方法
     */
    @Test
    public void run1(){
        jdbcTemplate.update("insert into account values (null,?,?)","ccc",5000);
    }
    }

Spring框架管理开源的连接池

配置开源的连接池,使用Druid开源的连接池,引入坐标如下

 
 

你可能感兴趣的:(spring,java,前端)