|
| 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.util.Optional; |
| 22 | + |
| 23 | +import org.apache.log4j.Logger; |
| 24 | + |
| 25 | +/** |
| 26 | + * The Data Mapper (DM) is a layer of software that separates the in-memory objects from the |
| 27 | + * database. Its responsibility is to transfer data between the two and also to isolate them from |
| 28 | + * each other. With Data Mapper the in-memory objects needn't know even that there's a database |
| 29 | + * present; they need no SQL interface code, and certainly no knowledge of the database schema. (The |
| 30 | + * database schema is always ignorant of the objects that use it.) Since it's a form of Mapper , |
| 31 | + * Data Mapper itself is even unknown to the domain layer. |
| 32 | + * <p> |
| 33 | + * The below example demonstrates basic CRUD operations: Create, Read, Update, and Delete. |
| 34 | + * |
| 35 | + */ |
| 36 | +public final class App { |
| 37 | + |
| 38 | + private static Logger log = Logger.getLogger(App.class); |
| 39 | + |
| 40 | + /** |
| 41 | + * Program entry point. |
| 42 | + * |
| 43 | + * @param args command line args. |
| 44 | + */ |
| 45 | + public static void main(final String... args) { |
| 46 | + |
| 47 | + /* Create new data mapper for type 'first' */ |
| 48 | + final StudentDataMapper mapper = new StudentDataMapperImpl(); |
| 49 | + |
| 50 | + /* Create new student */ |
| 51 | + Student student = new Student(1, "Adam", 'A'); |
| 52 | + |
| 53 | + /* Add student in respectibe store */ |
| 54 | + mapper.insert(student); |
| 55 | + |
| 56 | + log.debug("App.main(), student : " + student + ", is inserted"); |
| 57 | + |
| 58 | + /* Find this student */ |
| 59 | + final Optional<Student> studentToBeFound = mapper.find(student.getStudentId()); |
| 60 | + |
| 61 | + log.debug("App.main(), student : " + studentToBeFound + ", is searched"); |
| 62 | + |
| 63 | + /* Update existing student object */ |
| 64 | + student = new Student(student.getStudentId(), "AdamUpdated", 'A'); |
| 65 | + |
| 66 | + /* Update student in respectibe db */ |
| 67 | + mapper.update(student); |
| 68 | + |
| 69 | + log.debug("App.main(), student : " + student + ", is updated"); |
| 70 | + log.debug("App.main(), student : " + student + ", is going to be deleted"); |
| 71 | + |
| 72 | + /* Delete student in db */ |
| 73 | + mapper.delete(student); |
| 74 | + } |
| 75 | + |
| 76 | + private App() {} |
| 77 | +} |
0 commit comments