一、idea新建一個maven項目,首先看下我的項目結構
2.pom文件引入依賴
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.jjp.cloud</groupId> <artifactId>microservice-simple-provider-user</artifactId> <version>1.0-SNAPSHOT</version> <packaging>jar</packaging> <parent> <!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-parent --> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>1.5.9.RELEASE</version> </parent> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <java.version>1.8</java.version> </properties> <dependencies> <!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-data-jpa --> <!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-data-jpa --> <!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-data-jpa --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-jpa</artifactId> </dependency> <!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-web --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <!-- https://mvnrepository.com/artifact/com.h2database/h2 --> <dependency> <groupId>com.h2database</groupId> <artifactId>h2</artifactId> </dependency> <!-- https://mvnrepository.com/artifact/org.projectlombok/lombok --> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <version>1.18.8</version> <scope>provided</scope> </dependency> </dependencies> <!--引入spring cloud的依賴--> <dependencyManagement> <dependencies> <!-- https://mvnrepository.com/artifact/org.springframework.cloud/spring-cloud-dependencies --> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-dependencies</artifactId> <version>Edgware.RELEASE</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement> <build> <plugins> <plugin> <!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-maven-plugin --> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project>
3.下載並安裝h2數據庫
h2數據庫下載鏈接:http://www.h2database.com/html/download.html
下載好直接減壓縮安裝
找到安裝目錄下的bin目錄打開這個文件夾運行h2.bat 或者h2w.bat(后台運行)
運行成功后 http://localhost:8082 訪問 H2 數據庫的 Web Console
win10系統角標里面選擇h2 console ,鼠標右鍵選擇 create new databases
創建成功后,記住創建的賬號名和密碼
4.創建application.yml配置文件
server:
port:8000
spring:
jpa:
# 是否生成ddl語句
generate-ddl: false
# 是否打印sql語句
show-sql: true
# 自動生成ddl,由於指定了具體的ddl,此處設置為none
hibernate:
ddl-auto: none
datasource:
# 使用H2數據庫
platform: h2
# 指定生成數據庫的schema文件位置
schema: classpath:schema.sql
# 指定插入數據庫語句的腳本位置
data: classpath:data.sql
# 數據庫地址
url: jdbc:h2:./test
#數據庫驅動
driver-class-name: org.h2.Driver
#數據庫用戶名
username: sa
#數據庫密碼
password: 123456
h2:
console:
settings:
# 進行該配置后,h2 web consloe就可以在遠程訪問了。否則只能在本機訪問。
web-allow-others: true
#進行該配置,你就可以通過YOUR_URL/h2訪問h2 web consloe。YOUR_URL是你程序的訪問URl。
path: /h2
#進行該配置,程序開啟時就會啟動h2 web consloe。當然這是默認的,如果你不想在啟動程序時啟動h2 web consloe,那么就設置為false。
enabled: true
# 配置日志打印信息
logging:
level:
root: INFO
org.hibernate: INFO
org.hibernate.type.descriptor.sql.BasicBinder: trace
org.hibernate.type.descriptor.sql.BasicExtractor: trace
5.創建實體類
package com.jjp.cloud.entity; import lombok.Data; import javax.persistence.*; import java.math.BigDecimal; @Entity @Data public class User { @Id @GeneratedValue(strategy = GenerationType.AUTO) private Long id; @Column private String username; @Column private String name; @Column private Integer age; @Column private BigDecimal balance; }
6.創建dao
package com.jjp.cloud.dao; import com.jjp.cloud.entity.User; import org.springframework.data.jpa.repository.JpaRepository; import org.springframework.stereotype.Repository; @Repository public interface UserRepository extends JpaRepository<User,Long> { }
7.創建controller層,進行接口訪問測試
package com.jjp.cloud.controller; import com.jjp.cloud.dao.UserRepository; import com.jjp.cloud.entity.User; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RestController; @RestController public class UserController { @Autowired private UserRepository userRepository; @GetMapping("/{id}") public User findByid(@PathVariable Long id){ User findOne = this.userRepository.findOne(id); return findOne; } }
8.瀏覽器訪問:http://localhost:8080/1 進行測試 返回下面結果就表明成功整合h2數據庫
如果有疑問可以發郵件到我郵箱進行交流探討:java_class_hello@163.com