Skip to content

Commit 28a5a43

Browse files
committed
basic implementation ++
basic implementation ++
1 parent 35d6a54 commit 28a5a43

File tree

9 files changed

+780
-0
lines changed

9 files changed

+780
-0
lines changed

data-mapper/index.md

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
---
2+
layout: pattern
3+
title: Data Mapper
4+
folder: data-mapper
5+
permalink: /patterns/dm/
6+
categories: Persistence Tier
7+
tags:
8+
- Java
9+
- Difficulty-Beginner
10+
---
11+
12+
## Intent
13+
Object provides an abstract interface to some type of database or
14+
other persistence mechanism.
15+
16+
![alt text](./etc/dm.png "Data Mapper")
17+
18+
## Applicability
19+
Use the Data Mapper in any of the following situations
20+
21+
* when you want to consolidate how the data layer is accessed
22+
* when you want to avoid writing multiple data retrieval/persistence layers
23+
24+
## Credits
25+
26+
* [Data Mapper](http://richard.jp.leguen.ca/tutoring/soen343-f2010/tutorials/implementing-data-mapper/)

data-mapper/pom.xml

Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
<?xml version="1.0"?>
2+
<!--
3+
4+
The MIT License
5+
Copyright (c) 2014 Ilkka Seppälä
6+
7+
Permission is hereby granted, free of charge, to any person obtaining a copy
8+
of this software and associated documentation files (the "Software"), to deal
9+
in the Software without restriction, including without limitation the rights
10+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11+
copies of the Software, and to permit persons to whom the Software is
12+
furnished to do so, subject to the following conditions:
13+
14+
The above copyright notice and this permission notice shall be included in
15+
all copies or substantial portions of the Software.
16+
17+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
23+
THE SOFTWARE.
24+
25+
-->
26+
<project
27+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"
28+
xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
29+
<modelVersion>4.0.0</modelVersion>
30+
<parent>
31+
<groupId>com.iluwatar</groupId>
32+
<artifactId>java-design-patterns</artifactId>
33+
<version>1.11.0-SNAPSHOT</version>
34+
</parent>
35+
<artifactId>data-mapper</artifactId>
36+
37+
<dependencies>
38+
<dependency>
39+
<groupId>junit</groupId>
40+
<artifactId>junit</artifactId>
41+
<scope>test</scope>
42+
</dependency>
43+
<dependency>
44+
<groupId>log4j</groupId>
45+
<artifactId>log4j</artifactId>
46+
</dependency>
47+
</dependencies>
48+
49+
<build>
50+
51+
<!--
52+
log4j.xml file will be copied both in ${project.build.outputDirectory}
53+
and ${project.build.directory}. Thanks to Sean Patrick Floyd
54+
(http://stackoverflow.com/questions/5637532/maven-how-to-place-resource-file-together-with-jar)
55+
-->
56+
57+
<resources>
58+
<resource> <!-- regular processing for every resource file -->
59+
<directory>src/main/resources</directory>
60+
</resource>
61+
<resource> <!-- processing with a different output directory for log4j.xml -->
62+
<directory>src/main/resources</directory>
63+
<includes>
64+
<include>log4j.xml</include>
65+
</includes>
66+
<targetPath>..</targetPath> <!-- relative to target/classes i.e. ${project.build.directory} -->
67+
</resource>
68+
</resources>
69+
70+
<plugins>
71+
72+
<!--
73+
This will exclude log4j.xml file from generated JAR
74+
-->
75+
<plugin>
76+
<groupId>org.apache.maven.plugins</groupId>
77+
<artifactId>maven-jar-plugin</artifactId>
78+
<version>2.6</version>
79+
<configuration>
80+
<excludes>
81+
<exclude>log4j.xml</exclude>
82+
</excludes>
83+
<archive>
84+
<manifest>
85+
<addClasspath>true</addClasspath>
86+
</manifest>
87+
</archive>
88+
</configuration>
89+
</plugin>
90+
91+
</plugins>
92+
</build>
93+
</project>
Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
/**
2+
* The MIT License Copyright (c) 2014 Ilkka Seppälä
3+
*
4+
* Permission is hereby granted, free of charge, to any person obtaining a copy of this software and
5+
* associated documentation files (the "Software"), to deal in the Software without restriction,
6+
* including without limitation the rights to use, copy, modify, merge, publish, distribute,
7+
* sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is
8+
* furnished to do so, subject to the following conditions:
9+
*
10+
* The above copyright notice and this permission notice shall be included in all copies or
11+
* substantial portions of the Software.
12+
*
13+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT
14+
* NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
15+
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
16+
* DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
17+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
18+
*/
19+
package com.iluwatar.datamapper;
20+
21+
import java.util.Optional;
22+
import java.util.UUID;
23+
24+
import org.apache.log4j.Logger;
25+
26+
/**
27+
*
28+
* The Data Mapper (DM) is a layer of software that separates the in-memory objects from the
29+
* database. Its responsibility is to transfer data between the two and also to isolate them from
30+
* each other. With Data Mapper the in-memory objects needn't know even that there's a database
31+
* present; they need no SQL interface code, and certainly no knowledge of the database schema. (The
32+
* database schema is always ignorant of the objects that use it.) Since it's a form of Mapper ,
33+
* Data Mapper itself is even unknown to the domain layer.
34+
* <p>
35+
* The below example demonstrates basic CRUD operations: select, add, update, and delete.
36+
*
37+
*/
38+
public final class App {
39+
40+
private static Logger log = Logger.getLogger(App.class);
41+
42+
43+
private static final String DB_TYPE_ORACLE = "Oracle";
44+
private static final String DB_TYPE_MYSQL = "MySQL";
45+
46+
47+
/**
48+
* Program entry point.
49+
*
50+
* @param args command line args.
51+
*/
52+
public static final void main(final String... args) {
53+
54+
if (log.isInfoEnabled() & args.length > 0) {
55+
log.debug("App.main(), db type: " + args[0]);
56+
}
57+
58+
StudentDataMapper mapper = null;
59+
60+
/* Check the desired db type from runtime arguments */
61+
if (args.length == 0) {
62+
63+
/* Create default data mapper for mysql */
64+
mapper = new StudentMySQLDataMapper();
65+
66+
} else if (args.length > 0 && DB_TYPE_ORACLE.equalsIgnoreCase(args[0])) {
67+
68+
/* Create new data mapper for mysql */
69+
mapper = new StudentMySQLDataMapper();
70+
71+
} else if (args.length > 0 && DB_TYPE_MYSQL.equalsIgnoreCase(args[0])) {
72+
73+
/* Create new data mapper for oracle */
74+
mapper = new StudentMySQLDataMapper();
75+
} else {
76+
77+
/* Don't couple any Data Mapper to java.sql.SQLException */
78+
throw new DataMapperException("Following data source(" + args[0] + ") is not supported");
79+
}
80+
81+
/* Create new student */
82+
Student student = new Student(UUID.randomUUID(), 1, "Adam", 'A');
83+
84+
/* Add student in respectibe db */
85+
mapper.insert(student);
86+
87+
/* Find this student */
88+
final Optional<Student> studentToBeFound = mapper.find(student.getGuId());
89+
90+
if (log.isDebugEnabled()) {
91+
log.debug("App.main(), db find returned : " + studentToBeFound);
92+
}
93+
94+
/* Update existing student object */
95+
student = new Student(student.getGuId(), 1, "AdamUpdated", 'A');
96+
97+
/* Update student in respectibe db */
98+
mapper.update(student);
99+
100+
/* Delete student in db */
101+
mapper.delete(student);
102+
}
103+
104+
private App() {}
105+
}
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
/**
2+
* The MIT License Copyright (c) 2016 Amit Dixit
3+
*
4+
* Permission is hereby granted, free of charge, to any person obtaining a copy of this software and
5+
* associated documentation files (the "Software"), to deal in the Software without restriction,
6+
* including without limitation the rights to use, copy, modify, merge, publish, distribute,
7+
* sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is
8+
* furnished to do so, subject to the following conditions:
9+
*
10+
* The above copyright notice and this permission notice shall be included in all copies or
11+
* substantial portions of the Software.
12+
*
13+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT
14+
* NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
15+
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
16+
* DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
17+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
18+
*/
19+
package com.iluwatar.datamapper;
20+
21+
/**
22+
*
23+
* @author amit.dixit
24+
*
25+
*/
26+
public final class DataMapperException extends RuntimeException {
27+
28+
private static final long serialVersionUID = 1L;
29+
30+
/**
31+
* Constructs a new runtime exception with {@code null} as its detail message. The cause is not
32+
* initialized, and may subsequently be initialized by a call to {@link #initCause}.
33+
*/
34+
public DataMapperException() {
35+
super();
36+
}
37+
38+
39+
/**
40+
* Constructs a new runtime exception with the specified detail message. The cause is not
41+
* initialized, and may subsequently be initialized by a call to {@link #initCause}.
42+
*
43+
* @param message the detail message. The detail message is saved for later retrieval by the
44+
* {@link #getMessage()} method.
45+
*/
46+
public DataMapperException(final String message) {
47+
super(message);
48+
}
49+
50+
/**
51+
* Constructs a new runtime exception with the specified detail message and cause.
52+
* <p>
53+
* Note that the detail message associated with {@code cause} is <i>not</i> automatically
54+
* incorporated in this runtime exception's detail message.
55+
*
56+
* @param message the detail message (which is saved for later retrieval by the
57+
* {@link #getMessage()} method).
58+
* @param cause the cause (which is saved for later retrieval by the {@link #getCause()} method).
59+
* (A <tt>null</tt> value is permitted, and indicates that the cause is nonexistent or
60+
* unknown.)
61+
*/
62+
public DataMapperException(final String message, final Throwable cause) {
63+
super(message, cause);
64+
}
65+
66+
/**
67+
* Constructs a new runtime exception with the specified cause and a detail message of
68+
* <tt>(cause==null ? null : cause.toString())</tt> (which typically contains the class and detail
69+
* message of <tt>cause</tt>). This constructor is useful for runtime exceptions that are little
70+
* more than wrappers for other throwables.
71+
*
72+
* @param cause the cause (which is saved for later retrieval by the {@link #getCause()} method).
73+
* (A <tt>null</tt> value is permitted, and indicates that the cause is nonexistent or
74+
* unknown.)
75+
*/
76+
public DataMapperException(final Throwable cause) {
77+
super(cause);
78+
}
79+
}
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
/**
2+
* The MIT License Copyright (c) 2016 Amit Dixit
3+
*
4+
* Permission is hereby granted, free of charge, to any person obtaining a copy of this software and
5+
* associated documentation files (the "Software"), to deal in the Software without restriction,
6+
* including without limitation the rights to use, copy, modify, merge, publish, distribute,
7+
* sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is
8+
* furnished to do so, subject to the following conditions:
9+
*
10+
* The above copyright notice and this permission notice shall be included in all copies or
11+
* substantial portions of the Software.
12+
*
13+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT
14+
* NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
15+
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
16+
* DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
17+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
18+
*/
19+
package com.iluwatar.datamapper;
20+
21+
import java.io.Serializable;
22+
import java.util.UUID;
23+
24+
public final class Student implements Serializable {
25+
26+
private static final long serialVersionUID = 1L;
27+
28+
private UUID guid;
29+
private int studentID;
30+
private String name;
31+
private char grade;
32+
33+
public Student() {
34+
this.guid = UUID.randomUUID();
35+
}
36+
37+
public Student(final UUID guid, final int studentID, final String name, final char grade) {
38+
super();
39+
40+
this.guid = guid;
41+
this.studentID = studentID;
42+
this.name = name;
43+
this.grade = grade;
44+
}
45+
46+
47+
public Student(final UUID guid) {
48+
this.guid = guid;
49+
}
50+
51+
public final int getStudentId() {
52+
return studentID;
53+
}
54+
55+
public final void setStudentId(final int studentID) {
56+
this.studentID = studentID;
57+
}
58+
59+
public final String getName() {
60+
return name;
61+
}
62+
63+
public final void setName(final String name) {
64+
this.name = name;
65+
}
66+
67+
public final char getGrade() {
68+
return grade;
69+
}
70+
71+
public final void setGrade(final char grade) {
72+
this.grade = grade;
73+
}
74+
75+
public final UUID getGuId() {
76+
return guid;
77+
}
78+
79+
@Override
80+
public final String toString() {
81+
return "Student [guid=" + guid + ", studentID=" + studentID + ", name=" + name + ", grade=" + grade + "]";
82+
}
83+
}

0 commit comments

Comments
 (0)