springboot 整合 jpa
pom.xml
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.6.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
application.properties
# 数据库信息
spring.datasource.drive-class-name=com.mysql.jdbc.Driver
spring.datasource.url=jdbc:mysql:///jpa_db?serverTimezone=GMT%2B8
spring.datasource.username=root
spring.datasource.password=root
# 配置 jpa 相关信息
spring.jpa.database=mysql
spring.jpa.show-sql=true
spring.jpa.generate-ddl=true
spring.jpa.hibernate.ddl-auto=create
实体类
package com.mozq.boot.jpa.domain;
import javax.persistence.*;
@Entity
@Table(name="tab_customer")
public class Customer {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long custId;
@Column
private String custName;
@Column
private String custSource;
@Column
private String custLevel;
@Column
private String custIndustry;
@Column
private String custPhone;
@Column
private String custAddress;
}
Dao 接口
package com.mozq.boot.jpa.dao;
import com.mozq.boot.jpa.domain.Customer;
import org.springframework.data.jpa.repository.JpaRepository;
/*
操作 Customer 的接口
*/
public interface CustomerDao extends JpaRepository<Customer, Long> {}
启动类
package com.mozq.boot.jpa;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class BootJpaDemoApplication {
public static void main(String[] args) {
SpringApplication.run(BootJpaDemoApplication.class, args);
}
}
测试类
package com.mozq.boot.jpa.dao;
import com.mozq.boot.jpa.BootJpaDemoApplication;
import com.mozq.boot.jpa.domain.Customer;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
@RunWith(SpringRunner.class)
@SpringBootTest(classes = BootJpaDemoApplication.class)
public class CustomerDaoTest {
@Autowired
private CustomerDao customerDao;
@Test
public void testSave(){
Customer customer = new Customer();
customer.setCustName("大乔");
customerDao.save(customer);
}
}
Bugs
Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'entityManagerFactory' available
原因:
org.hibernate:hibernate-core:5.3.10.Final jar 包报红,将仓库相应目录删除,还是报红,重启 idea 好了。
java.sql.SQLException: The server time zone value '�й���ʱ��' is unrecognized or represents more than one time zone. You must configure either the server or JDBC driver (via the serverTimezone configuration property) to use a more specifc time zone value if you want to utilize time zone support.
原因:时区未指定或者表示多个时区
方案:
spring.datasource.url=jdbc:mysql:///jpa_db?serverTimezone=GMT%2B8
encodeURIComponent("GMT+8"); "GMT%2B8"