synced with master
This commit is contained in:
parent
37101781ae
commit
4b4ac40345
@ -0,0 +1,87 @@
|
|||||||
|
package com.baeldung.connectionpool.connectionpools;
|
||||||
|
|
||||||
|
import java.sql.Connection;
|
||||||
|
import java.sql.DriverManager;
|
||||||
|
import java.sql.SQLException;
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
public class BasicConnectionPool implements ConnectionPool {
|
||||||
|
|
||||||
|
private final String url;
|
||||||
|
private final String user;
|
||||||
|
private final String password;
|
||||||
|
private final List<Connection> connectionPool;
|
||||||
|
private final List<Connection> usedConnections = new ArrayList<>();
|
||||||
|
private static final int INITIAL_POOL_SIZE = 10;
|
||||||
|
private final int MAX_POOL_SIZE = 20;
|
||||||
|
|
||||||
|
public static BasicConnectionPool create(String url, String user, String password) throws SQLException {
|
||||||
|
List<Connection> pool = new ArrayList<>(INITIAL_POOL_SIZE);
|
||||||
|
for (int i = 0; i < INITIAL_POOL_SIZE; i++) {
|
||||||
|
pool.add(createConnection(url, user, password));
|
||||||
|
}
|
||||||
|
return new BasicConnectionPool(url, user, password, pool);
|
||||||
|
}
|
||||||
|
|
||||||
|
private BasicConnectionPool(String url, String user, String password, List<Connection> connectionPool) {
|
||||||
|
this.url = url;
|
||||||
|
this.user = user;
|
||||||
|
this.password = password;
|
||||||
|
this.connectionPool = connectionPool;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Connection getConnection() throws SQLException {
|
||||||
|
if (connectionPool.size() == 0) {
|
||||||
|
if (usedConnections.size() < MAX_POOL_SIZE) {
|
||||||
|
connectionPool.add(createConnection(url, user, password));
|
||||||
|
} else {
|
||||||
|
throw new RuntimeException("Maximum pool size reached, no available connections!");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
Connection connection = connectionPool.remove(connectionPool.size() - 1);
|
||||||
|
usedConnections.add(connection);
|
||||||
|
return connection;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean releaseConnection(Connection connection) {
|
||||||
|
connectionPool.add(connection);
|
||||||
|
return usedConnections.remove(connection);
|
||||||
|
}
|
||||||
|
|
||||||
|
private static Connection createConnection(String url, String user, String password) throws SQLException {
|
||||||
|
return DriverManager.getConnection(url, user, password);
|
||||||
|
}
|
||||||
|
|
||||||
|
public int getSize() {
|
||||||
|
return connectionPool.size() + usedConnections.size();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String getUrl() {
|
||||||
|
return url;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String getUser() {
|
||||||
|
return user;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String getPassword() {
|
||||||
|
return password;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void shutdown() throws SQLException {
|
||||||
|
for (Connection c : usedConnections) {
|
||||||
|
this.releaseConnection(c);
|
||||||
|
}
|
||||||
|
for (Connection c : connectionPool) {
|
||||||
|
c.close();
|
||||||
|
}
|
||||||
|
connectionPool.clear();
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,28 @@
|
|||||||
|
package com.baeldung.connectionpool.connectionpools;
|
||||||
|
|
||||||
|
import com.mchange.v2.c3p0.ComboPooledDataSource;
|
||||||
|
import java.beans.PropertyVetoException;
|
||||||
|
import java.sql.Connection;
|
||||||
|
import java.sql.SQLException;
|
||||||
|
|
||||||
|
public class C3poDataSource {
|
||||||
|
|
||||||
|
private static final ComboPooledDataSource cpds = new ComboPooledDataSource();
|
||||||
|
|
||||||
|
static {
|
||||||
|
try {
|
||||||
|
cpds.setDriverClass("org.h2.Driver");
|
||||||
|
cpds.setJdbcUrl("jdbc:h2:mem:test");
|
||||||
|
cpds.setUser("user");
|
||||||
|
cpds.setPassword("password");
|
||||||
|
} catch (PropertyVetoException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public static Connection getConnection() throws SQLException {
|
||||||
|
return cpds.getConnection();
|
||||||
|
}
|
||||||
|
|
||||||
|
private C3poDataSource(){}
|
||||||
|
}
|
@ -0,0 +1,18 @@
|
|||||||
|
package com.baeldung.connectionpool.connectionpools;
|
||||||
|
|
||||||
|
import java.sql.Connection;
|
||||||
|
import java.sql.SQLException;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
public interface ConnectionPool {
|
||||||
|
|
||||||
|
Connection getConnection() throws SQLException;
|
||||||
|
|
||||||
|
boolean releaseConnection(Connection connection);
|
||||||
|
|
||||||
|
String getUrl();
|
||||||
|
|
||||||
|
String getUser();
|
||||||
|
|
||||||
|
String getPassword();
|
||||||
|
}
|
@ -0,0 +1,25 @@
|
|||||||
|
package com.baeldung.connectionpool.connectionpools;
|
||||||
|
|
||||||
|
import java.sql.Connection;
|
||||||
|
import java.sql.SQLException;
|
||||||
|
import org.apache.commons.dbcp2.BasicDataSource;
|
||||||
|
|
||||||
|
public class DBCPDataSource {
|
||||||
|
|
||||||
|
private static final BasicDataSource ds = new BasicDataSource();
|
||||||
|
|
||||||
|
static {
|
||||||
|
ds.setUrl("jdbc:h2:mem:test");
|
||||||
|
ds.setUsername("user");
|
||||||
|
ds.setPassword("password");
|
||||||
|
ds.setMinIdle(5);
|
||||||
|
ds.setMaxIdle(10);
|
||||||
|
ds.setMaxOpenPreparedStatements(100);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static Connection getConnection() throws SQLException {
|
||||||
|
return ds.getConnection();
|
||||||
|
}
|
||||||
|
|
||||||
|
private DBCPDataSource(){}
|
||||||
|
}
|
@ -0,0 +1,28 @@
|
|||||||
|
package com.baeldung.connectionpool.connectionpools;
|
||||||
|
|
||||||
|
import com.zaxxer.hikari.HikariConfig;
|
||||||
|
import com.zaxxer.hikari.HikariDataSource;
|
||||||
|
import java.sql.Connection;
|
||||||
|
import java.sql.SQLException;
|
||||||
|
|
||||||
|
public class HikariCPDataSource {
|
||||||
|
|
||||||
|
private static final HikariConfig config = new HikariConfig();
|
||||||
|
private static final HikariDataSource ds;
|
||||||
|
|
||||||
|
static {
|
||||||
|
config.setJdbcUrl("jdbc:h2:mem:test");
|
||||||
|
config.setUsername("user");
|
||||||
|
config.setPassword("password");
|
||||||
|
config.addDataSourceProperty("cachePrepStmts", "true");
|
||||||
|
config.addDataSourceProperty("prepStmtCacheSize", "250");
|
||||||
|
config.addDataSourceProperty("prepStmtCacheSqlLimit", "2048");
|
||||||
|
ds = new HikariDataSource(config);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static Connection getConnection() throws SQLException {
|
||||||
|
return ds.getConnection();
|
||||||
|
}
|
||||||
|
|
||||||
|
private HikariCPDataSource(){}
|
||||||
|
}
|
@ -0,0 +1 @@
|
|||||||
|
Main-Class: com.baeldung.manifest.AppExample
|
@ -0,0 +1,69 @@
|
|||||||
|
package com.baeldung.connectionpool;
|
||||||
|
|
||||||
|
import com.baeldung.connectionpool.connectionpools.BasicConnectionPool;
|
||||||
|
import com.baeldung.connectionpool.connectionpools.ConnectionPool;
|
||||||
|
import java.sql.Connection;
|
||||||
|
import java.sql.SQLException;
|
||||||
|
import java.util.List;
|
||||||
|
import static org.assertj.core.api.Assertions.assertThat;
|
||||||
|
import static org.junit.Assert.assertTrue;
|
||||||
|
import static org.junit.Assert.fail;
|
||||||
|
|
||||||
|
import org.junit.BeforeClass;
|
||||||
|
import org.junit.Test;
|
||||||
|
|
||||||
|
public class BasicConnectionPoolUnitTest {
|
||||||
|
|
||||||
|
private static ConnectionPool connectionPool;
|
||||||
|
|
||||||
|
@BeforeClass
|
||||||
|
public static void setUpBasicConnectionPoolInstance() throws SQLException {
|
||||||
|
connectionPool = BasicConnectionPool.create("jdbc:h2:mem:test", "user", "password");
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void givenBasicConnectionPoolInstance_whenCalledgetConnection_thenCorrect() throws Exception {
|
||||||
|
assertTrue(connectionPool.getConnection().isValid(1));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void givenBasicConnectionPoolInstance_whenCalledreleaseConnection_thenCorrect() throws Exception {
|
||||||
|
Connection connection = connectionPool.getConnection();
|
||||||
|
assertThat(connectionPool.releaseConnection(connection)).isTrue();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void givenBasicConnectionPoolInstance_whenCalledgetUrl_thenCorrect() {
|
||||||
|
assertThat(connectionPool.getUrl()).isEqualTo("jdbc:h2:mem:test");
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void givenBasicConnectionPoolInstance_whenCalledgetUser_thenCorrect() {
|
||||||
|
assertThat(connectionPool.getUser()).isEqualTo("user");
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void givenBasicConnectionPoolInstance_whenCalledgetPassword_thenCorrect() {
|
||||||
|
assertThat(connectionPool.getPassword()).isEqualTo("password");
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test(expected = RuntimeException.class)
|
||||||
|
public void givenBasicConnectionPoolInstance_whenAskedForMoreThanMax_thenError() throws Exception {
|
||||||
|
// this test needs to be independent so it doesn't share the same connection pool as other tests
|
||||||
|
ConnectionPool cp = BasicConnectionPool.create("jdbc:h2:mem:test", "user", "password");
|
||||||
|
final int MAX_POOL_SIZE = 20;
|
||||||
|
for (int i = 0; i < MAX_POOL_SIZE + 1; i++) {
|
||||||
|
cp.getConnection();
|
||||||
|
}
|
||||||
|
fail();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void givenBasicConnectionPoolInstance_whenSutdown_thenEmpty() throws Exception {
|
||||||
|
ConnectionPool cp = BasicConnectionPool.create("jdbc:h2:mem:test", "user", "password");
|
||||||
|
assertThat(((BasicConnectionPool)cp).getSize()).isEqualTo(10);
|
||||||
|
|
||||||
|
((BasicConnectionPool) cp).shutdown();
|
||||||
|
assertThat(((BasicConnectionPool)cp).getSize()).isEqualTo(0);
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,14 @@
|
|||||||
|
package com.baeldung.connectionpool;
|
||||||
|
|
||||||
|
import com.baeldung.connectionpool.connectionpools.C3poDataSource;
|
||||||
|
import java.sql.SQLException;
|
||||||
|
import static org.junit.Assert.assertTrue;
|
||||||
|
import org.junit.Test;
|
||||||
|
|
||||||
|
public class C3poDataSourceUnitTest {
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void givenC3poDataSourceClass_whenCalledgetConnection_thenCorrect() throws SQLException {
|
||||||
|
assertTrue(C3poDataSource.getConnection().isValid(1));
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,14 @@
|
|||||||
|
package com.baeldung.connectionpool;
|
||||||
|
|
||||||
|
import com.baeldung.connectionpool.connectionpools.DBCPDataSource;
|
||||||
|
import java.sql.SQLException;
|
||||||
|
import static org.junit.Assert.assertTrue;
|
||||||
|
import org.junit.Test;
|
||||||
|
|
||||||
|
public class DBCPDataSourceUnitTest {
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void givenDBCPDataSourceClass_whenCalledgetConnection_thenCorrect() throws SQLException {
|
||||||
|
assertTrue(DBCPDataSource.getConnection().isValid(1));
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,14 @@
|
|||||||
|
package com.baeldung.connectionpool;
|
||||||
|
|
||||||
|
import com.baeldung.connectionpool.connectionpools.HikariCPDataSource;
|
||||||
|
import java.sql.SQLException;
|
||||||
|
import static org.junit.Assert.assertTrue;
|
||||||
|
import org.junit.Test;
|
||||||
|
|
||||||
|
public class HikariCPDataSourceUnitTest {
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void givenHikariDataSourceClass_whenCalledgetConnection_thenCorrect() throws SQLException {
|
||||||
|
assertTrue(HikariCPDataSource.getConnection().isValid(1));
|
||||||
|
}
|
||||||
|
}
|
56
libraries/src/main/java/com/baeldung/jmapper/User.java
Normal file
56
libraries/src/main/java/com/baeldung/jmapper/User.java
Normal file
@ -0,0 +1,56 @@
|
|||||||
|
package com.baeldung.jmapper;
|
||||||
|
|
||||||
|
import java.time.LocalDate;
|
||||||
|
|
||||||
|
|
||||||
|
public class User {
|
||||||
|
|
||||||
|
private long id;
|
||||||
|
private String email;
|
||||||
|
private LocalDate birthDate;
|
||||||
|
|
||||||
|
// constructors
|
||||||
|
|
||||||
|
public User() {
|
||||||
|
super();
|
||||||
|
}
|
||||||
|
|
||||||
|
public User(long id, String email, LocalDate birthDate) {
|
||||||
|
super();
|
||||||
|
this.id = id;
|
||||||
|
this.email = email;
|
||||||
|
this.birthDate = birthDate;
|
||||||
|
}
|
||||||
|
|
||||||
|
// getters and setters
|
||||||
|
|
||||||
|
public long getId() {
|
||||||
|
return id;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setId(long id) {
|
||||||
|
this.id = id;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getEmail() {
|
||||||
|
return email;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setEmail(String email) {
|
||||||
|
this.email = email;
|
||||||
|
}
|
||||||
|
|
||||||
|
public LocalDate getBirthDate() {
|
||||||
|
return birthDate;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setBirthDate(LocalDate birthDate) {
|
||||||
|
this.birthDate = birthDate;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String toString() {
|
||||||
|
return "User [id=" + id + ", email=" + email + ", birthDate=" + birthDate + "]";
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
69
libraries/src/main/java/com/baeldung/jmapper/UserDto.java
Normal file
69
libraries/src/main/java/com/baeldung/jmapper/UserDto.java
Normal file
@ -0,0 +1,69 @@
|
|||||||
|
package com.baeldung.jmapper;
|
||||||
|
|
||||||
|
import java.time.LocalDate;
|
||||||
|
import java.time.Period;
|
||||||
|
|
||||||
|
import com.googlecode.jmapper.annotations.JMap;
|
||||||
|
import com.googlecode.jmapper.annotations.JMapConversion;
|
||||||
|
|
||||||
|
public class UserDto {
|
||||||
|
|
||||||
|
@JMap
|
||||||
|
private long id;
|
||||||
|
|
||||||
|
@JMap("email")
|
||||||
|
private String username;
|
||||||
|
|
||||||
|
@JMap("birthDate")
|
||||||
|
private int age;
|
||||||
|
|
||||||
|
@JMapConversion(from={"birthDate"}, to={"age"})
|
||||||
|
public int conversion(LocalDate birthDate){
|
||||||
|
return Period.between(birthDate, LocalDate.now()).getYears();
|
||||||
|
}
|
||||||
|
|
||||||
|
// constructors
|
||||||
|
|
||||||
|
public UserDto() {
|
||||||
|
super();
|
||||||
|
}
|
||||||
|
|
||||||
|
public UserDto(long id, String username, int age) {
|
||||||
|
super();
|
||||||
|
this.id = id;
|
||||||
|
this.username = username;
|
||||||
|
this.age = age;
|
||||||
|
}
|
||||||
|
|
||||||
|
// getters and setters
|
||||||
|
|
||||||
|
public long getId() {
|
||||||
|
return id;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setId(long id) {
|
||||||
|
this.id = id;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getUsername() {
|
||||||
|
return username;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setUsername(String username) {
|
||||||
|
this.username = username;
|
||||||
|
}
|
||||||
|
|
||||||
|
public int getAge() {
|
||||||
|
return age;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setAge(int age) {
|
||||||
|
this.age = age;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String toString() {
|
||||||
|
return "UserDto [id=" + id + ", username=" + username + ", age=" + age + "]";
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
47
libraries/src/main/java/com/baeldung/jmapper/UserDto1.java
Normal file
47
libraries/src/main/java/com/baeldung/jmapper/UserDto1.java
Normal file
@ -0,0 +1,47 @@
|
|||||||
|
package com.baeldung.jmapper;
|
||||||
|
|
||||||
|
import com.googlecode.jmapper.annotations.JGlobalMap;
|
||||||
|
|
||||||
|
@JGlobalMap
|
||||||
|
public class UserDto1 {
|
||||||
|
|
||||||
|
private long id;
|
||||||
|
private String email;
|
||||||
|
|
||||||
|
|
||||||
|
// constructors
|
||||||
|
|
||||||
|
public UserDto1() {
|
||||||
|
super();
|
||||||
|
}
|
||||||
|
|
||||||
|
public UserDto1(long id, String email) {
|
||||||
|
super();
|
||||||
|
this.id = id;
|
||||||
|
this.email = email;
|
||||||
|
}
|
||||||
|
|
||||||
|
// getters and setters
|
||||||
|
|
||||||
|
public long getId() {
|
||||||
|
return id;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setId(long id) {
|
||||||
|
this.id = id;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getEmail() {
|
||||||
|
return email;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setEmail(String email) {
|
||||||
|
this.email = email;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String toString() {
|
||||||
|
return "UserDto [id=" + id + ", email=" + email + "]";
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -0,0 +1,49 @@
|
|||||||
|
package com.baeldung.jmapper.relational;
|
||||||
|
|
||||||
|
import com.googlecode.jmapper.annotations.JMap;
|
||||||
|
|
||||||
|
|
||||||
|
public class User {
|
||||||
|
|
||||||
|
@JMap(classes = {UserDto1.class, UserDto2.class})
|
||||||
|
private long id;
|
||||||
|
|
||||||
|
@JMap(attributes = {"username", "email"}, classes = {UserDto1.class, UserDto2.class})
|
||||||
|
private String email;
|
||||||
|
|
||||||
|
// constructors
|
||||||
|
|
||||||
|
public User() {
|
||||||
|
super();
|
||||||
|
}
|
||||||
|
|
||||||
|
public User(long id, String email) {
|
||||||
|
super();
|
||||||
|
this.id = id;
|
||||||
|
this.email = email;
|
||||||
|
}
|
||||||
|
|
||||||
|
// getters and setters
|
||||||
|
|
||||||
|
public long getId() {
|
||||||
|
return id;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setId(long id) {
|
||||||
|
this.id = id;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getEmail() {
|
||||||
|
return email;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setEmail(String email) {
|
||||||
|
this.email = email;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String toString() {
|
||||||
|
return "User [id=" + id + ", email=" + email + "]";
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -0,0 +1,44 @@
|
|||||||
|
package com.baeldung.jmapper.relational;
|
||||||
|
|
||||||
|
|
||||||
|
public class UserDto1 {
|
||||||
|
|
||||||
|
private long id;
|
||||||
|
private String username;
|
||||||
|
|
||||||
|
// constructors
|
||||||
|
|
||||||
|
public UserDto1() {
|
||||||
|
super();
|
||||||
|
}
|
||||||
|
|
||||||
|
public UserDto1(long id, String username) {
|
||||||
|
super();
|
||||||
|
this.id = id;
|
||||||
|
this.username = username;
|
||||||
|
}
|
||||||
|
|
||||||
|
// getters and setters
|
||||||
|
|
||||||
|
public long getId() {
|
||||||
|
return id;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setId(long id) {
|
||||||
|
this.id = id;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getUsername() {
|
||||||
|
return username;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setUsername(String username) {
|
||||||
|
this.username = username;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String toString() {
|
||||||
|
return "UserDto [id=" + id + ", username=" + username + "]";
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -0,0 +1,44 @@
|
|||||||
|
package com.baeldung.jmapper.relational;
|
||||||
|
|
||||||
|
|
||||||
|
public class UserDto2 {
|
||||||
|
|
||||||
|
private long id;
|
||||||
|
private String email;
|
||||||
|
|
||||||
|
// constructors
|
||||||
|
|
||||||
|
public UserDto2() {
|
||||||
|
super();
|
||||||
|
}
|
||||||
|
|
||||||
|
public UserDto2(long id, String email) {
|
||||||
|
super();
|
||||||
|
this.id = id;
|
||||||
|
this.email = email;
|
||||||
|
}
|
||||||
|
|
||||||
|
// getters and setters
|
||||||
|
|
||||||
|
public long getId() {
|
||||||
|
return id;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setId(long id) {
|
||||||
|
this.id = id;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getEmail() {
|
||||||
|
return email;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setEmail(String email) {
|
||||||
|
this.email = email;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String toString() {
|
||||||
|
return "UserDto2 [id=" + id + ", email=" + email + "]";
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
10
libraries/src/main/resources/user_jmapper.xml
Normal file
10
libraries/src/main/resources/user_jmapper.xml
Normal file
@ -0,0 +1,10 @@
|
|||||||
|
<jmapper>
|
||||||
|
<class name="com.baeldung.jmapper.UserDto">
|
||||||
|
<attribute name="id">
|
||||||
|
<value name="id"/>
|
||||||
|
</attribute>
|
||||||
|
<attribute name="username">
|
||||||
|
<value name="email"/>
|
||||||
|
</attribute>
|
||||||
|
</class>
|
||||||
|
</jmapper>
|
5
libraries/src/main/resources/user_jmapper1.xml
Normal file
5
libraries/src/main/resources/user_jmapper1.xml
Normal file
@ -0,0 +1,5 @@
|
|||||||
|
<jmapper>
|
||||||
|
<class name="com.baeldung.jmapper.UserDto1">
|
||||||
|
<global/>
|
||||||
|
</class>
|
||||||
|
</jmapper>
|
21
libraries/src/main/resources/user_jmapper2.xml
Normal file
21
libraries/src/main/resources/user_jmapper2.xml
Normal file
@ -0,0 +1,21 @@
|
|||||||
|
<jmapper>
|
||||||
|
<class name="com.baeldung.jmapper.relational.User">
|
||||||
|
<attribute name="id">
|
||||||
|
<value name="id"/>
|
||||||
|
<classes>
|
||||||
|
<class name="com.baeldung.jmapper.relational.UserDto1"/>
|
||||||
|
<class name="com.baeldung.jmapper.relational.UserDto2"/>
|
||||||
|
</classes>
|
||||||
|
</attribute>
|
||||||
|
<attribute name="email">
|
||||||
|
<attributes>
|
||||||
|
<attribute name="username"/>
|
||||||
|
<attribute name="email"/>
|
||||||
|
</attributes>
|
||||||
|
<classes>
|
||||||
|
<class name="com.baeldung.jmapper.relational.UserDto1"/>
|
||||||
|
<class name="com.baeldung.jmapper.relational.UserDto2"/>
|
||||||
|
</classes>
|
||||||
|
</attribute>
|
||||||
|
</class>
|
||||||
|
</jmapper>
|
@ -0,0 +1,114 @@
|
|||||||
|
package com.baeldung.jmapper;
|
||||||
|
|
||||||
|
import static com.googlecode.jmapper.api.JMapperAPI.attribute;
|
||||||
|
import static com.googlecode.jmapper.api.JMapperAPI.global;
|
||||||
|
import static com.googlecode.jmapper.api.JMapperAPI.mappedClass;
|
||||||
|
import static org.junit.Assert.assertEquals;
|
||||||
|
import static org.junit.Assert.assertTrue;
|
||||||
|
|
||||||
|
import java.time.LocalDate;
|
||||||
|
|
||||||
|
import org.junit.Test;
|
||||||
|
|
||||||
|
import com.googlecode.jmapper.JMapper;
|
||||||
|
import com.googlecode.jmapper.api.JMapperAPI;
|
||||||
|
|
||||||
|
public class JMapperIntegrationTest {
|
||||||
|
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void givenUser_whenUseAnnotation_thenConverted(){
|
||||||
|
JMapper<UserDto, User> userMapper = new JMapper<>(UserDto.class, User.class);
|
||||||
|
|
||||||
|
User user = new User(1L,"john@test.com", LocalDate.of(1980,8,20));
|
||||||
|
UserDto result = userMapper.getDestination(user);
|
||||||
|
|
||||||
|
System.out.println(result);
|
||||||
|
assertEquals(user.getId(), result.getId());
|
||||||
|
assertEquals(user.getEmail(), result.getUsername());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void givenUser_whenUseGlobalMapAnnotation_thenConverted(){
|
||||||
|
JMapper<UserDto1, User> userMapper= new JMapper<>(UserDto1.class, User.class);
|
||||||
|
|
||||||
|
User user = new User(1L,"john@test.com", LocalDate.of(1980,8,20));
|
||||||
|
UserDto1 result = userMapper.getDestination(user);
|
||||||
|
|
||||||
|
System.out.println(result);
|
||||||
|
assertEquals(user.getId(), result.getId());
|
||||||
|
assertEquals(user.getEmail(), result.getEmail());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void givenUser_whenUseAnnotationExplicitConversion_thenConverted(){
|
||||||
|
JMapper<UserDto, User> userMapper = new JMapper<>(UserDto.class, User.class);
|
||||||
|
|
||||||
|
User user = new User(1L,"john@test.com", LocalDate.of(1980,8,20));
|
||||||
|
UserDto result = userMapper.getDestination(user);
|
||||||
|
|
||||||
|
System.out.println(result);
|
||||||
|
assertEquals(user.getId(), result.getId());
|
||||||
|
assertEquals(user.getEmail(), result.getUsername());
|
||||||
|
assertTrue(result.getAge() > 0);
|
||||||
|
}
|
||||||
|
|
||||||
|
//======================= XML
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void givenUser_whenUseXml_thenConverted(){
|
||||||
|
JMapper<UserDto, User> userMapper = new JMapper<>(UserDto.class, User.class,"user_jmapper.xml");
|
||||||
|
|
||||||
|
User user = new User(1L,"john@test.com", LocalDate.of(1980,8,20));
|
||||||
|
UserDto result = userMapper.getDestination(user);
|
||||||
|
|
||||||
|
System.out.println(result);
|
||||||
|
assertEquals(user.getId(), result.getId());
|
||||||
|
assertEquals(user.getEmail(), result.getUsername());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void givenUser_whenUseXmlGlobal_thenConverted(){
|
||||||
|
JMapper<UserDto1, User> userMapper = new JMapper<>(UserDto1.class, User.class,"user_jmapper1.xml");
|
||||||
|
|
||||||
|
User user = new User(1L,"john@test.com", LocalDate.of(1980,8,20));
|
||||||
|
UserDto1 result = userMapper.getDestination(user);
|
||||||
|
|
||||||
|
System.out.println(result);
|
||||||
|
assertEquals(user.getId(), result.getId());
|
||||||
|
assertEquals(user.getEmail(), result.getEmail());
|
||||||
|
}
|
||||||
|
|
||||||
|
// ===== API
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void givenUser_whenUseApi_thenConverted(){
|
||||||
|
JMapperAPI jmapperApi = new JMapperAPI() .add(mappedClass(UserDto.class)
|
||||||
|
.add(attribute("id").value("id"))
|
||||||
|
.add(attribute("username").value("email"))
|
||||||
|
) ;
|
||||||
|
JMapper<UserDto, User> userMapper = new JMapper<>(UserDto.class, User.class, jmapperApi);
|
||||||
|
|
||||||
|
User user = new User(1L,"john@test.com", LocalDate.of(1980,8,20));
|
||||||
|
UserDto result = userMapper.getDestination(user);
|
||||||
|
|
||||||
|
System.out.println(result);
|
||||||
|
assertEquals(user.getId(), result.getId());
|
||||||
|
assertEquals(user.getEmail(), result.getUsername());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void givenUser_whenUseApiGlobal_thenConverted(){
|
||||||
|
JMapperAPI jmapperApi = new JMapperAPI() .add(mappedClass(UserDto.class)
|
||||||
|
.add(global())
|
||||||
|
) ;
|
||||||
|
JMapper<UserDto1, User> userMapper1 = new JMapper<>(UserDto1.class, User.class,jmapperApi);
|
||||||
|
|
||||||
|
User user = new User(1L,"john@test.com", LocalDate.of(1980,8,20));
|
||||||
|
UserDto1 result = userMapper1.getDestination(user);
|
||||||
|
|
||||||
|
System.out.println(result);
|
||||||
|
assertEquals(user.getId(), result.getId());
|
||||||
|
assertEquals(user.getEmail(), result.getEmail());
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,76 @@
|
|||||||
|
package com.baeldung.jmapper;
|
||||||
|
|
||||||
|
import static com.googlecode.jmapper.api.JMapperAPI.attribute;
|
||||||
|
import static com.googlecode.jmapper.api.JMapperAPI.mappedClass;
|
||||||
|
import static org.junit.Assert.assertEquals;
|
||||||
|
|
||||||
|
import org.junit.Test;
|
||||||
|
|
||||||
|
import com.baeldung.jmapper.relational.User;
|
||||||
|
import com.baeldung.jmapper.relational.UserDto1;
|
||||||
|
import com.baeldung.jmapper.relational.UserDto2;
|
||||||
|
import com.googlecode.jmapper.RelationalJMapper;
|
||||||
|
import com.googlecode.jmapper.api.JMapperAPI;
|
||||||
|
|
||||||
|
public class JMapperRelationalIntegrationTest {
|
||||||
|
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void givenUser_whenUseAnnotation_thenConverted(){
|
||||||
|
RelationalJMapper<User> relationalMapper = new RelationalJMapper<>(User.class);
|
||||||
|
|
||||||
|
User user = new User(1L,"john@test.com");
|
||||||
|
UserDto1 result1 = relationalMapper.oneToMany(UserDto1.class, user);
|
||||||
|
UserDto2 result2= relationalMapper.oneToMany(UserDto2.class, user);
|
||||||
|
|
||||||
|
System.out.println(result1);
|
||||||
|
System.out.println(result2);
|
||||||
|
assertEquals(user.getId(), result1.getId());
|
||||||
|
assertEquals(user.getEmail(), result1.getUsername());
|
||||||
|
assertEquals(user.getId(), result2.getId());
|
||||||
|
assertEquals(user.getEmail(), result2.getEmail());
|
||||||
|
}
|
||||||
|
|
||||||
|
//======================= XML
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void givenUser_whenUseXml_thenConverted(){
|
||||||
|
RelationalJMapper<User> relationalMapper = new RelationalJMapper<>(User.class,"user_jmapper2.xml");
|
||||||
|
|
||||||
|
User user = new User(1L,"john@test.com");
|
||||||
|
UserDto1 result1 = relationalMapper.oneToMany(UserDto1.class, user);
|
||||||
|
UserDto2 result2 = relationalMapper.oneToMany(UserDto2.class, user);
|
||||||
|
|
||||||
|
System.out.println(result1);
|
||||||
|
System.out.println(result2);
|
||||||
|
assertEquals(user.getId(), result1.getId());
|
||||||
|
assertEquals(user.getEmail(), result1.getUsername());
|
||||||
|
assertEquals(user.getId(), result2.getId());
|
||||||
|
assertEquals(user.getEmail(), result2.getEmail());
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
// ===== API
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void givenUser_whenUseApi_thenConverted(){
|
||||||
|
JMapperAPI jmapperApi = new JMapperAPI()
|
||||||
|
.add(mappedClass(User.class)
|
||||||
|
.add(attribute("id").value("id").targetClasses(UserDto1.class,UserDto2.class))
|
||||||
|
.add(attribute("email").targetAttributes("username","email").targetClasses(UserDto1.class,UserDto2.class)) )
|
||||||
|
;
|
||||||
|
RelationalJMapper<User> relationalMapper = new RelationalJMapper<>(User.class,jmapperApi);
|
||||||
|
|
||||||
|
User user = new User(1L,"john@test.com");
|
||||||
|
UserDto1 result1 = relationalMapper.oneToMany(UserDto1.class, user);
|
||||||
|
UserDto2 result2 = relationalMapper.oneToMany(UserDto2.class, user);
|
||||||
|
|
||||||
|
System.out.println(result1);
|
||||||
|
System.out.println(result2);
|
||||||
|
assertEquals(user.getId(), result1.getId());
|
||||||
|
assertEquals(user.getEmail(), result1.getUsername());
|
||||||
|
assertEquals(user.getId(), result2.getId());
|
||||||
|
assertEquals(user.getEmail(), result2.getEmail());
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
1
spring-5-reactive/src/main/java/com/baeldung/reactive/controller/.gitignore
vendored
Normal file
1
spring-5-reactive/src/main/java/com/baeldung/reactive/controller/.gitignore
vendored
Normal file
@ -0,0 +1 @@
|
|||||||
|
/FooReactiveController.java
|
@ -0,0 +1,23 @@
|
|||||||
|
package com.baeldung.domain;
|
||||||
|
|
||||||
|
import javax.persistence.*;
|
||||||
|
import java.util.Date;
|
||||||
|
|
||||||
|
@Entity
|
||||||
|
public class Article {
|
||||||
|
|
||||||
|
@Id
|
||||||
|
@GeneratedValue
|
||||||
|
private Integer id;
|
||||||
|
@Temporal(TemporalType.DATE)
|
||||||
|
private Date publicationDate;
|
||||||
|
@Temporal(TemporalType.TIME)
|
||||||
|
private Date publicationTime;
|
||||||
|
@Temporal(TemporalType.TIMESTAMP)
|
||||||
|
private Date creationDateTime;
|
||||||
|
|
||||||
|
public Integer getId() {
|
||||||
|
return id;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -0,0 +1,22 @@
|
|||||||
|
package com.baeldung.repository;
|
||||||
|
|
||||||
|
import com.baeldung.domain.Article;
|
||||||
|
import org.springframework.data.jpa.repository.JpaRepository;
|
||||||
|
import org.springframework.data.jpa.repository.Query;
|
||||||
|
import org.springframework.data.repository.query.Param;
|
||||||
|
|
||||||
|
import java.util.Date;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
public interface ArticleRepository extends JpaRepository<Article, Integer> {
|
||||||
|
|
||||||
|
List<Article> findAllByPublicationDate(Date publicationDate);
|
||||||
|
|
||||||
|
List<Article> findAllByPublicationTimeBetween(Date publicationTimeStart,
|
||||||
|
Date publicationTimeEnd);
|
||||||
|
|
||||||
|
@Query("select a from Article a where a.creationDateTime <= :creationDateTime")
|
||||||
|
List<Article> findAllWithCreationDateTimeBefore(
|
||||||
|
@Param("creationDateTime") Date creationDateTime);
|
||||||
|
|
||||||
|
}
|
@ -0,0 +1,66 @@
|
|||||||
|
package com.baeldung.repository;
|
||||||
|
|
||||||
|
import com.baeldung.domain.Article;
|
||||||
|
import org.junit.Test;
|
||||||
|
import org.junit.runner.RunWith;
|
||||||
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
|
import org.springframework.boot.test.autoconfigure.orm.jpa.DataJpaTest;
|
||||||
|
import org.springframework.test.context.junit4.SpringRunner;
|
||||||
|
|
||||||
|
import java.text.SimpleDateFormat;
|
||||||
|
import java.util.Arrays;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
import static org.junit.Assert.assertEquals;
|
||||||
|
import static org.junit.Assert.assertTrue;
|
||||||
|
|
||||||
|
@RunWith(SpringRunner.class)
|
||||||
|
@DataJpaTest
|
||||||
|
public class ArticleRepositoryIntegrationTest {
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
private ArticleRepository repository;
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void givenImportedArticlesWhenFindAllByPublicationDateThenArticles1And2Returned()
|
||||||
|
throws Exception {
|
||||||
|
List<Article> result = repository.findAllByPublicationDate(
|
||||||
|
new SimpleDateFormat("yyyy-MM-dd").parse("2018-01-01")
|
||||||
|
);
|
||||||
|
|
||||||
|
assertEquals(2, result.size());
|
||||||
|
assertTrue(result.stream()
|
||||||
|
.map(Article::getId)
|
||||||
|
.allMatch(id -> Arrays.asList(1, 2).contains(id))
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void givenImportedArticlesWhenFindAllByPublicationTimeBetweenThenArticles2And3Returned()
|
||||||
|
throws Exception {
|
||||||
|
List<Article> result = repository.findAllByPublicationTimeBetween(
|
||||||
|
new SimpleDateFormat("HH:mm").parse("15:15"),
|
||||||
|
new SimpleDateFormat("HH:mm").parse("16:30")
|
||||||
|
);
|
||||||
|
|
||||||
|
assertEquals(2, result.size());
|
||||||
|
assertTrue(result.stream()
|
||||||
|
.map(Article::getId)
|
||||||
|
.allMatch(id -> Arrays.asList(2, 3).contains(id))
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void givenImportedArticlesWhenFindAllWithCreationDateTimeBeforeThenArticles2And3Returned() throws Exception {
|
||||||
|
List<Article> result = repository.findAllWithCreationDateTimeBefore(
|
||||||
|
new SimpleDateFormat("yyyy-MM-dd HH:mm").parse("2017-12-15 10:00")
|
||||||
|
);
|
||||||
|
|
||||||
|
assertEquals(2, result.size());
|
||||||
|
assertTrue(result.stream()
|
||||||
|
.map(Article::getId)
|
||||||
|
.allMatch(id -> Arrays.asList(2, 3).contains(id))
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -13,4 +13,4 @@ hibernate.cache.use_query_cache=true
|
|||||||
hibernate.cache.region.factory_class=org.hibernate.cache.ehcache.EhCacheRegionFactory
|
hibernate.cache.region.factory_class=org.hibernate.cache.ehcache.EhCacheRegionFactory
|
||||||
|
|
||||||
spring.jpa.properties.hibernate.hbm2ddl.import_files=migrated_users.sql
|
spring.jpa.properties.hibernate.hbm2ddl.import_files=migrated_users.sql
|
||||||
spring.datasource.data=import_*_users.sql
|
spring.datasource.data=import_*_users.sql,import_articles.sql
|
@ -0,0 +1,3 @@
|
|||||||
|
insert into Article(id, publication_date, publication_time, creation_date_time) values(1, TO_DATE('01/01/2018', 'DD/MM/YYYY'), TO_DATE('15:00', 'HH24:MI'), TO_DATE('31/12/2017 07:30', 'DD/MM/YYYY HH24:MI'));
|
||||||
|
insert into Article(id, publication_date, publication_time, creation_date_time) values(2, TO_DATE('01/01/2018', 'DD/MM/YYYY'), TO_DATE('15:30', 'HH24:MI'), TO_DATE('15/12/2017 08:00', 'DD/MM/YYYY HH24:MI'));
|
||||||
|
insert into Article(id, publication_date, publication_time, creation_date_time) values(3, TO_DATE('15/12/2017', 'DD/MM/YYYY'), TO_DATE('16:00', 'HH24:MI'), TO_DATE('01/12/2017 13:45', 'DD/MM/YYYY HH24:MI'));
|
Loading…
x
Reference in New Issue
Block a user