21个文件已删除
5 文件已重命名
9个文件已修改
| | |
| | | <?xml version="1.0"?>
|
| | | <project xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" xmlns="http://maven.apache.org/POM/4.0.0"
|
| | | xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
|
| | | <modelVersion>4.0.0</modelVersion>
|
| | | <parent>
|
| | | <project xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"
|
| | | xmlns="http://maven.apache.org/POM/4.0.0"
|
| | | xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
|
| | | <modelVersion>4.0.0</modelVersion>
|
| | | <parent>
|
| | | <groupId>com.yeshi</groupId>
|
| | | <artifactId>FanLiMain</artifactId>
|
| | | <version>0.0.1-SNAPSHOT</version>
|
| | | </parent>
|
| | | <groupId>com.yeshi</groupId>
|
| | | <artifactId>FanLiMain</artifactId>
|
| | | <version>0.0.1-SNAPSHOT</version>
|
| | | </parent>
|
| | | <groupId>com.yeshi</groupId>
|
| | | <artifactId>utils</artifactId>
|
| | | <version>0.0.2-SNAPSHOT</version>
|
| | | <name>utils</name>
|
| | | <url>http://maven.apache.org</url>
|
| | | <properties>
|
| | | <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
| | | </properties>
|
| | | <dependencies>
|
| | | <dependency>
|
| | | <groupId>junit</groupId>
|
| | | <artifactId>junit</artifactId>
|
| | | <version>3.8.1</version>
|
| | | <scope>test</scope>
|
| | | </dependency>
|
| | | |
| | | <!-- csv文件解析依赖 -->
|
| | | <dependency>
|
| | | <groupId>com.opencsv</groupId>
|
| | | <artifactId>opencsv</artifactId>
|
| | | <version>4.3.2</version>
|
| | | </dependency>
|
| | | <artifactId>utils</artifactId>
|
| | | <version>0.0.2-SNAPSHOT</version>
|
| | | <name>utils</name>
|
| | | <url>http://maven.apache.org</url>
|
| | | <properties>
|
| | | <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
| | | </properties>
|
| | | <dependencies>
|
| | | <dependency>
|
| | | <groupId>junit</groupId>
|
| | | <artifactId>junit</artifactId>
|
| | | <version>3.8.1</version>
|
| | | <scope>test</scope>
|
| | | </dependency>
|
| | |
|
| | | <!-- https://mvnrepository.com/artifact/ch.ethz.ganymed/ganymed-ssh2 -->
|
| | | <dependency>
|
| | | <groupId>ch.ethz.ganymed</groupId>
|
| | | <artifactId>ganymed-ssh2</artifactId>
|
| | | <version>build210</version>
|
| | | </dependency>
|
| | | <!-- csv文件解析依赖 -->
|
| | | <dependency>
|
| | | <groupId>com.opencsv</groupId>
|
| | | <artifactId>opencsv</artifactId>
|
| | | <version>4.3.2</version>
|
| | | </dependency>
|
| | |
|
| | | <!-- https://mvnrepository.com/artifact/com.alipay.sdk/alipay-easysdk -->
|
| | | <dependency>
|
| | | <groupId>com.alipay.sdk</groupId>
|
| | | <artifactId>alipay-easysdk</artifactId>
|
| | | <version>2.1.0</version>
|
| | | </dependency>
|
| | | <!-- https://mvnrepository.com/artifact/ch.ethz.ganymed/ganymed-ssh2 -->
|
| | | <dependency>
|
| | | <groupId>ch.ethz.ganymed</groupId>
|
| | | <artifactId>ganymed-ssh2</artifactId>
|
| | | <version>build210</version>
|
| | | </dependency>
|
| | |
|
| | | <!-- https://mvnrepository.com/artifact/com.alipay.sdk/alipay-easysdk -->
|
| | | <dependency>
|
| | | <groupId>com.alipay.sdk</groupId>
|
| | | <artifactId>alipay-easysdk</artifactId>
|
| | | <version>2.1.0</version>
|
| | | </dependency>
|
| | |
|
| | |
|
| | | <dependency>
|
| | | <groupId>org.freemarker</groupId>
|
| | | <artifactId>freemarker</artifactId>
|
| | | <version>2.3.23</version>
|
| | | </dependency>
|
| | |
|
| | | </dependencies>
|
| | | <dependency>
|
| | | <groupId>com.qcloud</groupId>
|
| | | <artifactId>cmq-http-client</artifactId>
|
| | | <version>1.0.7</version>
|
| | | </dependency>
|
| | |
|
| | |
|
| | | </dependencies>
|
| | | </project>
|
| | |
| | | import java.util.ArrayList;
|
| | | import java.util.List;
|
| | |
|
| | | import com.qcloud.cmq.Account;
|
| | | import com.qcloud.cmq.Message;
|
| | | import com.qcloud.cmq.Queue;
|
| | | import com.qcloud.cmq.QueueMeta;
|
| | | import com.qcloud.cmq.Topic;
|
| | | import com.qcloud.cmq.*;
|
| | | import org.springframework.data.annotation.Transient;
|
| | |
|
| | | //腾讯CMQ消息
|
| | | public class CMQUtil {
|
| | |
|
| | | private static CMQUtil cmqUtil;
|
| | | private static CMQUtil cmqUtil;
|
| | |
|
| | | public static CMQUtil getInstance(String secretId, String secretKey) {
|
| | | if (cmqUtil == null) {
|
| | | cmqUtil = new CMQUtil();
|
| | | cmqUtil.init(secretId, secretKey);
|
| | | }
|
| | | return cmqUtil;
|
| | | }
|
| | | public static CMQUtil getInstance(String secretId, String secretKey) {
|
| | | if (cmqUtil == null) {
|
| | | cmqUtil = new CMQUtil();
|
| | | cmqUtil.init(secretId, secretKey);
|
| | | }
|
| | | return cmqUtil;
|
| | | }
|
| | |
|
| | | private String secretId = "";
|
| | | private String secretKey = "";
|
| | | // 内网 http://cmq-queue-gz.api.tencentyun.com
|
| | | // 外网 http://cmq-queue-gz.api.qcloud.com
|
| | | private static String endpoint = "http://cmq-queue-gz.api.qcloud.com";
|
| | | private static String topicEndPoint = "https://cmq-topic-gz.api.qcloud.com";
|
| | | // private static String endpoint =
|
| | | // "http://cmq-queue-gz.api.tencentyun.com";
|
| | | private String secretId = "";
|
| | | private String secretKey = "";
|
| | | // 内网 http://cmq-queue-gz.api.tencentyun.com
|
| | | // 外网 http://cmq-queue-gz.api.qcloud.com
|
| | | private static String endpoint = "http://cmq-queue-gz.api.qcloud.com";
|
| | | private static String topicEndPoint = "https://cmq-topic-gz.api.qcloud.com";
|
| | | // private static String endpoint =
|
| | | // "http://cmq-queue-gz.api.tencentyun.com";
|
| | |
|
| | | private Account account;
|
| | | private Account topicAccount;
|
| | | private Account account;
|
| | | private Account topicAccount;
|
| | |
|
| | | static {
|
| | | // if (SystemUtil.getSystemType() == SystemUtil.SYSTEM_LINUX)
|
| | | // endpoint = "http://cmq-queue-gz.api.tencentyun.com";
|
| | | // else
|
| | | endpoint = "http://cmq-queue-gz.api.qcloud.com";
|
| | | }
|
| | | static {
|
| | | // if (SystemUtil.getSystemType() == SystemUtil.SYSTEM_LINUX)
|
| | | // endpoint = "http://cmq-queue-gz.api.tencentyun.com";
|
| | | // else
|
| | | endpoint = "http://cmq-queue-gz.api.qcloud.com";
|
| | | }
|
| | |
|
| | | public void init(String secretId, String secretKey) {
|
| | | this.secretId = secretId;
|
| | | this.secretKey = secretKey;
|
| | | account = new Account(endpoint, this.secretId, this.secretKey);
|
| | | topicAccount = new Account(topicEndPoint, this.secretId, this.secretKey);
|
| | | }
|
| | | public void init(String secretId, String secretKey) {
|
| | | this.secretId = secretId;
|
| | | this.secretKey = secretKey;
|
| | | account = new Account(endpoint, this.secretId, this.secretKey);
|
| | | topicAccount = new Account(topicEndPoint, this.secretId, this.secretKey);
|
| | | }
|
| | |
|
| | | public boolean existQueue(String queueName) {
|
| | | public boolean existQueue(String queueName) {
|
| | |
|
| | | ArrayList<String> vtQueue = new ArrayList<String>();
|
| | | try {
|
| | | int totalCount = account.listQueue(queueName, 0, 1, vtQueue);
|
| | | if (totalCount <= 0)
|
| | | return false;
|
| | | } catch (Exception e) {
|
| | | e.printStackTrace();
|
| | | }
|
| | | if (vtQueue.size() > 0 && vtQueue.get(0).equalsIgnoreCase(queueName))
|
| | | return true;
|
| | | return false;
|
| | | }
|
| | | ArrayList<String> vtQueue = new ArrayList<String>();
|
| | | try {
|
| | | int totalCount = account.listQueue(queueName, 0, 1, vtQueue);
|
| | | if (totalCount <= 0)
|
| | | return false;
|
| | | } catch (Exception e) {
|
| | | e.printStackTrace();
|
| | | }
|
| | | if (vtQueue.size() > 0 && vtQueue.get(0).equalsIgnoreCase(queueName))
|
| | | return true;
|
| | | return false;
|
| | | }
|
| | |
|
| | | // 创建队列
|
| | | public boolean createQueue(String queueName) {
|
| | | private boolean queueNameExist(String queueName) {
|
| | | List<String> resultList = new ArrayList<>();
|
| | | try {
|
| | | account.listQueue(queueName, 0, 100, resultList);
|
| | | } catch (Exception e) {
|
| | | e.printStackTrace();
|
| | | }
|
| | |
|
| | | QueueMeta meta = new QueueMeta();
|
| | | meta.pollingWaitSeconds = 10;
|
| | | meta.visibilityTimeout = 5 * 60;// 消息可见性超时
|
| | | meta.maxMsgSize = 65536;
|
| | | meta.msgRetentionSeconds = 345600;
|
| | | try {
|
| | | account.createQueue(queueName, meta);
|
| | | return true;
|
| | | } catch (Exception e) {
|
| | | return false;
|
| | | }
|
| | | }
|
| | | //已经创建了
|
| | | if (resultList.contains(queueName)) {
|
| | | return true;
|
| | | }
|
| | | return false;
|
| | | }
|
| | |
|
| | | // 创建队列
|
| | | public boolean createQueue(String queueName, int maxMsgSize) {
|
| | | // 创建队列
|
| | | public boolean createQueue(String queueName) {
|
| | |
|
| | | QueueMeta meta = new QueueMeta();
|
| | | meta.pollingWaitSeconds = 10;
|
| | | meta.visibilityTimeout = 5 * 60;// 消息可见性超时
|
| | | meta.maxMsgSize = maxMsgSize;
|
| | | meta.msgRetentionSeconds = 345600;
|
| | | try {
|
| | | account.createQueue(queueName, meta);
|
| | | return true;
|
| | | } catch (Exception e) {
|
| | | return false;
|
| | | }
|
| | | }
|
| | | if (queueNameExist(queueName)) {
|
| | | return true;
|
| | | }
|
| | |
|
| | | /**
|
| | | * 指定参数创建队列
|
| | | * |
| | | * @param queueName
|
| | | * @param pollingWaitSeconds
|
| | | * -长轮训等待时间
|
| | | * @param visibilityTimeout
|
| | | * -消息消费后再次可见的时间
|
| | | * @return
|
| | | */
|
| | | public boolean createQueue(String queueName, int pollingWaitSeconds, int visibilityTimeout) {
|
| | | QueueMeta meta = new QueueMeta();
|
| | | meta.pollingWaitSeconds = 10;
|
| | | meta.visibilityTimeout = 5 * 60;// 消息可见性超时
|
| | | meta.maxMsgSize = 65536;
|
| | | meta.msgRetentionSeconds = 345600;
|
| | | try {
|
| | | account.createQueue(queueName, meta);
|
| | | return true;
|
| | | } catch (Exception e) {
|
| | | return false;
|
| | | }
|
| | | }
|
| | |
|
| | | QueueMeta meta = new QueueMeta();
|
| | | meta.pollingWaitSeconds = pollingWaitSeconds;
|
| | | meta.visibilityTimeout = visibilityTimeout;// 消息可见性超时
|
| | | meta.maxMsgSize = 65536;
|
| | | meta.msgRetentionSeconds = 345600;
|
| | | try {
|
| | | account.createQueue(queueName, meta);
|
| | | return true;
|
| | | } catch (Exception e) {
|
| | | return false;
|
| | | }
|
| | | }
|
| | | // 创建队列
|
| | | public boolean createQueue(String queueName, int maxMsgSize) {
|
| | | if (queueNameExist(queueName)) {
|
| | | return true;
|
| | | }
|
| | | QueueMeta meta = new QueueMeta();
|
| | | meta.pollingWaitSeconds = 10;
|
| | | meta.visibilityTimeout = 5 * 60;// 消息可见性超时
|
| | | meta.maxMsgSize = maxMsgSize;
|
| | | meta.msgRetentionSeconds = 345600;
|
| | | try {
|
| | | account.createQueue(queueName, meta);
|
| | | return true;
|
| | | } catch (Exception e) {
|
| | | return false;
|
| | | }
|
| | | }
|
| | |
|
| | | // 删除队列
|
| | | public boolean deleteQueue(String queueName) {
|
| | | try {
|
| | | account.deleteQueue(queueName);
|
| | | return true;
|
| | | } catch (Exception e) {
|
| | | return false;
|
| | | }
|
| | | }
|
| | | /**
|
| | | * 指定参数创建队列
|
| | | *
|
| | | * @param queueName
|
| | | * @param pollingWaitSeconds -长轮训等待时间
|
| | | * @param visibilityTimeout -消息消费后再次可见的时间
|
| | | * @return
|
| | | */
|
| | | public boolean createQueue(String queueName, int pollingWaitSeconds, int visibilityTimeout) {
|
| | |
|
| | | // 获取队列列表
|
| | | public List<String> getQueueNameList(String key) {
|
| | | account = new Account(endpoint, this.secretId, this.secretKey);
|
| | | ArrayList<String> vtQueue = new ArrayList<String>();
|
| | | try {
|
| | | int totalCount = account.listQueue(key, 0, 100, vtQueue);
|
| | | } catch (Exception e) {
|
| | | e.printStackTrace();
|
| | | }
|
| | | return vtQueue;
|
| | | }
|
| | | if (queueNameExist(queueName)) {
|
| | | return true;
|
| | | }
|
| | |
|
| | | // 获取队列
|
| | | public Queue getQueue(String queueName) {
|
| | | account = new Account(endpoint, this.secretId, this.secretKey);
|
| | | Queue queue = account.getQueue(queueName);
|
| | | return queue;
|
| | | }
|
| | | QueueMeta meta = new QueueMeta();
|
| | | meta.pollingWaitSeconds = pollingWaitSeconds;
|
| | | meta.visibilityTimeout = visibilityTimeout;// 消息可见性超时
|
| | | meta.maxMsgSize = 65536;
|
| | | meta.msgRetentionSeconds = 345600;
|
| | | try {
|
| | | account.createQueue(queueName, meta);
|
| | | return true;
|
| | | } catch (Exception e) {
|
| | | return false;
|
| | | }
|
| | | }
|
| | |
|
| | | // 获取队列属性
|
| | | public QueueMeta getQueueAtrribute(String queueName) {
|
| | | Queue queue = account.getQueue(queueName);
|
| | | QueueMeta meta2 = null;
|
| | | try {
|
| | | meta2 = queue.getQueueAttributes();
|
| | | } catch (Exception e) {
|
| | | e.printStackTrace();
|
| | | }
|
| | | return meta2;
|
| | | }
|
| | | // 删除队列
|
| | | public boolean deleteQueue(String queueName) {
|
| | | try {
|
| | | account.deleteQueue(queueName);
|
| | | return true;
|
| | | } catch (Exception e) {
|
| | | return false;
|
| | | }
|
| | | }
|
| | |
|
| | | // 发送消息
|
| | | public String sendMsg(String queueName, String msg) {
|
| | | try {
|
| | | Queue queue = getQueue(queueName);
|
| | | String msgId = queue.sendMessage(msg);
|
| | | return msgId;
|
| | | } catch (Exception e) {
|
| | | e.printStackTrace();
|
| | | }
|
| | | return null;
|
| | | }
|
| | | // 获取队列列表
|
| | | public List<String> getQueueNameList(String key) {
|
| | | account = new Account(endpoint, this.secretId, this.secretKey);
|
| | | ArrayList<String> vtQueue = new ArrayList<String>();
|
| | | try {
|
| | | int totalCount = account.listQueue(key, 0, 100, vtQueue);
|
| | | } catch (Exception e) {
|
| | | e.printStackTrace();
|
| | | }
|
| | | return vtQueue;
|
| | | }
|
| | |
|
| | | // 消费消息
|
| | | public Message recieveMsg(String queueName) {
|
| | | try {
|
| | | Queue queue = getQueue(queueName);
|
| | | Message msg = queue.receiveMessage(10);
|
| | | return msg;
|
| | | } catch (Exception e) {
|
| | | e.printStackTrace();
|
| | | }
|
| | | return null;
|
| | | }
|
| | | // 获取队列
|
| | | public Queue getQueue(String queueName) {
|
| | | account = new Account(endpoint, this.secretId, this.secretKey);
|
| | | Queue queue = account.getQueue(queueName);
|
| | | return queue;
|
| | | }
|
| | |
|
| | | /**
|
| | | * 消费消息
|
| | | * |
| | | * @param count
|
| | | * 1-16
|
| | | * @param queueName
|
| | | * 队列名字
|
| | | * @return
|
| | | */
|
| | | public List<Message> recieveMsg(int count, String queueName) {
|
| | | Queue queue = getQueue(queueName);
|
| | | // 获取队列属性
|
| | | public QueueMeta getQueueAtrribute(String queueName) {
|
| | | Queue queue = account.getQueue(queueName);
|
| | | QueueMeta meta2 = null;
|
| | | try {
|
| | | meta2 = queue.getQueueAttributes();
|
| | | } catch (Exception e) {
|
| | | e.printStackTrace();
|
| | | }
|
| | | return meta2;
|
| | | }
|
| | |
|
| | | if (queue == null) {
|
| | | return null;
|
| | | }
|
| | | // 发送消息
|
| | | public String sendMsg(String queueName, String msg) {
|
| | | try {
|
| | | Queue queue = getQueue(queueName);
|
| | | String msgId = queue.sendMessage(msg);
|
| | | return msgId;
|
| | | } catch (Exception e) {
|
| | | e.printStackTrace();
|
| | | }
|
| | | return null;
|
| | | }
|
| | |
|
| | | List<Message> msgList = null;
|
| | | try {
|
| | | msgList = queue.batchReceiveMessage(count, 20);
|
| | | return msgList;
|
| | | } catch (Exception e) {
|
| | | if (e.getMessage() != null && !e.getMessage().contains("no message"))
|
| | | e.printStackTrace();
|
| | | }
|
| | | return null;
|
| | | }
|
| | | // 消费消息
|
| | | public Message recieveMsg(String queueName) {
|
| | | try {
|
| | | Queue queue = getQueue(queueName);
|
| | | Message msg = queue.receiveMessage(10);
|
| | | return msg;
|
| | | } catch (Exception e) {
|
| | | e.printStackTrace();
|
| | | }
|
| | | return null;
|
| | | }
|
| | |
|
| | | public List<Message> recieveMsg(int count, String queueName, int waitSeconds) {
|
| | | Queue queue = getQueue(queueName);
|
| | | List<Message> msgList = null;
|
| | | try {
|
| | | msgList = queue.batchReceiveMessage(count, waitSeconds);
|
| | | return msgList;
|
| | | } catch (Exception e) {
|
| | | }
|
| | | return null;
|
| | | }
|
| | | /**
|
| | | * 消费消息
|
| | | *
|
| | | * @param count 1-16
|
| | | * @param queueName 队列名字
|
| | | * @return
|
| | | */
|
| | | public List<Message> recieveMsg(int count, String queueName) {
|
| | | Queue queue = getQueue(queueName);
|
| | |
|
| | | // 删除消息
|
| | | public boolean deleteMsg(String queueName, String receiptHandle) {
|
| | | try {
|
| | | Queue queue = getQueue(queueName);
|
| | | queue.deleteMessage(receiptHandle);
|
| | | return true;
|
| | | } catch (Exception e) {
|
| | | e.printStackTrace();
|
| | | }
|
| | | return false;
|
| | | }
|
| | | if (queue == null) {
|
| | | return null;
|
| | | }
|
| | |
|
| | | /**
|
| | | * 订阅消息相关
|
| | | */
|
| | | List<Message> msgList = null;
|
| | | try {
|
| | | msgList = queue.batchReceiveMessage(count, 20);
|
| | | return msgList;
|
| | | } catch (Exception e) {
|
| | | if (e.getMessage() != null && !e.getMessage().contains("no message"))
|
| | | e.printStackTrace();
|
| | | }
|
| | | return null;
|
| | | }
|
| | |
|
| | | /**
|
| | | * 创建订阅主题
|
| | | * |
| | | * @param topicName-主题名称
|
| | | * @param maxMsgSize-消息最大长度
|
| | | * @param filterType-过滤类型
|
| | | * @return
|
| | | */
|
| | | public boolean createTopic(String topicName, int maxMsgSize, int filterType) {
|
| | | try {
|
| | | topicAccount.createTopic(topicName, maxMsgSize, filterType);
|
| | | return true;
|
| | | } catch (Exception e) {
|
| | | e.printStackTrace();
|
| | | }
|
| | | return false;
|
| | | }
|
| | | public List<Message> recieveMsg(int count, String queueName, int waitSeconds) {
|
| | | Queue queue = getQueue(queueName);
|
| | | List<Message> msgList = null;
|
| | | try {
|
| | | msgList = queue.batchReceiveMessage(count, waitSeconds);
|
| | | return msgList;
|
| | | } catch (Exception e) {
|
| | | }
|
| | | return null;
|
| | | }
|
| | |
|
| | | /**
|
| | | * 创建默认参数的主题
|
| | | * |
| | | * @param topicName
|
| | | * @return
|
| | | */
|
| | | public boolean createTopic(String topicName) {
|
| | | try {
|
| | | topicAccount.createTopic(topicName, 65536);
|
| | | return true;
|
| | | } catch (Exception e) {
|
| | | e.printStackTrace();
|
| | | }
|
| | | return false;
|
| | | }
|
| | | // 删除消息
|
| | | public boolean deleteMsg(String queueName, String receiptHandle) {
|
| | | try {
|
| | | Queue queue = getQueue(queueName);
|
| | | queue.deleteMessage(receiptHandle);
|
| | | return true;
|
| | | } catch (Exception e) {
|
| | | e.printStackTrace();
|
| | | }
|
| | | return false;
|
| | | }
|
| | |
|
| | | /**
|
| | | * 订阅主题
|
| | | * |
| | | * @param topicName-主题名称
|
| | | * @param subscriptionName-订阅名称
|
| | | * @param queueName-接受消息的队列名称
|
| | | * @return
|
| | | */
|
| | | public boolean subscribeTopic(String topicName, String subscriptionName, String queueName) {
|
| | | try {
|
| | | topicAccount.createSubscribe(topicName, subscriptionName, queueName, "queue");
|
| | | } catch (Exception e) {
|
| | | e.printStackTrace();
|
| | | }
|
| | | return false;
|
| | | }
|
| | | /**
|
| | | * 订阅消息相关
|
| | | */
|
| | |
|
| | | /**
|
| | | * 删除订阅
|
| | | * |
| | | * @param topicName
|
| | | * @param subscriptionName
|
| | | * @return
|
| | | */
|
| | | public boolean deleteSubscribeTopic(String topicName, String subscriptionName) {
|
| | | try {
|
| | | topicAccount.deleteSubscribe(topicName, subscriptionName);
|
| | | return true;
|
| | | } catch (Exception e) {
|
| | | e.printStackTrace();
|
| | | }
|
| | |
|
| | | return false;
|
| | | }
|
| | | /**
|
| | | * 主题名称是否已经存在
|
| | | *
|
| | | * @param topicName
|
| | | * @return
|
| | | */
|
| | | private boolean topicNameExist(String topicName) {
|
| | | List<String> resultList = new ArrayList<>();
|
| | | try {
|
| | | account.listTopic(topicName, resultList, 0, 100);
|
| | | } catch (Exception e) {
|
| | | e.printStackTrace();
|
| | | }
|
| | |
|
| | | /**
|
| | | * 发布订阅消息
|
| | | * |
| | | * @param topicName
|
| | | * @param message
|
| | | * @return
|
| | | */
|
| | | public boolean publishTopicMessage(String topicName, String message) {
|
| | | try {
|
| | | Topic topic = topicAccount.getTopic(topicName);
|
| | | if (topic == null)
|
| | | return false;
|
| | | topic.publishMessage(message);
|
| | | return true;
|
| | | } catch (Exception e) {
|
| | | e.printStackTrace();
|
| | | }
|
| | | return false;
|
| | | }
|
| | | //已经创建了
|
| | | if (resultList.contains(topicName)) {
|
| | | return true;
|
| | | }
|
| | | return false;
|
| | | }
|
| | |
|
| | | /**
|
| | | * 批量发布消息
|
| | | * |
| | | * @param topicName
|
| | | * @param msgList
|
| | | * @return
|
| | | */
|
| | | public boolean batchPublishTopicMessage(String topicName, List<String> msgList) {
|
| | | try {
|
| | | Topic topic = topicAccount.getTopic(topicName);
|
| | | if (topic == null)
|
| | | return false;
|
| | | topic.batchPublishMessage(msgList);
|
| | | return true;
|
| | | } catch (Exception e) {
|
| | | e.printStackTrace();
|
| | | }
|
| | | return false;
|
| | | }
|
| | | /**
|
| | | * 创建订阅主题
|
| | | *
|
| | | * @param topicName-主题名称
|
| | | * @param maxMsgSize-消息最大长度
|
| | | * @param filterType-过滤类型
|
| | | * @return
|
| | | */
|
| | | public boolean createTopic(String topicName, int maxMsgSize, int filterType) {
|
| | | if (topicNameExist(topicName)) {
|
| | | return true;
|
| | | }
|
| | | try {
|
| | | topicAccount.createTopic(topicName, maxMsgSize, filterType);
|
| | | return true;
|
| | | } catch (Exception e) {
|
| | | e.printStackTrace();
|
| | | }
|
| | | return false;
|
| | | }
|
| | |
|
| | | /**
|
| | | * 创建默认参数的主题
|
| | | *
|
| | | * @param topicName
|
| | | * @return
|
| | | */
|
| | | public boolean createTopic(String topicName) {
|
| | | if (topicNameExist(topicName)) {
|
| | | return true;
|
| | | }
|
| | | try {
|
| | | topicAccount.createTopic(topicName, 65536);
|
| | | return true;
|
| | | } catch (Exception e) {
|
| | | e.printStackTrace();
|
| | | }
|
| | | return false;
|
| | | }
|
| | |
|
| | |
|
| | | /**
|
| | | * 是否已经订阅
|
| | | *
|
| | | * @param topicName
|
| | | * @param subscriptionName
|
| | | * @return
|
| | | */
|
| | | private boolean isAlreadySubscribe(String topicName, String subscriptionName) throws Exception {
|
| | | Topic topic = topicAccount.getTopic(topicName);
|
| | | List<String> resultList = new ArrayList<>();
|
| | | topic.ListSubscription(0, 100, subscriptionName, resultList);
|
| | | if (resultList.contains(subscriptionName)) {
|
| | | return true;
|
| | | }
|
| | | return false;
|
| | | }
|
| | |
|
| | | /**
|
| | | * 订阅主题
|
| | | *
|
| | | * @param topicName-主题名称
|
| | | * @param subscriptionName-订阅名称
|
| | | * @param queueName-接受消息的队列名称
|
| | | * @return
|
| | | */
|
| | | public boolean subscribeTopic(String topicName, String subscriptionName, String queueName) {
|
| | |
|
| | | try {
|
| | | if (isAlreadySubscribe(topicName, subscriptionName)) {
|
| | | return true;
|
| | | }
|
| | | } catch (Exception e) {
|
| | | }
|
| | | try {
|
| | | topicAccount.createSubscribe(topicName, subscriptionName, queueName, "queue");
|
| | | } catch (Exception e) {
|
| | | e.printStackTrace();
|
| | | }
|
| | | return false;
|
| | | }
|
| | |
|
| | | public boolean subscribeTopic(String topicName, String subscriptionName, String queueName, List<String> filterTags) {
|
| | |
|
| | | try {
|
| | | if (isAlreadySubscribe(topicName, subscriptionName)) {
|
| | | return true;
|
| | | }
|
| | | } catch (Exception e) {
|
| | | }
|
| | | try {
|
| | | topicAccount.createSubscribe(topicName, subscriptionName, queueName, "queue", filterTags, null, "BACKOFF_RETRY", "JSON");
|
| | | } catch (Exception e) {
|
| | | e.printStackTrace();
|
| | | }
|
| | | return false;
|
| | | }
|
| | |
|
| | | /**
|
| | | * 删除订阅
|
| | | *
|
| | | * @param topicName
|
| | | * @param subscriptionName
|
| | | * @return
|
| | | */
|
| | | public boolean deleteSubscribeTopic(String topicName, String subscriptionName) {
|
| | | try {
|
| | | topicAccount.deleteSubscribe(topicName, subscriptionName);
|
| | | return true;
|
| | | } catch (Exception e) {
|
| | | e.printStackTrace();
|
| | | }
|
| | |
|
| | | return false;
|
| | | }
|
| | |
|
| | | /**
|
| | | * 发布订阅消息
|
| | | *
|
| | | * @param topicName
|
| | | * @param message
|
| | | * @return
|
| | | */
|
| | | public boolean publishTopicMessage(String topicName, String message) {
|
| | | try {
|
| | | Topic topic = topicAccount.getTopic(topicName);
|
| | | if (topic == null)
|
| | | return false;
|
| | | topic.publishMessage(message);
|
| | | return true;
|
| | | } catch (Exception e) {
|
| | | e.printStackTrace();
|
| | | }
|
| | | return false;
|
| | | }
|
| | |
|
| | | /**
|
| | | * 发布订阅消息
|
| | | *
|
| | | * @param topicName
|
| | | * @param tagList -标签
|
| | | * @param message
|
| | | * @return
|
| | | */
|
| | | public boolean publishTopicMessage(String topicName, List<String> tagList, String message) {
|
| | | try {
|
| | | Topic topic = topicAccount.getTopic(topicName);
|
| | | if (topic == null)
|
| | | return false;
|
| | | topic.publishMessage(message, tagList, null);
|
| | | return true;
|
| | | } catch (Exception e) {
|
| | | e.printStackTrace();
|
| | | }
|
| | | return false;
|
| | | }
|
| | |
|
| | |
|
| | | /**
|
| | | * 批量发布消息
|
| | | *
|
| | | * @param topicName
|
| | | * @param msgList
|
| | | * @return
|
| | | */
|
| | | public boolean batchPublishTopicMessage(String topicName, List<String> msgList) {
|
| | | try {
|
| | | Topic topic = topicAccount.getTopic(topicName);
|
| | | if (topic == null)
|
| | | return false;
|
| | | topic.batchPublishMessage(msgList);
|
| | | return true;
|
| | | } catch (Exception e) {
|
| | | e.printStackTrace();
|
| | | }
|
| | | return false;
|
| | | }
|
| | |
|
| | | }
|
| | |
| | | package org.yeshi.utils;
|
| | |
|
| | | import java.io.File;
|
| | | import java.io.FileInputStream;
|
| | | import java.io.FileOutputStream;
|
| | | import java.io.IOException;
|
| | | import java.io.InputStream;
|
| | | import java.io.OutputStream;
|
| | | import java.io.*;
|
| | |
|
| | | import javax.servlet.http.HttpServletRequest;
|
| | |
|
| | |
| | |
|
| | | public class FileUtil {
|
| | |
|
| | | @SuppressWarnings("deprecation")
|
| | | public static String getRealPath(HttpServletRequest request, String folderName) {
|
| | | String f = request.getRealPath(folderName);
|
| | | if (!new File(f).exists())
|
| | | new File(f).mkdirs();
|
| | | return f;
|
| | | }
|
| | | @SuppressWarnings("deprecation")
|
| | | public static String getRealPath(HttpServletRequest request, String folderName) {
|
| | | String f = request.getRealPath(folderName);
|
| | | if (!new File(f).exists())
|
| | | new File(f).mkdirs();
|
| | | return f;
|
| | | }
|
| | |
|
| | | public static String getFileMD5(File file) throws IOException {
|
| | | FileInputStream fis = new FileInputStream(file);
|
| | | String md5 = DigestUtils.md5Hex(IOUtils.toByteArray(fis));
|
| | | IOUtils.closeQuietly(fis);
|
| | | return md5;
|
| | | }
|
| | | public static String getFileMD5(File file) throws IOException {
|
| | | FileInputStream fis = new FileInputStream(file);
|
| | | String md5 = DigestUtils.md5Hex(IOUtils.toByteArray(fis));
|
| | | IOUtils.closeQuietly(fis);
|
| | | return md5;
|
| | | }
|
| | |
|
| | | public static void deleteFileDir(File path) {
|
| | | if (!path.exists())
|
| | | return;
|
| | | if (path.isFile()) {
|
| | | path.delete();
|
| | | return;
|
| | | }
|
| | | File[] files = path.listFiles();
|
| | | for (int i = 0; i < files.length; i++) {
|
| | | deleteFileDir(files[i]);
|
| | | }
|
| | | path.delete();
|
| | | }
|
| | | public static void deleteFileDir(File path) {
|
| | | if (!path.exists())
|
| | | return;
|
| | | if (path.isFile()) {
|
| | | path.delete();
|
| | | return;
|
| | | }
|
| | | File[] files = path.listFiles();
|
| | | for (int i = 0; i < files.length; i++) {
|
| | | deleteFileDir(files[i]);
|
| | | }
|
| | | path.delete();
|
| | | }
|
| | |
|
| | | /**
|
| | | * 将输入流存为本地文件
|
| | | * |
| | | * @param inputStream
|
| | | * @param path
|
| | | * @return
|
| | | * @throws IOException
|
| | | */
|
| | | public static String saveAsFile(InputStream inputStream, String path) throws IOException {
|
| | | if (!new File(path).exists()) {
|
| | | new File(path).createNewFile();
|
| | | }
|
| | | /**
|
| | | * 将输入流存为本地文件
|
| | | *
|
| | | * @param inputStream
|
| | | * @param path
|
| | | * @return
|
| | | * @throws IOException
|
| | | */
|
| | | public static String saveAsFile(InputStream inputStream, String path) throws IOException {
|
| | | if (!new File(path).exists()) {
|
| | | new File(path).createNewFile();
|
| | | }
|
| | |
|
| | | FileOutputStream fileOut = new FileOutputStream(new File(path));
|
| | | byte[] buf = new byte[1024 * 8];
|
| | | while (true) {
|
| | | int read = 0;
|
| | | if (inputStream != null) {
|
| | | read = inputStream.read(buf);
|
| | | }
|
| | | if (read == -1) {
|
| | | break;
|
| | | }
|
| | | fileOut.write(buf, 0, read);
|
| | | }
|
| | | fileOut.close();
|
| | | return path;
|
| | | }
|
| | | FileOutputStream fileOut = new FileOutputStream(new File(path));
|
| | | byte[] buf = new byte[1024 * 8];
|
| | | while (true) {
|
| | | int read = 0;
|
| | | if (inputStream != null) {
|
| | | read = inputStream.read(buf);
|
| | | }
|
| | | if (read == -1) {
|
| | | break;
|
| | | }
|
| | | fileOut.write(buf, 0, read);
|
| | | }
|
| | | fileOut.close();
|
| | | return path;
|
| | | }
|
| | |
|
| | | /**
|
| | | * 获取缓存文件夹目录
|
| | | * |
| | | * @return
|
| | | */
|
| | | public static String getCacheDir() {
|
| | | String os = System.getProperty("os.name");
|
| | | if (os.toLowerCase().startsWith("win")) {
|
| | | File f = new File("D:/cache");
|
| | | if (!f.exists())
|
| | | f.mkdirs();
|
| | | return f.getPath();
|
| | | } else {
|
| | | File f = new File("/usr/local/cache");
|
| | | if (!f.exists())
|
| | | f.mkdirs();
|
| | | return f.getPath();
|
| | | }
|
| | | }
|
| | | /**
|
| | | * 获取缓存文件夹目录
|
| | | *
|
| | | * @return
|
| | | */
|
| | | public static String getCacheDir() {
|
| | | String os = System.getProperty("os.name");
|
| | | if (os.toLowerCase().startsWith("win")) {
|
| | | File f = new File("D:/cache");
|
| | | if (!f.exists())
|
| | | f.mkdirs();
|
| | | return f.getPath();
|
| | | } else {
|
| | | File f = new File("/usr/local/cache");
|
| | | if (!f.exists())
|
| | | f.mkdirs();
|
| | | return f.getPath();
|
| | | }
|
| | | }
|
| | |
|
| | | |
| | | /**
|
| | | * 将输入流存为本地文件
|
| | | * |
| | | * @param inputStream
|
| | | * @param path
|
| | | * @return
|
| | | * @throws IOException
|
| | | */
|
| | | public static String saveAsFileByte(byte[] b, String path) throws IOException {
|
| | | if (!new File(path).exists()) {
|
| | | new File(path).createNewFile();
|
| | | }
|
| | |
|
| | | FileOutputStream fileOut = new FileOutputStream(path); |
| | | /**
|
| | | * 将输入流存为本地文件
|
| | | *
|
| | | * @param inputStream
|
| | | * @param path
|
| | | * @return
|
| | | * @throws IOException
|
| | | */
|
| | | public static String saveAsFileByte(byte[] b, String path) throws IOException {
|
| | | if (!new File(path).exists()) {
|
| | | new File(path).createNewFile();
|
| | | }
|
| | |
|
| | | FileOutputStream fileOut = new FileOutputStream(path);
|
| | | fileOut.write(b);
|
| | | fileOut.flush();
|
| | | fileOut.close(); |
| | | return path;
|
| | | }
|
| | | fileOut.close();
|
| | | return path;
|
| | | }
|
| | |
|
| | |
|
| | | /**
|
| | | * 复制inputStream
|
| | | * @param input
|
| | | * @return
|
| | | */
|
| | | public static ByteArrayOutputStream cloneInputStream(InputStream input) {
|
| | | try {
|
| | | ByteArrayOutputStream baos = new ByteArrayOutputStream();
|
| | | byte[] buffer = new byte[1024];
|
| | | int len;
|
| | | while ((len = input.read(buffer)) > -1) {
|
| | | baos.write(buffer, 0, len);
|
| | | }
|
| | | baos.flush();
|
| | | return baos;
|
| | | } catch (IOException e) {
|
| | | e.printStackTrace();
|
| | | return null;
|
| | | }
|
| | | }
|
| | | }
|
| | |
| | | import com.alipay.api.response.AlipayFundTransCommonQueryResponse; |
| | | import com.alipay.api.response.AlipayFundTransUniTransferResponse; |
| | | import net.sf.json.JSONObject; |
| | | import org.apache.commons.beanutils.BeanUtils; |
| | | import org.apache.commons.io.IOUtils; |
| | | import org.yeshi.utils.StringUtil; |
| | | import org.yeshi.utils.entity.alipay.AlipayAppInfo; |
| | | import org.yeshi.utils.entity.alipay.AlipayTransferInfo; |
| | | import org.yeshi.utils.exception.AlipayCommonException; |
| | | import org.yeshi.utils.exception.AlipayTransferException; |
| | | |
| | | import java.lang.reflect.InvocationTargetException; |
| | | import java.math.BigDecimal; |
| | | |
| | | public class AlipayUtil { |
| | |
| | | app.getCertInfo().getAlipayPublicCertStream()); |
| | | certAlipayRequest.setRootCertStream( |
| | | app.getCertInfo().getAlipayRootCertStream()); |
| | | |
| | | return new CertAlipayClient(certAlipayRequest); |
| | | } |
| | | } |
| | |
| | | import com.alipay.api.AlipayClient; |
| | | import com.alipay.api.request.AlipayOpenAppMiniTemplatemessageSendRequest; |
| | | import com.alipay.api.request.AlipaySystemOauthTokenRequest; |
| | | import com.alipay.api.request.AlipayUserInfoShareRequest; |
| | | import com.alipay.api.response.AlipayOpenAppMiniTemplatemessageSendResponse; |
| | | import com.alipay.api.response.AlipaySystemOauthTokenResponse; |
| | | import com.alipay.api.response.AlipayUserInfoShareResponse; |
| | | import org.yeshi.utils.FileUtil; |
| | | import org.yeshi.utils.entity.alipay.AlipayAppInfo; |
| | | import org.yeshi.utils.entity.alipay.AlipayUserInfo; |
| | | import org.yeshi.utils.exception.AlipayXcxException; |
| | | |
| | | import java.io.ByteArrayInputStream; |
| | | import java.io.ByteArrayOutputStream; |
| | | |
| | | /** |
| | | * 支付宝小程序帮助类 |
| | |
| | | * @throws AlipayApiException |
| | | */ |
| | | public static String getUserId(AlipayAppInfo app, String code) throws AlipayApiException, AlipayXcxException { |
| | | AlipaySystemOauthTokenResponse response = getOauthTokenResponse(app, code); |
| | | return response.getUserId(); |
| | | } |
| | | |
| | | /** |
| | | * 获取用户token |
| | | * |
| | | * @param app |
| | | * @param code |
| | | * @return |
| | | * @throws AlipayApiException |
| | | * @throws AlipayXcxException |
| | | */ |
| | | private static String getAccessToken(AlipayAppInfo app, String code) throws AlipayApiException, AlipayXcxException { |
| | | AlipaySystemOauthTokenResponse response = getOauthTokenResponse(app, code); |
| | | return response.getAccessToken(); |
| | | } |
| | | |
| | | private static AlipaySystemOauthTokenResponse getOauthTokenResponse(AlipayAppInfo app, String code) throws AlipayApiException, AlipayXcxException { |
| | | AlipayClient alipayClient = AlipayUtil.getAlipayClient(app); |
| | | AlipaySystemOauthTokenRequest request = new AlipaySystemOauthTokenRequest(); |
| | | request.setGrantType("authorization_code"); |
| | |
| | | if (!response.isSuccess()) { |
| | | throw new AlipayXcxException(response.getCode(), response.getSubMsg(), response.getBody()); |
| | | } |
| | | return response.getUserId(); |
| | | return response; |
| | | } |
| | | |
| | | |
| | |
| | | } |
| | | } |
| | | |
| | | |
| | | /** |
| | | * 获取支付宝用户信息 |
| | | * |
| | | * @param app |
| | | * @param code |
| | | * @return |
| | | * @throws AlipayApiException |
| | | * @throws AlipayXcxException |
| | | */ |
| | | public static AlipayUserInfo getAlipayUserInfo(AlipayAppInfo app, String code) throws AlipayApiException, AlipayXcxException { |
| | | |
| | | ByteArrayOutputStream publicCert = FileUtil.cloneInputStream(app.getCertInfo().getAlipayPublicCertStream()); |
| | | ByteArrayOutputStream alipayRoot = FileUtil.cloneInputStream(app.getCertInfo().getAlipayRootCertStream()); |
| | | ByteArrayOutputStream appCert = FileUtil.cloneInputStream(app.getCertInfo().getAppCertPublicKeyStream()); |
| | | |
| | | //重置流 |
| | | app.getCertInfo().setAlipayPublicCertStream(new ByteArrayInputStream(publicCert.toByteArray())); |
| | | app.getCertInfo().setAlipayRootCertStream(new ByteArrayInputStream(alipayRoot.toByteArray())); |
| | | app.getCertInfo().setAppCertPublicKeyStream(new ByteArrayInputStream(appCert.toByteArray())); |
| | | |
| | | |
| | | String accessToken = getAccessToken(app, code); |
| | | |
| | | //重置流 |
| | | app.getCertInfo().setAlipayPublicCertStream(new ByteArrayInputStream(publicCert.toByteArray())); |
| | | app.getCertInfo().setAlipayRootCertStream(new ByteArrayInputStream(alipayRoot.toByteArray())); |
| | | app.getCertInfo().setAppCertPublicKeyStream(new ByteArrayInputStream(appCert.toByteArray())); |
| | | |
| | | |
| | | AlipayClient alipayClient = AlipayUtil.getAlipayClient(app); |
| | | AlipayUserInfoShareRequest request = new AlipayUserInfoShareRequest(); |
| | | |
| | | AlipayUserInfoShareResponse response = app.getCertInfo() != null ? alipayClient.certificateExecute(request, accessToken) : alipayClient.execute(request, accessToken); |
| | | |
| | | if (!response.isSuccess()) { |
| | | throw new AlipayXcxException(response.getCode(), response.getSubMsg(), response.getBody()); |
| | | } |
| | | AlipayUserInfo alipayUserInfo = new AlipayUserInfo(); |
| | | alipayUserInfo.setAvatar(response.getAvatar()); |
| | | alipayUserInfo.setCity(response.getCity()); |
| | | alipayUserInfo.setGender(response.getGender()); |
| | | alipayUserInfo.setNickName(response.getUserName()); |
| | | alipayUserInfo.setProvince(response.getProvince()); |
| | | alipayUserInfo.setUserId(response.getUserId()); |
| | | return alipayUserInfo; |
| | | } |
| | | |
| | | } |
File was renamed from utils/src/main/java/org/yeshi/utils/mybatis/Column.java |
| | |
| | | package org.yeshi.utils.mybatis;
|
| | |
|
| | | import java.lang.annotation.Documented;
|
| | | import java.lang.annotation.ElementType;
|
| | | import java.lang.annotation.Inherited;
|
| | | import java.lang.annotation.Retention;
|
| | | import java.lang.annotation.RetentionPolicy;
|
| | | import java.lang.annotation.Target;
|
| | |
|
| | | @Documented
|
| | | @Target(ElementType.FIELD)
|
| | | @Inherited
|
| | | @Retention(RetentionPolicy.RUNTIME)
|
| | | public @interface Column {
|
| | | String name();
|
| | |
|
| | | int length() default 0;
|
| | | }
|
| | | package org.yeshi.utils.generater.mybatis; |
| | | |
| | | import java.lang.annotation.Documented; |
| | | import java.lang.annotation.ElementType; |
| | | import java.lang.annotation.Inherited; |
| | | import java.lang.annotation.Retention; |
| | | import java.lang.annotation.RetentionPolicy; |
| | | import java.lang.annotation.Target; |
| | | |
| | | @Documented |
| | | @Target(ElementType.FIELD) |
| | | @Inherited |
| | | @Retention(RetentionPolicy.RUNTIME) |
| | | public @interface Column { |
| | | String name(); |
| | | |
| | | int length() default 0; |
| | | } |
File was renamed from utils/src/main/java/org/yeshi/utils/mybatis/ColumnParseUtil.java |
| | |
| | | package org.yeshi.utils.mybatis;
|
| | |
|
| | | import org.springframework.data.annotation.Transient;
|
| | |
|
| | | import java.lang.annotation.Annotation;
|
| | | import java.lang.reflect.Field;
|
| | |
|
| | | public class ColumnParseUtil {
|
| | |
|
| | | /**
|
| | | * 将属性转为数据库字段
|
| | | *
|
| | | * @param fieldName
|
| | | * @return
|
| | | */
|
| | | private static String convertFieldToColumn(String fieldName) {
|
| | | String column = "";
|
| | | for (int i = 0; i < fieldName.length(); i++) {
|
| | | //大写字母
|
| | | if (fieldName.charAt(i) >= 65 && fieldName.charAt(i) <= 90) {
|
| | | //前面一个为非大写
|
| | | if (i > 0 && (fieldName.charAt(i - 1) < 65 || fieldName.charAt(i - 1) > 90)) {
|
| | | column += "_" + fieldName.charAt(i);
|
| | | } else {
|
| | | column += fieldName.charAt(i);
|
| | | }
|
| | | } else {
|
| | | column += fieldName.charAt(i);
|
| | | }
|
| | | }
|
| | | return column.toLowerCase();
|
| | | }
|
| | |
|
| | | public static void parseColumn(Class<?> clz, String path) {
|
| | | Field[] fields = clz.getDeclaredFields();
|
| | | for (Field fd : fields) {
|
| | |
|
| | | //private/public 非final,static属性才会参与解析
|
| | | if (fd.getModifiers() != 1 && fd.getModifiers() != 2) {
|
| | | continue;
|
| | | }
|
| | |
|
| | |
|
| | | String columName = convertFieldToColumn(fd.getName());
|
| | | Annotation[] as = fd.getAnnotations();
|
| | | for (Annotation a : as) {
|
| | | //不参与解析
|
| | | if (a instanceof Column) {
|
| | | Column c = (Column) a;
|
| | | columName = c.name();
|
| | |
|
| | | }
|
| | | }
|
| | |
|
| | | for (Annotation a : as) {
|
| | | //不参与解析
|
| | | if (a instanceof Transient || a instanceof java.beans.Transient) {
|
| | | columName = null;
|
| | | break;
|
| | | }
|
| | | }
|
| | |
|
| | | if (columName != null) {
|
| | | ColumnUtil.addColumnToMapper(path, columName, fd.getName(), getJDBCType(fd.getType().getName()));
|
| | | }
|
| | |
|
| | | }
|
| | | }
|
| | |
|
| | | public static String getJDBCType(String type) {
|
| | | if (type.endsWith("java.lang.Long")) {
|
| | | return "BIGINT";
|
| | | } else if (type.endsWith("java.lang.Integer")) {
|
| | | return "INTEGER";
|
| | | } else if (type.endsWith("java.lang.String")) {
|
| | | return "VARCHAR";
|
| | | } else if (type.endsWith("java.lang.Boolean")) {
|
| | | return "BOOLEAN";
|
| | | } else if (type.endsWith("java.math.BigDecimal")) {
|
| | | return "DECIMAL";
|
| | | } else if (type.endsWith("java.util.Date")) {
|
| | | return "TIMESTAMP";
|
| | | } else if (type.endsWith("java.lang.Double")) {
|
| | | return "DOUBLE";
|
| | | } else {
|
| | | return "VARCHAR";
|
| | | }
|
| | | }
|
| | |
|
| | | }
|
| | | package org.yeshi.utils.generater.mybatis; |
| | | |
| | | import org.springframework.data.annotation.Transient; |
| | | |
| | | import java.lang.annotation.Annotation; |
| | | import java.lang.reflect.Field; |
| | | |
| | | public class ColumnParseUtil { |
| | | |
| | | /** |
| | | * 将属性转为数据库字段 |
| | | * |
| | | * @param fieldName |
| | | * @return |
| | | */ |
| | | private static String convertFieldToColumn(String fieldName) { |
| | | String column = ""; |
| | | for (int i = 0; i < fieldName.length(); i++) { |
| | | //大写字母 |
| | | if (fieldName.charAt(i) >= 65 && fieldName.charAt(i) <= 90) { |
| | | //前面一个为非大写 |
| | | if (i > 0 && (fieldName.charAt(i - 1) < 65 || fieldName.charAt(i - 1) > 90)) { |
| | | column += "_" + fieldName.charAt(i); |
| | | } else { |
| | | column += fieldName.charAt(i); |
| | | } |
| | | } else { |
| | | column += fieldName.charAt(i); |
| | | } |
| | | } |
| | | return column.toLowerCase(); |
| | | } |
| | | |
| | | public static void parseColumn(Class<?> clz, String path) { |
| | | Field[] fields = clz.getDeclaredFields(); |
| | | for (Field fd : fields) { |
| | | |
| | | //private/public 非final,static属性才会参与解析 |
| | | if (fd.getModifiers() != 1 && fd.getModifiers() != 2) { |
| | | continue; |
| | | } |
| | | |
| | | |
| | | String columName = convertFieldToColumn(fd.getName()); |
| | | Annotation[] as = fd.getAnnotations(); |
| | | for (Annotation a : as) { |
| | | //不参与解析 |
| | | if (a instanceof Column) { |
| | | Column c = (Column) a; |
| | | columName = c.name(); |
| | | |
| | | } |
| | | } |
| | | |
| | | for (Annotation a : as) { |
| | | //不参与解析 |
| | | if (a instanceof Transient || a instanceof java.beans.Transient) { |
| | | columName = null; |
| | | break; |
| | | } |
| | | } |
| | | |
| | | if (columName != null) { |
| | | ColumnUtil.addColumnToMapper(path, columName, fd.getName(), getJDBCType(fd.getType().getName())); |
| | | } |
| | | |
| | | } |
| | | } |
| | | |
| | | public static String getJDBCType(String type) { |
| | | if (type.endsWith("java.lang.Long")) { |
| | | return "BIGINT"; |
| | | } else if (type.endsWith("java.lang.Integer")) { |
| | | return "INTEGER"; |
| | | } else if (type.endsWith("java.lang.String")) { |
| | | return "VARCHAR"; |
| | | } else if (type.endsWith("java.lang.Boolean")) { |
| | | return "BOOLEAN"; |
| | | } else if (type.endsWith("java.math.BigDecimal")) { |
| | | return "DECIMAL"; |
| | | } else if (type.endsWith("java.util.Date")) { |
| | | return "TIMESTAMP"; |
| | | } else if (type.endsWith("java.lang.Double")) { |
| | | return "DOUBLE"; |
| | | } else { |
| | | return "VARCHAR"; |
| | | } |
| | | } |
| | | |
| | | } |
File was renamed from utils/src/main/java/org/yeshi/utils/mybatis/ColumnUtil.java |
| | |
| | | package org.yeshi.utils.mybatis;
|
| | |
|
| | | import java.io.File;
|
| | | import java.io.FileOutputStream;
|
| | | import java.util.ArrayList;
|
| | | import java.util.List;
|
| | |
|
| | | import org.dom4j.Document;
|
| | | import org.dom4j.DocumentException;
|
| | | import org.dom4j.DocumentHelper;
|
| | | import org.dom4j.Element;
|
| | | import org.dom4j.io.OutputFormat;
|
| | | import org.dom4j.io.SAXReader;
|
| | | import org.dom4j.io.XMLWriter;
|
| | | import org.dom4j.tree.AbstractAttribute;
|
| | |
|
| | | public class ColumnUtil {
|
| | |
|
| | | @SuppressWarnings("rawtypes")
|
| | | public static void addColumnToMapper(String path, String columnName, String property, String type) {
|
| | | File xmlPath = new File(path);
|
| | | SAXReader reader = new SAXReader();
|
| | | Document document = null;
|
| | | try {
|
| | | document = reader.read(xmlPath);
|
| | | Element root = document.getRootElement();
|
| | | // 得到根元素的所有子节点
|
| | | List<Element> elementList = root.elements();
|
| | | // 遍历所有子节点
|
| | | for (Element e : elementList) {
|
| | | if (e.getName().equals("resultMap")) {
|
| | | AbstractAttribute attr = (AbstractAttribute) e.attribute("id");
|
| | | if ("BaseResultMap".equalsIgnoreCase(attr.getValue())) {
|
| | | List list = e.elements("result");
|
| | | boolean isE = false;
|
| | | for (int i = 0; i < list.size(); i++) {
|
| | | Element item = (Element) list.get(i);
|
| | | attr = (AbstractAttribute) item.attribute("property");
|
| | | if (attr.getValue().trim().equalsIgnoreCase(property)) {
|
| | | isE = true;
|
| | | break;
|
| | | }
|
| | | }
|
| | | if (!isE) {
|
| | | List<KeyPair> li = new ArrayList<>();
|
| | | li.add(new KeyPair("column", columnName));
|
| | | li.add(new KeyPair("property", property));
|
| | | li.add(new KeyPair("jdbcType", type));
|
| | | e.add(createElement("result", li, null));
|
| | | }
|
| | | }
|
| | | } else if (e.getName().equals("sql")) {
|
| | | AbstractAttribute attr = (AbstractAttribute) e.attribute("id");
|
| | | if ("Base_Column_List".equalsIgnoreCase(attr.getValue())) {
|
| | | String columns = e.getText();
|
| | | String[] cs = columns.split(",");
|
| | | boolean isE = false;
|
| | | for (String c : cs) {
|
| | | if (c.trim().equalsIgnoreCase(columnName)) {
|
| | | isE = true;
|
| | | break;
|
| | | }
|
| | | }
|
| | | if (!isE) {
|
| | | columns += ("," + columnName);
|
| | | e.setText(columns);
|
| | | }
|
| | | }
|
| | |
|
| | | } else if (e.getName().equals("insert")) {
|
| | | AbstractAttribute attr = (AbstractAttribute) e.attribute("id");
|
| | | if ("insert".equalsIgnoreCase(attr.getValue())) {
|
| | | String insert = e.getText();
|
| | | String[] cs = insert.split("values ");
|
| | | if (cs.length > 1) {
|
| | | int start = cs[0].indexOf("(");
|
| | | int end = cs[0].indexOf(")");
|
| | | String columns = cs[0].substring(start, end);
|
| | | String[] cols = columns.split(",");
|
| | | boolean isE = false;
|
| | | for (String col : cols) {
|
| | | if (col.trim().equalsIgnoreCase(columnName)) {
|
| | | isE = true;
|
| | | break;
|
| | | }
|
| | | }
|
| | | if (!isE) {
|
| | | cs[0] = cs[0].replace(columns, columns + ("," + columnName));
|
| | | start = cs[1].indexOf("(");
|
| | | end = cs[1].indexOf(")");
|
| | | String columns2 = cs[1].substring(start, end);
|
| | | cs[1] = cs[1].replace(columns2,
|
| | | columns2 + String.format(",#{%s,jdbcType=%s}", property, type));
|
| | | insert = cs[0] + " values " + cs[1];
|
| | | e.setText(insert);
|
| | | }
|
| | | }
|
| | |
|
| | | } else if ("insertSelective".equalsIgnoreCase(attr.getValue())) {
|
| | | List trims = e.elements("trim");
|
| | | Element els = (Element) trims.get(0);
|
| | | boolean isE = false;
|
| | | for (int i = 0; i < els.elements("if").size(); i++) {
|
| | | String cn = ((Element) els.elements("if").get(i)).getText().replace(",", "").trim();
|
| | | if (columnName.equalsIgnoreCase(cn)) {
|
| | | isE = true;
|
| | | break;
|
| | | }
|
| | | }
|
| | | if (!isE) {
|
| | | List<KeyPair> list = new ArrayList<>();
|
| | | list.add(new KeyPair("test", String.format("%s != null", property)));
|
| | | els.add(createElement("if", list, columnName + ","));
|
| | |
|
| | | els = (Element) trims.get(1);
|
| | | list = new ArrayList<>();
|
| | | list.add(new KeyPair("test", String.format("%s != null", property)));
|
| | | els.add(createElement("if", list, String.format("#{%s,jdbcType=%s}", property, type)));
|
| | | }
|
| | | }
|
| | |
|
| | | } else if (e.getName().equals("update")) {
|
| | | AbstractAttribute attr = (AbstractAttribute) e.attribute("id");
|
| | | if ("updateByPrimaryKeySelective".equalsIgnoreCase(attr.getValue())) {
|
| | | List list = ((Element) e.elements("set").get(0)).elements("if");
|
| | | boolean isE = false;
|
| | | for (int i = 0; i < list.size(); i++) {
|
| | | if (((Element) list.get(i)).getText().indexOf(columnName) > -1) {
|
| | | isE = true;
|
| | | break;
|
| | | }
|
| | | }
|
| | | if (!isE) {
|
| | | List<KeyPair> klist = new ArrayList<>();
|
| | | klist.add(new KeyPair("test", String.format("%s !=null", property)));
|
| | | ((Element) e.elements("set").get(0)).add(createElement("if", klist,
|
| | | String.format("%s =#{%s,jdbcType=%s},", columnName, property, type)));
|
| | | }
|
| | | } else if ("updateByPrimaryKey".equalsIgnoreCase(attr.getValue())) {
|
| | | String value = e.getText();
|
| | | // 不存在该列
|
| | | if (value.indexOf(columnName) <= -1) {
|
| | | int wpos = value.indexOf("where ");
|
| | | if (value.substring(0, wpos).trim().endsWith(","))
|
| | | value = value.substring(0, wpos)
|
| | | + String.format("%s =#{%s,jdbcType=%s}", columnName, property, type) + " "
|
| | | + value.substring(wpos);
|
| | | else
|
| | | value = value.substring(0, wpos) + ","
|
| | | + String.format("%s =#{%s,jdbcType=%s}", columnName, property, type) + " "
|
| | | + value.substring(wpos);
|
| | |
|
| | | e.setText(value);
|
| | | }
|
| | | }
|
| | |
|
| | | }
|
| | | }
|
| | | } catch (DocumentException e1) {
|
| | | e1.printStackTrace();
|
| | | }
|
| | | if (document != null) {
|
| | | OutputFormat format = OutputFormat.createPrettyPrint();
|
| | | format.setEncoding("UTF-8");
|
| | | try {
|
| | | XMLWriter writer = new XMLWriter(new FileOutputStream(path), format);
|
| | | writer.write(document);
|
| | | } catch (Exception e) {
|
| | | e.printStackTrace();
|
| | | }
|
| | | }
|
| | | }
|
| | |
|
| | | private static Element createElement(String qName, List<KeyPair> keyPairList, String value) {
|
| | | Element element = DocumentHelper.createElement(qName);
|
| | | for (KeyPair kp : keyPairList) {
|
| | | element.addAttribute(kp.key, kp.value);
|
| | | }
|
| | | if (value != null)
|
| | | element.setText(value);
|
| | | return element;
|
| | | }
|
| | |
|
| | | }
|
| | |
|
| | | class KeyPair {
|
| | | String key;
|
| | | String value;
|
| | |
|
| | | public KeyPair(String key, String value) {
|
| | | this.key = key;
|
| | | this.value = value;
|
| | | }
|
| | |
|
| | | public KeyPair() {
|
| | |
|
| | | }
|
| | |
|
| | | }
|
| | | package org.yeshi.utils.generater.mybatis; |
| | | |
| | | import java.io.File; |
| | | import java.io.FileOutputStream; |
| | | import java.util.ArrayList; |
| | | import java.util.List; |
| | | |
| | | import org.dom4j.Document; |
| | | import org.dom4j.DocumentException; |
| | | import org.dom4j.DocumentHelper; |
| | | import org.dom4j.Element; |
| | | import org.dom4j.io.OutputFormat; |
| | | import org.dom4j.io.SAXReader; |
| | | import org.dom4j.io.XMLWriter; |
| | | import org.dom4j.tree.AbstractAttribute; |
| | | |
| | | public class ColumnUtil { |
| | | |
| | | @SuppressWarnings("rawtypes") |
| | | public static void addColumnToMapper(String path, String columnName, String property, String type) { |
| | | File xmlPath = new File(path); |
| | | SAXReader reader = new SAXReader(); |
| | | Document document = null; |
| | | try { |
| | | document = reader.read(xmlPath); |
| | | Element root = document.getRootElement(); |
| | | // 得到根元素的所有子节点 |
| | | List<Element> elementList = root.elements(); |
| | | // 遍历所有子节点 |
| | | for (Element e : elementList) { |
| | | if (e.getName().equals("resultMap")) { |
| | | AbstractAttribute attr = (AbstractAttribute) e.attribute("id"); |
| | | if ("BaseResultMap".equalsIgnoreCase(attr.getValue())) { |
| | | List list = e.elements("result"); |
| | | boolean isE = false; |
| | | for (int i = 0; i < list.size(); i++) { |
| | | Element item = (Element) list.get(i); |
| | | attr = (AbstractAttribute) item.attribute("property"); |
| | | if (attr.getValue().trim().equalsIgnoreCase(property)) { |
| | | isE = true; |
| | | break; |
| | | } |
| | | } |
| | | if (!isE) { |
| | | List<KeyPair> li = new ArrayList<>(); |
| | | li.add(new KeyPair("column", columnName)); |
| | | li.add(new KeyPair("property", property)); |
| | | li.add(new KeyPair("jdbcType", type)); |
| | | e.add(createElement("result", li, null)); |
| | | } |
| | | } |
| | | } else if (e.getName().equals("sql")) { |
| | | AbstractAttribute attr = (AbstractAttribute) e.attribute("id"); |
| | | if ("Base_Column_List".equalsIgnoreCase(attr.getValue())) { |
| | | String columns = e.getText(); |
| | | String[] cs = columns.split(","); |
| | | boolean isE = false; |
| | | for (String c : cs) { |
| | | if (c.trim().equalsIgnoreCase(columnName)) { |
| | | isE = true; |
| | | break; |
| | | } |
| | | } |
| | | if (!isE) { |
| | | columns += ("," + columnName); |
| | | e.setText(columns); |
| | | } |
| | | } |
| | | |
| | | } else if (e.getName().equals("insert")) { |
| | | AbstractAttribute attr = (AbstractAttribute) e.attribute("id"); |
| | | if ("insert".equalsIgnoreCase(attr.getValue())) { |
| | | String insert = e.getText(); |
| | | String[] cs = insert.split("values "); |
| | | if (cs.length > 1) { |
| | | int start = cs[0].indexOf("("); |
| | | int end = cs[0].indexOf(")"); |
| | | String columns = cs[0].substring(start, end); |
| | | String[] cols = columns.split(","); |
| | | boolean isE = false; |
| | | for (String col : cols) { |
| | | if (col.trim().equalsIgnoreCase(columnName)) { |
| | | isE = true; |
| | | break; |
| | | } |
| | | } |
| | | if (!isE) { |
| | | cs[0] = cs[0].replace(columns, columns + ("," + columnName)); |
| | | start = cs[1].indexOf("("); |
| | | end = cs[1].indexOf(")"); |
| | | String columns2 = cs[1].substring(start, end); |
| | | cs[1] = cs[1].replace(columns2, |
| | | columns2 + String.format(",#{%s,jdbcType=%s}", property, type)); |
| | | insert = cs[0] + " values " + cs[1]; |
| | | e.setText(insert); |
| | | } |
| | | } |
| | | |
| | | } else if ("insertSelective".equalsIgnoreCase(attr.getValue())) { |
| | | List trims = e.elements("trim"); |
| | | Element els = (Element) trims.get(0); |
| | | boolean isE = false; |
| | | for (int i = 0; i < els.elements("if").size(); i++) { |
| | | String cn = ((Element) els.elements("if").get(i)).getText().replace(",", "").trim(); |
| | | if (columnName.equalsIgnoreCase(cn)) { |
| | | isE = true; |
| | | break; |
| | | } |
| | | } |
| | | if (!isE) { |
| | | List<KeyPair> list = new ArrayList<>(); |
| | | list.add(new KeyPair("test", String.format("%s != null", property))); |
| | | els.add(createElement("if", list, columnName + ",")); |
| | | |
| | | els = (Element) trims.get(1); |
| | | list = new ArrayList<>(); |
| | | list.add(new KeyPair("test", String.format("%s != null", property))); |
| | | els.add(createElement("if", list, String.format("#{%s,jdbcType=%s}", property, type))); |
| | | } |
| | | } |
| | | |
| | | } else if (e.getName().equals("update")) { |
| | | AbstractAttribute attr = (AbstractAttribute) e.attribute("id"); |
| | | if ("updateByPrimaryKeySelective".equalsIgnoreCase(attr.getValue())) { |
| | | List list = ((Element) e.elements("set").get(0)).elements("if"); |
| | | boolean isE = false; |
| | | for (int i = 0; i < list.size(); i++) { |
| | | if (((Element) list.get(i)).getText().indexOf(columnName) > -1) { |
| | | isE = true; |
| | | break; |
| | | } |
| | | } |
| | | if (!isE) { |
| | | List<KeyPair> klist = new ArrayList<>(); |
| | | klist.add(new KeyPair("test", String.format("%s !=null", property))); |
| | | ((Element) e.elements("set").get(0)).add(createElement("if", klist, |
| | | String.format("%s =#{%s,jdbcType=%s},", columnName, property, type))); |
| | | } |
| | | } else if ("updateByPrimaryKey".equalsIgnoreCase(attr.getValue())) { |
| | | String value = e.getText(); |
| | | // 不存在该列 |
| | | if (value.indexOf(columnName) <= -1) { |
| | | int wpos = value.indexOf("where "); |
| | | if (value.substring(0, wpos).trim().endsWith(",")) |
| | | value = value.substring(0, wpos) |
| | | + String.format("%s =#{%s,jdbcType=%s}", columnName, property, type) + " " |
| | | + value.substring(wpos); |
| | | else |
| | | value = value.substring(0, wpos) + "," |
| | | + String.format("%s =#{%s,jdbcType=%s}", columnName, property, type) + " " |
| | | + value.substring(wpos); |
| | | |
| | | e.setText(value); |
| | | } |
| | | } |
| | | |
| | | } |
| | | } |
| | | } catch (DocumentException e1) { |
| | | e1.printStackTrace(); |
| | | } |
| | | if (document != null) { |
| | | OutputFormat format = OutputFormat.createPrettyPrint(); |
| | | format.setEncoding("UTF-8"); |
| | | try { |
| | | XMLWriter writer = new XMLWriter(new FileOutputStream(path), format); |
| | | writer.write(document); |
| | | } catch (Exception e) { |
| | | e.printStackTrace(); |
| | | } |
| | | } |
| | | } |
| | | |
| | | private static Element createElement(String qName, List<KeyPair> keyPairList, String value) { |
| | | Element element = DocumentHelper.createElement(qName); |
| | | for (KeyPair kp : keyPairList) { |
| | | element.addAttribute(kp.key, kp.value); |
| | | } |
| | | if (value != null) |
| | | element.setText(value); |
| | | return element; |
| | | } |
| | | |
| | | } |
| | | |
| | | class KeyPair { |
| | | String key; |
| | | String value; |
| | | |
| | | public KeyPair(String key, String value) { |
| | | this.key = key; |
| | | this.value = value; |
| | | } |
| | | |
| | | public KeyPair() { |
| | | |
| | | } |
| | | |
| | | } |
File was renamed from utils/src/main/java/org/yeshi/utils/mybatis/MyBatisMapperUtil.java |
| | |
| | | package org.yeshi.utils.mybatis;
|
| | |
|
| | | import java.io.File;
|
| | | import java.io.FileOutputStream;
|
| | | import java.io.PrintWriter;
|
| | | import java.lang.annotation.Annotation;
|
| | | import java.lang.reflect.Field;
|
| | | import java.util.ArrayList;
|
| | | import java.util.List;
|
| | |
|
| | | import org.dom4j.DocumentHelper;
|
| | | import org.dom4j.io.OutputFormat;
|
| | | import org.dom4j.io.XMLWriter;
|
| | |
|
| | | public class MyBatisMapperUtil {
|
| | | private static String basePath = "D:/mybatis";
|
| | |
|
| | | static {
|
| | | // 创建工作目录
|
| | | if (!new File(basePath).exists())
|
| | | new File(basePath).mkdirs();
|
| | | }
|
| | |
|
| | | // 获取dao所在的包名
|
| | | private static String getDaoPackageName(Class<?> bean) {
|
| | | String name = bean.getName();
|
| | | String[] ns = name.split("\\.");
|
| | | if (ns.length > 2) {
|
| | | String pks = "";
|
| | | for (int i = 0; i < ns.length - 2; i++) {
|
| | | pks += ns[i] + ".";
|
| | | }
|
| | | return pks + "dao";
|
| | | }
|
| | | return "";
|
| | | }
|
| | |
|
| | | private static String getMapperPackageName(Class<?> bean) {
|
| | | String name = bean.getName();
|
| | | String[] ns = name.split("\\.");
|
| | | if (ns.length > 2) {
|
| | | String pks = "";
|
| | | for (int i = 0; i < ns.length - 2; i++) {
|
| | | pks += ns[i] + ".";
|
| | | }
|
| | | return pks + "mapper";
|
| | | }
|
| | | return "";
|
| | | }
|
| | |
|
| | | public static void createMapper(Class<?> clz) {
|
| | | // 生成mapper java文件
|
| | | String pks = getDaoPackageName(clz);
|
| | | StringBuffer buffer = new StringBuffer("package " + pks + ";");
|
| | | buffer.append("\n\n");
|
| | | buffer.append("import " + clz.getName() + ";");
|
| | | buffer.append("\n\n");
|
| | | buffer.append(String.format("public interface %sMapper extends BaseMapper<%s> {", clz.getSimpleName(),clz.getSimpleName()));
|
| | | buffer.append("\n\n\t");
|
| | | buffer.append("}");
|
| | | String daoName = String.format("%sMapper.java", clz.getSimpleName());
|
| | | String daoPath = basePath + "/dao/" + daoName;
|
| | | try {
|
| | | if (!new File(basePath + "/dao/").exists())
|
| | | new File(basePath + "/dao/").mkdirs();
|
| | | if (!new File(daoPath).exists())
|
| | | new File(daoPath).createNewFile();
|
| | | FileOutputStream fos = new FileOutputStream(new File(daoPath));
|
| | | PrintWriter pw = new PrintWriter(fos);
|
| | | pw.write(buffer.toString().toCharArray());
|
| | | pw.flush();
|
| | | pw.close();
|
| | | fos.close();
|
| | | } catch (Exception e) {
|
| | | e.printStackTrace();
|
| | | }
|
| | |
|
| | | /**
|
| | | * 生成Mapper xml文件
|
| | | */
|
| | | // 获取需要映射的列
|
| | | List<AttributeColumnMap> keysList = new ArrayList<>();
|
| | | Field[] fields = clz.getDeclaredFields();
|
| | | for (Field fd : fields) {
|
| | | Annotation[] as = fd.getAnnotations();
|
| | | for (Annotation a : as) {
|
| | | if (a instanceof Column) {
|
| | | Column c = (Column) a;
|
| | | keysList.add(new AttributeColumnMap(fd.getName(), c.name(), fd.getType().getName()));
|
| | | }
|
| | | }
|
| | | }
|
| | | String tableName = "";
|
| | | Annotation[] as = clz.getAnnotations();
|
| | | for (Annotation a : as) {
|
| | | if (a instanceof Table) {
|
| | | Table t = (Table) a;
|
| | | tableName = t.value();
|
| | | }
|
| | | }
|
| | |
|
| | | try {
|
| | | String mapperName = String.format("%sMapper.xml", clz.getSimpleName());
|
| | | String mapperPath = basePath + "/mapper/" + mapperName;
|
| | |
|
| | | org.dom4j.Document document = DocumentHelper.createDocument();
|
| | | document.addDocType("mapper", "-//mybatis.org//DTD Mapper 3.0//EN",
|
| | | "http://mybatis.org/dtd/mybatis-3-mapper.dtd");
|
| | | org.dom4j.Element root = document.addElement("mapper");
|
| | | root.addAttribute("namespace", pks + "." + mapperName.replace(".xml", ""));
|
| | |
|
| | | org.dom4j.Element resultMap = root.addElement("resultMap");
|
| | | resultMap.addAttribute("id", "BaseResultMap");
|
| | | resultMap.addAttribute("type", clz.getName());
|
| | |
|
| | | AttributeColumnMap idKeys = getAttributeColumnMapByAttribute("id", keysList);
|
| | | if (idKeys != null) {
|
| | | org.dom4j.Element id = resultMap.addElement("id");
|
| | | id.addAttribute("column", idKeys.column);
|
| | | id.addAttribute("property", idKeys.attribute);
|
| | | id.addAttribute("jdbcType", ColumnParseUtil.getJDBCType(idKeys.type));
|
| | | }
|
| | |
|
| | | for (AttributeColumnMap key : keysList) {
|
| | | if (key.attribute.equalsIgnoreCase(idKeys.attribute) || ColumnParseUtil.getJDBCType(key.type) == null)
|
| | | continue;
|
| | | org.dom4j.Element result = resultMap.addElement("result");
|
| | | result.addAttribute("column", key.column);
|
| | | result.addAttribute("property", key.attribute);
|
| | | result.addAttribute("jdbcType", ColumnParseUtil.getJDBCType(key.type));
|
| | | }
|
| | |
|
| | | // 属性值中包含实体
|
| | | for (AttributeColumnMap key : keysList) {
|
| | | if (ColumnParseUtil.getJDBCType(key.type) == null) {
|
| | | Class<?> propertyClass = Class.forName(key.type);
|
| | | String propertyMapper = getDaoPackageName(propertyClass) + "." + propertyClass.getSimpleName()
|
| | | + "Mapper";
|
| | | org.dom4j.Element association = resultMap.addElement("association");
|
| | | association.addAttribute("property", key.attribute);
|
| | | association.addAttribute("column", key.column);
|
| | | association.addAttribute("resultMap", propertyMapper + ".BaseResultMap");
|
| | | key.attribute = key.attribute + ".id";
|
| | | key.type = "java.lang.Long";
|
| | | }
|
| | | }
|
| | |
|
| | | org.dom4j.Element sql = root.addElement("sql");
|
| | | sql.addAttribute("id", "Base_Column_List");
|
| | | sql.setText(getColumns(keysList));
|
| | |
|
| | | org.dom4j.Element select = root.addElement("select");
|
| | | select.addAttribute("id", "selectByPrimaryKey");
|
| | | select.addAttribute("resultMap", "BaseResultMap");
|
| | | select.addAttribute("parameterType", "java.lang.Long");
|
| | | select.addText("select");
|
| | | org.dom4j.Element include = select.addElement("include");
|
| | | include.addAttribute("refid", "Base_Column_List");
|
| | | select.addText(String.format("from %s where %s = #{%s,jdbcType=BIGINT}", tableName, idKeys.column,
|
| | | idKeys.attribute));
|
| | |
|
| | | org.dom4j.Element delete = root.addElement("delete");
|
| | | delete.addAttribute("id", "deleteByPrimaryKey");
|
| | | delete.addAttribute("parameterType", "java.lang.Long");
|
| | | delete.setText(String.format("delete from %s where %s = #{%s,jdbcType=BIGINT}", tableName, idKeys.column,
|
| | | idKeys.attribute));
|
| | |
|
| | | org.dom4j.Element insert = root.addElement("insert");
|
| | | insert.addAttribute("id", "insert");
|
| | | insert.addAttribute("parameterType", clz.getName());
|
| | | insert.addAttribute("useGeneratedKeys", "true");
|
| | | insert.addAttribute("keyProperty", "id");
|
| | | StringBuffer text = new StringBuffer();
|
| | | text.append(String.format("insert into %s (", tableName));
|
| | | text.append(getColumns(keysList));
|
| | | text.append(")");
|
| | | text.append(" values (");
|
| | | for (AttributeColumnMap acm : keysList)
|
| | | text.append(getKeyPair(acm)).append(",");
|
| | | text.deleteCharAt(text.length() - 1);
|
| | | text.append(")");
|
| | | insert.setText(text.toString());
|
| | |
|
| | | org.dom4j.Element insertSelective = root.addElement("insert");
|
| | | insertSelective.addAttribute("id", "insertSelective");
|
| | | insertSelective.addAttribute("parameterType", clz.getName());
|
| | | insertSelective.addAttribute("useGeneratedKeys", "true");
|
| | | insertSelective.addAttribute("keyProperty", "id");
|
| | | insertSelective.addText("insert into " + tableName);
|
| | |
|
| | | org.dom4j.Element trim = insertSelective.addElement("trim");
|
| | | trim.addAttribute("prefix", "(");
|
| | | trim.addAttribute("suffix", ")");
|
| | | trim.addAttribute("suffixOverrides", ",");
|
| | | for (AttributeColumnMap acm : keysList) {
|
| | | org.dom4j.Element iff = trim.addElement("if");
|
| | | iff.addAttribute("test",
|
| | | (acm.attribute.indexOf(".") > -1 ? acm.attribute.split("\\.")[0] : acm.attribute) + " != null");
|
| | | iff.setText(acm.column + ",");
|
| | | }
|
| | |
|
| | | insertSelective.addText("values");
|
| | |
|
| | | trim = insertSelective.addElement("trim");
|
| | | trim.addAttribute("prefix", "(");
|
| | | trim.addAttribute("suffix", ")");
|
| | | trim.addAttribute("suffixOverrides", ",");
|
| | | for (AttributeColumnMap acm : keysList) {
|
| | | org.dom4j.Element iff = trim.addElement("if");
|
| | | iff.addAttribute("test",
|
| | | (acm.attribute.indexOf(".") > -1 ? acm.attribute.split("\\.")[0] : acm.attribute) + " != null");
|
| | | iff.setText(getKeyPair(acm) + ",");
|
| | | }
|
| | |
|
| | | // update
|
| | |
|
| | | org.dom4j.Element update = root.addElement("update");
|
| | | update.addAttribute("id", "updateByPrimaryKey");
|
| | | update.addAttribute("parameterType", clz.getName());
|
| | | text = new StringBuffer(String.format("update %s set ", tableName));
|
| | | for (AttributeColumnMap acm : keysList) {
|
| | | if (acm.attribute.equalsIgnoreCase(idKeys.attribute))
|
| | | continue;
|
| | | text.append(String.format("%s = #{%s,jdbcType=%s}", acm.column, acm.attribute,
|
| | | ColumnParseUtil.getJDBCType(acm.type))).append(",");
|
| | | }
|
| | | text.deleteCharAt(text.length() - 1);
|
| | |
|
| | | text.append(String.format(" where %s = #{%s,jdbcType=%s}", idKeys.column, idKeys.attribute,
|
| | | ColumnParseUtil.getJDBCType(idKeys.type)));
|
| | |
|
| | | update.setText(text.toString());
|
| | |
|
| | | // updateSelective
|
| | | org.dom4j.Element updateSelective = root.addElement("update");
|
| | | updateSelective.addAttribute("id", "updateByPrimaryKeySelective");
|
| | | updateSelective.addAttribute("parameterType", clz.getName());
|
| | | updateSelective.addText("update " + tableName);
|
| | | org.dom4j.Element set = updateSelective.addElement("set");
|
| | |
|
| | | for (AttributeColumnMap acm : keysList) {
|
| | | if (acm.attribute.equalsIgnoreCase(idKeys.attribute))
|
| | | continue;
|
| | | org.dom4j.Element iff = set.addElement("if");
|
| | | iff.addAttribute("test",
|
| | | (acm.attribute.indexOf(".") > -1 ? acm.attribute.split("\\.")[0] : acm.attribute) + " != null");
|
| | | iff.setText(acm.column + "=" + getKeyPair(acm) + ",");
|
| | | }
|
| | |
|
| | | updateSelective.addText(String.format(" where %s = #{%s,jdbcType=%s}", idKeys.column, idKeys.attribute,
|
| | | ColumnParseUtil.getJDBCType(idKeys.type)));
|
| | |
|
| | | // 创建mapper文件
|
| | |
|
| | | if (!new File(basePath + "/mapper/").exists())
|
| | | new File(basePath + "/mapper/").mkdirs();
|
| | | if (!new File(mapperPath).exists())
|
| | | new File(mapperPath).createNewFile();
|
| | |
|
| | | XMLWriter writer = new XMLWriter(new FileOutputStream(new File(mapperPath)),
|
| | | OutputFormat.createPrettyPrint());
|
| | | writer.setEscapeText(false);// 字符是否转义,默认true
|
| | | writer.write(document);
|
| | | writer.close();
|
| | | } catch (Exception e) {
|
| | | e.printStackTrace();
|
| | | }
|
| | | }
|
| | |
|
| | | private static AttributeColumnMap getAttributeColumnMapByAttribute(String attributeName,
|
| | | List<AttributeColumnMap> list) {
|
| | | for (AttributeColumnMap acm : list)
|
| | | if (acm.attribute.equalsIgnoreCase(attributeName))
|
| | | return acm;
|
| | | return null;
|
| | | }
|
| | |
|
| | | private static String getColumns(List<AttributeColumnMap> list) {
|
| | | String columns = "";
|
| | | for (AttributeColumnMap map : list)
|
| | | columns += map.column + ",";
|
| | | return columns.length() > 0 ? columns.substring(0, columns.length() - 1) : columns;
|
| | | }
|
| | |
|
| | | private static String getKeyPair(AttributeColumnMap map) {
|
| | | return String.format("#{%s,jdbcType=%s}", map.attribute, ColumnParseUtil.getJDBCType(map.type));
|
| | | }
|
| | |
|
| | | }
|
| | |
|
| | | class AttributeColumnMap {
|
| | | String attribute;
|
| | | String column;
|
| | | String type;
|
| | |
|
| | | public AttributeColumnMap(String attribute, String column, String type) {
|
| | | this.attribute = attribute;
|
| | | this.column = column;
|
| | | this.type = type;
|
| | | }
|
| | |
|
| | | public AttributeColumnMap() {
|
| | |
|
| | | }
|
| | | }
|
| | | package org.yeshi.utils.generater.mybatis; |
| | | |
| | | import java.io.File; |
| | | import java.io.FileOutputStream; |
| | | import java.io.PrintWriter; |
| | | import java.lang.annotation.Annotation; |
| | | import java.lang.reflect.Field; |
| | | import java.util.ArrayList; |
| | | import java.util.List; |
| | | |
| | | import org.dom4j.DocumentHelper; |
| | | import org.dom4j.io.OutputFormat; |
| | | import org.dom4j.io.XMLWriter; |
| | | |
| | | public class MyBatisMapperUtil { |
| | | private static String basePath = "D:/mybatis"; |
| | | |
| | | static { |
| | | // 创建工作目录 |
| | | if (!new File(basePath).exists()) |
| | | new File(basePath).mkdirs(); |
| | | } |
| | | |
| | | // 获取dao所在的包名 |
| | | private static String getDaoPackageName(Class<?> bean) { |
| | | String name = bean.getName(); |
| | | String[] ns = name.split("\\."); |
| | | if (ns.length > 2) { |
| | | String pks = ""; |
| | | for (int i = 0; i < ns.length - 2; i++) { |
| | | pks += ns[i] + "."; |
| | | } |
| | | return pks + "dao"; |
| | | } |
| | | return ""; |
| | | } |
| | | |
| | | private static String getMapperPackageName(Class<?> bean) { |
| | | String name = bean.getName(); |
| | | String[] ns = name.split("\\."); |
| | | if (ns.length > 2) { |
| | | String pks = ""; |
| | | for (int i = 0; i < ns.length - 2; i++) { |
| | | pks += ns[i] + "."; |
| | | } |
| | | return pks + "mapper"; |
| | | } |
| | | return ""; |
| | | } |
| | | |
| | | public static void createMapper(Class<?> clz) { |
| | | // 生成mapper java文件 |
| | | String pks = getDaoPackageName(clz); |
| | | StringBuffer buffer = new StringBuffer("package " + pks + ";"); |
| | | buffer.append("\n\n"); |
| | | buffer.append("import " + clz.getName() + ";"); |
| | | buffer.append("\n\n"); |
| | | buffer.append(String.format("public interface %sMapper extends BaseMapper<%s> {", clz.getSimpleName(),clz.getSimpleName())); |
| | | buffer.append("\n\n\t"); |
| | | buffer.append("}"); |
| | | String daoName = String.format("%sMapper.java", clz.getSimpleName()); |
| | | String daoPath = basePath + "/dao/" + daoName; |
| | | try { |
| | | if (!new File(basePath + "/dao/").exists()) |
| | | new File(basePath + "/dao/").mkdirs(); |
| | | if (!new File(daoPath).exists()) |
| | | new File(daoPath).createNewFile(); |
| | | FileOutputStream fos = new FileOutputStream(new File(daoPath)); |
| | | PrintWriter pw = new PrintWriter(fos); |
| | | pw.write(buffer.toString().toCharArray()); |
| | | pw.flush(); |
| | | pw.close(); |
| | | fos.close(); |
| | | } catch (Exception e) { |
| | | e.printStackTrace(); |
| | | } |
| | | |
| | | /** |
| | | * 生成Mapper xml文件 |
| | | */ |
| | | // 获取需要映射的列 |
| | | List<AttributeColumnMap> keysList = new ArrayList<>(); |
| | | Field[] fields = clz.getDeclaredFields(); |
| | | for (Field fd : fields) { |
| | | Annotation[] as = fd.getAnnotations(); |
| | | for (Annotation a : as) { |
| | | if (a instanceof Column) { |
| | | Column c = (Column) a; |
| | | keysList.add(new AttributeColumnMap(fd.getName(), c.name(), fd.getType().getName())); |
| | | } |
| | | } |
| | | } |
| | | String tableName = ""; |
| | | Annotation[] as = clz.getAnnotations(); |
| | | for (Annotation a : as) { |
| | | if (a instanceof Table) { |
| | | Table t = (Table) a; |
| | | tableName = t.value(); |
| | | } |
| | | } |
| | | |
| | | try { |
| | | String mapperName = String.format("%sMapper.xml", clz.getSimpleName()); |
| | | String mapperPath = basePath + "/mapper/" + mapperName; |
| | | |
| | | org.dom4j.Document document = DocumentHelper.createDocument(); |
| | | document.addDocType("mapper", "-//mybatis.org//DTD Mapper 3.0//EN", |
| | | "http://mybatis.org/dtd/mybatis-3-mapper.dtd"); |
| | | org.dom4j.Element root = document.addElement("mapper"); |
| | | root.addAttribute("namespace", pks + "." + mapperName.replace(".xml", "")); |
| | | |
| | | org.dom4j.Element resultMap = root.addElement("resultMap"); |
| | | resultMap.addAttribute("id", "BaseResultMap"); |
| | | resultMap.addAttribute("type", clz.getName()); |
| | | |
| | | AttributeColumnMap idKeys = getAttributeColumnMapByAttribute("id", keysList); |
| | | if (idKeys != null) { |
| | | org.dom4j.Element id = resultMap.addElement("id"); |
| | | id.addAttribute("column", idKeys.column); |
| | | id.addAttribute("property", idKeys.attribute); |
| | | id.addAttribute("jdbcType", ColumnParseUtil.getJDBCType(idKeys.type)); |
| | | } |
| | | |
| | | for (AttributeColumnMap key : keysList) { |
| | | if (key.attribute.equalsIgnoreCase(idKeys.attribute) || ColumnParseUtil.getJDBCType(key.type) == null) |
| | | continue; |
| | | org.dom4j.Element result = resultMap.addElement("result"); |
| | | result.addAttribute("column", key.column); |
| | | result.addAttribute("property", key.attribute); |
| | | result.addAttribute("jdbcType", ColumnParseUtil.getJDBCType(key.type)); |
| | | } |
| | | |
| | | // 属性值中包含实体 |
| | | for (AttributeColumnMap key : keysList) { |
| | | if (ColumnParseUtil.getJDBCType(key.type) == null) { |
| | | Class<?> propertyClass = Class.forName(key.type); |
| | | String propertyMapper = getDaoPackageName(propertyClass) + "." + propertyClass.getSimpleName() |
| | | + "Mapper"; |
| | | org.dom4j.Element association = resultMap.addElement("association"); |
| | | association.addAttribute("property", key.attribute); |
| | | association.addAttribute("column", key.column); |
| | | association.addAttribute("resultMap", propertyMapper + ".BaseResultMap"); |
| | | key.attribute = key.attribute + ".id"; |
| | | key.type = "java.lang.Long"; |
| | | } |
| | | } |
| | | |
| | | org.dom4j.Element sql = root.addElement("sql"); |
| | | sql.addAttribute("id", "Base_Column_List"); |
| | | sql.setText(getColumns(keysList)); |
| | | |
| | | org.dom4j.Element select = root.addElement("select"); |
| | | select.addAttribute("id", "selectByPrimaryKey"); |
| | | select.addAttribute("resultMap", "BaseResultMap"); |
| | | select.addAttribute("parameterType", "java.lang.Long"); |
| | | select.addText("select"); |
| | | org.dom4j.Element include = select.addElement("include"); |
| | | include.addAttribute("refid", "Base_Column_List"); |
| | | select.addText(String.format("from %s where %s = #{%s,jdbcType=BIGINT}", tableName, idKeys.column, |
| | | idKeys.attribute)); |
| | | |
| | | org.dom4j.Element delete = root.addElement("delete"); |
| | | delete.addAttribute("id", "deleteByPrimaryKey"); |
| | | delete.addAttribute("parameterType", "java.lang.Long"); |
| | | delete.setText(String.format("delete from %s where %s = #{%s,jdbcType=BIGINT}", tableName, idKeys.column, |
| | | idKeys.attribute)); |
| | | |
| | | org.dom4j.Element insert = root.addElement("insert"); |
| | | insert.addAttribute("id", "insert"); |
| | | insert.addAttribute("parameterType", clz.getName()); |
| | | insert.addAttribute("useGeneratedKeys", "true"); |
| | | insert.addAttribute("keyProperty", "id"); |
| | | StringBuffer text = new StringBuffer(); |
| | | text.append(String.format("insert into %s (", tableName)); |
| | | text.append(getColumns(keysList)); |
| | | text.append(")"); |
| | | text.append(" values ("); |
| | | for (AttributeColumnMap acm : keysList) |
| | | text.append(getKeyPair(acm)).append(","); |
| | | text.deleteCharAt(text.length() - 1); |
| | | text.append(")"); |
| | | insert.setText(text.toString()); |
| | | |
| | | org.dom4j.Element insertSelective = root.addElement("insert"); |
| | | insertSelective.addAttribute("id", "insertSelective"); |
| | | insertSelective.addAttribute("parameterType", clz.getName()); |
| | | insertSelective.addAttribute("useGeneratedKeys", "true"); |
| | | insertSelective.addAttribute("keyProperty", "id"); |
| | | insertSelective.addText("insert into " + tableName); |
| | | |
| | | org.dom4j.Element trim = insertSelective.addElement("trim"); |
| | | trim.addAttribute("prefix", "("); |
| | | trim.addAttribute("suffix", ")"); |
| | | trim.addAttribute("suffixOverrides", ","); |
| | | for (AttributeColumnMap acm : keysList) { |
| | | org.dom4j.Element iff = trim.addElement("if"); |
| | | iff.addAttribute("test", |
| | | (acm.attribute.indexOf(".") > -1 ? acm.attribute.split("\\.")[0] : acm.attribute) + " != null"); |
| | | iff.setText(acm.column + ","); |
| | | } |
| | | |
| | | insertSelective.addText("values"); |
| | | |
| | | trim = insertSelective.addElement("trim"); |
| | | trim.addAttribute("prefix", "("); |
| | | trim.addAttribute("suffix", ")"); |
| | | trim.addAttribute("suffixOverrides", ","); |
| | | for (AttributeColumnMap acm : keysList) { |
| | | org.dom4j.Element iff = trim.addElement("if"); |
| | | iff.addAttribute("test", |
| | | (acm.attribute.indexOf(".") > -1 ? acm.attribute.split("\\.")[0] : acm.attribute) + " != null"); |
| | | iff.setText(getKeyPair(acm) + ","); |
| | | } |
| | | |
| | | // update |
| | | |
| | | org.dom4j.Element update = root.addElement("update"); |
| | | update.addAttribute("id", "updateByPrimaryKey"); |
| | | update.addAttribute("parameterType", clz.getName()); |
| | | text = new StringBuffer(String.format("update %s set ", tableName)); |
| | | for (AttributeColumnMap acm : keysList) { |
| | | if (acm.attribute.equalsIgnoreCase(idKeys.attribute)) |
| | | continue; |
| | | text.append(String.format("%s = #{%s,jdbcType=%s}", acm.column, acm.attribute, |
| | | ColumnParseUtil.getJDBCType(acm.type))).append(","); |
| | | } |
| | | text.deleteCharAt(text.length() - 1); |
| | | |
| | | text.append(String.format(" where %s = #{%s,jdbcType=%s}", idKeys.column, idKeys.attribute, |
| | | ColumnParseUtil.getJDBCType(idKeys.type))); |
| | | |
| | | update.setText(text.toString()); |
| | | |
| | | // updateSelective |
| | | org.dom4j.Element updateSelective = root.addElement("update"); |
| | | updateSelective.addAttribute("id", "updateByPrimaryKeySelective"); |
| | | updateSelective.addAttribute("parameterType", clz.getName()); |
| | | updateSelective.addText("update " + tableName); |
| | | org.dom4j.Element set = updateSelective.addElement("set"); |
| | | |
| | | for (AttributeColumnMap acm : keysList) { |
| | | if (acm.attribute.equalsIgnoreCase(idKeys.attribute)) |
| | | continue; |
| | | org.dom4j.Element iff = set.addElement("if"); |
| | | iff.addAttribute("test", |
| | | (acm.attribute.indexOf(".") > -1 ? acm.attribute.split("\\.")[0] : acm.attribute) + " != null"); |
| | | iff.setText(acm.column + "=" + getKeyPair(acm) + ","); |
| | | } |
| | | |
| | | updateSelective.addText(String.format(" where %s = #{%s,jdbcType=%s}", idKeys.column, idKeys.attribute, |
| | | ColumnParseUtil.getJDBCType(idKeys.type))); |
| | | |
| | | // 创建mapper文件 |
| | | |
| | | if (!new File(basePath + "/mapper/").exists()) |
| | | new File(basePath + "/mapper/").mkdirs(); |
| | | if (!new File(mapperPath).exists()) |
| | | new File(mapperPath).createNewFile(); |
| | | |
| | | XMLWriter writer = new XMLWriter(new FileOutputStream(new File(mapperPath)), |
| | | OutputFormat.createPrettyPrint()); |
| | | writer.setEscapeText(false);// 字符是否转义,默认true |
| | | writer.write(document); |
| | | writer.close(); |
| | | } catch (Exception e) { |
| | | e.printStackTrace(); |
| | | } |
| | | } |
| | | |
| | | private static AttributeColumnMap getAttributeColumnMapByAttribute(String attributeName, |
| | | List<AttributeColumnMap> list) { |
| | | for (AttributeColumnMap acm : list) |
| | | if (acm.attribute.equalsIgnoreCase(attributeName)) |
| | | return acm; |
| | | return null; |
| | | } |
| | | |
| | | private static String getColumns(List<AttributeColumnMap> list) { |
| | | String columns = ""; |
| | | for (AttributeColumnMap map : list) |
| | | columns += map.column + ","; |
| | | return columns.length() > 0 ? columns.substring(0, columns.length() - 1) : columns; |
| | | } |
| | | |
| | | private static String getKeyPair(AttributeColumnMap map) { |
| | | return String.format("#{%s,jdbcType=%s}", map.attribute, ColumnParseUtil.getJDBCType(map.type)); |
| | | } |
| | | |
| | | } |
| | | |
| | | class AttributeColumnMap { |
| | | String attribute; |
| | | String column; |
| | | String type; |
| | | |
| | | public AttributeColumnMap(String attribute, String column, String type) { |
| | | this.attribute = attribute; |
| | | this.column = column; |
| | | this.type = type; |
| | | } |
| | | |
| | | public AttributeColumnMap() { |
| | | |
| | | } |
| | | } |
File was renamed from utils/src/main/java/org/yeshi/utils/mybatis/Table.java |
| | |
| | | package org.yeshi.utils.mybatis;
|
| | |
|
| | | import java.lang.annotation.Documented;
|
| | | import java.lang.annotation.ElementType;
|
| | | import java.lang.annotation.Inherited;
|
| | | import java.lang.annotation.Retention;
|
| | | import java.lang.annotation.RetentionPolicy;
|
| | | import java.lang.annotation.Target;
|
| | |
|
| | | @Documented
|
| | | @Target(ElementType.TYPE)
|
| | | @Inherited
|
| | | @Retention(RetentionPolicy.RUNTIME)
|
| | | public @interface Table {
|
| | | String value();
|
| | | }
|
| | | package org.yeshi.utils.generater.mybatis; |
| | | |
| | | import java.lang.annotation.Documented; |
| | | import java.lang.annotation.ElementType; |
| | | import java.lang.annotation.Inherited; |
| | | import java.lang.annotation.Retention; |
| | | import java.lang.annotation.RetentionPolicy; |
| | | import java.lang.annotation.Target; |
| | | |
| | | @Documented |
| | | @Target(ElementType.TYPE) |
| | | @Inherited |
| | | @Retention(RetentionPolicy.RUNTIME) |
| | | public @interface Table { |
| | | String value(); |
| | | } |
| | |
| | | import com.qcloud.cos.region.Region;
|
| | |
|
| | | public class COSManager {
|
| | | static long appId = 0L;
|
| | | static String secretId = "";
|
| | | static String secretKey = "";
|
| | | // 设置要操作的bucket
|
| | | static String bucketName = "";
|
| | | static COSClient cosClient;
|
| | | static String totalBucketName = "";
|
| | | static long appId = 0L;
|
| | | static String secretId = "";
|
| | | static String secretKey = "";
|
| | | // 设置要操作的bucket
|
| | | static String bucketName = "";
|
| | | static COSClient cosClient;
|
| | | static String totalBucketName = "";
|
| | | static String accessHost = "";
|
| | |
|
| | | static COSManager instance;
|
| | | static COSManager instance;
|
| | |
|
| | | public static COSManager getInstance() {
|
| | | if (instance == null)
|
| | | instance = new COSManager();
|
| | | return instance;
|
| | | }
|
| | | public static COSManager getInstance() {
|
| | | if (instance == null)
|
| | | instance = new COSManager();
|
| | | return instance;
|
| | | }
|
| | |
|
| | | /**
|
| | | * 初始化,在使用之前必须调用此方法
|
| | | * |
| | | * @param params
|
| | | */
|
| | | public void init(COSInitParams params) {
|
| | | appId = params.getAppId();
|
| | | secretId = params.getSecretId();
|
| | | secretKey = params.getSecretKey();
|
| | | bucketName = params.getBucketName();
|
| | | COSCredentials cred = new BasicCOSCredentials(secretId, secretKey);
|
| | | ClientConfig clientConfig = new ClientConfig(new Region(params.getRegion()));
|
| | | cosClient = new COSClient(cred, clientConfig);
|
| | | // bucket的命名规则为{name}-{appid} ,此处填写的存储桶名称必须为此格式
|
| | | totalBucketName = bucketName + "-" + appId;
|
| | | }
|
| | | /**
|
| | | * 初始化,在使用之前必须调用此方法
|
| | | *
|
| | | * @param params
|
| | | */
|
| | | public void init(COSInitParams params) {
|
| | | appId = params.getAppId();
|
| | | secretId = params.getSecretId();
|
| | | secretKey = params.getSecretKey();
|
| | | bucketName = params.getBucketName();
|
| | | COSCredentials cred = new BasicCOSCredentials(secretId, secretKey);
|
| | | ClientConfig clientConfig = new ClientConfig(new Region(params.getRegion()));
|
| | | cosClient = new COSClient(cred, clientConfig);
|
| | | // bucket的命名规则为{name}-{appid} ,此处填写的存储桶名称必须为此格式
|
| | | totalBucketName = bucketName + "-" + appId;
|
| | | accessHost = params.getAccessHost();
|
| | | }
|
| | |
|
| | | public FileUploadResult uploadFile(File f, String key) {
|
| | | if (key != null && !key.startsWith("/"))
|
| | | key = "/" + key;
|
| | | PutObjectRequest putObjectRequest = new PutObjectRequest(totalBucketName, key, f);
|
| | | public FileUploadResult uploadFile(File f, String key) {
|
| | | if (key != null && !key.startsWith("/"))
|
| | | key = "/" + key;
|
| | | PutObjectRequest putObjectRequest = new PutObjectRequest(totalBucketName, key, f);
|
| | |
|
| | | try {
|
| | | PutObjectResult result = cosClient.putObject(putObjectRequest);
|
| | | try {
|
| | | PutObjectResult result = cosClient.putObject(putObjectRequest);
|
| | |
|
| | | return new FileUploadResult(String.format("http://%s.file.myqcloud.com%s", totalBucketName, key),
|
| | | result.getETag());
|
| | | } catch (Exception e) {
|
| | | e.printStackTrace();
|
| | | }
|
| | | return null;
|
| | | }
|
| | | if (StringUtil.isNullOrEmpty(accessHost)) {
|
| | | return new FileUploadResult(String.format("http://%s.file.myqcloud.com%s", totalBucketName, key),
|
| | | result.getETag());
|
| | | } else {
|
| | | return new FileUploadResult(String.format("%s%s", accessHost, key),
|
| | | result.getETag());
|
| | | }
|
| | | } catch (Exception e) {
|
| | | e.printStackTrace();
|
| | | }
|
| | | return null;
|
| | | }
|
| | |
|
| | | public FileUploadResult uploadInputStream(InputStream inputStream, String key) {
|
| | | int count = 0;
|
| | | try {
|
| | | count = inputStream.available();
|
| | | } catch (IOException e1) {
|
| | | e1.printStackTrace();
|
| | | }
|
| | | public FileUploadResult uploadInputStream(InputStream inputStream, String key) {
|
| | | int count = 0;
|
| | | try {
|
| | | count = inputStream.available();
|
| | | } catch (IOException e1) {
|
| | | e1.printStackTrace();
|
| | | }
|
| | |
|
| | | ObjectMetadata metadata = new ObjectMetadata();
|
| | | metadata.setContentLength(count);
|
| | | ObjectMetadata metadata = new ObjectMetadata();
|
| | | metadata.setContentLength(count);
|
| | |
|
| | | if (key != null && !key.startsWith("/"))
|
| | | key = "/" + key;
|
| | | PutObjectRequest putObjectRequest = new PutObjectRequest(totalBucketName, key, inputStream, metadata);
|
| | | try {
|
| | | PutObjectResult result = cosClient.putObject(putObjectRequest);
|
| | | if (key != null && !key.startsWith("/"))
|
| | | key = "/" + key;
|
| | | PutObjectRequest putObjectRequest = new PutObjectRequest(totalBucketName, key, inputStream, metadata);
|
| | | try {
|
| | | PutObjectResult result = cosClient.putObject(putObjectRequest);
|
| | |
|
| | | return new FileUploadResult(String.format("http://%s.file.myqcloud.com%s", totalBucketName, key),
|
| | | result.getETag());
|
| | | } catch (Exception e) {
|
| | | e.printStackTrace();
|
| | | }
|
| | | return null;
|
| | | }
|
| | | return new FileUploadResult(String.format("http://%s.file.myqcloud.com%s", totalBucketName, key),
|
| | | result.getETag());
|
| | | } catch (Exception e) {
|
| | | e.printStackTrace();
|
| | | }
|
| | | return null;
|
| | | }
|
| | |
|
| | | public FileUploadResult uploadFile(InputStream inputStream, String key) {
|
| | | if (key != null && !key.startsWith("/"))
|
| | | key = "/" + key;
|
| | | String dir = FileUtil.getCacheDir();
|
| | | String tempPath = dir + "/temp_" + System.currentTimeMillis() + "_" + (long) (Math.random() * 100000000);
|
| | | try {
|
| | | public FileUploadResult uploadFile(InputStream inputStream, String key) {
|
| | | if (key != null && !key.startsWith("/"))
|
| | | key = "/" + key;
|
| | | String dir = FileUtil.getCacheDir();
|
| | | String tempPath = dir + "/temp_" + System.currentTimeMillis() + "_" + (long) (Math.random() * 100000000);
|
| | | try {
|
| | |
|
| | | try {
|
| | | FileUtil.saveAsFile(inputStream, tempPath);
|
| | | } catch (IOException e) {
|
| | | e.printStackTrace();
|
| | | }
|
| | | if (new File(tempPath).exists()) {
|
| | | return uploadFile(new File(tempPath), key);
|
| | | }
|
| | | return null;
|
| | | } finally {
|
| | | if (new File(tempPath).exists())
|
| | | new File(tempPath).delete();
|
| | | }
|
| | | }
|
| | | try {
|
| | | FileUtil.saveAsFile(inputStream, tempPath);
|
| | | } catch (IOException e) {
|
| | | e.printStackTrace();
|
| | | }
|
| | | if (new File(tempPath).exists()) {
|
| | | return uploadFile(new File(tempPath), key);
|
| | | }
|
| | | return null;
|
| | | } finally {
|
| | | if (new File(tempPath).exists())
|
| | | new File(tempPath).delete();
|
| | | }
|
| | | }
|
| | |
|
| | | /**
|
| | | * 获取Object列表
|
| | | * |
| | | * @param prefix-匹配字符
|
| | | * @param count-最大1000
|
| | | * @param marker-起点标记
|
| | | * @return
|
| | | */
|
| | | /**
|
| | | * 获取Object列表
|
| | | *
|
| | | * @param prefix-匹配字符
|
| | | * @param count-最大1000
|
| | | * @param marker-起点标记
|
| | | * @return
|
| | | */
|
| | |
|
| | | public ObjectListing getObjectList(String prefix, String marker, int count) {
|
| | | ListObjectsRequest listObjectsRequest = new ListObjectsRequest();
|
| | | listObjectsRequest.setBucketName(totalBucketName);
|
| | | listObjectsRequest.setDelimiter("/");
|
| | | listObjectsRequest.setPrefix(prefix);
|
| | | listObjectsRequest.setMaxKeys(count);
|
| | | listObjectsRequest.setMarker(marker);
|
| | | ObjectListing objectListing = cosClient.listObjects(listObjectsRequest);
|
| | | return objectListing;
|
| | | }
|
| | | public ObjectListing getObjectList(String prefix, String marker, int count) {
|
| | | ListObjectsRequest listObjectsRequest = new ListObjectsRequest();
|
| | | listObjectsRequest.setBucketName(totalBucketName);
|
| | | listObjectsRequest.setDelimiter("/");
|
| | | listObjectsRequest.setPrefix(prefix);
|
| | | listObjectsRequest.setMaxKeys(count);
|
| | | listObjectsRequest.setMarker(marker);
|
| | | ObjectListing objectListing = cosClient.listObjects(listObjectsRequest);
|
| | | return objectListing;
|
| | | }
|
| | |
|
| | | public COSObjectSummary getObjectDetail(String prefix) {
|
| | | ListObjectsRequest listObjectsRequest = new ListObjectsRequest();
|
| | | listObjectsRequest.setBucketName(totalBucketName);
|
| | | listObjectsRequest.setDelimiter("/");
|
| | | listObjectsRequest.setPrefix(prefix);
|
| | | listObjectsRequest.setMaxKeys(1);
|
| | | listObjectsRequest.setMarker(null);
|
| | | ObjectListing objectListing = cosClient.listObjects(listObjectsRequest);
|
| | | if (objectListing.getObjectSummaries() != null && objectListing.getObjectSummaries().size() > 0) {
|
| | | return objectListing.getObjectSummaries().get(0);
|
| | | }
|
| | | return null;
|
| | | }
|
| | | public COSObjectSummary getObjectDetail(String prefix) {
|
| | | ListObjectsRequest listObjectsRequest = new ListObjectsRequest();
|
| | | listObjectsRequest.setBucketName(totalBucketName);
|
| | | listObjectsRequest.setDelimiter("/");
|
| | | listObjectsRequest.setPrefix(prefix);
|
| | | listObjectsRequest.setMaxKeys(1);
|
| | | listObjectsRequest.setMarker(null);
|
| | | ObjectListing objectListing = cosClient.listObjects(listObjectsRequest);
|
| | | if (objectListing.getObjectSummaries() != null && objectListing.getObjectSummaries().size() > 0) {
|
| | | return objectListing.getObjectSummaries().get(0);
|
| | | }
|
| | | return null;
|
| | | }
|
| | |
|
| | | // 文件是否存在
|
| | | public boolean isFileExist(String url) {
|
| | | String key = url.replace(String.format("https://%s.file.myqcloud.com", totalBucketName), "");
|
| | | try {
|
| | | cosClient.getObjectMetadata(totalBucketName, key);
|
| | | return true;
|
| | | } catch (Exception e) {
|
| | | }
|
| | | return false;
|
| | | }
|
| | | // 文件是否存在
|
| | | public boolean isFileExist(String url) {
|
| | | String key = url.replace(String.format("https://%s.file.myqcloud.com", totalBucketName), "");
|
| | | try {
|
| | | cosClient.getObjectMetadata(totalBucketName, key);
|
| | | return true;
|
| | | } catch (Exception e) {
|
| | | }
|
| | | return false;
|
| | | }
|
| | |
|
| | | public boolean deleteFile(String url) {
|
| | | String key = url.replace(String.format("https://%s.file.myqcloud.com", totalBucketName), "");
|
| | | try {
|
| | | cosClient.deleteObject(totalBucketName, key);
|
| | | return true;
|
| | | } catch (Exception e) {
|
| | | }
|
| | | return false;
|
| | | }
|
| | | public boolean deleteFile(String url) {
|
| | | String key = url.replace(String.format("https://%s.file.myqcloud.com", totalBucketName), "");
|
| | | try {
|
| | | cosClient.deleteObject(totalBucketName, key);
|
| | | return true;
|
| | | } catch (Exception e) {
|
| | | }
|
| | | return false;
|
| | | }
|
| | |
|
| | | public static String getUploadVideoUrl(String name) {
|
| | | if (name != null && !name.startsWith("/"))
|
| | | name = "/" + name;
|
| | | return "https://gz.file.myqcloud.com/files/v2/" + appId + "/" + bucketName + name;
|
| | | }
|
| | | public static String getUploadVideoUrl(String name) {
|
| | | if (name != null && !name.startsWith("/"))
|
| | | name = "/" + name;
|
| | | return "https://gz.file.myqcloud.com/files/v2/" + appId + "/" + bucketName + name;
|
| | | }
|
| | |
|
| | | public static String getUploadVideoFileId(String name) {
|
| | | if (name != null && !name.startsWith("/"))
|
| | | name = "/" + name;
|
| | | return "/" + appId + "/" + bucketName + name;
|
| | | }
|
| | | public static String getUploadVideoFileId(String name) {
|
| | | if (name != null && !name.startsWith("/"))
|
| | | name = "/" + name;
|
| | | return "/" + appId + "/" + bucketName + name;
|
| | | }
|
| | |
|
| | | public static String getUploadVideoAuthorization() {
|
| | | long time = System.currentTimeMillis() / 1000;
|
| | | long expireTime = time + 60 * 20;// 20分钟的上传时间
|
| | | long random = (long) (1000000L * Math.random());
|
| | | String original = String.format("a=%s&b=%s&k=%s&e=%s&t=%s&r=%s&f=", appId + "", bucketName, secretId,
|
| | | (expireTime) + "", time + "", random + "");
|
| | | byte[] so = HmacUtils.hmacSha1(secretKey, original);
|
| | | byte[] or = original.getBytes();
|
| | | byte[] bts = ArrayUtils.addAll(so, or);
|
| | | String sign = StringUtil.getBase64FromByte(bts).replace("\n", "").replace("\r", "").trim();
|
| | | return sign;
|
| | | }
|
| | | public static String getUploadVideoAuthorization() {
|
| | | long time = System.currentTimeMillis() / 1000;
|
| | | long expireTime = time + 60 * 20;// 20分钟的上传时间
|
| | | long random = (long) (1000000L * Math.random());
|
| | | String original = String.format("a=%s&b=%s&k=%s&e=%s&t=%s&r=%s&f=", appId + "", bucketName, secretId,
|
| | | (expireTime) + "", time + "", random + "");
|
| | | byte[] so = HmacUtils.hmacSha1(secretKey, original);
|
| | | byte[] or = original.getBytes();
|
| | | byte[] bts = ArrayUtils.addAll(so, or);
|
| | | String sign = StringUtil.getBase64FromByte(bts).replace("\n", "").replace("\r", "").trim();
|
| | | return sign;
|
| | | }
|
| | |
|
| | | public FileUploadResult uploadFileByte(byte[] b, String key) {
|
| | | if (key != null && !key.startsWith("/"))
|
| | | key = "/" + key;
|
| | | String dir = FileUtil.getCacheDir();
|
| | | String tempPath = dir + "/temp_" + System.currentTimeMillis() + "_" + (long) (Math.random() * 100000000);
|
| | | try {
|
| | | public FileUploadResult uploadFileByte(byte[] b, String key) {
|
| | | if (key != null && !key.startsWith("/"))
|
| | | key = "/" + key;
|
| | | String dir = FileUtil.getCacheDir();
|
| | | String tempPath = dir + "/temp_" + System.currentTimeMillis() + "_" + (long) (Math.random() * 100000000);
|
| | | try {
|
| | |
|
| | | try {
|
| | | FileUtil.saveAsFileByte(b, tempPath);
|
| | | } catch (IOException e) {
|
| | | e.printStackTrace();
|
| | | }
|
| | | if (new File(tempPath).exists()) {
|
| | | return uploadFile(new File(tempPath), key);
|
| | | }
|
| | | return null;
|
| | | } finally {
|
| | | if (new File(tempPath).exists())
|
| | | new File(tempPath).delete();
|
| | | }
|
| | | }
|
| | | try {
|
| | | FileUtil.saveAsFileByte(b, tempPath);
|
| | | } catch (IOException e) {
|
| | | e.printStackTrace();
|
| | | }
|
| | | if (new File(tempPath).exists()) {
|
| | | return uploadFile(new File(tempPath), key);
|
| | | }
|
| | | return null;
|
| | | } finally {
|
| | | if (new File(tempPath).exists())
|
| | | new File(tempPath).delete();
|
| | | }
|
| | | }
|
| | | }
|
| | |
| | |
|
| | | /**
|
| | | * 腾讯云COS初始化参数
|
| | | * |
| | | * @author hexiaohui
|
| | | *
|
| | | * @author hexiaohui
|
| | | */
|
| | | public class COSInitParams {
|
| | | private long appId;
|
| | | private String secretId;
|
| | | private String secretKey;
|
| | | // 设置要操作的bucket
|
| | | private String bucketName;
|
| | | private String region;
|
| | | private long appId;
|
| | | private String secretId;
|
| | | private String secretKey;
|
| | | // 设置要操作的bucket
|
| | | private String bucketName;
|
| | | private String region;
|
| | | //访问域名
|
| | | private String accessHost;
|
| | |
|
| | | public long getAppId() {
|
| | | return appId;
|
| | | }
|
| | | public long getAppId() {
|
| | | return appId;
|
| | | }
|
| | |
|
| | | public void setAppId(long appId) {
|
| | | this.appId = appId;
|
| | | }
|
| | | public void setAppId(long appId) {
|
| | | this.appId = appId;
|
| | | }
|
| | |
|
| | | public String getSecretId() {
|
| | | return secretId;
|
| | | }
|
| | | public String getSecretId() {
|
| | | return secretId;
|
| | | }
|
| | |
|
| | | public void setSecretId(String secretId) {
|
| | | this.secretId = secretId;
|
| | | }
|
| | | public void setSecretId(String secretId) {
|
| | | this.secretId = secretId;
|
| | | }
|
| | |
|
| | | public String getSecretKey() {
|
| | | return secretKey;
|
| | | }
|
| | | public String getSecretKey() {
|
| | | return secretKey;
|
| | | }
|
| | |
|
| | | public void setSecretKey(String secretKey) {
|
| | | this.secretKey = secretKey;
|
| | | }
|
| | | public void setSecretKey(String secretKey) {
|
| | | this.secretKey = secretKey;
|
| | | }
|
| | |
|
| | | public String getBucketName() {
|
| | | return bucketName;
|
| | | }
|
| | | public String getBucketName() {
|
| | | return bucketName;
|
| | | }
|
| | |
|
| | | public void setBucketName(String bucketName) {
|
| | | this.bucketName = bucketName;
|
| | | }
|
| | | public void setBucketName(String bucketName) {
|
| | | this.bucketName = bucketName;
|
| | | }
|
| | |
|
| | | public String getRegion() {
|
| | | return region;
|
| | | }
|
| | | public String getRegion() {
|
| | | return region;
|
| | | }
|
| | |
|
| | | public void setRegion(String region) {
|
| | | this.region = region;
|
| | | }
|
| | | public void setRegion(String region) {
|
| | | this.region = region;
|
| | | }
|
| | |
|
| | | public String getAccessHost() {
|
| | | return accessHost;
|
| | | }
|
| | |
|
| | | public void setAccessHost(String accessHost) {
|
| | | this.accessHost = accessHost;
|
| | | }
|
| | | }
|
| | |
| | | package com.yeshi.utils; |
| | | |
| | | import com.alipay.api.AlipayApiException; |
| | | import org.bouncycastle.jcajce.provider.asymmetric.rsa.RSAUtil; |
| | | import org.yeshi.utils.alipay.AlipayUtil; |
| | | import org.yeshi.utils.alipay.AlipayXcxUtil; |
| | | import org.yeshi.utils.entity.alipay.AlipayAppInfo; |
| | | import org.yeshi.utils.entity.alipay.AlipayCertInfo; |
| | | import org.yeshi.utils.entity.alipay.AlipayTransferInfo; |
| | | import org.yeshi.utils.entity.alipay.AlipayUserInfo; |
| | | import org.yeshi.utils.exception.AlipayCommonException; |
| | | |
| | | import java.io.FileInputStream; |
| | |
| | | private static AlipayCertInfo getCertInfo() throws FileNotFoundException { |
| | | String rootPath = "D:\\项目\\支付宝小程序\\密匙\\天天免费抽奖\\证书\\"; |
| | | return new AlipayCertInfo(new FileInputStream(rootPath + "应用公钥证书_appCertPublicKey_2021002111652055.crt"), new FileInputStream(rootPath + "支付宝公钥证书_alipayCertPublicKey_RSA2.crt"), new FileInputStream(rootPath + "支付宝根证书_alipayRootCert.crt")); |
| | | |
| | | } |
| | | |
| | | private static AlipayAppInfo getAppInfo() { |
| | | String privateKey = "MIIEvwIBADANBgkqhkiG9w0BAQEFAASCBKkwggSlAgEAAoIBAQCYoo2UGR2VYGOIhNg2jvDGYuilR1aHH6kz6HbYVuFbpCwfnONkvcY5TMw0oDrzKceHGZOt04gPcQdTacIZs9cMQwUapy/735xW6Ix7QihvWlqjCxBa5o0LN2cXbsaF06a4TUxuNrXZwk32k3uuwHZdZ+EsI1C5J9wN1B2ltDIwniSk7RTEFNEWpgKtdAd25KLkElUFXAYK2aWZbQ/ogPAPc2wSpj0fmLvUB5OZLvW/PMFIsAA+63lrlcAe+SksCTJkFsKg7uWzAnpPVrrVkbCKVGsMd1Xi29OSWWJfFch3TJ+mmkOIsKA4f1Zf99rsGdODXMqHqeba+pG6vaZ/zzbbAgMBAAECggEAYGUAftBhw43NLlGfbs1VOCUNAhyMg+e150dfjv9HcaW8C/b8EQpc3qEmsfGjaSAwl5zKjFP0MLRUSRSYdb0DCgZledCuWCZ/SM+ysME2YiSw6xk4qf/CgZvqqnzWOXxSZUDEDTLWJAE3VYsM6G86EYFiToq7F0QaEE2ur5Cexd6Owif9A/ruvOAmbtMfMeKTvsfmNQHT2N5eUYNf1HvvqogKjYuhyz7D3LH51rCXpmKPxo3ohKFL4eBNO42VhvC+78jAfICsWm5l2x706iWGRv6wofifR/rIpnEsIa7RyTENihR2yI77as5vWqJy7J+BZHNtGnBec4FMqQpbdUkWqQKBgQDdbPKm4AA0IN7kSqtVBhxLhK8U5xXlQi3qpWZq0w9jAo1iZJCiy2j0G8dEnLxT40zZlj6tAe5n0p4lUP4q0zpQTfHQcJHoqCX9h09BujOds4Q4AKJSAv61IxrNreZ8A0LktrvpqmjiTDpfo0ASA6hTBsBR3lbzsEb0rx/Kc7AYhwKBgQCwd9YEDLe81Wxymp1QVKD+RCGzGIo/LHljPDWYQBRoJxUZwglo4Pibeu6Ud3zXinB7fftKzFtb9hpjw2R5y2AQOAEOyBeCwpBVT25TrF767halKM7qOhGNox74h1Rmn3P7R+8FY/NQVaRFVqgmuyo3mB/K3rfU2pRwXhDD9UhIDQKBgQDSUslOX1lyKoLtoEuMMIix5u+TUElJAgqxZriY4bAs4ODBRDrql4Iy1Sv7bVGp8S8vQvp3xWzaK4JSedNjhE5QtyjpT/QYZZfocR2ElkSvGDSQKXIFqpicICM2mWdkBokEzNpDgr2rrrKH435ZXiYF71TArwwy3w8XHj15RVhQgwKBgQCqU4BmeEAAK595HnMozgZ2ZRyB2PXW8/5WIg843ODVhqN/MSIT8NEEgSs2cf41rJHVIEwkbgrgQDJIiJe6VuppvSSIN+k5dMILFZfPe1hRquyAjVrqCRJYO1HxX+2+/7jamRvDgC2hJIhhxLqYZ57f+hz1mvKkqDUOIDzeZylVIQKBgQCP6gZtZ8eCM3jx5Z6gpgHFdvAIcwJVdRep4dr2f1aHZ/f1sy45UxOoFIgWJt2IWo5jPRBWDPeImN9w8gLSkJSSIS1yFWkoXCoDXSF0aO+61I9S1vJf383HawBgLBrVMG7dOUA+8PTFJCt4o6NQ7weMPH9xzgMkFB6g+MQYRYIelA=="; |
| | | String alipayPublicKey = "MIIDrDCCApSgAwIBAgIQICARGS8npSJhwLjG7UhKEDANBgkqhkiG9w0BAQsFADCBgjELMAkGA1UEBhMCQ04xFjAUBgNVBAoMDUFudCBGaW5hbmNpYWwxIDAeBgNVBAsMF0NlcnRpZmljYXRpb24gQXV0aG9yaXR5MTkwNwYDVQQDDDBBbnQgRmluYW5jaWFsIENlcnRpZmljYXRpb24gQXV0aG9yaXR5IENsYXNzIDIgUjEwHhcNMjAxMTE5MDM0NzEwWhcNMjIxMTE5MDM0NzEwWjCBjDELMAkGA1UEBhMCQ04xJzAlBgNVBAoMHumHjeW6huW/q+ecgeenkeaKgOaciemZkOWFrOWPuDEPMA0GA1UECwwGQWxpcGF5MUMwQQYDVQQDDDrmlK/ku5jlrp0o5Lit5Zu9Kee9kee7nOaKgOacr+aciemZkOWFrOWPuC0yMDg4NDMxMjExNDgzNDg1MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAok8S9kzeLpZGvPX/g4tcYuYZU3Kcot4vx3seU0lS52DgtsIi/4TvBVOj55vNRSJkmJV7GtUeMpgpJSmf3kyNlN9X1POyWxotSN495wAgxOIe8848F/BJekkm/L50v1QScsVxRr10R7Fj3GvsX9bwdmKMfPjYcLJiPsH/rMynAP9BdqN6xTDeoE9bLvZeIVhA4rEQOVZcwoi0fPtyWuUwviSGG/DVcMewF5GYNM0XFxUghUDkjRdFrd5Ew42rCXbVZF2BuESdilN+xZb39N9hxwo1GPpo1p76Ut+yUUl5ZlKqJn1tQ4sjVDtmBFuyNjLjad4ocdSU4sc2NPoMgUsfEQIDAQABoxIwEDAOBgNVHQ8BAf8EBAMCA/gwDQYJKoZIhvcNAQELBQADggEBAFijTHijQKuai1jGckt2V7TuOIJ9UE0iHkMSETE35XZoEm19JeBnOtNSLr8a1ngmDxcDQ+szrMsw/Z7BmxxGzxJRmADrpkVaQ/2+4gp9+CBRzI4YBPkxgogUsmON6DFnwZPFe35imszPvV0y3kPf7tKJSDXVeV8HLcOOJa0rd1kyjXYXmNG5XrXR2MdYf+py6sOvNGttb3HTq2P+3FmcUrxNp8yK1KH2vnewAp5BBg2OfSkW9R2fKNlzqsUI3OVLWfOgnGHBkR/PL0VKyP479BJtiWmkN4Ccc7P9rx1R5ro+bkqfPdfDMpchW5A5TlQOKuzzt6glqQuVrJqBjjzvj3M="; |
| | | return new AlipayAppInfo("2021002111652055", privateKey, alipayPublicKey); |
| | | AlipayAppInfo app = new AlipayAppInfo("2021002111652055", privateKey, alipayPublicKey); |
| | | try { |
| | | app.setCertInfo(getCertInfo()); |
| | | } catch (FileNotFoundException e) { |
| | | e.printStackTrace(); |
| | | } |
| | | return app; |
| | | } |
| | | |
| | | |
| | | private static void getUserId() { |
| | | try { |
| | | String userId = AlipayXcxUtil.getUserId(getAppInfo(), "edac277c44954270a9bcfd210fc8YX77"); |
| | | System.out.println(userId); |
| | | AlipayUserInfo userInfo = AlipayXcxUtil.getAlipayUserInfo(getAppInfo(), "dc8b30a62fce455ea533326d4ba4YF77"); |
| | | System.out.println(userInfo); |
| | | } catch (Exception e) { |
| | | e.printStackTrace(); |
| | | } |
| | |
| | | } |
| | | |
| | | public static void main(String[] args) { |
| | | getOrderDetail(); |
| | | getUserId(); |
| | | } |
| | | |
| | | |
| | |
| | | package com.yeshi.utils; |
| | | |
| | | import org.springframework.data.annotation.Transient; |
| | | import org.yeshi.utils.mybatis.Column; |
| | | import org.yeshi.utils.mybatis.MyBatisMapperUtil; |
| | | import org.yeshi.utils.mybatis.Table; |
| | | import org.yeshi.utils.generater.mybatis.Column; |
| | | import org.yeshi.utils.generater.mybatis.MyBatisMapperUtil; |
| | | import org.yeshi.utils.generater.mybatis.Table; |
| | | |
| | | import java.util.Date; |
| | | |