[JAVA-6455] Update H2 code with latest Spring Boot changes

This commit is contained in:
Haroon Khan 2021-11-16 20:44:48 +00:00
parent af8deeaf19
commit 9e22d43bab
9 changed files with 43 additions and 107 deletions

View File

@ -2,8 +2,10 @@ package com.baeldung.h2db.springboot;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.PropertySource;
@SpringBootApplication
@PropertySource("classpath:application-h2.properties")
public class SpringBootH2Application {
public static void main(String... args) {

View File

@ -0,0 +1,7 @@
package com.baeldung.h2db.springboot.daos;
import com.baeldung.h2db.springboot.models.Country;
import org.springframework.data.repository.CrudRepository;
public interface CountryRepository extends CrudRepository<Country, Integer> {
}

View File

@ -1,10 +0,0 @@
package com.baeldung.h2db.springboot.daos;
import com.baeldung.h2db.springboot.models.User;
import org.springframework.data.repository.CrudRepository;
public interface UserRepository extends CrudRepository<User, Integer> {
}

View File

@ -0,0 +1,21 @@
package com.baeldung.h2db.springboot.models;
import lombok.Data;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.Table;
@Table(name = "countries")
@Entity
@Data
public class Country {
@Id
@GeneratedValue
private int id;
private String name;
}

View File

@ -1,54 +0,0 @@
package com.baeldung.h2db.springboot.models;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.Table;
@Table(name = "users")
@Entity
public class User {
@Id
@GeneratedValue
private int id;
private String firstName;
private String lastName;
public User() { }
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
@Override
public String toString() {
return "User{" +
"id=" + id +
", firstName='" + firstName + '\'' +
", lastName='" + lastName + '\'' +
'}';
}
}

View File

@ -0,0 +1 @@
spring.sql.init.data-locations=data-h2.sql

View File

@ -0,0 +1,5 @@
INSERT INTO countries (id, name) VALUES (1, 'USA');
INSERT INTO countries (id, name) VALUES (2, 'France');
INSERT INTO countries (id, name) VALUES (3, 'Brazil');
INSERT INTO countries (id, name) VALUES (4, 'Italy');
INSERT INTO countries (id, name) VALUES (5, 'Canada');

View File

@ -1,17 +1,3 @@
DROP TABLE IF EXISTS billionaires;
CREATE TABLE billionaires (
id INT AUTO_INCREMENT PRIMARY KEY,
first_name VARCHAR(250) NOT NULL,
last_name VARCHAR(250) NOT NULL,
career VARCHAR(250) DEFAULT NULL
);
INSERT INTO billionaires (first_name, last_name, career) VALUES
('Aliko', 'Dangote', 'Billionaire Industrialist'),
('Bill', 'Gates', 'Billionaire Tech Entrepreneur'),
('Folrunsho', 'Alakija', 'Billionaire Oil Magnate');
insert into USER values (101, 'user1', 'comment1');
insert into USER values (102, 'user2', 'comment2');
insert into USER values (103, 'user3', 'comment3');

View File

@ -1,50 +1,28 @@
package com.baeldung;
import com.baeldung.h2db.springboot.SpringBootH2Application;
import com.baeldung.h2db.springboot.daos.UserRepository;
import com.baeldung.h2db.springboot.models.User;
import com.baeldung.h2db.springboot.daos.CountryRepository;
import com.baeldung.h2db.springboot.models.Country;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
import java.util.List;
import static org.junit.Assert.*;
import static org.assertj.core.api.Assertions.assertThat;
@RunWith(SpringRunner.class)
@SpringBootTest(classes = SpringBootH2Application.class)
public class SpringBootH2IntegrationTest {
@Autowired
private UserRepository userRepository;
private CountryRepository countryRepository;
@Test
public void contextLoads() { }
public void givenInitData_whenApplicationStarts_thenDataIsLoaded() {
Iterable<Country> users = countryRepository.findAll();
@Test
public void givenUserProfile_whenAddUser_thenCreateNewUser() {
User user = new User();
user.setFirstName("John");
user.setLastName("Doe");
userRepository.save(user);
List<User> users = (List<User>) userRepository.findAll();
assertFalse(users.isEmpty());
String firstName = "Aliko";
String lastName = "Dangote";
User user1 = userRepository.findById(users.get(0).getId()).get();
user1.setLastName(lastName);
user1.setFirstName(firstName);
userRepository.save(user1);
user = userRepository.findById(user.getId()).get();
assertEquals(user.getFirstName(), firstName);
assertEquals(user.getLastName(), lastName);
userRepository.deleteById(user.getId());
assertTrue( ((List<User>) userRepository.findAll()).isEmpty());
assertThat(users).hasSize(5);
}
}