一、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