From 8ec2af6e29ecb29645367a28fb1a48cf7f4f24ff Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=A9r=C3=A9my=20Zurcher?= Date: Wed, 19 Apr 2017 16:01:19 +0200 Subject: java : MyMyBatis : add AbstractModel<> to implement save in AbstractDAOOperations<> --- java/MyMyBatis/src/AbstractDAOOperations.java | 9 ++++++++- java/MyMyBatis/src/AbstractModel.java | 7 +++++++ java/MyMyBatis/src/Main.java | 6 +++--- java/MyMyBatis/src/Model.java | 17 ++++++++++++++--- java/MyMyBatis/src/ModelDAO.java | 2 +- 5 files changed, 33 insertions(+), 8 deletions(-) create mode 100644 java/MyMyBatis/src/AbstractModel.java diff --git a/java/MyMyBatis/src/AbstractDAOOperations.java b/java/MyMyBatis/src/AbstractDAOOperations.java index 5307641..a96e336 100644 --- a/java/MyMyBatis/src/AbstractDAOOperations.java +++ b/java/MyMyBatis/src/AbstractDAOOperations.java @@ -2,7 +2,9 @@ package ch.asynk; import java.util.List; -public abstract class AbstractDAOOperations> extends AbstractDAO +public abstract class AbstractDAOOperations, + TMapper extends AbstractMapper> extends AbstractDAO { public int insert(final TObject obj) @@ -15,6 +17,11 @@ public abstract class AbstractDAOOperations m.update(obj), true); } + public int save(final TObject obj) + { + return (obj.isUnsaved() ? insert(obj) : update(obj)); + } + public int delete(final TObject obj) { return execInt(m -> m.delete(obj), true); diff --git a/java/MyMyBatis/src/AbstractModel.java b/java/MyMyBatis/src/AbstractModel.java new file mode 100644 index 0000000..81bab76 --- /dev/null +++ b/java/MyMyBatis/src/AbstractModel.java @@ -0,0 +1,7 @@ +package ch.asynk; + +public interface AbstractModel +{ + boolean isUnsaved(); +} + diff --git a/java/MyMyBatis/src/Main.java b/java/MyMyBatis/src/Main.java index 043e978..ad371f7 100644 --- a/java/MyMyBatis/src/Main.java +++ b/java/MyMyBatis/src/Main.java @@ -38,7 +38,7 @@ public class Main System.out.println("selectOne : " + m.toString()); System.out.println("update ..."); m.setName("adios"); - dao.update(m); + dao.save(m); m = dao.selectOne(2); failIf(m == null , "selectOne()"); failIf(!m.getName().equals("adios") , "selectOne()"); @@ -50,8 +50,8 @@ public class Main failIf(count != 1, "count()"); System.out.println("count : " + count); System.out.println("insert ..."); - m = new Model(-1, "HoMySatan"); - count = dao.insert(m); + m = new Model("HoMySatan"); + count = dao.save(m); 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 6e817ed..676cdde 100644 --- a/java/MyMyBatis/src/Model.java +++ b/java/MyMyBatis/src/Model.java @@ -3,18 +3,29 @@ package ch.asynk; import lombok.Setter; import lombok.Getter; -public class Model +public class Model implements AbstractModel { private @Setter @Getter Integer id; private @Setter @Getter String name; - public Model(Integer id, String name) + public Model() { - this.id = id; + this.id = null; + } + + public Model(String name) + { + this.id = null; this.name = name; } @Override + public boolean isUnsaved() + { + return (id == null); + } + + @Override public String toString() { return String.format("model [%d] %s", id, name); diff --git a/java/MyMyBatis/src/ModelDAO.java b/java/MyMyBatis/src/ModelDAO.java index 908f35a..dd0e10e 100644 --- a/java/MyMyBatis/src/ModelDAO.java +++ b/java/MyMyBatis/src/ModelDAO.java @@ -2,7 +2,7 @@ package ch.asynk; import java.util.List; -public class ModelDAO extends AbstractDAOOperations implements ModelMapper +public class ModelDAO extends AbstractDAOOperations implements ModelMapper { private static final ModelDAO instance = new ModelDAO(); -- cgit v1.1-2-g2b99