Spring boot关于JPA

1、需要添加相应的依赖包

        <!-- 添加mysql数据库驱动依赖包 -->

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

        <!-- 添加Spring-data-jpa依赖 -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
        </dependency>

 

2、需要在application.properties文件添加配置信息

#######################################
###########   database   #############
######################################
spring.datasource.url=jdbc:mysql://localhost:3306/test
spring.datasource.username=root
spring.datasource.password=123456
spring.datasource.driverClassName=com.mysql.jdbc.Driver
spring.datasource.max-active=20
spring.datasource.max-idle=8
spring.datasource.min-idle=8
spring.datasource.initial-size=10



########################################
#########  spring JPA  #################
########################################
#JPA Configuration:  
spring.jpa.database=MYSQL
# Show or not log for each sql query
spring.jpa.show-sql=true
spring.jpa.generate-ddl=true  
# Hibernate ddl auto (create, create-drop, update)
spring.jpa.hibernate.ddl-auto=update  
#spring.jpa.database-platform=org.hibernate.dialect.MySQL5Dialect  
spring.jpa.hibernate.naming_strategy=org.hibernate.cfg.ImprovedNamingStrategy  
#spring.jpa.database=org.hibernate.dialect.MySQL5InnoDBDialect
spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.MySQL5Dialect

3、创建实体类

这个实体类大家可以随意写

4、创建一个接口继承CrudRepository

!!!这是最重要的,创建的必须是接口,要继承CrudRepository

5、创建一个service

service里面编写的是操作数据库的代码

6、创建一个Controller

controller里面进行service方法的测试

7、测试

最后运行项目,并测试就行了

 

关注我的微信公众号,会不定期的发布学习总结与学习资源

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值