diff --git a/tigon-model/src/main/java/me/chyxion/tigon/model/BaseModel.java b/tigon-model/src/main/java/me/chyxion/tigon/model/BaseModel.java index 5904ea4..a2d9fa2 100644 --- a/tigon-model/src/main/java/me/chyxion/tigon/model/BaseModel.java +++ b/tigon-model/src/main/java/me/chyxion/tigon/model/BaseModel.java @@ -3,6 +3,7 @@ package me.chyxion.tigon.model; import java.util.*; import java.lang.reflect.Field; import com.alibaba.fastjson.JSON; +import lombok.extern.slf4j.Slf4j; import me.chyxion.tigon.util.WordUtils; import me.chyxion.tigon.mybatis.PrimaryKey; import org.springframework.util.ReflectionUtils; @@ -12,7 +13,8 @@ import org.springframework.util.ReflectionUtils; * chyxion@163.com
* Feb 21, 2017 18:39:39 */ -public class BaseModel extends MappableSupport implements Mappable { +@Slf4j +public abstract class BaseModel extends MappableSupport implements Mappable { private static final long serialVersionUID = 1L; // id @@ -53,8 +55,18 @@ public class BaseModel extends MappableSupport implements Mappable { @SuppressWarnings("unchecked") public PK primaryKeyValue() { return (PK) ReflectionUtils.getField( + ReflectionUtils.findField(getClass(), primaryKeyName()), this); + } + + /** + * set primary key + * @param value primary key + * @return previous primary key value + */ + public void primaryKeyValue(PK value) { + ReflectionUtils.setField( ReflectionUtils.findField(getClass(), - primaryKeyName()), getClass()); + primaryKeyName()), this, value); } /** @@ -101,14 +113,14 @@ public class BaseModel extends MappableSupport implements Mappable { * before insert model, for override */ public void beforeInsert() { - + log.debug("Model [{}] Before Insert.", this); } /** * before update model, for override */ public void beforeUpdate() { - + log.debug("Model [{}] Before Update.", this); } /** diff --git a/tigon-model/src/main/java/me/chyxion/tigon/model/M0.java b/tigon-model/src/main/java/me/chyxion/tigon/model/M0.java index 08706d0..40afc91 100644 --- a/tigon-model/src/main/java/me/chyxion/tigon/model/M0.java +++ b/tigon-model/src/main/java/me/chyxion/tigon/model/M0.java @@ -32,4 +32,12 @@ public class M0 extends BaseModel { public Id primaryKeyValue() { return id; } + + /** + * {@inheritDoc} + */ + @Override + public void primaryKeyValue(Id id) { + this.id = id; + } } diff --git a/tigon-mybatis/src/main/resources/__tigon_mybatis__.xml b/tigon-mybatis/src/main/resources/__tigon_mybatis__.xml index e71900d..7ee1e40 100644 --- a/tigon-mybatis/src/main/resources/__tigon_mybatis__.xml +++ b/tigon-mybatis/src/main/resources/__tigon_mybatis__.xml @@ -80,7 +80,8 @@ - + ${__col__} @@ -98,6 +99,7 @@ values + #{__val__} @@ -134,6 +136,7 @@ + ${__m_col__} = #{__val__} diff --git a/tigon-mybatis/src/test/resources/log4j2.xml b/tigon-mybatis/src/test/resources/log4j2.xml index 4b2521a..56a323c 100644 --- a/tigon-mybatis/src/test/resources/log4j2.xml +++ b/tigon-mybatis/src/test/resources/log4j2.xml @@ -9,8 +9,8 @@ + fileName="${log.dir}/tigon-mybatis.log" + filePattern="${log.dir}/tigon-mybatis-%d{yyyy-MM-dd}-%i.log"> diff --git a/tigon-service-support/pom.xml b/tigon-service-support/pom.xml index d163a32..6069459 100644 --- a/tigon-service-support/pom.xml +++ b/tigon-service-support/pom.xml @@ -21,6 +21,10 @@ me.chyxion.tigon tigon-service-api + + me.chyxion.tigon + tigon-sequence + me.chyxion.tigon tigon-mybatis @@ -35,10 +39,45 @@ provided - - junit - junit - test - + + org.springframework + spring-test + test + + + junit + junit + test + + + com.alibaba + druid + test + + + mysql + mysql-connector-java + test + + + org.apache.logging.log4j + log4j-slf4j-impl + test + + + org.apache.logging.log4j + log4j-core + test + + + org.slf4j + jcl-over-slf4j + test + + + javax.el + javax.el-api + test + diff --git a/tigon-service-support/src/main/java/me/chyxion/tigon/service/support/BaseCrudServiceSupport.java b/tigon-service-support/src/main/java/me/chyxion/tigon/service/support/BaseCrudServiceSupport.java index 2272a04..6800ff3 100644 --- a/tigon-service-support/src/main/java/me/chyxion/tigon/service/support/BaseCrudServiceSupport.java +++ b/tigon-service-support/src/main/java/me/chyxion/tigon/service/support/BaseCrudServiceSupport.java @@ -10,6 +10,7 @@ import me.chyxion.tigon.mybatis.Search; import me.chyxion.tigon.model.ViewModel; import me.chyxion.tigon.mybatis.BaseMapper; import javax.validation.constraints.NotNull; +import org.apache.commons.lang3.StringUtils; import me.chyxion.tigon.service.BaseCrudService; import org.hibernate.validator.constraints.NotEmpty; import me.chyxion.tigon.validation.annotation.NotNullOrBlank; @@ -51,7 +52,6 @@ public class BaseCrudServiceSupport */ @Override public ViewModel create(Model model) { - model.beforeInsert(); beforeInsert(model); mapper.insert(model); afterInsert(model); @@ -76,7 +76,6 @@ public class BaseCrudServiceSupport */ @Override public ViewModel update(Model model) { - model.beforeUpdate(); beforeUpdate(model); mapper.update(model); afterUpdate(model); @@ -113,6 +112,12 @@ public class BaseCrudServiceSupport // private methods protected void beforeInsert(Model model) { + if (idType.equals(String.class) && + StringUtils.isBlank((String) model.primaryKeyValue())) { + final String id = idSeq.get(); + log.debug("Insert Model [{}] Id Is Blank, Generate [{}].", model, id); + model.primaryKeyValue((PrimaryKey) id); + } // Hook } diff --git a/tigon-service-support/src/main/java/me/chyxion/tigon/service/support/BaseServiceSupport.java b/tigon-service-support/src/main/java/me/chyxion/tigon/service/support/BaseServiceSupport.java index 9d21c67..961d071 100644 --- a/tigon-service-support/src/main/java/me/chyxion/tigon/service/support/BaseServiceSupport.java +++ b/tigon-service-support/src/main/java/me/chyxion/tigon/service/support/BaseServiceSupport.java @@ -1,5 +1,7 @@ package me.chyxion.tigon.service.support; +import lombok.Getter; +import lombok.Setter; import java.util.Date; import java.util.List; import java.util.ArrayList; @@ -9,6 +11,7 @@ import me.chyxion.tigon.model.M1; import me.chyxion.tigon.model.BaseModel; import me.chyxion.tigon.model.ViewModel; import me.chyxion.tigon.mybatis.BaseMapper; +import me.chyxion.tigon.sequence.IdSequence; import me.chyxion.tigon.service.BaseService; import org.springframework.core.GenericTypeResolver; import org.springframework.beans.factory.annotation.Autowired; @@ -21,6 +24,8 @@ import org.springframework.beans.factory.annotation.Autowired; * Nov 8, 2016 10:09:29 AM */ @Slf4j +@Getter +@Setter public class BaseServiceSupport , Mapper extends BaseMapper> @@ -28,6 +33,8 @@ public class BaseServiceSupport @Autowired protected Mapper mapper; + @Autowired + protected IdSequence idSeq; protected Class idType; protected Class modelType; @@ -44,20 +51,6 @@ public class BaseServiceSupport modelType = (Class) clazzArr[1]; } - /** - * @return the mapper - */ - public Mapper getMapper() { - return mapper; - } - - /** - * @param mapper the mapper to set - */ - public void setMapper(Mapper mapper) { - this.mapper = mapper; - } - /** * {@inheritDoc} */ @@ -89,9 +82,9 @@ public class BaseServiceSupport } /** - * @param model - * @param url - * @return + * @param model model + * @param url url + * @return url */ protected String urlCacheClear(M1 model, String url) { Date dateUpdated = model.getDateUpdated(); @@ -100,8 +93,8 @@ public class BaseServiceSupport } /** - * @param viewModel - * @param model + * @param viewModel view model + * @param model model */ protected void processViewModel(ViewModel viewModel, Model model) { // For Subclass Override diff --git a/tigon-service-support/src/test/java/me/chyxion/tigon/mapper/TestModelMapper.java b/tigon-service-support/src/test/java/me/chyxion/tigon/mapper/TestModelMapper.java new file mode 100644 index 0000000..64dccca --- /dev/null +++ b/tigon-service-support/src/test/java/me/chyxion/tigon/mapper/TestModelMapper.java @@ -0,0 +1,12 @@ +package me.chyxion.tigon.mapper; + +import me.chyxion.tigon.model.TestModel; +import me.chyxion.tigon.mybatis.BaseMapper; + +/** + * @author Shaun Chyxion
+ * chyxion@163.com
+ * Apr 19, 2017 10:58:45 + */ +public interface TestModelMapper extends BaseMapper { +} diff --git a/tigon-service-support/src/test/java/me/chyxion/tigon/model/TestModel.java b/tigon-service-support/src/test/java/me/chyxion/tigon/model/TestModel.java new file mode 100644 index 0000000..3221668 --- /dev/null +++ b/tigon-service-support/src/test/java/me/chyxion/tigon/model/TestModel.java @@ -0,0 +1,17 @@ +package me.chyxion.tigon.model; + +import lombok.Getter; +import lombok.Setter; +import me.chyxion.tigon.mybatis.Table; + +/** + * @author Shaun Chyxion
+ * chyxion@163.com
+ * Apr 19, 2017 10:58:52 + */ +@Getter +@Setter +@Table("tigon_test_model") +public class TestModel extends M3 { + private String name; +} diff --git a/tigon-service-support/src/test/java/me/chyxion/tigon/service/support/TestDriver.java b/tigon-service-support/src/test/java/me/chyxion/tigon/service/TestDriver.java similarity index 61% rename from tigon-service-support/src/test/java/me/chyxion/tigon/service/support/TestDriver.java rename to tigon-service-support/src/test/java/me/chyxion/tigon/service/TestDriver.java index f520196..7a48b1a 100644 --- a/tigon-service-support/src/test/java/me/chyxion/tigon/service/support/TestDriver.java +++ b/tigon-service-support/src/test/java/me/chyxion/tigon/service/TestDriver.java @@ -1,8 +1,6 @@ -package me.chyxion.tigon.service.support; +package me.chyxion.tigon.service; /** - * @version 0.0.1 - * @since 0.0.1 * @author Shaun Chyxion
* chyxion@163.com
* Nov 23, 2016 3:22:44 PM diff --git a/tigon-service-support/src/test/java/me/chyxion/tigon/service/TestModelService.java b/tigon-service-support/src/test/java/me/chyxion/tigon/service/TestModelService.java new file mode 100644 index 0000000..4e41e1a --- /dev/null +++ b/tigon-service-support/src/test/java/me/chyxion/tigon/service/TestModelService.java @@ -0,0 +1,11 @@ +package me.chyxion.tigon.service; + +import me.chyxion.tigon.model.TestModel; + +/** + * @author Shaun Chyxion
+ * chyxion@163.com
+ * Apr 19, 2017 10:59:31 + */ +public interface TestModelService extends BaseCrudService { +} diff --git a/tigon-service-support/src/test/java/me/chyxion/tigon/service/support/TestModelServiceSupport.java b/tigon-service-support/src/test/java/me/chyxion/tigon/service/support/TestModelServiceSupport.java new file mode 100644 index 0000000..cfbd087 --- /dev/null +++ b/tigon-service-support/src/test/java/me/chyxion/tigon/service/support/TestModelServiceSupport.java @@ -0,0 +1,15 @@ +package me.chyxion.tigon.service.support; + +import me.chyxion.tigon.model.TestModel; +import me.chyxion.tigon.mapper.TestModelMapper; +import me.chyxion.tigon.service.TestModelService; + +/** + * @author Shaun Chyxion
+ * chyxion@163.com
+ * Apr 19, 2017 10:59:00 + */ +public class TestModelServiceSupport + extends BaseCrudServiceSupport + implements TestModelService { +} diff --git a/tigon-service-support/src/test/java/me/chyxion/tigon/service/test/TestModelServiceTest.java b/tigon-service-support/src/test/java/me/chyxion/tigon/service/test/TestModelServiceTest.java new file mode 100644 index 0000000..6f3b673 --- /dev/null +++ b/tigon-service-support/src/test/java/me/chyxion/tigon/service/test/TestModelServiceTest.java @@ -0,0 +1,36 @@ +package me.chyxion.tigon.service.test; + +import org.junit.Test; +import org.junit.runner.RunWith; +import me.chyxion.tigon.model.TestModel; +import me.chyxion.tigon.service.TestModelService; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.test.context.ContextConfiguration; +import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; + +/** + * @author Shaun Chyxion
+ * chyxion@163.com
+ * Apr 19, 2017 10:59:07 + */ +@RunWith(SpringJUnit4ClassRunner.class) +@ContextConfiguration("classpath*:spring/spring-*.xml") +public class TestModelServiceTest { + @Autowired + private TestModelService testModelService; + + @Test + public void testInsert() { + TestModel testModel = new TestModel(); + testModel.setName("test"); + testModel.setNote("test note"); + testModel.setCreatedBy("donghuang"); + testModel.setEnabled(true); + testModelService.create(testModel); + } + + @Test + public void testUpdate() { + System.err.println(testModelService.list(null)); + } +} diff --git a/tigon-service-support/src/test/resources/db/tigon_test.sql b/tigon-service-support/src/test/resources/db/tigon_test.sql new file mode 100644 index 0000000..c29fecb --- /dev/null +++ b/tigon-service-support/src/test/resources/db/tigon_test.sql @@ -0,0 +1,36 @@ +/* + Navicat Premium Data Transfer + + Source Server : Localhost + Source Server Type : MySQL + Source Server Version : 50717 + Source Host : localhost + Source Database : tigon_test + + Target Server Type : MySQL + Target Server Version : 50717 + File Encoding : utf-8 + + Date: 04/19/2017 11:19:03 AM +*/ + +SET NAMES utf8; +SET FOREIGN_KEY_CHECKS = 0; + +-- ---------------------------- +-- Table structure for `tigon_test_model` +-- ---------------------------- +DROP TABLE IF EXISTS `tigon_test_model`; +CREATE TABLE `tigon_test_model` ( + `id` varchar(36) NOT NULL, + `date_created` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP, + `date_updated` timestamp NULL DEFAULT NULL, + `created_by` varchar(36) NOT NULL, + `updated_by` varchar(36) DEFAULT NULL, + `note` varchar(64) DEFAULT NULL, + `name` varchar(36) DEFAULT NULL, + `enabled` tinyint(1) DEFAULT NULL, + PRIMARY KEY (`id`) +) ENGINE=InnoDB DEFAULT CHARSET=utf8; + +SET FOREIGN_KEY_CHECKS = 1; diff --git a/tigon-service-support/src/test/resources/log4j2.xml b/tigon-service-support/src/test/resources/log4j2.xml index 4196c5b..f6a6964 100644 --- a/tigon-service-support/src/test/resources/log4j2.xml +++ b/tigon-service-support/src/test/resources/log4j2.xml @@ -1,15 +1,16 @@ - + %-d{yyyy-MM-dd HH:mm:ss,SSS} %-5p [%t][%c{1}] %m%n + .logs - + @@ -17,29 +18,15 @@ - - - - - - + - ${log.appender} + + diff --git a/tigon-service-support/src/test/resources/mybatis/mapper/test-model-mapper.xml b/tigon-service-support/src/test/resources/mybatis/mapper/test-model-mapper.xml new file mode 100644 index 0000000..6e66e6f --- /dev/null +++ b/tigon-service-support/src/test/resources/mybatis/mapper/test-model-mapper.xml @@ -0,0 +1,15 @@ + + + + + diff --git a/tigon-service-support/src/test/resources/spring/config.properties b/tigon-service-support/src/test/resources/spring/config.properties new file mode 100644 index 0000000..74878ff --- /dev/null +++ b/tigon-service-support/src/test/resources/spring/config.properties @@ -0,0 +1,9 @@ +# Config Dev + +# Database +db.url=jdbc:mysql://127.0.0.1:3306/tigon_test?useUnicode=true&characterEncoding=utf-8&useSSL=false&useJDBCCompliantTimezoneShift=true&useLegacyDatetimeCode=false&serverTimezone=UTC +db.user=root +db.password=12345678 + +redis.host=127.0.0.1 +redis.password=0211 diff --git a/tigon-service-support/src/test/resources/spring/spring-mybatis.xml b/tigon-service-support/src/test/resources/spring/spring-mybatis.xml new file mode 100644 index 0000000..460444f --- /dev/null +++ b/tigon-service-support/src/test/resources/spring/spring-mybatis.xml @@ -0,0 +1,24 @@ + + + + + + + + \ No newline at end of file diff --git a/tigon-service-support/src/test/resources/spring/spring-test.xml b/tigon-service-support/src/test/resources/spring/spring-test.xml new file mode 100644 index 0000000..dbfda4c --- /dev/null +++ b/tigon-service-support/src/test/resources/spring/spring-test.xml @@ -0,0 +1,11 @@ + + + + +