SpringBoot和JDBC整合使用

操作:

1、创建springboot项目(略):附上我的项目结构

2、确保项目的pom.xml要有下面两个依赖

        <dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-data-jdbc</artifactId>
		</dependency>

		<dependency>
			<groupId>mysql</groupId>
			<artifactId>mysql-connector-java</artifactId>
			<version>5.1.6</version>
			<scope>runtime</scope>
		</dependency>

3、在application.yml文件中(或application.properties文件,但内容的格式会不同)配置连接数据库信息

spring:
  datasource:
    driver-class-name: com.mysql.jdbc.Driver
    #ssmbulid是我的数据库名
    url: jdbc:mysql://localhost:3306/ssmbulid?useUnicode=true&characterEncoding=utf-8
    username: root
    password: hytsql

4、配置好这些信息后,SpringBoot会默认帮我们进行自动配置(已经连上本机数据库),我们可以去测试类Springboot04DataApplicationTests里测试

package com.hq;

import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;

import javax.sql.DataSource;
import java.sql.Connection;
import java.sql.SQLException;


@SpringBootTest
class Springboot04DataApplicationTests {
 @Autowired
	 DataSource dataSource;
	@Test
	void contextLoads() throws SQLException {
		//默认使用的数据源
		System.out.println("数据源:" + dataSource.getClass());
		Connection connection = dataSource.getConnection();
		System.out.println("connection:" + connection);
	}
}

结果:

【在SpringBoot2.25默认使用的数据源是HikariDataSource,而以前的版本如SpringBoot1.5默认使用的是tomcat-jdbc数据源(org.apache.tomcat.jdbc.pool.DataSource)】

HikariDataSource是Java Web当前速度最快的数据源,相比于传统的c3p0、DBCP、tomcat jdbc等数据源更优秀

5、有了数据库连接我们就可以进行CRUD操作了,在进行操作前我们先了解一下JdbcTemplate类

  • SpringBoot本身就对原生JDBC做了轻量级的封装,对于查询、增、删、改这些操作都封装在JdbcTemplate类中
  • SpringBoot已经将JdbcTemplate放在了容器中,我们使用时自己注入即可使用
package com.hq.contrlloer;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;

@RestController
public class JDBCCRUD {
    @Autowired
    JdbcTemplate jdbcTemplate;
    //查询所有
    @RequestMapping("/list")
    public List<Map<String,Object>> queryAll(){
        String sql = "select * from ssmbulid.books";
        return jdbcTemplate.queryForList(sql);
    }
    //添加
  @RequestMapping("/add")
    public String add(){
        String sql = "insert into books values(6,'jdbc',6,'jjj')";
        jdbcTemplate.update(sql);
        return "add-ok";
    }
    //修改
    @RequestMapping("/upd")
    public String upd(){
        String sql = "update books set bookName=?,detail=? where bookId=6";
        Object[] objects = new Object[3];
        objects[0] = "spring";
        objects[1] = "框架";
        jdbcTemplate.update(sql,objects);
        return "update-ok";
    }
}

上述测试结果省略

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值