summaryrefslogtreecommitdiffstats
path: root/java
diff options
context:
space:
mode:
authorJérémy Zurcher <jeremy@asynk.ch>2017-03-23 17:10:39 +0100
committerJérémy Zurcher <jeremy@asynk.ch>2017-03-23 17:10:39 +0100
commit724efad67fe61d2c02c41af24ebc352eb79ff2f8 (patch)
tree777e62835ca2f6901d8d2542e328fcea403f9680 /java
parentbb1caf716a18b65fa42d7bed2369b66a0837d977 (diff)
downloadshare-724efad67fe61d2c02c41af24ebc352eb79ff2f8.zip
share-724efad67fe61d2c02c41af24ebc352eb79ff2f8.tar.gz
java : add MyMyBatis
Diffstat (limited to 'java')
-rw-r--r--java/MyMyBatis/build.xml33
-rw-r--r--java/MyMyBatis/ivy.xml18
-rw-r--r--java/MyMyBatis/src/AbstractDAO.java75
-rw-r--r--java/MyMyBatis/src/AbstractDAOOperations.java48
-rw-r--r--java/MyMyBatis/src/AbstractMapper.java18
-rw-r--r--java/MyMyBatis/src/Main.java41
-rw-r--r--java/MyMyBatis/src/MapperRegistration.java11
-rw-r--r--java/MyMyBatis/src/Model.java22
-rw-r--r--java/MyMyBatis/src/ModelDAO.java12
-rw-r--r--java/MyMyBatis/src/ModelMapper.java39
-rw-r--r--java/MyMyBatis/src/SqlConnection.java81
-rw-r--r--java/MyMyBatis/src/create.sql10
-rw-r--r--java/MyMyBatis/src/logback-test.xml14
-rwxr-xr-xjava/do3
14 files changed, 425 insertions, 0 deletions
diff --git a/java/MyMyBatis/build.xml b/java/MyMyBatis/build.xml
new file mode 100644
index 0000000..4d17c6c
--- /dev/null
+++ b/java/MyMyBatis/build.xml
@@ -0,0 +1,33 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<project xmlns:ivy="antlib:org.apache.ivy.ant"
+ name="MyMyBatis"
+ basedir="."
+ default="test">
+
+ <target name="resolve" description="retrieve dependencies with Ivy">
+ <ivy:resolve file="ivy.xml" log="download-only"/>
+ <ivy:cachepath pathid="ivy.deps.default" conf="default" />
+ </target>
+
+ <target name="compile" depends="resolve">
+ <mkdir dir="./build" />
+ <javac srcdir="./src"
+ destdir="./build"
+ includeantruntime="false">
+ <classpath refid="ivy.deps.default"/>
+ <compilerarg value="-Xlint:all"/>
+ <compilerarg value="-Xlint:-processing"/>
+ </javac>
+ </target>
+
+ <target name="test" depends="compile">
+ <java classname="ch.asynk.Main" fork="true">
+ <classpath>
+ <dirset dir="./src"/>
+ <dirset dir="./build"/>
+ </classpath>
+ <classpath refid="ivy.deps.default"/>
+ </java>
+ </target>
+
+</project>
diff --git a/java/MyMyBatis/ivy.xml b/java/MyMyBatis/ivy.xml
new file mode 100644
index 0000000..fbde5de
--- /dev/null
+++ b/java/MyMyBatis/ivy.xml
@@ -0,0 +1,18 @@
+<?xml version="1.0"?>
+<ivy-module version="2.0"
+ xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+ xsi:noNamespaceSchemaLocation="http://ant.apache.org/ivy/schemas/ivy.xsd">
+
+ <info organisation="ch.asynk" module="MyMyBatis" />
+
+ <configurations>
+ <conf name="default"/>
+ </configurations>
+
+ <dependencies defaultconf="default" defaultconfmapping="default->default">
+ <dependency org="org.mybatis" name="mybatis" rev="3.4.2"/>
+ <dependency org="org.hsqldb" name="hsqldb" rev="2.3.4"/>
+ <dependency org="org.projectlombok" name="lombok" rev="1.16.14"/>
+ <dependency org="ch.qos.logback" name="logback-classic" rev="1.2.1"/>
+ </dependencies>
+</ivy-module>
diff --git a/java/MyMyBatis/src/AbstractDAO.java b/java/MyMyBatis/src/AbstractDAO.java
new file mode 100644
index 0000000..916d511
--- /dev/null
+++ b/java/MyMyBatis/src/AbstractDAO.java
@@ -0,0 +1,75 @@
+package ch.asynk;
+
+import java.util.List;
+
+import org.apache.ibatis.session.SqlSession;
+import org.apache.ibatis.exceptions.PersistenceException;
+
+public abstract class AbstractDAO<TObject, TMapper>
+{
+ public final int DAO_ERROR = -1;
+ protected Class<TMapper> mapperClass = null;
+
+ public static boolean logErrors = true;
+ private static SqlConnection conn = null;
+
+ static {
+ conn = new SqlConnection();
+ MapperRegistration.registerMappers(conn);
+ }
+
+ static public SqlConnection getSqlConnection() { return conn; }
+
+ protected abstract class CallBack<TRet, TMapper>
+ {
+ public abstract TRet call(TMapper m);
+ }
+
+ protected int execInt(boolean commit, CallBack<Integer, TMapper> cb)
+ {
+ int status = DAO_ERROR;
+ SqlSession session = conn.openSqlSession();
+ try {
+ status = cb.call(session.getMapper(mapperClass));
+ if (commit) session.commit();
+ }
+ catch(PersistenceException e) {
+ if (commit) session.rollback();
+ if (logErrors) conn.error(e);
+ }
+ finally { session.close(); }
+ return status;
+ }
+
+ protected TObject execObject(boolean commit, CallBack<TObject, TMapper> cb)
+ {
+ TObject obj = null;
+ SqlSession session = conn.openSqlSession();
+ try {
+ obj = cb.call(session.getMapper(mapperClass));
+ if (commit) session.commit();
+ }
+ catch(PersistenceException e) {
+ if (commit) session.rollback();
+ if (logErrors) conn.error(e);
+ }
+ finally { session.close(); }
+ return obj;
+ }
+
+ protected List<TObject> execObjects(boolean commit, CallBack<List<TObject>, TMapper> cb)
+ {
+ List<TObject> list = null;
+ SqlSession session = conn.openSqlSession();
+ try {
+ list = cb.call(session.getMapper(mapperClass));
+ if (commit) session.commit();
+ }
+ catch(PersistenceException e) {
+ if (commit) session.rollback();
+ if (logErrors) conn.error(e);
+ }
+ finally { session.close(); }
+ return list;
+ }
+}
diff --git a/java/MyMyBatis/src/AbstractDAOOperations.java b/java/MyMyBatis/src/AbstractDAOOperations.java
new file mode 100644
index 0000000..e39f680
--- /dev/null
+++ b/java/MyMyBatis/src/AbstractDAOOperations.java
@@ -0,0 +1,48 @@
+package ch.asynk;
+
+import java.util.List;
+
+public abstract class AbstractDAOOperations<TObject, TIdentity, TMapper extends AbstractMapper<TObject, TIdentity>> extends AbstractDAO<TObject, TMapper>
+{
+ public int insert(final TObject obj)
+ {
+ return execInt(true, new CallBack<Integer, TMapper>() {
+ public Integer call(TMapper m) { return m.insert(obj); }
+ });
+ }
+
+ public int update(final TObject obj)
+ {
+ return execInt(true, new CallBack<Integer, TMapper>() {
+ public Integer call(TMapper m) { return m.update(obj); }
+ });
+ }
+
+ public int delete(final TObject obj)
+ {
+ return execInt(true, new CallBack<Integer, TMapper>() {
+ public Integer call(TMapper m) { return m.delete(obj); }
+ });
+ }
+
+ public int count()
+ {
+ return execInt(false, new CallBack<Integer, TMapper>() {
+ public Integer call(TMapper m) { return m.count(); }
+ });
+ }
+
+ public List<TObject> select()
+ {
+ return execObjects(false, new CallBack<List<TObject>, TMapper>() {
+ public List<TObject> call(TMapper m) { return m.select(); }
+ });
+ }
+
+ public TObject selectOne(final TIdentity id)
+ {
+ return execObject(false, new CallBack<TObject, TMapper>() {
+ public TObject call(TMapper m) { return m.selectOne(id); }
+ });
+ }
+}
diff --git a/java/MyMyBatis/src/AbstractMapper.java b/java/MyMyBatis/src/AbstractMapper.java
new file mode 100644
index 0000000..54887b8
--- /dev/null
+++ b/java/MyMyBatis/src/AbstractMapper.java
@@ -0,0 +1,18 @@
+package ch.asynk;
+
+import java.util.List;
+
+public interface AbstractMapper<TObject, TIdentity>
+{
+ int insert(TObject obj);
+
+ int update(TObject obj);
+
+ int delete(TObject obj);
+
+ int count();
+
+ List<TObject> select();
+
+ TObject selectOne(TIdentity id);
+}
diff --git a/java/MyMyBatis/src/Main.java b/java/MyMyBatis/src/Main.java
new file mode 100644
index 0000000..9e69b0d
--- /dev/null
+++ b/java/MyMyBatis/src/Main.java
@@ -0,0 +1,41 @@
+package ch.asynk;
+
+import java.util.List;
+
+public class Main
+{
+ public static void main(String [] args )
+ {
+ try {
+ ModelDAO.getSqlConnection().runScript("create.sql");
+ } catch (java.io.IOException e) {
+ System.err.println(e.getMessage());
+ }
+
+ ModelDAO dao = ModelDAO.getInstance();
+ Model m;
+ List<Model> models;
+
+ System.out.println("count : " + dao.count());
+ models = dao.select();
+ System.out.println("select : ");
+ for (Model _m : models) System.out.println(" - " +_m.toString());
+
+ m = dao.selectOne(2);
+ System.out.println("selectOne : " + m.toString());
+ System.out.println("update ...");
+ m.setName("adios");
+ dao.update(m);
+ m = dao.selectOne(2);
+ System.out.println("selectOne : " + m.toString());
+ System.out.println("delete : " + dao.delete(m));
+ System.out.println("count : " + dao.count());
+ System.out.println("insert ...");
+ m = new Model(-1, "HoMySatan");
+ dao.insert(m);
+ System.out.println("count : " + dao.count());
+ System.out.println("select : ");
+ models = dao.select();
+ for (Model _m : models) System.out.println(" - " +_m.toString());
+ }
+}
diff --git a/java/MyMyBatis/src/MapperRegistration.java b/java/MyMyBatis/src/MapperRegistration.java
new file mode 100644
index 0000000..caf5587
--- /dev/null
+++ b/java/MyMyBatis/src/MapperRegistration.java
@@ -0,0 +1,11 @@
+package ch.asynk;
+
+import org.apache.ibatis.session.Configuration;
+
+public class MapperRegistration
+{
+ static public void registerMappers(SqlConnection conn)
+ {
+ conn.registerMapper("Model", Model.class, ModelMapper.class);
+ }
+}
diff --git a/java/MyMyBatis/src/Model.java b/java/MyMyBatis/src/Model.java
new file mode 100644
index 0000000..6e817ed
--- /dev/null
+++ b/java/MyMyBatis/src/Model.java
@@ -0,0 +1,22 @@
+package ch.asynk;
+
+import lombok.Setter;
+import lombok.Getter;
+
+public class Model
+{
+ private @Setter @Getter Integer id;
+ private @Setter @Getter String name;
+
+ public Model(Integer id, String name)
+ {
+ this.id = id;
+ this.name = name;
+ }
+
+ @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
new file mode 100644
index 0000000..908f35a
--- /dev/null
+++ b/java/MyMyBatis/src/ModelDAO.java
@@ -0,0 +1,12 @@
+package ch.asynk;
+
+import java.util.List;
+
+public class ModelDAO extends AbstractDAOOperations<Model, Integer, ModelMapper> implements ModelMapper
+{
+ private static final ModelDAO instance = new ModelDAO();
+
+ private ModelDAO() { mapperClass = ModelMapper.class; }
+
+ public static ModelDAO getInstance() { return instance; }
+}
diff --git a/java/MyMyBatis/src/ModelMapper.java b/java/MyMyBatis/src/ModelMapper.java
new file mode 100644
index 0000000..6eb566c
--- /dev/null
+++ b/java/MyMyBatis/src/ModelMapper.java
@@ -0,0 +1,39 @@
+package ch.asynk;
+
+import java.util.List;
+
+import org.apache.ibatis.annotations.Insert;
+import org.apache.ibatis.annotations.Delete;
+import org.apache.ibatis.annotations.Update;
+import org.apache.ibatis.annotations.Select;
+
+public interface ModelMapper extends AbstractMapper<Model, Integer>
+{
+ final static String attrs = " id, name ";
+ final static String where = " where id=#{id} ";
+
+
+ final static String INSERT = "insert into models(name) values (#{name})";
+ @Insert(INSERT)
+ int insert(Model obj);
+
+ final static String UPDATE = "update models set name=#{name}" + where;
+ @Update(UPDATE)
+ int update(Model obj);
+
+ final static String DELETE = "delete from models" + where;
+ @Delete(DELETE)
+ int delete(Model obj);
+
+ final static String COUNT = "select count(*) from models";
+ @Select(COUNT)
+ int count();
+
+ final static String SELECT = "select" + attrs + "from models";
+ @Select(SELECT)
+ List<Model> select();
+
+ final static String SELECT_ONE = "select" + attrs + "from models" + where;
+ @Select(SELECT_ONE)
+ Model selectOne(Integer id);
+}
diff --git a/java/MyMyBatis/src/SqlConnection.java b/java/MyMyBatis/src/SqlConnection.java
new file mode 100644
index 0000000..57f2ea5
--- /dev/null
+++ b/java/MyMyBatis/src/SqlConnection.java
@@ -0,0 +1,81 @@
+package ch.asynk;
+
+import java.io.Reader;
+import java.util.List;
+
+import org.apache.ibatis.io.Resources;
+import org.apache.ibatis.jdbc.ScriptRunner;
+import org.apache.ibatis.mapping.Environment;
+import org.apache.ibatis.session.SqlSession;
+import org.apache.ibatis.session.SqlSessionFactory;
+import org.apache.ibatis.session.SqlSessionFactoryBuilder;
+import org.apache.ibatis.session.SqlSessionManager;
+import org.apache.ibatis.session.Configuration;
+import org.apache.ibatis.transaction.jdbc.JdbcTransactionFactory;
+import org.apache.ibatis.datasource.pooled.PooledDataSource;
+
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+public class SqlConnection
+{
+ private Logger logger;
+ private SqlSessionManager manager;
+
+ public void info(String msg) { logger.info(msg); }
+ public void error(Exception e) { logger.error("SQL ERROR : " + e.getMessage()); }
+
+ public SqlConnection()
+ {
+ this.logger = logger = LoggerFactory.getLogger(Main.class);
+ PooledDataSource ds = new PooledDataSource("org.hsqldb.jdbcDriver", "jdbc:hsqldb:mem:test;sql.syntax_pgs=true;check_props=true", "username", "password");
+ ds.setDefaultAutoCommit(false);
+ // ds.setPoolMaximumActiveConnections();
+ // ds.setPoolMaximumIdleConnections();
+ // ds.setPoolMaximumCheckoutTime();
+ // ds.setPoolTimeToWait();
+ // ds.setPoolPingQuery();
+ // ds.setPoolPingEnabled();
+ // ds.setPoolPingConnectionsNotUsedFor();
+
+ Environment e = new Environment("default", new JdbcTransactionFactory(), ds);
+ // e.();
+
+ Configuration c = new Configuration(e);
+ // c.();
+
+ SqlSessionFactory ssf = new SqlSessionFactoryBuilder().build(c);
+ // ssf.();
+
+ SqlSessionManager sm = SqlSessionManager.newInstance(ssf);
+ // sm.();
+
+ this.manager = sm;
+ }
+
+ protected void registerMapper(String alias, Class<?> beanClass, Class<?> mapperClass)
+ {
+ logger.info("register "+alias+ " -> "+beanClass.getName()+" "+mapperClass.getName());
+ Configuration config = manager.getConfiguration();
+ config.addMapper(mapperClass);
+ config.getTypeAliasRegistry().registerAlias(alias, beanClass);
+ }
+
+ public void runScript(String filename) throws java.io.IOException
+ {
+ info("Run script : " + filename);
+ SqlSession session = manager.openSession();
+ Reader reader = Resources.getResourceAsReader(filename);
+ ScriptRunner runner = new ScriptRunner(session.getConnection());
+ runner.setLogWriter(null);
+ runner.runScript(reader);
+ reader.close();
+ session.close();
+ }
+
+ public SqlSession openSqlSession()
+ {
+ // no autocommit
+ return manager.openSession(false);
+ }
+}
diff --git a/java/MyMyBatis/src/create.sql b/java/MyMyBatis/src/create.sql
new file mode 100644
index 0000000..337d230
--- /dev/null
+++ b/java/MyMyBatis/src/create.sql
@@ -0,0 +1,10 @@
+CREATE TABLE models
+(
+ id serial NOT NULL,
+ name text,
+ CONSTRAINT pk_id PRIMARY KEY (id)
+);
+
+INSERT INTO models(name) VALUES ('asynk');
+INSERT INTO models(name) VALUES ('fake');
+SET AUTOCOMMIT FALSE;
diff --git a/java/MyMyBatis/src/logback-test.xml b/java/MyMyBatis/src/logback-test.xml
new file mode 100644
index 0000000..2298b90
--- /dev/null
+++ b/java/MyMyBatis/src/logback-test.xml
@@ -0,0 +1,14 @@
+<configuration debug="false">
+
+ <contextName>MyMyBatis</contextName>
+
+ <appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
+ <encoder>
+ <pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n</pattern>
+ </encoder>
+ </appender>
+
+ <root level="error">
+ <appender-ref ref="STDOUT" />
+ </root>
+</configuration>
diff --git a/java/do b/java/do
index 3b7bddd..50ca674 100755
--- a/java/do
+++ b/java/do
@@ -3,4 +3,7 @@ for java in *.java
do
echo "############" && javac $java && java ${java%.java}
done
+pushd MyMyBatis > /dev/null
+rm -fr build && ant && rm -fr build
+popd
rm *.class