本文主要是介绍解决springboot+整合h2数据库,自动建表不支持驼峰命名规则的解决办法,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
问题描述:使用springboot+h2(内嵌式内存数据库),定义的User实体,可以动态生成表users,并完成crud操作,而定义的EurekaUser的实体,无法动态生成表eurekauser表,报xxxxxxx,no statment sql 等异常。
解决办法:
方法一:在定义的实体上面添加注解@Table(name=‘eurekauser’),指定要映射的表名。
package com.ljf.weifuwu.springcloud.test.model;import javax.persistence.*;
import java.math.BigDecimal;@Entity
@Table(name="eurekauser")
public class EurekaUser {@Id@GeneratedValue(strategy = GenerationType.AUTO)private Long id;@Columnprivate String username;@Columnprivate String name;@Columnprivate Short age;@Columnprivate BigDecimal balance;public Long getId() {return this.id;}public void setId(Long id) {this.id = id;}public String getUsername() {return this.username;}public void setUsername(String username) {this.username = username;}public String getName() {return this.name;}public void setName(String name) {this.name = name;}public Short getAge() {return this.age;}public void setAge(Short age) {this.age = age;}public BigDecimal getBalance() {return this.balance;}public void setBalance(BigDecimal balance) {this.balance = balance;}
}
Respository:
@Repository
public interface EurekaUserRespository extends JpaRepository<EurekaUser, Long> {
}
data.sql:
insert into eurekauser(id,username, name, age, balance) values(1,'user1', '张三', 20, 100.00);
insert into eurekauser(id,username, name, age, balance) values(2,'user2', '李四', 20, 100.00);
insert into eurekauser(id,username, name, age, balance) values(3,'user3', '王五', 20, 100.00);
insert into eurekauser(id,username, name, age, balance) values(4,'user4', '马六', 20, 100.00);=
schema.sql:
drop table eurekauser if exists;
create table eurekauser(id bigint generated by default as identity,username varchar(40),name varchar(20),age int(3),balance decimal(10,2), primary key(id)
);
访问:http://localhost:7900/simpleuser/1
方法二:定义的实体EurekaUser不安驼峰命名规则,将两个词当成一个词Eurekauser。
package com.ljf.weifuwu.springcloud.test.model;import javax.persistence.*;
import java.math.BigDecimal;@Entity
//@Table(name="eurekauser")
public class Eurekauser {@Id@GeneratedValue(strategy = GenerationType.AUTO)private Long id;@Columnprivate String username;@Columnprivate String name;@Columnprivate Short age;@Columnprivate BigDecimal balance;public Long getId() {return this.id;}public void setId(Long id) {this.id = id;}public String getUsername() {return this.username;}public void setUsername(String username) {this.username = username;}public String getName() {return this.name;}public void setName(String name) {this.name = name;}public Short getAge() {return this.age;}public void setAge(Short age) {this.age = age;}public BigDecimal getBalance() {return this.balance;}public void setBalance(BigDecimal balance) {this.balance = balance;}
}
Respository:
package com.ljf.weifuwu.springcloud.test.respository;import com.ljf.weifuwu.springcloud.test.model.Eurekauser;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;@Repository
public interface EurekaUserRespository extends JpaRepository<Eurekauser, Long> {
}
datasql:
insert into eurekauser(id,username, name, age, balance) values(1,'user1', '张三', 20, 100.00); insert into eurekauser(id,username, name, age, balance) values(2,'user2', '李四', 20, 100.00); insert into eurekauser(id,username, name, age, balance) values(3,'user3', '王五', 20, 100.00); insert into eurekauser(id,username, name, age, balance) values(4,'user4', '马六', 20, 100.00);
schema.sql:
drop table eurekauser if exists; create table eurekauser(id bigint generated by default as identity,username varchar(40),name varchar(20),age int(3),balance decimal(10,2), primary key(id) );
访问:http://localhost:7900/simpleuser/1
这篇关于解决springboot+整合h2数据库,自动建表不支持驼峰命名规则的解决办法的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!