From 4eaaddb6813b0f563c9022e67f7a4d4a2fc8bae1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=A9r=C3=A9my=20Zurcher?= Date: Wed, 19 Apr 2017 16:52:31 +0200 Subject: java : MyMyBatis : use java 8 default interface implementation to gain free save() and delete() methods --- java/MyMyBatis/src/AbstractDAOOperations.java | 2 +- java/MyMyBatis/src/AbstractMapper.java | 2 ++ java/MyMyBatis/src/AbstractModel.java | 10 +++++++++- java/MyMyBatis/src/Main.java | 6 +++--- java/MyMyBatis/src/Model.java | 14 +++++++++++++- 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, + TObject extends AbstractModel, TMapper extends AbstractMapper> extends AbstractDAO { 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 { + 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 +public interface AbstractModel { boolean isUnsaved(); + + TObject self(); + + AbstractMapper 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 +public class Model implements AbstractModel { private @Setter @Getter Integer id; private @Setter @Getter String name; @@ -26,6 +26,18 @@ public class Model implements AbstractModel } @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); -- cgit v1.1-2-g2b99