summaryrefslogtreecommitdiffstats
path: root/java/MyMyBatis/src
diff options
context:
space:
mode:
Diffstat (limited to 'java/MyMyBatis/src')
-rw-r--r--java/MyMyBatis/src/AbstractDAOOperations.java9
-rw-r--r--java/MyMyBatis/src/AbstractModel.java7
-rw-r--r--java/MyMyBatis/src/Main.java6
-rw-r--r--java/MyMyBatis/src/Model.java17
-rw-r--r--java/MyMyBatis/src/ModelDAO.java2
5 files changed, 33 insertions, 8 deletions
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<TObject, TIdentity, TMapper extends AbstractMapper<TObject, TIdentity>> extends AbstractDAO<TObject, TMapper>
+public abstract class AbstractDAOOperations<TIdentity,
+ TObject extends AbstractModel<TIdentity>,
+ TMapper extends AbstractMapper<TObject, TIdentity>> extends AbstractDAO<TObject, TMapper>
{
public int insert(final TObject obj)
@@ -15,6 +17,11 @@ public abstract class AbstractDAOOperations<TObject, TIdentity, TMapper extends
return execInt(m -> 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<TIdentity>
+{
+ 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<Integer>
{
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<Model, Integer, ModelMapper> implements ModelMapper
+public class ModelDAO extends AbstractDAOOperations<Integer, Model, ModelMapper> implements ModelMapper
{
private static final ModelDAO instance = new ModelDAO();