Skip to content

Commit

Permalink
Persistent embedded H2 db added
Browse files Browse the repository at this point in the history
  • Loading branch information
lhhong committed Feb 2, 2017
1 parent e212a67 commit 19adb4e
Show file tree
Hide file tree
Showing 8 changed files with 109 additions and 1 deletion.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,4 @@ ethereum/privateDB
logs/
privateDB/
ethereum/src/main/resources/user.conf
datafile.mv.db
19 changes: 19 additions & 0 deletions ethereum/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,25 @@
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>

<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
</dependency>

<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.16.6</version>
</dependency>

<dependency>
<groupId>com.alibaba</groupId>
Expand Down
2 changes: 2 additions & 0 deletions ethereum/src/main/java/com/crumbs/Application.java
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
package com.crumbs;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.scheduling.annotation.EnableScheduling;

@SpringBootApplication
@EnableScheduling
@EnableAutoConfiguration
public class Application {

public static void main(String[] args) {
Expand Down
22 changes: 22 additions & 0 deletions ethereum/src/main/java/com/crumbs/models/Test.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package com.crumbs.models;

import lombok.Getter;
import lombok.Setter;

import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Table;
import java.io.Serializable;

/**
* Created by low on 2/2/17 12:01 PM.
*/
@Getter
@Setter
@Entity
@Table (name = "test")
public class Test implements Serializable {
@Id
private long id;
private String myString;
}
13 changes: 13 additions & 0 deletions ethereum/src/main/java/com/crumbs/repositories/TestRepo.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package com.crumbs.repositories;

import com.crumbs.models.Test;
import org.springframework.data.repository.CrudRepository;
import org.springframework.stereotype.Repository;

/**
* Created by low on 2/2/17 12:03 PM.
*/

@Repository
public interface TestRepo extends CrudRepository<Test, Long> {
}
24 changes: 23 additions & 1 deletion ethereum/src/main/java/com/crumbs/rest/MyRestController.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@


import com.crumbs.ethereum.EthereumBean;
import org.ethereum.crypto.ECKey;
import com.crumbs.models.Test;
import com.crumbs.repositories.TestRepo;
import com.crumbs.services.TestService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

Expand All @@ -15,6 +17,12 @@
@RestController
public class MyRestController {

@Autowired
TestRepo testRepo;

@Autowired
TestService testService;

@Autowired
EthereumBean ethereumBean;

Expand Down Expand Up @@ -42,4 +50,18 @@ public void sendMockTx(@RequestParam ("sender") String sender, @RequestParam("re
ethereumBean.sendMockTx(sender, receiver);
}

@RequestMapping(value = "/test", method = POST, produces = APPLICATION_JSON_VALUE)
@ResponseBody
public void saveRandom(@RequestParam ("id") long id, @RequestParam("msg") String msg) {
Test t = new Test();
t.setId(id);
t.setMyString(msg);
testRepo.save(t);
}

@RequestMapping(value = "/test/{id}", method = GET, produces = APPLICATION_JSON_VALUE)
@ResponseBody
public Test getRandom(@PathVariable ("id") long id) {
return testService.getById(id);
}
}
20 changes: 20 additions & 0 deletions ethereum/src/main/java/com/crumbs/services/TestService.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package com.crumbs.services;

import com.crumbs.models.Test;
import com.crumbs.repositories.TestRepo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
* Created by low on 2/2/17 12:10 PM.
*/
@Service
public class TestService {

@Autowired
TestRepo testRepo;

public Test getById(long id) {
return testRepo.findOne(id);
}
}
9 changes: 9 additions & 0 deletions ethereum/src/main/resources/application.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
spring.datasource.url=jdbc:h2:file:./datafile;DB_CLOSE_ON_EXIT=FALSE
spring.datasource.username=test
spring.datasource.password=test
spring.datasource.driverClassName=org.h2.Driver
spring.jpa.hibernate.ddl-auto = update
spring.jpa.database = H2
spring.jpa.show-sql = true
spring.jpa.hibernate.naming-strategy = org.hibernate.cfg.ImprovedNamingStrategy
spring.jpa.properties.hibernate.dialect = org.hibernate.dialect.H2Dialect

0 comments on commit 19adb4e

Please sign in to comment.