微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

Spring Boot jdbc

Spring Boot jdbc

添加依赖

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>2.0.6.RELEASE</version>
    <relativePath/> <!-- lookup parent from repository -->
</parent>
<dependency>

<groupId>MysqL</groupId>

<artifactId>mysql-connector-java</artifactId>

</dependency>

Repository

import org.springframework.data.repository.CrudRepository;

在Spring中有Repository的概念,repository原意指的是仓库,即数据仓库的意思。Repository居于业务层和数据层之间,将两者隔离开来,在它的内部封装了数据查询和存储的逻辑。这样设计的好处有两个:

降低层级之间的耦合:更换、升级ORM引擎(Hibernate)并不会影响业务逻辑
提高测试效率:如果在测试时能用Mock数据对象代替实际的数据库操作,运行速度会快很多

public @interface Query

sql语句注解

import org.springframework.data.jdbc.repository.query.Query;

Param;

import org.springframework.data.repository.query.Param;

application.properties

Spring Boot使用了一个全局的配置文件application.properties,放在src/main/resources目录下或者类路径的/config下。Sping Boot的全局配置文件的作用是对一些认配置的配置值进行修改

spring.datasource.url=jdbc:MysqL://localhost:3306/crawler
spring.datasource.username=root
spring.datasource.password=root
# 数据库驱动
spring.datasource.driverClassName = com.MysqL.cj.jdbc.Driver
## 指定连接池中最大的活跃连接数.
#spring.datasource.max-active=20
## 指定连接池最大的空闲连接数量.
#spring.datasource.max-idle=8
## 指定必须保持连接的最小值
#spring.datasource.min-idle=8
## 指定启动连接池时,初始建立的连接数量
#spring.datasource.initial-size=10
#########################################################
#### JPA持久化配置
#########################################################
## 指定数据库的类型
#spring.jpa.database = MysqL
## 指定是否需要在日志中显示sql语句
#spring.jpa.show-sql = true
## 指定自动创建|更新|验证数据库表结构等配置,配置成update
## 表示如果数据库中存在持久化类对应的表就不创建,不存在就创建对应的表
#spring.jpa.hibernate.ddl-auto = update
## Naming strategy
## 指定命名策略
#spring.jpa.hibernate.naming-strategy = org.hibernate.cfg.ImprovednamingStrategy
## 指定数据库方言
#spring.jpa.properties.hibernate.dialect = org.hibernate.dialect.MysqL5Dialect

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐