SpringBoot基础day05 - Mysql JPA

SpringBoot基础day05 - Mysql JPA

Tags: SpringBoot

摘要

SpringBoot基础day05 - Mysql JPA

jpa接口

package app.repository;

import app.model.User;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;

/**
 * Created by zhu on 17-1-3.
 */
@Repository
public interface UserRepository extends JpaRepository<User, Long> {
}

application.properties

##Data Source Configuration
# Connection url for the database "db_springboot"
spring.datasource.url=jdbc:mysql://192.168.1.217:3306/db_springboot?useSSL=false
spring.datasource.username=root
spring.datasource.password=admin
# Keep the connection alive if idle for a long time (needed in production)
spring.datasource.testWhileIdle=true
spring.datasource.validationQuery=SELECT 1
##jpa
spring.jpa.hibernate.ddl-auto=validate
spring.datasource.schema=classpath:/db/schema.sql
spring.datasource.data=classpath:/db/data.sql
# Naming strategy
spring.jpa.hibernate.naming.strategy=org.hibernate.cfg.ImprovedNamingStrategy
# Allows Hibernate to generate SQL optimized for a particular DBMS
spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.MySQL5Dialect

指定数据库连接地址
因为是测试阶段,每次重启都删除表,重新初始化表结构和数据.

注入Repository

在UserServiceBean里注入UserRepository,之前的在内存中模拟数据的增删改查的代码删除掉即可.

  @Autowired
  private UserRepository repository;

代码地址

https://coding.net/u/javacore/p/spring-boot-fundamentals/git/tree/master/day05