From d1e50ef7ecd095188a1964be01e6c6746454a20d Mon Sep 17 00:00:00 2001
From: lsy <18332763730@163.com>
Date: Mon, 31 Jul 2017 08:34:31 +0800
Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0mybatis=E6=A8=A1=E6=9D=BF?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
bash.sh | 1 +
bash/docker_shipyarn_run.sh | 5 +
bash/docker_shipyarn_stop.sh | 5 +
code_check.xml | 235 ++++++++++++++++++
common/.gitignore | 32 +++
common/pom.xml | 137 ++++++++++
.../micro/fast/common/CommonApplication.java | 15 ++
.../com/micro/fast/common/util/AESUtil.java | 147 +++++++++++
.../com/micro/fast/common/util/JdbcUtil.java | 89 +++++++
.../common/util/MybatisGeneratorUtil.java | 173 +++++++++++++
.../micro/fast/common/util/StringUtil.java | 157 ++++++++++++
.../micro/fast/common/util/VelocityUtil.java | 68 +++++
.../src/main/resources/application.properties | 0
.../main/resources/templates/Controller.vm | 25 ++
.../src/main/resources/templates/Service.vm | 13 +
.../main/resources/templates/ServiceImpl.vm | 29 +++
.../main/resources/templates/ServiceMock.vm | 14 ++
.../resources/templates/generatorConfig.vm | 74 ++++++
.../fast/common/CommonApplicationTests.java | 16 ++
docker-compose.yml | 1 +
.../server/EurekaServerApplication.java | 7 +-
.../server2/EurekaServer2Application.java | 8 +-
.../com/micro/fast/upms/UpmsApplication.java | 6 +-
23 files changed, 1247 insertions(+), 10 deletions(-)
create mode 100644 bash.sh
create mode 100644 bash/docker_shipyarn_run.sh
create mode 100644 bash/docker_shipyarn_stop.sh
create mode 100644 code_check.xml
create mode 100644 common/.gitignore
create mode 100644 common/pom.xml
create mode 100644 common/src/main/java/com/micro/fast/common/CommonApplication.java
create mode 100644 common/src/main/java/com/micro/fast/common/util/AESUtil.java
create mode 100644 common/src/main/java/com/micro/fast/common/util/JdbcUtil.java
create mode 100644 common/src/main/java/com/micro/fast/common/util/MybatisGeneratorUtil.java
create mode 100644 common/src/main/java/com/micro/fast/common/util/StringUtil.java
create mode 100644 common/src/main/java/com/micro/fast/common/util/VelocityUtil.java
create mode 100644 common/src/main/resources/application.properties
create mode 100644 common/src/main/resources/templates/Controller.vm
create mode 100644 common/src/main/resources/templates/Service.vm
create mode 100644 common/src/main/resources/templates/ServiceImpl.vm
create mode 100644 common/src/main/resources/templates/ServiceMock.vm
create mode 100644 common/src/main/resources/templates/generatorConfig.vm
create mode 100644 common/src/test/java/com/micro/fast/common/CommonApplicationTests.java
diff --git a/bash.sh b/bash.sh
new file mode 100644
index 0000000..f1f641a
--- /dev/null
+++ b/bash.sh
@@ -0,0 +1 @@
+#!/usr/bin/env bash
diff --git a/bash/docker_shipyarn_run.sh b/bash/docker_shipyarn_run.sh
new file mode 100644
index 0000000..fd134e1
--- /dev/null
+++ b/bash/docker_shipyarn_run.sh
@@ -0,0 +1,5 @@
+#!/usr/bin/env bash
+# 开启shipyarnf服务
+echo "准备开始"
+curl -s https://shipyard-project.com/deploy | bash -s
+echo "开启成功"
\ No newline at end of file
diff --git a/bash/docker_shipyarn_stop.sh b/bash/docker_shipyarn_stop.sh
new file mode 100644
index 0000000..6bc72a6
--- /dev/null
+++ b/bash/docker_shipyarn_stop.sh
@@ -0,0 +1,5 @@
+#!/usr/bin/env bash
+# 关闭shipyarnf服务
+echo "准备关闭"
+curl -sSL https://shipyard-project.com/deploy| ACTION=remove bash -s
+echo "关闭成功"
diff --git a/code_check.xml b/code_check.xml
new file mode 100644
index 0000000..d0ccbd2
--- /dev/null
+++ b/code_check.xml
@@ -0,0 +1,235 @@
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
\ No newline at end of file
diff --git a/common/.gitignore b/common/.gitignore
new file mode 100644
index 0000000..7a8343d
--- /dev/null
+++ b/common/.gitignore
@@ -0,0 +1,32 @@
+target/
+!.mvn/wrapper/maven-wrapper.jar
+
+### STS ###
+.apt_generated
+.classpath
+.factorypath
+.project
+.settings
+.springBeans
+
+### IntelliJ IDEA ###
+.idea
+*.iws
+*.iml
+*.ipr
+
+### NetBeans ###
+nbproject/private/
+build/
+nbbuild/
+dist/
+nbdist/
+.nb-gradle/
+
+### maven ###
+target
+out
+.mvn
+mvnw
+mvnw.cmd
+classes
diff --git a/common/pom.xml b/common/pom.xml
new file mode 100644
index 0000000..a607881
--- /dev/null
+++ b/common/pom.xml
@@ -0,0 +1,137 @@
+
+
+ 4.0.0
+
+ com.micro.fast
+ common
+ 0.0.1-SNAPSHOT
+ jar
+
+ common
+ common
+
+
+ org.springframework.boot
+ spring-boot-starter-parent
+ 1.5.4.RELEASE
+
+
+
+
+ UTF-8
+ UTF-8
+ 1.8
+ Dalston.SR2
+ 1.8
+ 1.8
+ 1.8
+
+
+
+
+ org.springframework.boot
+ spring-boot-starter-actuator
+
+
+ org.springframework.cloud
+ spring-cloud-starter-bus-amqp
+
+
+ org.springframework.cloud
+ spring-cloud-starter-config
+
+
+ org.springframework.cloud
+ spring-cloud-starter-eureka
+
+
+ org.springframework.cloud
+ spring-cloud-starter-feign
+
+
+ org.springframework.cloud
+ spring-cloud-starter-hystrix
+
+
+ org.springframework.cloud
+ spring-cloud-starter-ribbon
+
+
+ org.springframework.cloud
+ spring-cloud-starter-sleuth
+
+
+ org.springframework.cloud
+ spring-cloud-starter-zipkin
+
+
+ org.springframework.boot
+ spring-boot-starter-data-redis
+
+
+ org.springframework.boot
+ spring-boot-starter-freemarker
+
+
+ org.springframework.boot
+ spring-boot-starter-web
+
+
+
+ mysql
+ mysql-connector-java
+ runtime
+
+
+ io.zipkin.java
+ zipkin-autoconfigure-ui
+ runtime
+
+
+ org.springframework.boot
+ spring-boot-starter-test
+ test
+
+
+
+ org.mybatis.generator
+ mybatis-generator-core
+ 1.3.5
+
+
+ org.apache.velocity
+ velocity
+ 1.7
+
+
+
+ commons-io
+ commons-io
+ 2.4
+
+
+
+
+
+
+ org.springframework.cloud
+ spring-cloud-dependencies
+ ${spring-cloud.version}
+ pom
+ import
+
+
+
+
+
+
+
+ org.springframework.boot
+ spring-boot-maven-plugin
+
+
+
+
+
+
diff --git a/common/src/main/java/com/micro/fast/common/CommonApplication.java b/common/src/main/java/com/micro/fast/common/CommonApplication.java
new file mode 100644
index 0000000..18aee0a
--- /dev/null
+++ b/common/src/main/java/com/micro/fast/common/CommonApplication.java
@@ -0,0 +1,15 @@
+package com.micro.fast.common;
+
+import org.springframework.boot.SpringApplication;
+import org.springframework.boot.autoconfigure.SpringBootApplication;
+import org.springframework.cloud.client.SpringCloudApplication;
+import org.springframework.cloud.netflix.feign.EnableFeignClients;
+
+@SpringCloudApplication
+@EnableFeignClients
+public class CommonApplication {
+
+ public static void main(String[] args) {
+ SpringApplication.run(CommonApplication.class, args);
+ }
+}
diff --git a/common/src/main/java/com/micro/fast/common/util/AESUtil.java b/common/src/main/java/com/micro/fast/common/util/AESUtil.java
new file mode 100644
index 0000000..a504b19
--- /dev/null
+++ b/common/src/main/java/com/micro/fast/common/util/AESUtil.java
@@ -0,0 +1,147 @@
+package com.micro.fast.common.util;
+
+
+
+import javax.crypto.*;
+import javax.crypto.spec.SecretKeySpec;
+import java.io.IOException;
+import java.io.UnsupportedEncodingException;
+import java.security.InvalidKeyException;
+import java.security.NoSuchAlgorithmException;
+import java.security.SecureRandom;
+
+import sun.misc.BASE64Decoder;
+import sun.misc.BASE64Encoder;
+/**
+ * AES加解密工具类
+ * Created by shuzheng on 2017/2/5.
+ */
+public class AESUtil {
+ /**
+ * 注释 .
+ */
+ private static final String encodeRules = "zheng";
+
+ /**
+ * 加密
+ * 1.构造密钥生成器
+ * 2.根据ecnodeRules规则初始化密钥生成器
+ * 3.产生密钥
+ * 4.创建和初始化密码器
+ * 5.内容加密
+ * 6.返回字符串.
+ */
+ private static String AESEncode(String content) {
+ try {
+ //1.构造密钥生成器,指定为AES算法,不区分大小写
+ KeyGenerator keygen = KeyGenerator.getInstance("AES");
+ //2.根据ecnodeRules规则初始化密钥生成器
+ //生成一个128位的随机源,根据传入的字节数组
+ SecureRandom random = SecureRandom.getInstance("SHA1PRNG");
+ random.setSeed(encodeRules.getBytes());
+ keygen.init(128, random);
+ //3.产生原始对称密钥
+ SecretKey original_key = keygen.generateKey();
+ //4.获得原始对称密钥的字节数组
+ byte[] raw = original_key.getEncoded();
+ //5.根据字节数组生成AES密钥
+ SecretKey key = new SecretKeySpec(raw, "AES");
+ //6.根据指定算法AES自成密码器
+ Cipher cipher = Cipher.getInstance("AES");
+ //7.初始化密码器,第一个参数为加密(Encrypt_mode)或者解密解密(Decrypt_mode)操作,第二个参数为使用的KEY
+ cipher.init(Cipher.ENCRYPT_MODE, key);
+ //8.获取加密内容的字节数组(这里要设置为utf-8)不然内容中如果有中文和英文混合中文就会解密为乱码
+ byte[] byte_encode = content.getBytes("utf-8");
+ //9.根据密码器的初始化方式--加密:将数据加密
+ byte[] byte_AES = cipher.doFinal(byte_encode);
+ //10.将加密后的数据转换为字符串
+ //这里用Base64Encoder中会找不到包
+ //解决办法:
+ //在项目的Build path中先移除JRE System Library,再添加库JRE System Library,重新编译后就一切正常了。
+ String AES_encode = new String(new BASE64Encoder().encode(byte_AES));
+ //11.将字符串返回
+ return AES_encode;
+ } catch (NoSuchAlgorithmException e) {
+ e.printStackTrace();
+ } catch (NoSuchPaddingException e) {
+ e.printStackTrace();
+ } catch (InvalidKeyException e) {
+ e.printStackTrace();
+ } catch (IllegalBlockSizeException e) {
+ e.printStackTrace();
+ } catch (BadPaddingException e) {
+ e.printStackTrace();
+ } catch (UnsupportedEncodingException e) {
+ e.printStackTrace();
+ }
+ //如果有错就返加nulll
+ return null;
+ }
+
+ /**
+ * 解密
+ * 解密过程:
+ * 1.同加密1-4步
+ * 2.将加密后的字符串反纺成byte[]数组
+ * 3.将加密内容解密
+ */
+ public static String AESDecode(String content) {
+ try {
+ //1.构造密钥生成器,指定为AES算法,不区分大小写
+ KeyGenerator keygen = KeyGenerator.getInstance("AES");
+ //2.根据ecnodeRules规则初始化密钥生成器
+ //生成一个128位的随机源,根据传入的字节数组
+ SecureRandom random = SecureRandom.getInstance("SHA1PRNG");
+ random.setSeed(encodeRules.getBytes());
+ keygen.init(128, random);
+ //3.产生原始对称密钥
+ SecretKey original_key = keygen.generateKey();
+ //4.获得原始对称密钥的字节数组
+ byte[] raw = original_key.getEncoded();
+ //5.根据字节数组生成AES密钥
+ SecretKey key = new SecretKeySpec(raw, "AES");
+ //6.根据指定算法AES自成密码器
+ Cipher cipher = Cipher.getInstance("AES");
+ //7.初始化密码器,第一个参数为加密(Encrypt_mode)或者解密(Decrypt_mode)操作,第二个参数为使用的KEY
+ cipher.init(Cipher.DECRYPT_MODE, key);
+ //8.将加密并编码后的内容解码成字节数组
+ byte[] byte_content = new BASE64Decoder().decodeBuffer(content);
+ /*
+ * 解密
+ */
+ byte[] byte_decode = cipher.doFinal(byte_content);
+ String AES_decode = new String(byte_decode, "utf-8");
+ return AES_decode;
+ } catch (NoSuchAlgorithmException e) {
+ e.printStackTrace();
+ } catch (NoSuchPaddingException e) {
+ e.printStackTrace();
+ } catch (InvalidKeyException e) {
+ e.printStackTrace();
+ } catch (IOException e) {
+ e.printStackTrace();
+ } catch (IllegalBlockSizeException e) {
+ throw new RuntimeException("兄弟,配置文件中的密码需要使用AES加密,请使用com.zheng.common.util.AESUtil工具类修改这些值!");
+ //e.printStackTrace();
+ } catch (BadPaddingException e) {
+ e.printStackTrace();
+ }
+ //如果有错就返加nulll
+ return null;
+ }
+
+ public static void main(String[] args) {
+ String[] keys = {
+ "", "123456"
+ };
+ System.out.println("key | AESEncode | AESDecode");
+ for (String key : keys) {
+ System.out.print(key + " | ");
+ String encryptString = AESEncode(key);
+ System.out.print(encryptString + " | ");
+ String decryptString = AESDecode(encryptString);
+ System.out.println(decryptString);
+ }
+ }
+
+}
diff --git a/common/src/main/java/com/micro/fast/common/util/JdbcUtil.java b/common/src/main/java/com/micro/fast/common/util/JdbcUtil.java
new file mode 100644
index 0000000..8cebb93
--- /dev/null
+++ b/common/src/main/java/com/micro/fast/common/util/JdbcUtil.java
@@ -0,0 +1,89 @@
+package com.micro.fast.common.util;
+
+import java.sql.*;
+import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+
+/**
+ * JDBC工具类
+ * Created by ZhangShuzheng on 2017/1/10.
+ */
+public class JdbcUtil {
+
+ // 定义数据库的链接
+ private Connection conn;
+ // 定义sql语句的执行对象
+ private PreparedStatement pstmt;
+ // 定义查询返回的结果集合
+ private ResultSet rs;
+
+ // 初始化
+ public JdbcUtil(String driver, String url, String username, String password) {
+ try {
+ Class.forName(driver);
+ conn = DriverManager.getConnection(url, username, password);
+ System.out.println("数据库连接成功");
+ } catch (Exception e) {
+ e.printStackTrace();
+ }
+ }
+
+ // 更新数据
+ public boolean updateByParams(String sql, List params) throws SQLException {
+ // 影响行数
+ int result = -1;
+ pstmt = conn.prepareStatement(sql);
+ int index = 1;
+ // 填充sql语句中的占位符
+ if (null != params && !params.isEmpty()) {
+ for (int i = 0; i < params.size(); i ++) {
+ pstmt.setObject(index ++, params.get(i));
+ }
+ }
+ result = pstmt.executeUpdate();
+ return result > 0 ? true : false;
+ }
+
+ // 查询多条记录
+ public List