jt-common-core

Sonatype helps open source projects to set up Maven repositories on https://oss.sonatype.org/

License

License

Categories

Categories

Bamboo Build Tools Continuous Integration and Continuous Delivery
GroupId

GroupId

com.github.bamboo-cn
ArtifactId

ArtifactId

jt-common-core
Last Version

Last Version

1.0.3
Release Date

Release Date

Type

Type

jar
Description

Description

jt-common-core
Sonatype helps open source projects to set up Maven repositories on https://oss.sonatype.org/
Project URL

Project URL

http://maven.apache.org
Source Code Management

Source Code Management

https://github.com/bamboo-cn/jt-common-core

Download jt-common-core

How to add to project

<!-- https://jarcasting.com/artifacts/com.github.bamboo-cn/jt-common-core/ -->
<dependency>
    <groupId>com.github.bamboo-cn</groupId>
    <artifactId>jt-common-core</artifactId>
    <version>1.0.3</version>
</dependency>
// https://jarcasting.com/artifacts/com.github.bamboo-cn/jt-common-core/
implementation 'com.github.bamboo-cn:jt-common-core:1.0.3'
// https://jarcasting.com/artifacts/com.github.bamboo-cn/jt-common-core/
implementation ("com.github.bamboo-cn:jt-common-core:1.0.3")
'com.github.bamboo-cn:jt-common-core:jar:1.0.3'
<dependency org="com.github.bamboo-cn" name="jt-common-core" rev="1.0.3">
  <artifact name="jt-common-core" type="jar" />
</dependency>
@Grapes(
@Grab(group='com.github.bamboo-cn', module='jt-common-core', version='1.0.3')
)
libraryDependencies += "com.github.bamboo-cn" % "jt-common-core" % "1.0.3"
[com.github.bamboo-cn/jt-common-core "1.0.3"]

Dependencies

compile (24)

Group / Artifact Type Version
org.springframework.boot : spring-boot-starter jar 2.1.5.RELEASE
org.springframework.boot : spring-boot-starter-data-redis jar 2.1.5.RELEASE
org.springframework : spring-core jar 5.0.0.RELEASE
org.springframework : spring-context jar 5.0.0.RELEASE
org.springframework : spring-tx jar 5.0.0.RELEASE
org.springframework : spring-webmvc jar 5.0.0.RELEASE
org.hibernate : hibernate-validator jar 5.0.2.Final
commons-beanutils : commons-beanutils jar 1.8.3
org.apache.commons : commons-lang3 jar 3.4
commons-fileupload : commons-fileupload jar 1.3.1
com.alibaba : fastjson jar 1.2.58
com.thoughtworks.xstream : xstream jar 1.4.3
dom4j : dom4j jar 1.6.1
org.apache.httpcomponents : httpclient jar 4.5.3
commons-codec : commons-codec jar 1.10
net.sf.json-lib : json-lib jar 2.4
org.quartz-scheduler : quartz jar 2.2.1
javax.mail : mail jar 1.4.7
org.apache.poi : poi jar 3.17
org.apache.poi : poi-ooxml jar 3.17
redis.clients : jedis jar 2.9.0
org.slf4j : slf4j-api jar 1.7.7
org.aspectj : aspectjrt jar 1.8.9
org.apache.maven.plugins : maven-gpg-plugin jar 1.6

provided (1)

Group / Artifact Type Version
javax : javaee-api jar 7.0

Project Modules

There are no modules declared in this project.

jt-common-core

在个人原有组件xm-common-core的基础上组成,主要封装一些常用方法,方便Java项目快速开发

微信订阅号 mu-zhuzi

版更新记录

  • 1.0.4[2020-1-17] GsonUtil工具类,身份证验证和手机号码隐藏中间部位
  • 1.0.3[2020-1-15] 修复spring cache单独设置key过期时间bug
  • 1.0.2[2020-1-10] 支持spring cache使用redis做缓存,Redis故障或不可用时仍然执行sql层方法使服务可用
  • 1.0.1[2020-1-1] 支持跨域,redis分布式锁

常用通用方法集合

├─base
├─beanvalidator
├─cache
├─config
├─constant
├─entity
├─exception
├─http
│  ├─request
│  └─response
└─utils
    └─email

maven 中央仓库依赖

<dependency>
  <groupId>com.github.bamboo-cn</groupId>
  <artifactId>jt-common-core</artifactId>
  <version>1.0.1</version>
</dependency>

以下依赖方式已经作废,推荐MAVEN中央仓库的方式引入

	<repositories>
		<repository>
		    <id>jitpack.io</id>
		    <url>https://jitpack.io</url>
		</repository>
	</repositories>

	<dependency>
	    <groupId>com.github.bamboo-cn</groupId>
	    <artifactId>jt-common-core</artifactId>
	    <version>1.0.4</version>
	</dependency>

spring-boot web项目启动类

@SpringBootApplication
@RestController
@SpringBootApplication(scanBasePackages = {"com.bamboo.common"})//加入扫描路径
@Import(value = {CorsConfig.class, LogFilter.class,SynRedisLockAspect.class}) //跨域,接口访问请求日志,redis分布式锁
public class DemoApplication {

	public static void main(String[] args) {
		SpringApplication.run(DemoApplication.class, args);
	}

	@RequestMapping("/")
	public Object index(){
		return "helll demo"+DateUtils.getDate();
	}


    /**分布式锁**/
    @RequestMapping("/synLock/{uid}")
	@SynRedisLock(path = "syn:test:%s", indexProps = {"0"})
	public Object synLock(@PathVariable("uid")Integer uid){
		redisClient.set("test",uid.toString());
		try {
			Thread.sleep(5000);//停顿5秒
		}catch (Exception e){

		}

		return redisClient.get("test");
	}
}

根据需要引入工具类 import 实现可跨域和日志请求拦截处理,分布式锁

Reids分布式锁

    /**分布式锁**/
    @RequestMapping("/synLock/{uid}")
	@SynRedisLock(path = "syn:test:%s", indexProps = {"0"})
    public Object synLock(@PathVariable("uid")Integer uid)

path:key路径 indexProps:key路径中替换值对应的参数下标(从0开始),如果是对象则为:参数名.属性

	@SynRedisLock(path = "syn:test:%s", indexProps = {"user.id"})
    public Object synLock(User user)

缓存功能配置

spring:
    cache: #缓存配置
        prefix: bamboo #key前缀
        ttlTime: 2000  #缓存时长单位秒
    redis: #redis配置同spring-redis配置相同
        host: localhost
        port: 6379
        timeout: 2000
        password: redis
        database: 0
        timeout: 2000
        maxIdle: 10
        maxTotal: 100
        

个别属性如果不配置默认和上面一样,password默认是空值

cache配置默认值

  • prefix: spring-cache
  • ttlTime: 3600 一个小时

默认支持spring cache使用redis做缓存

  • 使用方式和spring cache注解相同
  • 使用fastjson作为序列化
  • Cacheable支持自定义单个key设置超时时间

自定义单个key设置超时时间,key加上字符串TTL=1000实现扩展单个KEY过期时间

    //@Cacheable(key = "T(String).valueOf(#code).concat('TTL=10')")
    @Cacheable(key = "#code+'TTL=10'")
    public Double getRetailByCode(Integer code) {
        RetailRatio retailRatio =  retailRatioMapper.selectByPrimaryKey(code);
        return retailRatio.getUnratio();
    }

以上两种方式都可以(1.0.3版本已经修复)

具体使用参考以下地址 https://blog.csdn.net/zjcjava/article/details/103920388

GsonUtil工具类

GsonUtil提供了常用的对象转json字符串以及反向转换

        Map<String,String> headMap = new LinkedHashMap<String,String>();
        headMap.put("name","姓名");
        headMap.put("age","20");
        headMap.put("birthday","1990-12-20");
        headMap.put("height","180");
        headMap.put("weight","120");
        headMap.put("sex","m");

        //map转str
        String str =GsonUtil.toJSONString(headMap);
        System.out.println(str);
        //str转obj
        Student  student= GsonUtil.parseObject(str,Student.class);
        System.out.println(GsonUtil.toJSONString(student));
        System.out.println(student.getBirthday());
        System.out.println(student.getName());

        //list转str
       List<Student> list = new ArrayList<Student>();
        list.add(student);
        str =GsonUtil.toJSONString(list);
        System.out.println(str);
        //str转list
        list= GsonUtil.parseObjectList(str,Student.class);
        student = list.get(0);
        System.out.println(student.getName());

个人订阅号

在这里插入图片描述

Versions

Version
1.0.3
1.0.2
1.0.1
1.0.0