我正在做一个简单的个人项目,以学习SpringBoot.项目概述:获取数据并通过RestController端点将数据插入Docker上的MariaDb示例的Sping Boot 应用程序。
我的MariaDb表如下所示:
\+------------------+------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
\+------------------+------------+------+-----+---------+----------------+
| ACCOUNT_ID | int(11) | NO | PRI | NULL | auto_increment |
| ACCOUNT_VALUE | int(11) | YES | | 0 | |
| ACCOUNT_CURRENCY | varchar(5) | YES | | NULL | |
\+------------------+------------+------+-----+---------+----------------+
我的表有数据(我从终端手动插入):
MariaDB \[dev\]\> select \* from CUSTOMER_ACCOUNT;
\+------------+---------------+------------------+
| ACCOUNT_ID | ACCOUNT_VALUE | ACCOUNT_CURRENCY |
\+------------+---------------+------------------+
| 1 | 300 | RON |
| 2 | 300 | RON |
| 3 | 300 | RON |
| 4 | 300 | RON |
| 5 | 300 | RON |
| 6 | 300 | RON |
| 7 | 300 | RON |
| 8 | 300 | RON |
| 9 | 300 | RON |
| 10 | 300 | RON |
| 11 | 300 | RON |
| 12 | 300 | RON |
| 13 | 300 | RON |
| 14 | 300 | RON |
| 15 | 300 | RON |
| 16 | 300 | RON |
| 17 | 300 | RON |
| 18 | 300 | RON |
\+------------+---------------+------------------+
我的实体类是这样的:
@Entity
@Table(name = "CUSTOMER_ACCOUNT", schema = "dev", catalog = "")
public class CustomerAccountEntity {
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Id
@Column(name = "ACCOUNT_ID")
private int accountId;
@Basic
@Column(name = "ACCOUNT_VALUE")
private Integer accountValue;
@Basic
@Column(name = "ACCOUNT_CURRENCY")
private String accountCurrency;
public int getAccountId() {
return accountId;
}
public void setAccountId(int accountId) {
this.accountId = accountId;
}
public Integer getAccountValue() {
return accountValue;
}
public void setAccountValue(Integer accountValue) {
this.accountValue = accountValue;
}
public String getAccountCurrency() {
return accountCurrency;
}
public void setAccountCurrency(String accountCurrency) {
this.accountCurrency = accountCurrency;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
CustomerAccountEntity that = (CustomerAccountEntity) o;
return accountId == that.accountId && Objects.equals(accountValue, that.accountValue) && Objects.equals(accountCurrency, that.accountCurrency);
}
@Override
public int hashCode() {
return Objects.hash(accountId, accountValue, accountCurrency);
}
}
我的存储库类是这样的:
@Repository
public interface CustomerAccountRepository extends JpaRepository\<CustomerAccountEntity,Long\> {
@Query("Select ca.accountId, ca.accountValue, ca.accountCurrency from CustomerAccountEntity ca")
List<CustomerAccountEntity> findAllCA();
}
我的设置是:
# connect via localhost on port 3306
spring.datasource.url=jdbc:mariadb://localhost:3306/dev
spring.datasource.username=root
spring.datasource.password=mypass
spring.jpa.properties.hibernate.globally_quoted_identifiers=true
spring.jpa.hibernate.ddl-auto=create-drop
spring.jpa.show-sql=true
spring.datasource.driver=cdata.jdbc.mariadb.MariaDBDriver
当我点击控制器检索所有数据时,它在浏览器中返回空。
我尝试使用设置来更改驱动程序和休眠方言。注意:我确实成功连接到DB
当我启动应用程序时,mariadb中会弹出一个新表
| Tables_in_dev |
+------------------+
| CUSTOMER_ACCOUNT |
| customer_account |
+------------------+
2 rows in set (0.000 sec)
Hibernate调用是小写的,但是这个是空的。好像是我停止应用程序时删除的。
我还在学习...但有人能解释一下发生了什么吗?
2条答案
按热度按时间r8uurelv1#
spring.jpa.hibernate.ddl-auto=create-drop;
如果在属性文件中包含此属性,则每次重新启动或运行spring-boot应用程序时,都会删除表中的所有现有内容。要保留表中的所有记录,请更改为spring.jpa.hibernate.ddl-auto=update;
7fhtutme2#
您的存储库的id类型为
Long
,而实体的id类型为int
。请尝试使它们都具有相同的类型Long
为了避免在Docker中使用mariadb时丢失数据,您应该创建一个Docker卷,并在创建容器时将其插入/var/lib/mysql/data