-
單數(shù)據(jù)源配置
因?yàn)閷?shí)際項(xiàng)目多是多數(shù)據(jù)源配置摘能,所以單數(shù)據(jù)源這里簡單描述帶過
- POM依賴
<dependency>
<groupId>org.Springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
- 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
- 構(gòu)建實(shí)體示例
@Entity
public class User{
@Id
@GeneratedValue
private Long id;
@Column(nullable=false,unique=true)
private String userName;
//setter...getter...
}
- 構(gòu)建dao接口
public interface UserDao extends JpaRepository<User席赂,Long>{}
5.junit 測(cè)試
@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);
}
}