spring data jpa 环境搭建

  • 单数据源配置

因为实际项目多是多数据源配置,所以单数据源这里简单描述带过

  1. POM依赖


org.Springframework.boot
spring-boot-starter-data-jpa


mysql
mysql-connector-java

  1. spring boot配置

spring.datasource.url=jdbc:mysql://localhost:3306/test?serverTimezone=GMT%2b8&autoReconnect=true&useUnicode=true&characterEncoding=UTF-8&useSSL=truede=true&characterEncoding=utf-8&useSSL=true
spring.datasource.username=root
spring.datasource.password=
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.jpa.properties.hibernate.hbm2ddl.auto=create
spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.MySQL5InnoDBDialect
spring.jpa.show-sql=true
spring.jpa.properties.hibernate.format_sql=true

  1. 构建实体示例

@Entity
public class User{
@Id
@GeneratedValue
private Long id;
@Column(nullable=false,unique=true)
private String userName;
//setter...getter...
}

  1. 构建dao接口

public interface UserDao extends JpaRepository{}

5.junit 测试

@RunWith(SpringRunner.class)
@SpringBootTest
public class RepositoryTest{
@Resource
private UserDao userDao;
@Test
public void test(){
User u = new User();
u.setUserName("tery");
userDao.save(u);
}
}

你可能感兴趣的:(spring data jpa 环境搭建)