-
Notifications
You must be signed in to change notification settings - Fork 17
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
a5241c3
commit 659be91
Showing
6 changed files
with
139 additions
and
23 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
package main | ||
|
||
import ( | ||
"crypto/rand" | ||
"crypto/rsa" | ||
"crypto/x509" | ||
"encoding/base64" | ||
"fmt" | ||
) | ||
|
||
func main() { | ||
|
||
// Java KeyPairGenerator 生成的密钥对 | ||
publicKeyStr := "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAvyKd9UmWRG1/maRgYhdaetwWcRZ/TQMVeI/dL4/dqmVqWgv5VQ8r97thDxyrVyqQBWxfut1yOwc9HA10T62tuxD1x/MD9uU4pGgk5CbiGMEYylJ3OJWVzFGpcSlCuc52ZB95o+Ao2UPxn5t5Fd4ulOC4uLPBrWI2pnZo80iU13YfWPVZNTnqAMQuJhsF9G8wrpTPP5a/92+AJLj6/sQC5AdLZRr8sHp9LQ7hU6Nk8nPM9S/4ciXrJFRcx6+NHAzo6c0geDMzjb/enzDaICaWujXkrljCPRCOaXdgjU7PTMNQjvhEWX7jK9hvcBztgPwsmysJOoDUtebzsEpEJJwrFwIDAQAB" | ||
privateKeyStr := "MIIEvQIBADANBgkqhkiG9w0BAQEFAASCBKcwggSjAgEAAoIBAQC/Ip31SZZEbX+ZpGBiF1p63BZxFn9NAxV4j90vj92qZWpaC/lVDyv3u2EPHKt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uhDALAWcedmXWJV+dr+cC6fLZvb92rcQQfRLA54cgfcIhvDbarq+zys2ZX3byhD6ACPtKTRAVnRLedTwDqswKBgHqWGOlxtDR/Kl9tSAsutTxuQqGQhoNT3DCDahkRTEeIbsfXHTEu9D4yRIVx9/ctxNNohWtq1CfOs7DbYypyJcBsZBopUIpljn5gamAIiz6Ekbn+eu5MRwbCYQXXJPAmUt7EwQiclipjE6Jzcb//tYNnlPh8MUuyDUJJ+yrx6xX5AoGAUgozGJsnDtE8qWMIDhbY7J7di0CsSMsFiWR3roJCgH3KkyftXGNtOwOjlyn6POeXbemFKE9ESGqVjTHZ7wuGO6tDB7gqJ3+cBFaRq2B827OvJ7W1DUo2gMfa1w7kIPwz4AfLaQ/wnpqx7O1kcNfeYAn/5Lf5bX+abjutRTYGdCA=" | ||
|
||
// Base64 解码 | ||
publicKeyBytes, err := base64.StdEncoding.DecodeString(publicKeyStr) | ||
if err != nil { | ||
fmt.Printf("解码公钥失败 err=%s\n", err.Error()) | ||
return | ||
} | ||
|
||
privateKeyBytes, err := base64.StdEncoding.DecodeString(privateKeyStr) | ||
if err != nil { | ||
fmt.Printf("解码私钥失败 err=%s\n", err.Error()) | ||
return | ||
} | ||
|
||
// 解析为 RSA 公钥 & 私钥 | ||
publicKey, err := x509.ParsePKIXPublicKey(publicKeyBytes) | ||
if err != nil { | ||
fmt.Printf("解析公钥失败 err=%s\n", err.Error()) | ||
return | ||
} | ||
|
||
rsaPublicKey := publicKey.(*rsa.PublicKey) | ||
|
||
privateKey, err := x509.ParsePKCS8PrivateKey(privateKeyBytes) | ||
if err != nil { | ||
fmt.Printf("解析公钥失败 err=%s\n", err.Error()) | ||
return | ||
} | ||
rsaPrivateKey := privateKey.(*rsa.PrivateKey) | ||
|
||
// 公钥加密 & 私钥解密 | ||
|
||
cypher, err := rsa.EncryptPKCS1v15(rand.Reader, rsaPublicKey, []byte("Hello World")) | ||
if err != nil { | ||
fmt.Printf("公钥加密失败 err=%s\n", err.Error()) | ||
return | ||
} | ||
fmt.Printf("密文:%s\n", base64.StdEncoding.EncodeToString(cypher)) | ||
|
||
raw, err := rsa.DecryptPKCS1v15(rand.Reader, rsaPrivateKey, cypher) | ||
if err != nil { | ||
fmt.Printf("私钥解密失败 err=%s\n", err.Error()) | ||
return | ||
} | ||
|
||
fmt.Printf("原文:%s\n", string(raw)) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
package app.test; | ||
|
||
import java.sql.Connection; | ||
import java.sql.DriverManager; | ||
import java.sql.PreparedStatement; | ||
import java.sql.ResultSet; | ||
|
||
public class ApplicationMainTest { | ||
|
||
public static void main(String[] args) throws Exception { | ||
|
||
|
||
// 注意,流式查询,连接对象不能公用,也就是说在迭代期间,不能把连接用于其他查询 | ||
try (Connection connection = DriverManager.getConnection( | ||
"jdbc:mysql://localhost:3306/demo?useUnicode=true&characterEncoding=utf8&autoReconnect=true&allowMultiQueries=true", "root", "root")) { | ||
|
||
// 设置查询语句,和 ResultSet 设置 | ||
// ResultSet.TYPE_FORWARD_ONLY:只允许结果集的游标向下移动。 | ||
// ResultSet.CONCUR_READ_ONLY:游标只读 | ||
PreparedStatement statement = connection.prepareStatement("SELECT * FROM sku ORDER BY id", ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_READ_ONLY); | ||
|
||
// 一定要把 FetchSize 设置为 nteger.MIN_VALUE, 在执行 ResultSet.next() 方法时, 会通过数据库连接一条一条的返回, 这样也不会大量占用客户端的内存. | ||
// MySQL 驱动实现做了特殊的判断 | ||
statement.setFetchSize(Integer.MIN_VALUE); | ||
|
||
|
||
// 开始流式查询,迭代每一条记录 | ||
try(ResultSet resultSet = statement.executeQuery()){ | ||
while (resultSet.next()) { | ||
Long id = resultSet.getLong("id"); | ||
System.out.println(id); | ||
} | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
--------------------------- | ||
流式读取 | ||
--------------------------- | ||
|
||
|
||
@Select("select * from xxx order by xx desc") | ||
@Options(resultSetType = ResultSetType.FORWARD_ONLY, fetchSize = Integer.MIN_VALUE) | ||
@ResultType(XxxObject.class) | ||
void queryStreamResult(ResultHandler<XxxObject> handler); | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
------------------------ | ||
流式检索 | ||
------------------------ | ||
|
||
import static org.hibernate.jpa.QueryHints.HINT_FETCH_SIZE; | ||
|
||
// AvailableHints.HINT_FETCH_SIZE | ||
@QueryHints(value = @QueryHint(name = HINT_FETCH_SIZE, value = "" + Integer.MIN_VALUE)) | ||
@Query(value = "select t from Todo t") | ||
Stream<Todo> streamAll(); |