在整合的遇到各類坑,如下是我整合的流程java
一、pom.xml文件mysql
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jooq</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
二、application.properties文件,這裏特地說明如下,我這裏用的是spring data jpa 2.10版本,支持的數據庫驅動有三種,分別是hikari、tomcat-jdbc,dbcp2,優先是hikari,其餘兩個須要單獨導入包:
#hikari
spring.datasource.hikari.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.hikari.jdbc-url=jdbc:mysql://127.0.0.1:3306/yangld?useUnicode=true&characterEncoding=utf-8
spring.datasource.hikari.username=root
spring.datasource.hikari.password=1234
spring.jpa.database-platform=org.hibernate.dialect.MySQL57Dialect
spring.jpa.show-sql=true
spring.jpa.hibernate.ddl-auto=create
三、JpaConfig類配置有兩種形式,一種是xml方式,還有註解的方式,咱們用的是springboot,xml的形式是咱們要唾棄的,因此用註解的形式:
@Configuration
@EnableJpaRepositories(basePackages={"com.example.springbootdemo.dao"}) //這個是你Repositorie所在的包
@EnableTransactionManagement //這個是事務
public class JpaConfig{
@Primary //springboot默認是多數據源,因此你要指定一個主數據源,否則會錯誤
@Bean
@ConfigurationProperties(prefix = "spring.datasource.hikari") //須要導入配置
public DataSource dataSource(){
return DataSourceBuilder.create().build();
}
@Primary //springboot默認是多數據源,因此你要指定一個主數據源,否則會錯誤
@Bean
public LocalContainerEntityManagerFactoryBean entityManagerFactory() {
HibernateJpaVendorAdapter vendorAdapter = new HibernateJpaVendorAdapter();
vendorAdapter.setGenerateDdl(true);
vendorAdapter.setDatabase(Database.MYSQL);//這裏指定的你數據庫的類型
LocalContainerEntityManagerFactoryBean factory = new LocalContainerEntityManagerFactoryBean();
factory.setJpaVendorAdapter(vendorAdapter);
factory.setPackagesToScan("com.example.springbootdemo.entity");//這個是你entity所在的包
factory.setDataSource(dataSource());
return factory;
}
@Bean
public PlatformTransactionManager transactionManager(EntityManagerFactory entityManagerFactory) {
JpaTransactionManager txManager = new JpaTransactionManager();
txManager.setEntityManagerFactory(entityManagerFactory);
return txManager;
}
}
四、dao層的Repository
@Repository
public interface CityRepository extends JpaRepository<City,Integer> {
}
五、實體類
@Entity
@Getter
@Setter
public class City {
@Id
@GeneratedValue
@Column(name="id")
private Integer id;
@Column(name="name")
private String name;
@Column(name="countryCode")
private String countryCode;
@Column(name="district")
private String district;
@Column(name="population")
private String population;
}
六、controller層
@RestController
public class WorkController {
@Autowired
private CityRepository cityRepository;
@RequestMapping("/list")
public List<City> queryCityAll(){
return cityRepository.findAll();
}
}
七、整合完畢http://localhost:8080/list