summaryrefslogtreecommitdiffstats
path: root/java
diff options
context:
space:
mode:
authorJérémy Zurcher <jeremy@asynk.ch>2017-04-19 16:52:31 +0200
committerJérémy Zurcher <jeremy@asynk.ch>2017-04-19 16:52:31 +0200
commit4eaaddb6813b0f563c9022e67f7a4d4a2fc8bae1 (patch)
treee0be79c1294562957089ca8641f65d6cf83677af /java
parent6fcde2655e8aedd56bff1de4e85bb83fee15c591 (diff)
downloadshare-4eaaddb6813b0f563c9022e67f7a4d4a2fc8bae1.zip
share-4eaaddb6813b0f563c9022e67f7a4d4a2fc8bae1.tar.gz
java : MyMyBatis : use java 8 default interface implementation to gain free save() and delete() methods
Diffstat (limited to 'java')
-rw-r--r--java/MyMyBatis/src/AbstractDAOOperations.java2
-rw-r--r--java/MyMyBatis/src/AbstractMapper.java2
-rw-r--r--java/MyMyBatis/src/AbstractModel.java10
-rw-r--r--java/MyMyBatis/src/Main.java6
-rw-r--r--java/MyMyBatis/src/Model.java14
5 files changed, 28 insertions, 6 deletions
diff --git a/java/MyMyBatis/src/AbstractDAOOperations.java b/java/MyMyBatis/src/AbstractDAOOperations.java
index cc183c9..90e0ff4 100644
--- a/java/MyMyBatis/src/AbstractDAOOperations.java
+++ b/java/MyMyBatis/src/AbstractDAOOperations.java
@@ -3,7 +3,7 @@ package ch.asynk;
import java.util.List;
public abstract class AbstractDAOOperations<TIdentity,
- TObject extends AbstractModel<TIdentity>,
+ TObject extends AbstractModel<TIdentity, TObject>,
TMapper extends AbstractMapper<TIdentity, TObject>> extends AbstractDAO<TObject, TMapper>
{
diff --git a/java/MyMyBatis/src/AbstractMapper.java b/java/MyMyBatis/src/AbstractMapper.java
index 33bd567..c1ec675 100644
--- a/java/MyMyBatis/src/AbstractMapper.java
+++ b/java/MyMyBatis/src/AbstractMapper.java
@@ -4,6 +4,8 @@ import java.util.List;
public interface AbstractMapper<TIdentity, TObject>
{
+ int save(TObject obj);
+
int insert(TObject obj);
int update(TObject obj);
diff --git a/java/MyMyBatis/src/AbstractModel.java b/java/MyMyBatis/src/AbstractModel.java
index 81bab76..365472c 100644
--- a/java/MyMyBatis/src/AbstractModel.java
+++ b/java/MyMyBatis/src/AbstractModel.java
@@ -1,7 +1,15 @@
package ch.asynk;
-public interface AbstractModel<TIdentity>
+public interface AbstractModel<TIdentity, TObject>
{
boolean isUnsaved();
+
+ TObject self();
+
+ AbstractMapper<TIdentity, TObject> getDAO();
+
+ default void save() { getDAO().save(self()); }
+
+ default int delete() { return getDAO().delete(self()); }
}
diff --git a/java/MyMyBatis/src/Main.java b/java/MyMyBatis/src/Main.java
index ad371f7..deb274f 100644
--- a/java/MyMyBatis/src/Main.java
+++ b/java/MyMyBatis/src/Main.java
@@ -38,12 +38,12 @@ public class Main
System.out.println("selectOne : " + m.toString());
System.out.println("update ...");
m.setName("adios");
- dao.save(m);
+ m.save();
m = dao.selectOne(2);
failIf(m == null , "selectOne()");
failIf(!m.getName().equals("adios") , "selectOne()");
System.out.println("selectOne : " + m.toString());
- count = dao.delete(m);
+ count = m.delete();
failIf(count != 1, "delete()");
System.out.println("delete : " + count);
count = dao.count();
@@ -51,7 +51,7 @@ public class Main
System.out.println("count : " + count);
System.out.println("insert ...");
m = new Model("HoMySatan");
- count = dao.save(m);
+ m.save();
failIf(count != 1, "insert()");
count = dao.count();
failIf(count != 2, "count()");
diff --git a/java/MyMyBatis/src/Model.java b/java/MyMyBatis/src/Model.java
index 676cdde..daa7938 100644
--- a/java/MyMyBatis/src/Model.java
+++ b/java/MyMyBatis/src/Model.java
@@ -3,7 +3,7 @@ package ch.asynk;
import lombok.Setter;
import lombok.Getter;
-public class Model implements AbstractModel<Integer>
+public class Model implements AbstractModel<Integer, Model>
{
private @Setter @Getter Integer id;
private @Setter @Getter String name;
@@ -26,6 +26,18 @@ public class Model implements AbstractModel<Integer>
}
@Override
+ public Model self()
+ {
+ return this;
+ }
+
+ @Override
+ public ModelMapper getDAO()
+ {
+ return ModelDAO.getInstance();
+ }
+
+ @Override
public String toString()
{
return String.format("model [%d] %s", id, name);