mirror of
https://github.com/tiennm99/java-design-patterns.git
synced 2026-05-17 04:59:21 +00:00
deps: Refactor dependencies (#3224)
* remove spring dep move junit, logging, mockito under dep mgmt * upgrade anti-corruption-layer deps * async method invocation * balking, bloc * bridge to bytecode * caching * callback - cqrs * component - health check * hexagonal - metadata mapping * rest of the patterns * remove checkstyle, take spotless into use
This commit is contained in:
@@ -30,21 +30,22 @@ import javax.sql.DataSource;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.h2.jdbcx.JdbcDataSource;
|
||||
|
||||
|
||||
/**
|
||||
* Serialized Entity Pattern.
|
||||
*
|
||||
* <p> Serialized Entity Pattern allow us to easily persist Java objects to the database. It uses Serializable interface
|
||||
* and DAO pattern. Serialized Entity Pattern will first use Serializable to convert a Java object into a set of bytes,
|
||||
* then it will using DAO pattern to store this set of bytes as BLOB to database.</p>
|
||||
*
|
||||
* <p> In this example, we first initialize two Java objects (Country) "China" and "UnitedArabEmirates", then we
|
||||
* initialize "serializedChina" with "China" object and "serializedUnitedArabEmirates" with "UnitedArabEmirates",
|
||||
* then we use method "serializedChina.insertCountry()" and "serializedUnitedArabEmirates.insertCountry()" to serialize
|
||||
* "China" and "UnitedArabEmirates" and persist them to database.
|
||||
* Last, with "serializedChina.selectCountry()" and "serializedUnitedArabEmirates.selectCountry()" we could read "China"
|
||||
* and "UnitedArabEmirates" from database as sets of bytes, then deserialize them back to Java object (Country). </p>
|
||||
* <p>Serialized Entity Pattern allow us to easily persist Java objects to the database. It uses
|
||||
* Serializable interface and DAO pattern. Serialized Entity Pattern will first use Serializable to
|
||||
* convert a Java object into a set of bytes, then it will using DAO pattern to store this set of
|
||||
* bytes as BLOB to database.
|
||||
*
|
||||
* <p>In this example, we first initialize two Java objects (Country) "China" and
|
||||
* "UnitedArabEmirates", then we initialize "serializedChina" with "China" object and
|
||||
* "serializedUnitedArabEmirates" with "UnitedArabEmirates", then we use method
|
||||
* "serializedChina.insertCountry()" and "serializedUnitedArabEmirates.insertCountry()" to serialize
|
||||
* "China" and "UnitedArabEmirates" and persist them to database. Last, with
|
||||
* "serializedChina.selectCountry()" and "serializedUnitedArabEmirates.selectCountry()" we could
|
||||
* read "China" and "UnitedArabEmirates" from database as sets of bytes, then deserialize them back
|
||||
* to Java object (Country).
|
||||
*/
|
||||
@Slf4j
|
||||
public class App {
|
||||
@@ -55,6 +56,7 @@ public class App {
|
||||
|
||||
/**
|
||||
* Program entry point.
|
||||
*
|
||||
* @param args command line args.
|
||||
* @throws IOException if any
|
||||
* @throws ClassNotFoundException if any
|
||||
@@ -66,20 +68,10 @@ public class App {
|
||||
createSchema(dataSource);
|
||||
|
||||
// Initializing Country Object China
|
||||
final var China = new Country(
|
||||
86,
|
||||
"China",
|
||||
"Asia",
|
||||
"Chinese"
|
||||
);
|
||||
final var China = new Country(86, "China", "Asia", "Chinese");
|
||||
|
||||
// Initializing Country Object UnitedArabEmirates
|
||||
final var UnitedArabEmirates = new Country(
|
||||
971,
|
||||
"United Arab Emirates",
|
||||
"Asia",
|
||||
"Arabic"
|
||||
);
|
||||
final var UnitedArabEmirates = new Country(971, "United Arab Emirates", "Asia", "Arabic");
|
||||
|
||||
// Initializing CountrySchemaSql Object with parameter "China" and "dataSource"
|
||||
final var serializedChina = new CountrySchemaSql(China, dataSource);
|
||||
@@ -105,7 +97,7 @@ public class App {
|
||||
|
||||
private static void deleteSchema(DataSource dataSource) {
|
||||
try (var connection = dataSource.getConnection();
|
||||
var statement = connection.createStatement()) {
|
||||
var statement = connection.createStatement()) {
|
||||
statement.execute(CountrySchemaSql.DELETE_SCHEMA_SQL);
|
||||
} catch (SQLException e) {
|
||||
LOGGER.info("Exception thrown " + e.getMessage());
|
||||
@@ -114,7 +106,7 @@ public class App {
|
||||
|
||||
private static void createSchema(DataSource dataSource) {
|
||||
try (var connection = dataSource.getConnection();
|
||||
var statement = connection.createStatement()) {
|
||||
var statement = connection.createStatement()) {
|
||||
statement.execute(CountrySchemaSql.CREATE_SCHEMA_SQL);
|
||||
} catch (SQLException e) {
|
||||
LOGGER.info("Exception thrown " + e.getMessage());
|
||||
@@ -126,4 +118,4 @@ public class App {
|
||||
dataSource.setURL(DB_URL);
|
||||
return dataSource;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -23,6 +23,7 @@
|
||||
* THE SOFTWARE.
|
||||
*/
|
||||
package com.iluwatar.serializedentity;
|
||||
|
||||
import java.io.Serial;
|
||||
import java.io.Serializable;
|
||||
import lombok.AllArgsConstructor;
|
||||
@@ -31,9 +32,7 @@ import lombok.Getter;
|
||||
import lombok.Setter;
|
||||
import lombok.ToString;
|
||||
|
||||
/**
|
||||
* A Country POJO that represents the data that will serialize and store in database.
|
||||
*/
|
||||
/** A Country POJO that represents the data that will serialize and store in database. */
|
||||
@Getter
|
||||
@Setter
|
||||
@EqualsAndHashCode
|
||||
@@ -45,7 +44,5 @@ public class Country implements Serializable {
|
||||
private String name;
|
||||
private String continents;
|
||||
private String language;
|
||||
@Serial
|
||||
private static final long serialVersionUID = 7149851;
|
||||
|
||||
@Serial private static final long serialVersionUID = 7149851;
|
||||
}
|
||||
|
||||
@@ -42,10 +42,9 @@ package com.iluwatar.serializedentity;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
/**
|
||||
* DAO interface for Country transactions.
|
||||
*/
|
||||
/** DAO interface for Country transactions. */
|
||||
public interface CountryDao {
|
||||
int insertCountry() throws IOException;
|
||||
|
||||
int selectCountry() throws IOException, ClassNotFoundException;
|
||||
}
|
||||
|
||||
+18
-19
@@ -35,12 +35,11 @@ import java.sql.SQLException;
|
||||
import javax.sql.DataSource;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
|
||||
/**
|
||||
* Country Schema SQL Class.
|
||||
*/
|
||||
/** Country Schema SQL Class. */
|
||||
@Slf4j
|
||||
public class CountrySchemaSql implements CountryDao {
|
||||
public static final String CREATE_SCHEMA_SQL = "CREATE TABLE IF NOT EXISTS WORLD (ID INT PRIMARY KEY, COUNTRY BLOB)";
|
||||
public static final String CREATE_SCHEMA_SQL =
|
||||
"CREATE TABLE IF NOT EXISTS WORLD (ID INT PRIMARY KEY, COUNTRY BLOB)";
|
||||
|
||||
public static final String DELETE_SCHEMA_SQL = "DROP TABLE WORLD IF EXISTS";
|
||||
|
||||
@@ -54,27 +53,26 @@ public class CountrySchemaSql implements CountryDao {
|
||||
* @param country country
|
||||
*/
|
||||
public CountrySchemaSql(Country country, DataSource dataSource) {
|
||||
this.country = new Country(
|
||||
country.getCode(),
|
||||
country.getName(),
|
||||
country.getContinents(),
|
||||
country.getLanguage()
|
||||
);
|
||||
this.country =
|
||||
new Country(
|
||||
country.getCode(), country.getName(), country.getContinents(), country.getLanguage());
|
||||
this.dataSource = dataSource;
|
||||
}
|
||||
|
||||
/**
|
||||
* This method will serialize a Country object and store it to database.
|
||||
* @return int type, if successfully insert a serialized object to database then return country code, else return -1.
|
||||
*
|
||||
* @return int type, if successfully insert a serialized object to database then return country
|
||||
* code, else return -1.
|
||||
* @throws IOException if any.
|
||||
*/
|
||||
@Override
|
||||
public int insertCountry() throws IOException {
|
||||
var sql = "INSERT INTO WORLD (ID, COUNTRY) VALUES (?, ?)";
|
||||
try (var connection = dataSource.getConnection();
|
||||
var preparedStatement = connection.prepareStatement(sql);
|
||||
ByteArrayOutputStream baos = new ByteArrayOutputStream();
|
||||
ObjectOutputStream oss = new ObjectOutputStream(baos)) {
|
||||
var preparedStatement = connection.prepareStatement(sql);
|
||||
ByteArrayOutputStream baos = new ByteArrayOutputStream();
|
||||
ObjectOutputStream oss = new ObjectOutputStream(baos)) {
|
||||
|
||||
oss.writeObject(country);
|
||||
oss.flush();
|
||||
@@ -91,8 +89,9 @@ public class CountrySchemaSql implements CountryDao {
|
||||
|
||||
/**
|
||||
* This method will select a data item from database and deserialize it.
|
||||
* @return int type, if successfully select and deserialized object from database then return country code,
|
||||
* else return -1.
|
||||
*
|
||||
* @return int type, if successfully select and deserialized object from database then return
|
||||
* country code, else return -1.
|
||||
* @throws IOException if any.
|
||||
* @throws ClassNotFoundException if any.
|
||||
*/
|
||||
@@ -100,14 +99,15 @@ public class CountrySchemaSql implements CountryDao {
|
||||
public int selectCountry() throws IOException, ClassNotFoundException {
|
||||
var sql = "SELECT ID, COUNTRY FROM WORLD WHERE ID = ?";
|
||||
try (var connection = dataSource.getConnection();
|
||||
var preparedStatement = connection.prepareStatement(sql)) {
|
||||
var preparedStatement = connection.prepareStatement(sql)) {
|
||||
|
||||
preparedStatement.setInt(1, country.getCode());
|
||||
|
||||
try (ResultSet rs = preparedStatement.executeQuery()) {
|
||||
if (rs.next()) {
|
||||
Blob countryBlob = rs.getBlob("country");
|
||||
ByteArrayInputStream baos = new ByteArrayInputStream(countryBlob.getBytes(1, (int) countryBlob.length()));
|
||||
ByteArrayInputStream baos =
|
||||
new ByteArrayInputStream(countryBlob.getBytes(1, (int) countryBlob.length()));
|
||||
ObjectInputStream ois = new ObjectInputStream(baos);
|
||||
country = (Country) ois.readObject();
|
||||
LOGGER.info("Country: " + country);
|
||||
@@ -119,5 +119,4 @@ public class CountrySchemaSql implements CountryDao {
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user