[JAVA-2306] Moved articles from spring-persistence-simple-2

* https://www.baeldung.com/spring-jdbctemplate-testing went to spring-jdbc

* https://www.baeldung.com/spring-jdbctemplate-in-list went to spring-jdbc

* https://www.baeldung.com/spring-mock-jndi-datasource went to spring-persistence-simple

* Deleted spring-persistence-simple-2 module as all articles have been moved
This commit is contained in:
fdpro 2020-08-30 15:59:35 +02:00
parent 4e4ac650fa
commit eb4c306451
30 changed files with 224 additions and 89 deletions

View File

@ -86,7 +86,7 @@
<module>spring-jpa-2</module> <module>spring-jpa-2</module>
<module>spring-jdbc</module> <module>spring-jdbc</module>
<!-- <module>spring-mybatis</module> --> <!-- needs fixing in BAEL-9021 --> <!-- <module>spring-mybatis</module> --> <!-- needs fixing in BAEL-9021 -->
<module>spring-persistence-simple-2</module> <module>spring-persistence-simple</module>
</modules> </modules>
<properties> <properties>

View File

@ -1,4 +1,4 @@
package com.baeldung.spring.jdbc; package com.baeldung.spring.jdbc.template.guide;
import java.sql.SQLException; import java.sql.SQLException;

View File

@ -1,4 +1,4 @@
package com.baeldung.spring.jdbc; package com.baeldung.spring.jdbc.template.guide;
public class Employee { public class Employee {
private int id; private int id;

View File

@ -1,4 +1,4 @@
package com.baeldung.spring.jdbc; package com.baeldung.spring.jdbc.template.guide;
import java.sql.PreparedStatement; import java.sql.PreparedStatement;
import java.sql.SQLException; import java.sql.SQLException;

View File

@ -1,4 +1,4 @@
package com.baeldung.spring.jdbc; package com.baeldung.spring.jdbc.template.guide;
import java.sql.ResultSet; import java.sql.ResultSet;
import java.sql.SQLException; import java.sql.SQLException;

View File

@ -1,4 +1,4 @@
package com.baeldung.spring.jdbc.config; package com.baeldung.spring.jdbc.template.guide.config;
import javax.sql.DataSource; import javax.sql.DataSource;
@ -17,8 +17,8 @@ public class SpringJdbcConfig {
public DataSource dataSource() { public DataSource dataSource() {
return new EmbeddedDatabaseBuilder() return new EmbeddedDatabaseBuilder()
.setType(EmbeddedDatabaseType.H2) .setType(EmbeddedDatabaseType.H2)
.addScript("classpath:jdbc/schema.sql") .addScript("classpath:com/baeldung/spring/jdbc/template/guide/schema.sql")
.addScript("classpath:jdbc/test-data.sql") .addScript("classpath:com/baeldung/spring/jdbc/template/guide/test-data.sql")
.build(); .build();
} }

View File

@ -0,0 +1,42 @@
package com.baeldung.spring.jdbc.template.inclause;
public class Employee {
private int id;
private String firstName;
private String lastName;
public Employee(int id, String firstName, String lastName) {
this.id = id;
this.firstName = firstName;
this.lastName = lastName;
}
public int getId() {
return id;
}
public void setId(final int id) {
this.id = id;
}
public String getFirstName() {
return firstName;
}
public void setFirstName(final String firstName) {
this.firstName = firstName;
}
public String getLastName() {
return lastName;
}
public void setLastName(final String lastName) {
this.lastName = lastName;
}
}

View File

@ -1,10 +1,4 @@
package com.baeldung.spring.jdbc; package com.baeldung.spring.jdbc.template.inclause;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import javax.sql.DataSource;
import org.springframework.jdbc.core.JdbcTemplate; import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.namedparam.MapSqlParameterSource; import org.springframework.jdbc.core.namedparam.MapSqlParameterSource;
@ -12,6 +6,11 @@ import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
import org.springframework.jdbc.core.namedparam.SqlParameterSource; import org.springframework.jdbc.core.namedparam.SqlParameterSource;
import org.springframework.stereotype.Repository; import org.springframework.stereotype.Repository;
import javax.sql.DataSource;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
@Repository @Repository
public class EmployeeDAO { public class EmployeeDAO {
private JdbcTemplate jdbcTemplate; private JdbcTemplate jdbcTemplate;
@ -22,10 +21,6 @@ public class EmployeeDAO {
namedJdbcTemplate = new NamedParameterJdbcTemplate(dataSource); namedJdbcTemplate = new NamedParameterJdbcTemplate(dataSource);
} }
public int getCountOfEmployees() {
return jdbcTemplate.queryForObject("SELECT COUNT(*) FROM EMPLOYEE", Integer.class);
}
public List<Employee> getEmployeesFromIdListNamed(List<Integer> ids) { public List<Employee> getEmployeesFromIdListNamed(List<Integer> ids) {
SqlParameterSource parameters = new MapSqlParameterSource("ids", ids); SqlParameterSource parameters = new MapSqlParameterSource("ids", ids);
List<Employee> employees = namedJdbcTemplate.query( List<Employee> employees = namedJdbcTemplate.query(
@ -63,5 +58,4 @@ public class EmployeeDAO {
return employees; return employees;
} }
} }

View File

@ -1,4 +1,4 @@
package com.baeldung.spring.jdbc; package com.baeldung.spring.jdbc.template.testing;
public class Employee { public class Employee {
private int id; private int id;

View File

@ -0,0 +1,19 @@
package com.baeldung.spring.jdbc.template.testing;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;
import javax.sql.DataSource;
@Repository
public class EmployeeDAO {
private JdbcTemplate jdbcTemplate;
public void setDataSource(DataSource dataSource) {
jdbcTemplate = new JdbcTemplate(dataSource);
}
public int getCountOfEmployees() {
return jdbcTemplate.queryForObject("SELECT COUNT(*) FROM EMPLOYEE", Integer.class);
}
}

View File

@ -0,0 +1,7 @@
CREATE TABLE EMPLOYEE
(
ID int NOT NULL PRIMARY KEY,
FIRST_NAME varchar(255),
LAST_NAME varchar(255),
ADDRESS varchar(255)
);

View File

@ -0,0 +1,7 @@
INSERT INTO EMPLOYEE VALUES (1, 'James', 'Gosling', 'Canada');
INSERT INTO EMPLOYEE VALUES (2, 'Donald', 'Knuth', 'USA');
INSERT INTO EMPLOYEE VALUES (3, 'Linus', 'Torvalds', 'Finland');
INSERT INTO EMPLOYEE VALUES (4, 'Dennis', 'Ritchie', 'USA');

View File

@ -0,0 +1,7 @@
CREATE TABLE EMPLOYEE
(
ID int NOT NULL PRIMARY KEY,
FIRST_NAME varchar(255),
LAST_NAME varchar(255),
ADDRESS varchar(255)
);

View File

@ -0,0 +1,7 @@
INSERT INTO EMPLOYEE VALUES (1, 'James', 'Gosling', 'Canada');
INSERT INTO EMPLOYEE VALUES (2, 'Donald', 'Knuth', 'USA');
INSERT INTO EMPLOYEE VALUES (3, 'Linus', 'Torvalds', 'Finland');
INSERT INTO EMPLOYEE VALUES (4, 'Dennis', 'Ritchie', 'USA');

View File

@ -1,9 +1,11 @@
package com.baeldung.spring.jdbc; package com.baeldung.spring.jdbc.template.guide;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
import com.baeldung.spring.jdbc.config.SpringJdbcConfig; import com.baeldung.spring.jdbc.template.guide.Employee;
import com.baeldung.spring.jdbc.template.guide.EmployeeDAO;
import com.baeldung.spring.jdbc.template.guide.config.SpringJdbcConfig;
import org.junit.Assert; import org.junit.Assert;
import org.junit.Test; import org.junit.Test;
import org.junit.runner.RunWith; import org.junit.runner.RunWith;

View File

@ -1,22 +1,19 @@
package com.baeldung.spring.jdbc; package com.baeldung.spring.jdbc.template.inclause;
import static org.junit.jupiter.api.Assertions.assertEquals;
import java.util.ArrayList;
import java.util.List;
import javax.sql.DataSource;
import org.junit.Before; import org.junit.Before;
import org.junit.Test; import org.junit.Test;
import org.junit.runner.RunWith; import org.junit.runner.RunWith;
import org.mockito.Mock; import org.mockito.Mock;
import org.mockito.Mockito;
import org.mockito.junit.MockitoJUnitRunner; import org.mockito.junit.MockitoJUnitRunner;
import org.springframework.jdbc.core.JdbcTemplate; import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseBuilder; import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseBuilder;
import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseType; import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseType;
import org.springframework.test.util.ReflectionTestUtils;
import javax.sql.DataSource;
import java.util.ArrayList;
import java.util.List;
import static org.junit.jupiter.api.Assertions.assertEquals;
@RunWith(MockitoJUnitRunner.class) @RunWith(MockitoJUnitRunner.class)
@ -30,33 +27,9 @@ public class EmployeeDAOUnitTest {
public void setup() { public void setup() {
dataSource = new EmbeddedDatabaseBuilder().setType(EmbeddedDatabaseType.H2) dataSource = new EmbeddedDatabaseBuilder().setType(EmbeddedDatabaseType.H2)
.generateUniqueName(true) .generateUniqueName(true)
.addScript("classpath:jdbc/schema.sql") .addScript("classpath:com/baeldung/spring/jdbc/template/inclause/schema.sql")
.addScript("classpath:jdbc/test-data.sql") .addScript("classpath:com/baeldung/spring/jdbc/template/inclause/test-data.sql")
.build(); .build();
}
@Test
public void whenMockJdbcTemplate_thenReturnCorrectEmployeeCount() {
EmployeeDAO employeeDAO = new EmployeeDAO();
ReflectionTestUtils.setField(employeeDAO, "jdbcTemplate", jdbcTemplate);
Mockito.when(jdbcTemplate.queryForObject("SELECT COUNT(*) FROM EMPLOYEE", Integer.class))
.thenReturn(4);
assertEquals(4, employeeDAO.getCountOfEmployees());
Mockito.when(jdbcTemplate.queryForObject(Mockito.anyString(), Mockito.eq(Integer.class)))
.thenReturn(3);
assertEquals(3, employeeDAO.getCountOfEmployees());
}
@Test
public void whenInjectInMemoryDataSource_thenReturnCorrectEmployeeCount() {
EmployeeDAO employeeDAO = new EmployeeDAO();
employeeDAO.setDataSource(dataSource);
assertEquals(4, employeeDAO.getCountOfEmployees());
} }
@Test @Test

View File

@ -0,0 +1,56 @@
package com.baeldung.spring.jdbc.template.testing;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.Mockito;
import org.mockito.junit.MockitoJUnitRunner;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseBuilder;
import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseType;
import org.springframework.test.util.ReflectionTestUtils;
import javax.sql.DataSource;
import static org.junit.jupiter.api.Assertions.assertEquals;
@RunWith(MockitoJUnitRunner.class)
public class EmployeeDAOUnitTest {
@Mock
JdbcTemplate jdbcTemplate;
DataSource dataSource;
@Before
public void setup() {
dataSource = new EmbeddedDatabaseBuilder().setType(EmbeddedDatabaseType.H2)
.generateUniqueName(true)
.addScript("classpath:com/baeldung/spring/jdbc/template/testing/schema.sql")
.addScript("classpath:com/baeldung/spring/jdbc/template/testing/test-data.sql")
.build();
}
@Test
public void whenMockJdbcTemplate_thenReturnCorrectEmployeeCount() {
EmployeeDAO employeeDAO = new EmployeeDAO();
ReflectionTestUtils.setField(employeeDAO, "jdbcTemplate", jdbcTemplate);
Mockito.when(jdbcTemplate.queryForObject("SELECT COUNT(*) FROM EMPLOYEE", Integer.class))
.thenReturn(4);
assertEquals(4, employeeDAO.getCountOfEmployees());
Mockito.when(jdbcTemplate.queryForObject(Mockito.anyString(), Mockito.eq(Integer.class)))
.thenReturn(3);
assertEquals(3, employeeDAO.getCountOfEmployees());
}
@Test
public void whenInjectInMemoryDataSource_thenReturnCorrectEmployeeCount() {
EmployeeDAO employeeDAO = new EmployeeDAO();
employeeDAO.setDataSource(dataSource);
assertEquals(4, employeeDAO.getCountOfEmployees());
}
}

View File

@ -1,6 +0,0 @@
### Relevant Articles:
- [Spring JdbcTemplate Unit Testing](https://www.baeldung.com/spring-jdbctemplate-testing)
- [Using a List of Values in a JdbcTemplate IN Clause](https://www.baeldung.com/spring-jdbctemplate-in-list)
- [Transactional Annotations: Spring vs. JTA](https://www.baeldung.com/spring-vs-jta-transactional)
- [Test a Mock JNDI Datasource with Spring](https://www.baeldung.com/spring-mock-jndi-datasource)

View File

@ -1,6 +0,0 @@
CREATE TABLE EMPLOYEE
(
ID int NOT NULL PRIMARY KEY,
FIRST_NAME varchar(255),
LAST_NAME varchar(255)
);

View File

@ -1,4 +0,0 @@
INSERT INTO EMPLOYEE VALUES (1, 'James', 'Gosling');
INSERT INTO EMPLOYEE VALUES (2, 'Donald', 'Knuth');
INSERT INTO EMPLOYEE VALUES (3, 'Linus', 'Torvalds');
INSERT INTO EMPLOYEE VALUES (4, 'Dennis', 'Ritchie');

View File

@ -0,0 +1,13 @@
*.class
#folders#
/target
/neoDb*
/data
/src/main/webapp/WEB-INF/classes
*/META-INF/*
# Packaged files #
*.jar
*.war
*.ear

View File

@ -0,0 +1,25 @@
=========
## Spring Persistence Example Project
### Relevant Articles:
- [A Guide to JPA with Spring](https://www.baeldung.com/the-persistence-layer-with-spring-and-jpa)
- [Bootstrapping Hibernate 5 with Spring](http://www.baeldung.com/hibernate-5-spring)
- [The DAO with Spring and Hibernate](https://www.baeldung.com/persistence-layer-with-spring-and-hibernate)
- [Simplify the DAO with Spring and Java Generics](https://www.baeldung.com/simplifying-the-data-access-layer-with-spring-and-java-generics)
- [Transactions with Spring and JPA](https://www.baeldung.com/transaction-configuration-with-jpa-and-spring)
- [Introduction to Spring Data JPA](http://www.baeldung.com/the-persistence-layer-with-spring-data-jpa)
- [Spring Data JPA @Query](http://www.baeldung.com/spring-data-jpa-query)
- [Spring JDBC](https://www.baeldung.com/spring-jdbc-jdbctemplate)
- [Transaction Propagation and Isolation in Spring @Transactional](https://www.baeldung.com/spring-transactional-propagation-isolation)
### Eclipse Config
After importing the project into Eclipse, you may see the following error:
"No persistence xml file found in project"
This can be ignored:
- Project -> Properties -> Java Persistance -> JPA -> Error/Warnings -> Select Ignore on "No persistence xml file found in project"
Or:
- Eclipse -> Preferences - Validation - disable the "Build" execution of the JPA Validator

View File

@ -2,9 +2,9 @@
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion> <modelVersion>4.0.0</modelVersion>
<artifactId>spring-persistence-simple-2</artifactId> <artifactId>spring-persistence-simple</artifactId>
<version>0.1-SNAPSHOT</version> <version>0.1-SNAPSHOT</version>
<name>spring-persistence-simple-2</name> <name>spring-persistence-simple</name>
<parent> <parent>
<groupId>com.baeldung</groupId> <groupId>com.baeldung</groupId>
@ -65,5 +65,4 @@
<!-- test scoped --> <!-- test scoped -->
<mockito.version>3.3.3</mockito.version> <mockito.version>3.3.3</mockito.version>
</properties> </properties>
</project> </project>

View File

@ -3,4 +3,4 @@ org.osjava.sj.jndi.shared=true
org.osjava.sj.delimiter=. org.osjava.sj.delimiter=.
jndi.syntax.separator=/ jndi.syntax.separator=/
org.osjava.sj.space=java:/comp/env org.osjava.sj.space=java:/comp/env
org.osjava.sj.root=src/main/resources/jndi org.osjava.sj.root=src/main/resources/com/baeldung/spring/jndi/datasource/mock

View File

@ -1,4 +1,4 @@
package com.baeldung.jndi.datasource; package com.baeldung.spring.jndi.datasource.mock;
import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertEquals;

View File

@ -1,4 +1,4 @@
package com.baeldung.jndi.datasource; package com.baeldung.spring.jndi.datasource.mock;
import static org.junit.Assert.assertNotNull; import static org.junit.Assert.assertNotNull;