在路上

 找回密码
 立即注册
在路上 站点首页 学习 查看内容

基于领域驱动的Java开发工具包Common项目分享

2017-2-7 13:40| 发布者: zhangjf| 查看: 671| 评论: 0

摘要: 项目地址:https://github.com/xuliugen/common 喜欢的请加星。 先上图: 本项目主要包含:constant、domain、exception、util这四个主要部分。 Constant 主要包含了一些项目中的常用常量。 示例: (1)标点符号 ...

项目地址:https://github.com/xuliugen/common 喜欢的请加星。

先上图:

这里写图片描述

本项目主要包含:constant、domain、exception、util这四个主要部分。

Constant

这里写图片描述

主要包含了一些项目中的常用常量。

示例:
(1)标点符号常量类

  1. public interface ConstPunctuation {
  2. /**
  3. * : 冒号
  4. */
  5. String COLON = ":";
  6. /**
  7. * - 中划线
  8. */
  9. String MINUS = "-";
  10. /**
  11. * -- 双中划线
  12. */
  13. String TWO_MINUS = "--";
  14. /**
  15. * _ 下划线
  16. */
  17. String UNDERLINE = "_";
  18. String SLASH = "/";
  19. /**
  20. * , 逗号
  21. */
  22. String COMMA = ",";
  23. }
复制代码

(2)日期时间格式常量

  1. /**
  2. * yyyy-MM-dd HH:mm:ss
  3. */
  4. String yyyy_MM_ddHHmmss = "yyyy-MM-dd HH:mm:ss";
  5. /**
  6. * MM/dd/yy
  7. */
  8. String MMddyy1 = "MM/dd/yy";
  9. /**
  10. * yyyy-MM-dd
  11. */
  12. String yyyy_MM_dd = "yyyy-MM-dd";
  13. /**
  14. * MMddyy
  15. */
  16. String MMddyy = "MMddyy";
  17. /**
  18. * yyMMdd
  19. */
  20. String yyMMdd = "yyMMdd";
  21. /**
  22. * yyyy-MM-dd-HH
  23. */
  24. String yyyy_MM_dd_HH = "yyyy-MM-dd-HH";
  25. /**
  26. * mmssSSS
  27. */
  28. String mmssSSS = "mmssSSS";
  29. /**
  30. * HHmmssSSS
  31. */
  32. String HHmmssSSS = "HHmmssSSS";
复制代码

(3)正则表达式

  1. public interface ConstRegex {
  2. /**
  3. * 邮件
  4. */
  5. String REGEX_EMAIL = "[\w!#$%&'*+/=?^_`{|}~-]+(?:\.[\w!#$%&'*+/=?^_`{|}~-]+)*@(?:[\w](?:[\w-]*[\w])?\.)+[\w](?:[\w-]*[\w])?";
  6. }
复制代码
Domain

这里写图片描述

主要包含:基础实体对象、分页对象、Response返回对象(可以实现自定义异常的封装)、进行RPC调用的Response返回对象、枚举等。

示例:
(1)分页对象

  1. public class Page<T> implements Serializable {
  2. private static final long serialVersionUID = 5859907455479273251L;
  3. public static final int DEFAULT_PAGE_SIZE = 10;
  4. private int pageSize = DEFAULT_PAGE_SIZE; // 每页的记录数
  5. /**
  6. * 当前页码,从1开始记数
  7. */
  8. private long currentPage = 1;
  9. private long start = 0; // 当前页第一条数据在List中的位置,从0开始
  10. private List<T> data = new ArrayList<T>(); // 当前页中存放的记录,类型一般为List
  11. private long resultCount; // 总记录数
  12. public Page(int pageSize, long start, long currentPage) {
  13. this.pageSize = pageSize;
  14. this.start = start;
  15. this.currentPage = currentPage;
  16. }
  17. /**
  18. * 默认构造方法.
  19. * @param start 本页数据在数据库中的起始位置
  20. * @param totalSize 数据库中总记录条数
  21. * @param data 本页包含的数据
  22. */
  23. public Page(long start, long totalSize, List<T> data) {
  24. this.start = start;
  25. this.resultCount = totalSize;
  26. this.data = data;
  27. if (this.data == null) {
  28. this.data = new ArrayList<T>();
  29. }
  30. }
  31. /**
  32. * 获得第一条记录的截取位置
  33. * @return 第一条记录的截取位置
  34. */
  35. public long getStart() {
  36. if (currentPage - 1 >= 0) {
  37. return (currentPage - 1) * pageSize;
  38. }
  39. return 0;
  40. }
  41. /**
  42. * 默认构造方法.
  43. * @param start 本页数据在数据库中的起始位置
  44. * @param totalSize 数据库中总记录条数
  45. * @param pageSize 本页容量
  46. * @param data 本页包含的数据
  47. */
  48. public Page(long start, long totalSize, int pageSize, List<T> data) {
  49. this(start, totalSize, data);
  50. this.pageSize = pageSize;
  51. }
  52. public Page() {
  53. }
  54. 。。。
  55. }
复制代码
Utils

这里写图片描述

主要包含:bean的处理工具类(包含反射、转换等)、时间、编码、加密、验证、邮件服务、HTTP请求、文件上传、URL地址等String类型的组装等等。

示例:
(1)将Map转化为Bean实体对象集合:

  1. /**
  2. * 把map转换成JavaBean对象,要求map中的可以、名称与JavaBean中的属性相对应
  3. * 如果一个map中有多个javaBean中对应的属性,则同样可以实现对不同JavaBean的映射
  4. * @param map
  5. * @param clazz
  6. * @param <T>
  7. * @return 把map转换成对象
  8. */
  9. public static <T> T MaptoBean(Map map, Class<T> clazz) {
  10. try {
  11. /*
  12. * 1. 通过参数clazz创建实例
  13. * 2. 使用BeanUtils.populate把map的数据封闭到bean中
  14. */
  15. T bean = clazz.newInstance();
  16. ConvertUtils.register(new DateConverter(), java.util.Date.class);
  17. BeanUtils.populate(bean, map);
  18. return bean;
  19. } catch (Exception e) {
  20. throw new RuntimeException(e);
  21. }
  22. }
复制代码

(2)将Map组装成String,只组装第1层的

  1. /**
  2. * 将Map组装成String,只组装第1层的
  3. * @param paramKeyAndValueMap
  4. * @return
  5. */
  6. private static String assembleByMap1Level(Map<String, Object> paramKeyAndValueMap) {
  7. String url = "";
  8. for (Map.Entry<String, Object> entry : paramKeyAndValueMap.entrySet()) {
  9. String key = entry.getKey();
  10. Object value = entry.getValue();
  11. if (StringUtils.isNotBlank(key) && value != null) {
  12. StringBuffer urlBuffer = new StringBuffer();
  13. try {
  14. Map<String, Object> innerKeyValueMap = JSONObject.parseObject(value.toString(), Map.class);
  15. //如果value是个Json字符串则跳过
  16. continue;
  17. } catch (Exception e) {//如果抛异常说明value不是Json字符串
  18. urlBuffer.append(key + ConstPunctuation.EQUAL + value + ConstPunctuation.LINK);
  19. }
  20. url = url + urlBuffer.toString();
  21. }
  22. }
  23. if (url.endsWith(ConstPunctuation.LINK)) {
  24. url = url.substring(0, url.length() - 1);
  25. }
  26. return url;
  27. }
复制代码

还有很多好用的工具类的哈!

如何使用

项目之间的使用,不存在依赖,使用Maven项目打包:

  1. mvn clean
  2. mvn install
复制代码

可以进行分模块引用,例如引用util:

  1. <dependency>
  2. <groupId>com.xuliugen.common</groupId>
  3. <artifactId>util</artifactId>
  4. <version>1.0.0-SNAPSHOT</version>
  5. </dependency>
复制代码


号外

另外,本人也在写一个基于领域驱动的lib小框架,先看一下大致的架构:

这里写图片描述

包含缓存、数据库、日志服务、消息队列等等这些基础的领域基础模块,实现过程是通过:SPI的方式切换扩展点(类似Dubbo的实现过程),自定义xsd:schema、Spring配置文件注入的方式进行实现,开发者只需要选择熟悉的方式进行使用即可。

例如:缓存的实现可以有Redis、Memecached等,那我做的是提供统一的Api接口,供开发者调用,开发者只需要引用相应的依赖和在Spring配置文件中设置使用哪一个扩展点,比如使用Redis,那只需要将Redis的账户密码信息通过Spring配置文件的方式就可以实现对Redis的使用,当然我还会对Redis、Memecached进行进一步的封装,来提高大家的开发效率。

目前这个项目正在开发,有兴趣的请关注新浪微博:1573876303@qq.com,最新动态会在第一时间推送。

来自: http://blog.csdn.net/xlgen157387/article/details/50491559

最新评论

小黑屋|在路上 ( 蜀ICP备15035742号-1 

;

GMT+8, 2025-7-9 09:19

Copyright 2015-2025 djqfx

返回顶部