最新下载
热门教程
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
SpringBoot封装JDBC代码实现方法
时间:2022-06-29 01:59:47 编辑:袖梨 来源:一聚教程网
本篇文章小编给大家分享一下SpringBoot封装JDBC代码实现方法,文章代码介绍的很详细,小编觉得挺不错的,现在分享给大家供大家参考,有需要的小伙伴们可以来看看。
Spring Boot中可以在配置文件中直接进行数据库配置,
spring.datasource.username= root spring.datasource.password= 123456 spring.datasource.url=jdbc:mysql://localhost:3306/mybatis?useUnicode=true&characterEncoding=utf-8 spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
SpringBoot可以直接生成数据库对象
默认数据源为Hikari
jdbc连接
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 DataSpringbootApplicationTests { @Autowired DataSource dataSource; @Test void contextLoads() throws SQLException { System.out.println("默认数据源"); System.out.println(dataSource.getClass()); System.out.println("获得数据库连接"); Connection connection = dataSource.getConnection(); System.out.println(connection); System.out.println("关闭数据源"); connection.close(); } }
springboot中有很多template已经写好可以直接拿来用
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.jdbc.core.JdbcTemplate; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RestController; import java.util.List; import java.util.Map; @RestController public class JDBCController { @Autowired JdbcTemplate jdbcTemplate; //查询数据库所有信息 @GetMapping("/userList") public List
相关文章
- 洛克王国世界妮蔓在哪里 妮蔓具体位置 10-20
- 鸣潮卜灵什么时候上线 卜灵上线时间预测 10-20
- 鸣潮千咲什么时候上线 千咲上线时间一览 10-20
- 鸣潮千咲是什么属性 千咲属性介绍一览 10-20
- 洛克王国世界拾遗任务怎么做 拾遗任务攻略一览 10-20
- 荒野起源木材怎么获得 木材获取攻略 10-20