【问题标题】:How to insert data (string array) to h2 database?如何将数据(字符串数组)插入 h2 数据库?
【发布时间】:2019-07-18 15:32:44
【问题描述】:

我的 JPA 实体类中有一个字符串数组,声明如下

private String[] suggestion = new String[3];

我通过在我的 'resource' 文件夹中创建 sql 文件来插入数据。如何使用INSERT INTO .. VALUES (..) ???将字符串数组插入数据库

【问题讨论】:

  • 您可以通过在data.sql 文件中编写多个插入语句来做到这一点。或者解释一下你的桌子应该是什么样子?

标签: spring spring-boot spring-data-jpa spring-data h2


【解决方案1】:

您可以使用 H2 检查这个工作示例,因为我已经对其进行了测试。

首先在 application.properties 文件中添加这些属性:

# To See H2 Console in Browser:
# http://localhost:8080/h2-console
# Enabling H2 Console
spring.h2.console.enabled=true

# ===============================
# DB
# ===============================

spring.datasource.url=jdbc:h2:~/test
#jdbc:h2:mem:testdb
spring.datasource.driverClassName=org.h2.Driver
spring.datasource.username=sa
spring.datasource.password=

# ===============================
# JPA / HIBERNATE
# ===============================

spring.jpa.show-sql=true
spring.jpa.hibernate.ddl-auto=update
spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.H2Dialect

控制器代码:

@RestController
public class PersonController {
    @Autowired
    private PersonService personService;

    @PostMapping(value = "/test")
    public Person checkTest() {
        System.out.println("PWan");
        return personService.savePerson();
    }

}

服务代码:

@Service
public class PersonService {

    @Autowired
    private PersonRepository personRepository;

    public Person savePerson() {
        Person p = new Person();
        p.setFullname("Pawan");
        personRepository.save(p);
        return p;

    }

}

存储库代码:

public interface PersonRepository extends JpaRepository<Person, Long>{

    public Person findByfullname(String name);

    public Optional<Person> findById(Long id);

}

请参考以下链接:

Spring boot with H2 DB My git

如果有任何问题,请告诉我。

【讨论】:

  • @pindo,很高兴听到这个消息。请考虑投票并接受答案,以便其他人发现它有用。
  • 不幸的是,这个答案没有显示如何使用 JPA 将字符串数组实际插入到 h2 数据库中。
【解决方案2】:

你可以实现一个Application Listener来监听spring的Context Refreshed Event并重写onApplicationEvent()方法来将你的数据插入到数据库中。

import org.springframework.context.ApplicationListener;
import org.springframework.context.event.ContextRefreshedEvent;
import org.springframework.stereotype.Component;

    @Component
        public class Bootstrap implements ApplicationListener<ContextRefreshedEvent> {
            //    Array Initialization
            private String[] suggestion = new String[3];

            @Override
            public void onApplicationEvent(ContextRefreshedEvent contextRefreshedEvent) {
                initData();
            }

            private void initData(){
        //        Loop through array
                for (String item : suggestion){
        //            Insert data using a repository
                }
            }
        }

当 ApplicationContext 被初始化或刷新时引发此事件。 Link 到 java 文档。

【讨论】:

  • 你认为应该在哪里填写建议?
  • 这取决于您要在哪里填充建议。您可以为建议创建一个实体,以便 spring 上下文将生成架构。因此,您可以使用 Spring 数据 crud 存储库来持久化建议。查看代码注释 // 使用存储库插入数据
猜你喜欢
  • 2017-03-09
  • 1970-01-01
  • 2013-08-20
  • 2020-05-24
  • 2021-11-20
  • 2022-12-20
  • 1970-01-01
  • 1970-01-01
  • 2020-08-01
相关资源
最近更新 更多