summaryrefslogtreecommitdiffstats
path: root/java
diff options
context:
space:
mode:
authorJérémy Zurcher <jeremy@asynk.ch>2017-04-25 10:49:07 +0200
committerJérémy Zurcher <jeremy@asynk.ch>2017-04-25 10:49:07 +0200
commit609c5d6d7a228f027c01dd203f78fe64ed641641 (patch)
tree615480127b3db4a41d0558246f79e7c688e0c666 /java
parent4eaaddb6813b0f563c9022e67f7a4d4a2fc8bae1 (diff)
downloadshare-609c5d6d7a228f027c01dd203f78fe64ed641641.zip
share-609c5d6d7a228f027c01dd203f78fe64ed641641.tar.gz
java : MyMyBatis : add selectSelf(TObject) -> reload() for free
Diffstat (limited to 'java')
-rw-r--r--java/MyMyBatis/src/AbstractDAOOperations.java5
-rw-r--r--java/MyMyBatis/src/AbstractMapper.java2
-rw-r--r--java/MyMyBatis/src/AbstractModel.java4
-rw-r--r--java/MyMyBatis/src/Main.java9
-rw-r--r--java/MyMyBatis/src/Model.java6
-rw-r--r--java/MyMyBatis/src/ModelMapper.java4
6 files changed, 26 insertions, 4 deletions
diff --git a/java/MyMyBatis/src/AbstractDAOOperations.java b/java/MyMyBatis/src/AbstractDAOOperations.java
index 90e0ff4..854811a 100644
--- a/java/MyMyBatis/src/AbstractDAOOperations.java
+++ b/java/MyMyBatis/src/AbstractDAOOperations.java
@@ -41,4 +41,9 @@ public abstract class AbstractDAOOperations<TIdentity,
{
return execObject(m -> m.selectOne(id));
}
+
+ public TObject selectSelf(final TObject obj)
+ {
+ return execObject(m -> m.selectSelf(obj));
+ }
}
diff --git a/java/MyMyBatis/src/AbstractMapper.java b/java/MyMyBatis/src/AbstractMapper.java
index c1ec675..42a0983 100644
--- a/java/MyMyBatis/src/AbstractMapper.java
+++ b/java/MyMyBatis/src/AbstractMapper.java
@@ -17,4 +17,6 @@ public interface AbstractMapper<TIdentity, TObject>
List<TObject> select();
TObject selectOne(TIdentity id);
+
+ TObject selectSelf(TObject obj);
}
diff --git a/java/MyMyBatis/src/AbstractModel.java b/java/MyMyBatis/src/AbstractModel.java
index 365472c..7ed83aa 100644
--- a/java/MyMyBatis/src/AbstractModel.java
+++ b/java/MyMyBatis/src/AbstractModel.java
@@ -11,5 +11,9 @@ public interface AbstractModel<TIdentity, TObject>
default void save() { getDAO().save(self()); }
default int delete() { return getDAO().delete(self()); }
+
+ default void reload() { feedFrom(getDAO().selectSelf(self())); }
+
+ void feedFrom(TObject obj);
}
diff --git a/java/MyMyBatis/src/Main.java b/java/MyMyBatis/src/Main.java
index deb274f..d3b8332 100644
--- a/java/MyMyBatis/src/Main.java
+++ b/java/MyMyBatis/src/Main.java
@@ -39,10 +39,11 @@ public class Main
System.out.println("update ...");
m.setName("adios");
m.save();
- m = dao.selectOne(2);
- failIf(m == null , "selectOne()");
- failIf(!m.getName().equals("adios") , "selectOne()");
- System.out.println("selectOne : " + m.toString());
+ m.setName("wrong");
+ m.reload();
+ failIf(m == null , "reload()");
+ failIf(!m.getName().equals("adios") , "selectSelf()");
+ System.out.println("selectSelf : " + m.toString());
count = m.delete();
failIf(count != 1, "delete()");
System.out.println("delete : " + count);
diff --git a/java/MyMyBatis/src/Model.java b/java/MyMyBatis/src/Model.java
index daa7938..a8b1bca 100644
--- a/java/MyMyBatis/src/Model.java
+++ b/java/MyMyBatis/src/Model.java
@@ -42,4 +42,10 @@ public class Model implements AbstractModel<Integer, Model>
{
return String.format("model [%d] %s", id, name);
}
+
+ @Override
+ public void feedFrom(Model other)
+ {
+ this.name = other.name;
+ }
}
diff --git a/java/MyMyBatis/src/ModelMapper.java b/java/MyMyBatis/src/ModelMapper.java
index e71f851..901af2c 100644
--- a/java/MyMyBatis/src/ModelMapper.java
+++ b/java/MyMyBatis/src/ModelMapper.java
@@ -36,4 +36,8 @@ public interface ModelMapper extends AbstractMapper<Integer, Model>
final static String SELECT_ONE = SELECT + where;
@Select(SELECT_ONE)
Model selectOne(Integer id);
+
+ final static String SELECT_SELF = SELECT + where;
+ @Select(SELECT_SELF)
+ Model selectSelf(Model obj);
}