项目github地址:https://github.com/ludi-jiangtou/spring-learning-example/tree/master
IDEA提供了快速创建Spring Boot项目方式:使用Spring Initializr初始化项目,这样只能创建单模块Spring Boot项目。如果需要创建Spring Boot多模块项目,我们需要先创建一个空白的项目, 然后在project空间创建各个模块,这样方便多模块管理。此处我用后一种方式创建。
创建项目
创建空白项目spring-learning-example,在项目空间创建空白模块 spring-data-jpa-learning.目录结构如下:
image
设置根目录gradle
buildscript {
repositories {
maven {url 'http://maven.aliyun.com/nexus/content/groups/public' }
}
}
plugins {
id 'java'
}
group 'spring-learning'
version '1.20190914.001'
sourceCompatibility = 11
引入依赖
引入项目需要的依赖。spring-data-jpa-learning module下的build.gradle.利用webFlux构建异步非阻塞式的 Web 。
plugins {
//添加spring boot 插件
id 'org.springframework.boot' version '2.1.8.RELEASE'
//依赖包管理器
id 'io.spring.dependency-management' version '1.0.8.RELEASE'
id 'java'
}
group 'spring-learning'
version '1.20190914.001'
sourceCompatibility = 11
repositories {
maven {url 'http://maven.aliyun.com/nexus/content/groups/public' }
mavenCentral()
}
dependencies {
//本项目采用响应式web框架webFlux
implementation 'org.springframework.boot:spring-boot-starter-webflux'
//数据库依赖
implementation 'com.microsoft.sqlserver:mssql-jdbc'
//采用阿里提供的JDBC组件
implementation "com.alibaba:druid-spring-boot-starter:1.1.17"
implementation "org.apache.commons:commons-pool2:2.6.1"
//引入jpa
implementation 'org.springframework.boot:spring-boot-starter-data-jpa'
//测试依赖
testImplementation 'org.springframework.boot:spring-boot-starter-test'
testCompile group: 'junit', name: 'junit', version: '4.12'
}
配置文件
application.yml
spring:
profiles:
active: dev
application-dev.yml
server:
port: 808
spring:
application:
name: spring-data-jpa-learning
datasource:
druid:
spring-learning:
url: jdbc:sqlserver://127.0.0.1:1433;DatabaseName=SPRING_LEARING
driver-class-name: com.microsoft.sqlserver.jdbc.SQLServerDriver
username: sa
password: 123456
max-active: 1
initial-size: 1
min-idle: 1
max-wait: 6000
jpa:
generate-ddl: true
show-sql: true
hibernate:
ddl-auto: create
编写代码
启动类SpringDataJpaLearningApplication
package com.wangkong.learning.jpa;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
/**
* @author 忘空
* @version 1.0
* @date 2019-09-14 11:03
*/
@SpringBootApplication
public class SpringDataJpaLearningApplication {
public static void main(String[] args) {
SpringApplication.run(SpringDataJpaLearningApplication.class, args);
}
}
数据库配置类JpaLearningDatasourceConfig
package com.wangkong.learning.jpa.config;
import com.alibaba.druid.spring.boot.autoconfigure.DruidDataSourceBuilder;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
import org.springframework.orm.jpa.JpaTransactionManager;
import org.springframework.orm.jpa.JpaVendorAdapter;
import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean;
import org.springframework.orm.jpa.vendor.Database;
import org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter;
import org.springframework.transaction.annotation.EnableTransactionManagement;
import javax.persistence.EntityManagerFactory;
import javax.sql.DataSource;
import java.util.Properties;
/**
* @author 忘空
* @version 1.0
* @date 2019-09-14 11:21
*/
@Configuration
@EnableTransactionManagement
@EnableJpaRepositories(
entityManagerFactoryRef = "springLearningEntityManagerFactory",
basePackages = "com.wangkong.learning.jpa.repository",
transactionManagerRef = "springLearningTransactionManager"
)
public class JpaLearningDatasourceConfig {
@Value(value = "${spring.jpa.generate-ddl}")
private boolean generateDdl;
@Value(value = "${spring.jpa.show-sql}")
private boolean showSql;
@Value(value = "${spring.jpa.hibernate.ddl-auto}")
private String ddlAuto;
/**
* 配置数据库连接
*/
@Bean("springLearningDataSource")
@ConfigurationProperties(prefix = "spring.datasource.druid.spring-learning")
public DataSource springLearningDataSource() {
DataSource dataSource;
dataSource = DruidDataSourceBuilder.create().build();
return dataSource;
}
@Bean
public JpaVendorAdapter jpaVendorAdapter() {
HibernateJpaVendorAdapter jpaVendorAdapter = new HibernateJpaVendorAdapter();
jpaVendorAdapter.setGenerateDdl(generateDdl);
jpaVendorAdapter.setShowSql(showSql);
jpaVendorAdapter.setDatabase(Database.SQL_SERVER);
return jpaVendorAdapter;
}
@Bean(name = "hibernateProperties")
@Qualifier(value = "hibernateProperties")
public Properties hibernateProperties() {
Properties properties = new Properties();
properties.setProperty("hibernate.hbm2ddl.auto", ddlAuto);
return properties;
}
/**
* 配置EntityManager工厂对象
*/
@Bean(name = "springLearningEntityManagerFactory")
public LocalContainerEntityManagerFactoryBean springLearningEntityManagerFactory() {
LocalContainerEntityManagerFactoryBean lemfb = new LocalContainerEntityManagerFactoryBean();
lemfb.setDataSource(springLearningDataSource());
lemfb.setJpaVendorAdapter(jpaVendorAdapter());
lemfb.setPackagesToScan("com.wangkong.learning.jpa.domain");
lemfb.setJpaProperties(hibernateProperties());
lemfb.afterPropertiesSet();
return lemfb;
}
/**
* 配置事务
*/
@Bean(name = "springLearningTransactionManager")
public JpaTransactionManager authInfoTransactionManager(
@Qualifier(value = "springLearningEntityManagerFactory") EntityManagerFactory authInfoEntityManagerFactory) {
JpaTransactionManager jpaTransactionManager;
jpaTransactionManager = new JpaTransactionManager(authInfoEntityManagerFactory);
return jpaTransactionManager;
}
}
启动项目
启动SpringDataJpaLearningApplication。