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、测试

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


猜你喜欢

转载自blog.csdn.net/bookfish/article/details/79617552