前言
持久层框架的替换,mybatis换成spring data.
使用步骤
首先创建好一个spring boot项目之后
第一步在pom.xml添加依赖:
<!--SpringData JPA 启动器-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
第二步在application.properties里配置
#配置数据库连接
spring.datasource.url=jdbc:mysql://localhost:3306/boot?serverTimezone=UTC&useUnicode=true&characterEncoding=utf8&useSSL=false
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.username=boot
spring.datasource.password=123
# 主要是这两个
#JPA正向工程
spring.jpa.hibernate.ddl-auto=update
#显示sql
spring.jpa.show-sql=true
#JPA Configuration:
spring.jpa.database=mysql
spring.jpa.generate-ddl=true
spring.jpa.hibernate.naming_strategy=org.hibernate.cfg.ImprovedNamingStrategy
另一种application.yml文件配置
application.yml文件
spring:
datasource:
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://39.105.167.131:3306/smile_boot?serverTimezone=UTC&useUnicode=true&characterEncoding=utf-8&useSSL=true
username: root
password: Nrblwbb7$
jpa:
properties:
hibernate:
hbm2ddl:
auto: create
dialect: org.hibernate.dialect.MySQL5InnoDBDialect
format_sql: true
show-sql: true
第三步就是创建一个实体类(关于注解,会在最后总结)
packag