项目地址:https://github.com/xuliugen/common 喜欢的请加星。 先上图:  本项目主要包含:constant、domain、exception、util这四个主要部分。 Constant  主要包含了一些项目中的常用常量。 示例: (1)标点符号常量类 - public interface ConstPunctuation {
- /**
- * : 冒号
- */
- String COLON = ":";
- /**
- * - 中划线
- */
- String MINUS = "-";
- /**
- * -- 双中划线
- */
- String TWO_MINUS = "--";
- /**
- * _ 下划线
- */
- String UNDERLINE = "_";
- String SLASH = "/";
- /**
- * , 逗号
- */
- String COMMA = ",";
- }
复制代码(2)日期时间格式常量 - /**
- * yyyy-MM-dd HH:mm:ss
- */
- String yyyy_MM_ddHHmmss = "yyyy-MM-dd HH:mm:ss";
- /**
- * MM/dd/yy
- */
- String MMddyy1 = "MM/dd/yy";
- /**
- * yyyy-MM-dd
- */
- String yyyy_MM_dd = "yyyy-MM-dd";
- /**
- * MMddyy
- */
- String MMddyy = "MMddyy";
- /**
- * yyMMdd
- */
- String yyMMdd = "yyMMdd";
- /**
- * yyyy-MM-dd-HH
- */
- String yyyy_MM_dd_HH = "yyyy-MM-dd-HH";
- /**
- * mmssSSS
- */
- String mmssSSS = "mmssSSS";
- /**
- * HHmmssSSS
- */
- String HHmmssSSS = "HHmmssSSS";
复制代码(3)正则表达式 - public interface ConstRegex {
- /**
- * 邮件
- */
- String REGEX_EMAIL = "[\w!#$%&'*+/=?^_`{|}~-]+(?:\.[\w!#$%&'*+/=?^_`{|}~-]+)*@(?:[\w](?:[\w-]*[\w])?\.)+[\w](?:[\w-]*[\w])?";
- }
复制代码 Domain  主要包含:基础实体对象、分页对象、Response返回对象(可以实现自定义异常的封装)、进行RPC调用的Response返回对象、枚举等。 示例: (1)分页对象 - public class Page<T> implements Serializable {
- private static final long serialVersionUID = 5859907455479273251L;
- public static final int DEFAULT_PAGE_SIZE = 10;
- private int pageSize = DEFAULT_PAGE_SIZE; // 每页的记录数
- /**
- * 当前页码,从1开始记数
- */
- private long currentPage = 1;
- private long start = 0; // 当前页第一条数据在List中的位置,从0开始
- private List<T> data = new ArrayList<T>(); // 当前页中存放的记录,类型一般为List
- private long resultCount; // 总记录数
- public Page(int pageSize, long start, long currentPage) {
- this.pageSize = pageSize;
- this.start = start;
- this.currentPage = currentPage;
- }
- /**
- * 默认构造方法.
- * @param start 本页数据在数据库中的起始位置
- * @param totalSize 数据库中总记录条数
- * @param data 本页包含的数据
- */
- public Page(long start, long totalSize, List<T> data) {
- this.start = start;
- this.resultCount = totalSize;
- this.data = data;
- if (this.data == null) {
- this.data = new ArrayList<T>();
- }
- }
- /**
- * 获得第一条记录的截取位置
- * @return 第一条记录的截取位置
- */
- public long getStart() {
- if (currentPage - 1 >= 0) {
- return (currentPage - 1) * pageSize;
- }
- return 0;
- }
- /**
- * 默认构造方法.
- * @param start 本页数据在数据库中的起始位置
- * @param totalSize 数据库中总记录条数
- * @param pageSize 本页容量
- * @param data 本页包含的数据
- */
- public Page(long start, long totalSize, int pageSize, List<T> data) {
- this(start, totalSize, data);
- this.pageSize = pageSize;
- }
- public Page() {
- }
- 。。。
- }
复制代码 Utils  主要包含:bean的处理工具类(包含反射、转换等)、时间、编码、加密、验证、邮件服务、HTTP请求、文件上传、URL地址等String类型的组装等等。 示例: (1)将Map转化为Bean实体对象集合: - /**
- * 把map转换成JavaBean对象,要求map中的可以、名称与JavaBean中的属性相对应
- * 如果一个map中有多个javaBean中对应的属性,则同样可以实现对不同JavaBean的映射
- * @param map
- * @param clazz
- * @param <T>
- * @return 把map转换成对象
- */
- public static <T> T MaptoBean(Map map, Class<T> clazz) {
- try {
- /*
- * 1. 通过参数clazz创建实例
- * 2. 使用BeanUtils.populate把map的数据封闭到bean中
- */
- T bean = clazz.newInstance();
- ConvertUtils.register(new DateConverter(), java.util.Date.class);
- BeanUtils.populate(bean, map);
- return bean;
- } catch (Exception e) {
- throw new RuntimeException(e);
- }
- }
复制代码(2)将Map组装成String,只组装第1层的 - /**
- * 将Map组装成String,只组装第1层的
- * @param paramKeyAndValueMap
- * @return
- */
- private static String assembleByMap1Level(Map<String, Object> paramKeyAndValueMap) {
- String url = "";
- for (Map.Entry<String, Object> entry : paramKeyAndValueMap.entrySet()) {
- String key = entry.getKey();
- Object value = entry.getValue();
- if (StringUtils.isNotBlank(key) && value != null) {
- StringBuffer urlBuffer = new StringBuffer();
- try {
- Map<String, Object> innerKeyValueMap = JSONObject.parseObject(value.toString(), Map.class);
- //如果value是个Json字符串则跳过
- continue;
- } catch (Exception e) {//如果抛异常说明value不是Json字符串
- urlBuffer.append(key + ConstPunctuation.EQUAL + value + ConstPunctuation.LINK);
- }
- url = url + urlBuffer.toString();
- }
- }
- if (url.endsWith(ConstPunctuation.LINK)) {
- url = url.substring(0, url.length() - 1);
- }
- return url;
- }
复制代码还有很多好用的工具类的哈! 如何使用 项目之间的使用,不存在依赖,使用Maven项目打包: 可以进行分模块引用,例如引用util: - <dependency>
- <groupId>com.xuliugen.common</groupId>
- <artifactId>util</artifactId>
- <version>1.0.0-SNAPSHOT</version>
- </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 |