Spring-jdbcTemplate-配置数据库连接池

1、pomxml 

    
        
            org.projectlombok
            lombok
        

        
            mysql
            mysql-connector-java
            8.0.25
        

        
            com.alibaba
            druid
            1.2.8
        

        
            org.springframework
            spring-jdbc
        

        
            org.junit.jupiter
            junit-jupiter-api
        
        
            org.springframework
            spring-test
        

        
            org.springframework
            spring-context
        

    

2、jdbc.properties 

jdbc.driver=com.mysql.cj.jdbc.Driver
jdbc.url=jdbc:mysql:///studb
jdbc.user=root
jdbc.pwd=123456

3、beans.xml




    
    

    
    
        
        
        
        
        
        
    

    
        
    

4、Student.java

package com.atguigu.pojo;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
@Data
@AllArgsConstructor
@NoArgsConstructor
public class Student {
    private Integer id;
    private String name;
    private String gender;
    private Integer age;
    private String classBean;
}

5、JdbcTemplateTest.java

package com.atguigu;
import com.atguigu.pojo.Student;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.test.context.junit.jupiter.SpringJUnitConfig;
import javax.sql.DataSource;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;
@SpringJUnitConfig(locations = "classpath:beans.xml")
public class JdbcTemplateTest {

    @Autowired
    private DataSource dataSource;

    @Autowired
    private JdbcTemplate jdbcTemplate;

    //测试连接数据库
    @Test
    public void TestConnection() throws SQLException {
        System.out.println(dataSource.getConnection()==null?"连接失败":"连接成功");//连接成功
    }

    //测试插入操作
    @Test
    public void InsertTest() {
        Student s1= new Student(0, "阿碧", "女", 18, "Java1班");
        String sql = "insert into students values(0,?,?,?,?)";
        jdbcTemplate.update(sql, s1.getName(), s1.getGender(), s1.getAge(), s1.getClassBean());
    }

    //演示删除学生信息测试方法
    @Test
    public void DeleteTest() {
        String sql = "delete from students where id=?";
        jdbcTemplate.update(sql, 0);
    }

    //演示查询单个字段值
    @Test
    public void QueryTest() {
        String sql = "select name from students where id=?";
        String name = jdbcTemplate.queryForObject(sql, String.class, 8);
        System.out.println("name = " + name);//name = 吴十
    }

    //演示查询单个实体对象
    @Test
    public void QueryObjectTest() {
        String sql = "select * from students where id=?";
        Student s = jdbcTemplate.queryForObject(sql, new RowMapper() {
            @Override
            public Student mapRow(ResultSet rs, int rowNum) throws SQLException {
                return new Student(rs.getInt(1), rs.getString(2), rs.getString(3), rs.getInt(4), rs.getString(5));
            }
        }, 8);
        System.out.println("s = " + s);//s = Student(id=8, name=吴十, gender=男, age=19, classBean=高中二班)
    }
    //演示查询单个实体对象
    @Test
    public void QueryObjectLambdaTest() {
        String sql = "select * from students where id=?";
        Student s = jdbcTemplate.queryForObject(sql,(rs,rowNum)->
            new Student(rs.getInt(1), rs.getString(2), rs.getString(3), rs.getInt(4), rs.getString(5))
                , 8);
        System.out.println("s = " + s);//s = Student(id=8, name=吴十, gender=男, age=19, classBean=高中二班)
    }


    //演示查询实体对象列表
    @Test
    public void QueryObjectListTest() {
        String sql = "select id,name,gender,age,class as classBean from students";
        List studentList = jdbcTemplate.query(sql, new BeanPropertyRowMapper<>(Student.class));
        studentList.forEach(System.out::println);
        //Student(id=1, name=张三, gender=男, age=20, classBean=高中一班)
        //Student(id=2, name=李四, gender=男, age=19, classBean=高中二班)
        //Student(id=3, name=王五, gender=女, age=18, classBean=高中一班)
        //Student(id=4, name=赵六, gender=女, age=20, classBean=高中三班)
        //Student(id=5, name=刘七, gender=男, age=19, classBean=高中二班)
        //Student(id=6, name=陈八, gender=女, age=18, classBean=高中一班)
        //Student(id=7, name=杨九, gender=男, age=20, classBean=高中三班)
        //Student(id=8, name=吴十, gender=男, age=19, classBean=高中二班)
    }


}

6、studb.sql

create database studb;

use studb;

CREATE TABLE students (
  id INT PRIMARY KEY,
  name VARCHAR(50) NOT NULL,
  gender VARCHAR(10) NOT NULL,
  age INT,
  class VARCHAR(50)
);

INSERT INTO students (id, name, gender, age, class)
VALUES
  (1, '张三', '男', 20, '高中一班'),
  (2, '李四', '男', 19, '高中二班'),
  (3, '王五', '女', 18, '高中一班'),
  (4, '赵六', '女', 20, '高中三班'),
  (5, '刘七', '男', 19, '高中二班'),
  (6, '陈八', '女', 18, '高中一班'),
  (7, '杨九', '男', 20, '高中三班'),
  (8, '吴十', '男', 19, '高中二班');

Spring-jdbcTemplate-配置数据库连接池_第1张图片 

spring-jdbc-template是Spring框架中提供的一个JDBC模板,简化了开发者对JDBC API的调用。你可以使用它来执行各种JDBC操作,如查询、插入、更新、删除等。下面是使用spring-jdbc-template的步骤:

  1. 添加spring-jdbc依赖

首先需要在Maven或Gradle项目中添加spring-jdbc依赖。


    org.springframework
    spring-jdbc
    5.3.6

 

  1. 配置数据源

配置使用的数据源,在Spring配置文件中添加以下配置。


    
    
    
    

  1. 配置JdbcTemplate

配置JdbcTemplate,配置JdbcTemplate时需要注入数据源。


    

  1. 使用JdbcTemplate

最后,你可以在Java代码中使用JdbcTemplate。

@Autowired
private JdbcTemplate jdbcTemplate;

public void insert(User user) {
    String sql = "INSERT INTO users (name, age) VALUES (?, ?)";
    jdbcTemplate.update(sql, user.getName(), user.getAge());
}

以上是使用spring-jdbc-template的步骤,你可以根据项目的需要来调整代码实现,具体的JDBC操作方法可以在Spring的官方文档中查找。

 

你可能感兴趣的:(#,Spring,Framework,spring,property,placeholder,location,dataSource,jdbcTemplate,locations)