<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<version>1.4.197</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
<version>2.1.3.RELEASE</version>
</dependency>
在课程级别添加 @Entity 注释。
使用注释 @Table 定义表名。
通过添加注释 @Id为实体定义 Id 。
通过在每个字段上方添加注释 @Column 来定义列,并指定列名称。
package com.lidihuo.microservices.currencyexchangeservice;
import java.math.BigDecimal;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Table;
@Entity
@Table(name="Exchange_Value")
public class ExchangeValue
{
@Id
@Column(name="id")
private long id;
@Column(name="currency_from")
private String from;
@Column(name="currency_to")
private String to;
@Column(name="conversion_multiple")
private BigDecimal conversionMultiple;
@Column(name="port")
private int port;
//default conatructor
public ExchangeValue()
{
}
//generating constructor using fields
public ExchangeValue(long id, String from, String to, BigDecimal conversionMultiple)
{
super();
this.id = id;
this.from = from;
this.to = to;
this.conversionMultiple = conversionMultiple;
}
//generating getters and setters
public int getPort()
{
return port;
}
public void setPort(int port)
{
this.port = port;
}
public long getId()
{
return id;
}
public String getFrom()
{
return from;
}
public String getTo()
{
return to;
}
public BigDecimal getConversionMultiple()
{
return conversionMultiple;
}
}
insert into exchange_value(id,currency_from,currency_to,conversion_multiple,port)
values(10001,'USD', 'INR' ,65,0);
insert into exchange_value(id,currency_from,currency_to,conversion_multiple,port)
values(10002,'EUR', 'INR' ,75,0);
insert into exchange_value(id,currency_from,currency_to,conversion_multiple,port)
values(10003,'AUD', 'INR' ,25,0);
spring.application.name=currency-exchange-service
server.port=8000
spring.jpa.show-sql=true
spring.h2.console.enabled=true
spring.datasource.platform=h2
spring.datasource.url=jdbc:h2:mem:lidihuo
在浏览器中,输入 http://localhost:8000/h2-console。它显示以下页面:
SELECT *FROM EXCHANGE_VALUE;