├── .gitignore ├── LICENSE ├── README.md ├── pom.xml └── src ├── main ├── java │ └── com │ │ └── dtmcli │ │ └── java │ │ └── sample │ │ ├── DtmcliJavaSampleApplication.java │ │ ├── controller │ │ ├── SagaTestController.java │ │ ├── TccTestController.java │ │ ├── TransBarrierController.java │ │ └── TransController.java │ │ ├── param │ │ └── TransReq.java │ │ └── util │ │ └── DataSourceUtil.java └── resources │ └── application.yml └── test └── java └── com └── dtmcli └── java └── sample └── DtmcliJavaExamplesApplicationTests.java /.gitignore: -------------------------------------------------------------------------------- 1 | # Compiled class file 2 | *.class 3 | 4 | # Log file 5 | *.log 6 | 7 | # BlueJ files 8 | *.ctxt 9 | 10 | # Mobile Tools for Java (J2ME) 11 | .mtj.tmp/ 12 | 13 | # Package Files # 14 | *.jar 15 | *.war 16 | *.nar 17 | *.ear 18 | *.zip 19 | *.tar.gz 20 | *.rar 21 | 22 | # virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml 23 | hs_err_pid* 24 | .idea 25 | /target 26 | /dtmcli-java-sample.iml 27 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2021 yedf 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | ### 快速开始 2 | 3 | #### 安装运行dtm 4 | 5 | 参考[dtm安装运行](https://dtm.pub/guide/install.html) 6 | 7 | #### 运行示例 8 | 运行服务 9 | 10 | ``` 11 | mvn package && java -jar target/dtmcli-java-sample-0.0.1-SNAPSHOT.jar 12 | ``` 13 | 14 | 触发成功的TCC事务 15 | ``` 16 | curl localhost:8081/tccBarrier 17 | ``` 18 | 19 | 触发回滚的TCC事务 20 | ``` 21 | curl localhost:8081/tccBarrierError 22 | ``` 23 | -------------------------------------------------------------------------------- /pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 4.0.0 5 | 6 | 7 | org.springframework.boot 8 | spring-boot-starter-parent 9 | 2.5.4 10 | 11 | 12 | 13 | com.github.viticis 14 | dtmcli-java-sample 15 | 0.0.1-SNAPSHOT 16 | dtmcli-java-sample 17 | dtmcli-java-sample 18 | 19 | 20 | 1.8 21 | v1.5.4 22 | 23 | 24 | 25 | 26 | org.springframework.boot 27 | spring-boot-starter-web 28 | 29 | 30 | mysql 31 | mysql-connector-java 32 | 33 | 34 | org.springframework.boot 35 | spring-boot-devtools 36 | runtime 37 | true 38 | 39 | 40 | org.springframework.boot 41 | spring-boot-starter-test 42 | test 43 | 44 | 45 | io.github.dtm-labs 46 | dtmcli-java 47 | 2.1.4 48 | 49 | 50 | 51 | 52 | 53 | 54 | org.springframework.boot 55 | spring-boot-maven-plugin 56 | 57 | 58 | 59 | 60 | 61 | -------------------------------------------------------------------------------- /src/main/java/com/dtmcli/java/sample/DtmcliJavaSampleApplication.java: -------------------------------------------------------------------------------- 1 | package com.dtmcli.java.sample; 2 | 3 | import org.springframework.boot.SpringApplication; 4 | import org.springframework.boot.autoconfigure.SpringBootApplication; 5 | 6 | @SpringBootApplication 7 | public class DtmcliJavaSampleApplication { 8 | 9 | public static void main(String[] args) { 10 | SpringApplication.run(DtmcliJavaSampleApplication.class, args); 11 | } 12 | 13 | } -------------------------------------------------------------------------------- /src/main/java/com/dtmcli/java/sample/controller/SagaTestController.java: -------------------------------------------------------------------------------- 1 | package com.dtmcli.java.sample.controller; 2 | 3 | import lombok.extern.slf4j.Slf4j; 4 | import org.springframework.beans.factory.annotation.Value; 5 | import org.springframework.web.bind.annotation.RequestMapping; 6 | import org.springframework.web.bind.annotation.RestController; 7 | import pub.dtm.client.DtmClient; 8 | import pub.dtm.client.saga.Saga; 9 | 10 | import java.util.UUID; 11 | 12 | @RestController 13 | @RequestMapping(("api")) 14 | @Slf4j 15 | public class SagaTestController { 16 | 17 | private static final String svc = "http://localhost:8081/api"; 18 | 19 | @Value("${dtm.ipport}") 20 | private String ipPort; 21 | 22 | /** 23 | * normal saga demo 24 | * 25 | * @return 26 | */ 27 | @RequestMapping("testSaga") 28 | public String testSage() { 29 | 30 | DtmClient dtmClient = new DtmClient(ipPort); 31 | 32 | try { 33 | // create saga transaction 34 | String customGid = UUID.randomUUID().toString(); 35 | Saga saga = dtmClient 36 | .newSaga(customGid) 37 | .add(svc + "/TransOut", svc + "/TransOutCompensate", "") 38 | .add(svc + "/TransIn", svc + "/TransInCompensate", "") 39 | .enableWaitResult(); 40 | 41 | saga.submit(); 42 | } catch (Exception e) { 43 | log.error("saga submit error", e); 44 | return "fail"; 45 | } 46 | return "success"; 47 | } 48 | } 49 | -------------------------------------------------------------------------------- /src/main/java/com/dtmcli/java/sample/controller/TccTestController.java: -------------------------------------------------------------------------------- 1 | package com.dtmcli.java.sample.controller; 2 | 3 | import com.dtmcli.java.sample.param.TransReq; 4 | import lombok.extern.slf4j.Slf4j; 5 | import okhttp3.Response; 6 | import org.springframework.beans.factory.annotation.Value; 7 | import org.springframework.web.bind.annotation.RequestMapping; 8 | import org.springframework.web.bind.annotation.RestController; 9 | import pub.dtm.client.DtmClient; 10 | import pub.dtm.client.tcc.Tcc; 11 | 12 | import java.util.UUID; 13 | 14 | 15 | @RestController 16 | @RequestMapping(("api")) 17 | @Slf4j 18 | public class TccTestController { 19 | 20 | private static final String svc = "http://localhost:8081/api"; 21 | 22 | @Value("${dtm.ipport}") 23 | private String endpoint; 24 | 25 | /** 26 | * 常规tcc demo 27 | * 28 | * @return 29 | */ 30 | @RequestMapping("testTcc") 31 | public String testTcc() { 32 | //创建dtm clinet 33 | DtmClient dtmClient = new DtmClient(endpoint); 34 | //创建tcc事务 35 | String customGid = UUID.randomUUID().toString(); 36 | try { 37 | dtmClient.tccGlobalTransaction(customGid, TccTestController::tccTrans); 38 | } catch (Exception e) { 39 | log.error("tccGlobalTransaction error", e); 40 | return "fail"; 41 | } 42 | return "success"; 43 | } 44 | 45 | 46 | /** 47 | * 具有子事务屏障功能的tcc demo (转账成功) 48 | * 49 | * @return 50 | */ 51 | @RequestMapping("tccBarrier") 52 | public String tccBarrier() { 53 | // 创建dmt client 54 | DtmClient dtmClient = new DtmClient(endpoint); 55 | //创建tcc事务 56 | String customGid = UUID.randomUUID().toString(); 57 | try { 58 | dtmClient.tccGlobalTransaction(customGid, TccTestController::tccBarrierTrans); 59 | } catch (Exception e) { 60 | log.error("tccGlobalTransaction error", e); 61 | return "fail"; 62 | } 63 | return "success"; 64 | } 65 | 66 | /** 67 | * 具有子事务屏障功能的tcc demo (转账失败) 68 | * 69 | * @return 70 | */ 71 | @RequestMapping("tccBarrierError") 72 | public String tccBarrierError() { 73 | // 创建dmt client 74 | DtmClient dtmClient = new DtmClient(endpoint); 75 | //创建tcc事务 76 | String customGid = UUID.randomUUID().toString(); 77 | try { 78 | dtmClient.tccGlobalTransaction(customGid, TccTestController::tccBarrierTransError); 79 | } catch (Exception e) { 80 | log.error("tccGlobalTransaction error", e); 81 | return "fail"; 82 | } 83 | return "success"; 84 | } 85 | 86 | 87 | /** 88 | * 定义tcc事务函数,内部需要通过callBranch注册事务子分支 89 | * 90 | * @param tcc 91 | * @return 92 | * @see TransController 93 | */ 94 | public static void tccTrans(Tcc tcc) throws Exception { 95 | Response outResponse = tcc 96 | .callBranch("", svc + "/TransOutTry", svc + "/TransOutConfirm", svc + "/TransOutCancel"); 97 | log.info("outResponse:{}", outResponse); 98 | Response inResponse = tcc.callBranch("", svc + "/TransInTry", svc + "/TransInConfirm", svc + "/TransInCancel"); 99 | log.info("inResponse:{}", inResponse); 100 | } 101 | 102 | 103 | /** 104 | * 定义tcc事务函数,内部需要通过callBranch注册事务子分支 105 | * 106 | * @param tcc 107 | * @return 108 | * @see TransBarrierController 109 | */ 110 | public static void tccBarrierTrans(Tcc tcc) throws Exception { 111 | // 用户1 转出30元 112 | Response outResponse = tcc 113 | .callBranch(new TransReq(1, -30), svc + "/barrierTransOutTry", svc + "/barrierTransOutConfirm", 114 | svc + "/barrierTransOutCancel"); 115 | log.info("outResponse:{}", outResponse); 116 | 117 | // 用户2 转入30元 118 | Response inResponse = tcc 119 | .callBranch(new TransReq(2, 30), svc + "/barrierTransInTry", svc + "/barrierTransInConfirm", 120 | svc + "/barrierTransInCancel"); 121 | log.info("inResponse:{}", inResponse); 122 | 123 | } 124 | 125 | /** 126 | * 定义tcc事务函数,内部需要通过callBranch注册事务子分支, 转账金额大于余额 转账失败 127 | * 128 | * @param tcc 129 | * @return 130 | * @see TransBarrierController 131 | */ 132 | public static void tccBarrierTransError(Tcc tcc) throws Exception { 133 | // 用户1 转出100000元 134 | Response outResponse = tcc 135 | .callBranch(new TransReq(1, -100000), svc + "/barrierTransOutTry", svc + "/barrierTransOutConfirm", 136 | svc + "/barrierTransOutCancel"); 137 | log.info("outResponse:{}", outResponse); 138 | 139 | // 用户2 转入100000元 140 | Response inResponse = tcc 141 | .callBranch(new TransReq(2, 100000), svc + "/barrierTransInTry", svc + "/barrierTransInConfirm", 142 | svc + "/barrierTransInCancel"); 143 | log.info("inResponse:{}", inResponse); 144 | 145 | } 146 | } -------------------------------------------------------------------------------- /src/main/java/com/dtmcli/java/sample/controller/TransBarrierController.java: -------------------------------------------------------------------------------- 1 | package com.dtmcli.java.sample.controller; 2 | 3 | import com.dtmcli.java.sample.param.TransReq; 4 | import com.dtmcli.java.sample.util.DataSourceUtil; 5 | import lombok.extern.slf4j.Slf4j; 6 | import org.slf4j.Logger; 7 | import org.slf4j.LoggerFactory; 8 | import org.springframework.beans.factory.annotation.Autowired; 9 | import org.springframework.util.StreamUtils; 10 | import org.springframework.web.bind.annotation.RequestMapping; 11 | import org.springframework.web.bind.annotation.RestController; 12 | import pub.dtm.client.barrier.BranchBarrier; 13 | import pub.dtm.client.constant.Constants; 14 | import pub.dtm.client.exception.FailureException; 15 | import pub.dtm.client.model.responses.DtmResponse; 16 | import pub.dtm.client.utils.JsonUtils; 17 | 18 | import javax.servlet.http.HttpServletRequest; 19 | import java.io.IOException; 20 | import java.sql.Connection; 21 | import java.sql.PreparedStatement; 22 | import java.sql.SQLException; 23 | 24 | @RestController 25 | @RequestMapping("api") 26 | @Slf4j 27 | public class TransBarrierController { 28 | 29 | Logger logger = LoggerFactory.getLogger(TransBarrierController.class); 30 | 31 | @Autowired 32 | private DataSourceUtil dataSourceUtil; 33 | 34 | 35 | @RequestMapping("barrierTransOutTry") 36 | public Object TransOutTry(HttpServletRequest request) throws Exception { 37 | 38 | BranchBarrier branchBarrier = new BranchBarrier(request.getParameterMap()); 39 | logger.info("barrierTransOutTry branchBarrier:{}", branchBarrier); 40 | 41 | TransReq transReq = extracted(request); 42 | Connection connection = dataSourceUtil.getConnecion(); 43 | branchBarrier.call(connection, (barrier) -> { 44 | System.out.println("用户: +" + transReq.getUserId() + ",转出" + Math.abs(transReq.getAmount()) + "元准备"); 45 | this.adjustTrading(connection, transReq); 46 | }); 47 | connection.close(); 48 | return DtmResponse.buildDtmResponse(Constants.SUCCESS_RESULT); 49 | } 50 | 51 | 52 | @RequestMapping("barrierTransOutConfirm") 53 | public Object TransOutConfirm(HttpServletRequest request) throws Exception { 54 | BranchBarrier branchBarrier = new BranchBarrier(request.getParameterMap()); 55 | logger.info("barrierTransOutConfirm branchBarrier:{}", branchBarrier); 56 | Connection connection = dataSourceUtil.getConnecion(); 57 | TransReq transReq = extracted(request); 58 | branchBarrier.call(connection, (barrier) -> { 59 | System.out.println("用户: +" + transReq.getUserId() + ",转出" + Math.abs(transReq.getAmount()) + "元提交"); 60 | adjustBalance(connection, transReq); 61 | }); 62 | connection.close(); 63 | return DtmResponse.buildDtmResponse(Constants.SUCCESS_RESULT); 64 | } 65 | 66 | @RequestMapping("barrierTransOutCancel") 67 | public Object TransOutCancel(HttpServletRequest request) throws Exception { 68 | BranchBarrier branchBarrier = new BranchBarrier(request.getParameterMap()); 69 | logger.info("barrierTransOutCancel branchBarrier:{}", branchBarrier); 70 | TransReq transReq = extracted(request); 71 | Connection connection = dataSourceUtil.getConnecion(); 72 | branchBarrier.call(connection, (barrier) -> { 73 | System.out.println("用户: +" + transReq.getUserId() + ",转出" + Math.abs(transReq.getAmount()) + "元回滚"); 74 | this.adjustTrading(connection, transReq); 75 | }); 76 | connection.close(); 77 | return DtmResponse.buildDtmResponse(Constants.SUCCESS_RESULT); 78 | } 79 | 80 | @RequestMapping("barrierTransInTry") 81 | public Object TransInTry(HttpServletRequest request) throws Exception { 82 | BranchBarrier branchBarrier = new BranchBarrier(request.getParameterMap()); 83 | logger.info("barrierTransInTry branchBarrier:{}", branchBarrier); 84 | Connection connection = dataSourceUtil.getConnecion(); 85 | TransReq transReq = extracted(request); 86 | branchBarrier.call(connection, (barrier) -> { 87 | System.out.println("用户: +" + transReq.getUserId() + ",转入" + Math.abs(transReq.getAmount()) + "元准备"); 88 | this.adjustTrading(connection, transReq); 89 | }); 90 | connection.close(); 91 | return DtmResponse.buildDtmResponse(Constants.SUCCESS_RESULT); 92 | } 93 | 94 | @RequestMapping("barrierTransInConfirm") 95 | public Object TransInConfirm(HttpServletRequest request) throws Exception { 96 | BranchBarrier branchBarrier = new BranchBarrier(request.getParameterMap()); 97 | logger.info("barrierTransInConfirm TransInCancel branchBarrier:{}", branchBarrier); 98 | Connection connection = dataSourceUtil.getConnecion(); 99 | TransReq transReq = extracted(request); 100 | branchBarrier.call(connection, (barrier) -> { 101 | System.out.println("用户: +" + transReq.getUserId() + ",转入" + Math.abs(transReq.getAmount()) + "元提交"); 102 | adjustBalance(connection, transReq); 103 | }); 104 | connection.close(); 105 | return DtmResponse.buildDtmResponse(Constants.SUCCESS_RESULT); 106 | } 107 | 108 | @RequestMapping("barrierTransInCancel") 109 | public Object TransInCancel(HttpServletRequest request) throws Exception { 110 | BranchBarrier branchBarrier = new BranchBarrier(request.getParameterMap()); 111 | logger.info("barrierTransInCancel branchBarrier:{}", branchBarrier); 112 | Connection connection = dataSourceUtil.getConnecion(); 113 | TransReq transReq = extracted(request); 114 | branchBarrier.call(connection, (barrier) -> { 115 | System.out.println("用户: +" + transReq.getUserId() + ",转入" + Math.abs(transReq.getAmount()) + "回滚"); 116 | this.adjustTrading(connection, transReq); 117 | }); 118 | connection.close(); 119 | return DtmResponse.buildDtmResponse(Constants.SUCCESS_RESULT); 120 | } 121 | 122 | /** 123 | * 提取body中参数 124 | * 125 | * @param request 126 | * @return 127 | * @throws IOException 128 | */ 129 | private TransReq extracted(HttpServletRequest request) throws IOException { 130 | byte[] bytes = StreamUtils.copyToByteArray(request.getInputStream()); 131 | return JsonUtils.parseJson(bytes, TransReq.class); 132 | } 133 | 134 | /** 135 | * 更新交易金额 136 | * 137 | * @param connection 138 | * @param transReq 139 | * @throws SQLException 140 | */ 141 | public void adjustTrading(Connection connection, TransReq transReq) throws Exception { 142 | String sql = "update dtm_busi.user_account set trading_balance=trading_balance+?" 143 | + " where user_id=? and trading_balance + ? + balance >= 0"; 144 | PreparedStatement preparedStatement = null; 145 | try { 146 | preparedStatement = connection.prepareStatement(sql); 147 | preparedStatement.setInt(1, transReq.getAmount()); 148 | preparedStatement.setInt(2, transReq.getUserId()); 149 | preparedStatement.setInt(3, transReq.getAmount()); 150 | if (preparedStatement.executeUpdate() > 0) { 151 | System.out.println("交易金额更新成功"); 152 | } else { 153 | throw new FailureException("交易失败"); 154 | } 155 | } finally { 156 | if (null != preparedStatement) { 157 | preparedStatement.close(); 158 | } 159 | } 160 | 161 | } 162 | 163 | /** 164 | * 更新余额 165 | */ 166 | public void adjustBalance(Connection connection, TransReq transReq) throws SQLException { 167 | PreparedStatement preparedStatement = null; 168 | try { 169 | String sql = "update dtm_busi.user_account set trading_balance=trading_balance-?,balance=balance+? where user_id=?"; 170 | preparedStatement = connection.prepareStatement(sql); 171 | preparedStatement.setInt(1, transReq.getAmount()); 172 | preparedStatement.setInt(2, transReq.getAmount()); 173 | preparedStatement.setInt(3, transReq.getUserId()); 174 | if (preparedStatement.executeUpdate() > 0) { 175 | System.out.println("余额更新成功"); 176 | } 177 | } finally { 178 | if (null != preparedStatement) { 179 | preparedStatement.close(); 180 | } 181 | } 182 | } 183 | 184 | } -------------------------------------------------------------------------------- /src/main/java/com/dtmcli/java/sample/controller/TransController.java: -------------------------------------------------------------------------------- 1 | package com.dtmcli.java.sample.controller; 2 | 3 | import org.slf4j.Logger; 4 | import org.slf4j.LoggerFactory; 5 | import org.springframework.web.bind.annotation.RequestMapping; 6 | import org.springframework.web.bind.annotation.RestController; 7 | import pub.dtm.client.constant.Constants; 8 | import pub.dtm.client.model.responses.DtmResponse; 9 | 10 | 11 | @RestController 12 | @RequestMapping("api") 13 | public class TransController { 14 | 15 | Logger logger = LoggerFactory.getLogger(TransController.class); 16 | 17 | @RequestMapping("TransOutTry") 18 | public Object TransOutTry() { 19 | logger.info("TransOutTry"); 20 | return DtmResponse.buildDtmResponse(Constants.SUCCESS_RESULT); 21 | } 22 | 23 | @RequestMapping("TransOutConfirm") 24 | public Object TransOutConfirm() { 25 | logger.info("TransOutConfirm"); 26 | return DtmResponse.buildDtmResponse(Constants.SUCCESS_RESULT); 27 | 28 | } 29 | 30 | @RequestMapping("TransOutCancel") 31 | public Object TransOutCancel() { 32 | logger.info("TransOutCancel"); 33 | return DtmResponse.buildDtmResponse(Constants.SUCCESS_RESULT); 34 | 35 | } 36 | 37 | @RequestMapping("TransInTry") 38 | public Object TransInTry() { 39 | logger.info("TransInTry"); 40 | return DtmResponse.buildDtmResponse(Constants.SUCCESS_RESULT); 41 | 42 | } 43 | 44 | @RequestMapping("TransInConfirm") 45 | public Object TransInConfirm() { 46 | logger.info("TransInConfirm"); 47 | return DtmResponse.buildDtmResponse(Constants.SUCCESS_RESULT); 48 | } 49 | 50 | @RequestMapping("TransInCancel") 51 | public Object TransInCancel() { 52 | logger.info("TransInCancel"); 53 | return DtmResponse.buildDtmResponse(Constants.SUCCESS_RESULT); 54 | } 55 | 56 | @RequestMapping("TransOut") 57 | public Object TransOut() { 58 | logger.info("TransOut"); 59 | return DtmResponse.buildDtmResponse(Constants.SUCCESS_RESULT); 60 | } 61 | 62 | @RequestMapping("TransOutCompensate") 63 | public Object TransOutCompensate() { 64 | logger.info("TransOutCompensate"); 65 | return DtmResponse.buildDtmResponse(Constants.SUCCESS_RESULT); 66 | } 67 | 68 | @RequestMapping("TransIn") 69 | public Object TransIn() { 70 | logger.info("TransIn"); 71 | return DtmResponse.buildDtmResponse(Constants.SUCCESS_RESULT); 72 | } 73 | 74 | @RequestMapping("TransInCompensate") 75 | public Object TransInCompensate() { 76 | logger.info("TransInCompensate"); 77 | return DtmResponse.buildDtmResponse(Constants.SUCCESS_RESULT); 78 | } 79 | } -------------------------------------------------------------------------------- /src/main/java/com/dtmcli/java/sample/param/TransReq.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2021 Gypsophila open source organization. 3 | * 4 | * Licensed under the Apache License,Version2.0(the"License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package com.dtmcli.java.sample.param; 18 | 19 | import lombok.Data; 20 | 21 | /** 22 | * @author lixiaoshuang 23 | */ 24 | @Data 25 | public class TransReq { 26 | 27 | /** 28 | * 用户id 29 | */ 30 | private int userId; 31 | 32 | /** 33 | * 转入/转出金额 34 | */ 35 | private int amount; 36 | 37 | /** 38 | * jackson 必须使用无参构造 39 | */ 40 | public TransReq(){ 41 | } 42 | 43 | public TransReq(int userId, int amount) { 44 | this.userId = userId; 45 | this.amount = amount; 46 | } 47 | } 48 | -------------------------------------------------------------------------------- /src/main/java/com/dtmcli/java/sample/util/DataSourceUtil.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2021 Gypsophila open source organization. 3 | * 4 | * Licensed under the Apache License,Version2.0(the"License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package com.dtmcli.java.sample.util; 18 | 19 | import com.mysql.cj.jdbc.MysqlDataSource; 20 | import org.springframework.beans.factory.annotation.Value; 21 | import org.springframework.stereotype.Component; 22 | 23 | import java.sql.Connection; 24 | import java.sql.SQLException; 25 | 26 | /** 27 | * @author lixiaoshuang 28 | */ 29 | @Component 30 | public class DataSourceUtil { 31 | 32 | @Value("${spring.datasource.url}") 33 | private String url; 34 | 35 | @Value("${spring.datasource.username}") 36 | private String userName; 37 | 38 | @Value("${spring.datasource.password}") 39 | private String password; 40 | 41 | public Connection getConnecion() throws SQLException { 42 | MysqlDataSource mysqlDataSource = new MysqlDataSource(); 43 | mysqlDataSource.setUser(userName); 44 | mysqlDataSource.setURL(url); 45 | mysqlDataSource.setPassword(password); 46 | return mysqlDataSource.getConnection(); 47 | } 48 | 49 | } 50 | -------------------------------------------------------------------------------- /src/main/resources/application.yml: -------------------------------------------------------------------------------- 1 | server: 2 | port: 8081 3 | 4 | dtm: 5 | ipport: '127.0.0.1:36789' 6 | 7 | spring: 8 | datasource: 9 | url: jdbc:mysql://127.0.0.1:3306/dtm_barrier?useUnicode=true&characterEncoding=utf-8&useSSL=true&serverTimezone=UTC 10 | username: root 11 | password: 123456 12 | driver-class-name: com.mysql.cj.jdbc.Driver 13 | -------------------------------------------------------------------------------- /src/test/java/com/dtmcli/java/sample/DtmcliJavaExamplesApplicationTests.java: -------------------------------------------------------------------------------- 1 | package com.dtmcli.java.sample; 2 | 3 | import org.junit.jupiter.api.Test; 4 | import org.springframework.boot.test.context.SpringBootTest; 5 | 6 | @SpringBootTest 7 | class DtmcliJavaExamplesApplicationTests { 8 | 9 | @Test 10 | void contextLoads() { 11 | } 12 | 13 | } --------------------------------------------------------------------------------