标签:class exception port address res style 获得 fast model
自己学习用,待补充!
工程结构:
pom:
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.4.5</version> <relativePath/> <!-- lookup parent from repository --> </parent> <groupId>com.example</groupId> <artifactId>lcn-producer</artifactId> <version>0.0.1-SNAPSHOT</version> <name>lcn-producer</name> <description>lcn-producer</description> <properties> <java.version>1.8</java.version> <spring-cloud.version>2020.0.2</spring-cloud.version> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> <!-- lcn --> <dependency> <groupId>com.codingapi.txlcn</groupId> <artifactId>txlcn-tc</artifactId> <version>5.0.2.RELEASE</version> </dependency> <dependency> <groupId>com.codingapi.txlcn</groupId> <artifactId>txlcn-txmsg-netty</artifactId> <version>5.0.2.RELEASE</version> </dependency> <!-- euekea 依赖 --> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId> </dependency> <!-- mysql:MyBatis相关依赖--> <dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> <version>2.0.0</version> </dependency> <!-- 整合MyBatis java类依赖 --> <dependency> <groupId>org.mybatis.generator</groupId> <artifactId>mybatis-generator-maven-plugin</artifactId> <version>1.4.0</version> <type>maven-plugin</type> </dependency> <!-- mysql:mysql驱动--> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> </dependency> <!-- mysql:阿里巴巴数据库连接池 --> <dependency> <groupId>com.alibaba</groupId> <artifactId>druid</artifactId> <version>1.1.12</version> </dependency> <!-- JSONObject --> <dependency> <groupId>com.alibaba</groupId> <artifactId>fastjson</artifactId> <version>1.2.60</version> </dependency> </dependencies> <dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-dependencies</artifactId> <version>${spring-cloud.version}</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project>
yml:
server: port: 8080 spring: #应用名称及验证账号 application: name: lcn-producer datasource: driver-class-name: com.mysql.cj.jdbc.Driver url: jdbc:mysql://localhost:3306/lcn-producer?characterEncoding=UTF-8&serverTimezone=Asia/Shanghai username: root password: root dbcp2: initial-size: 5 min-idle: 5 max-total: 5 max-wait-millis: 200 validation-query: SELECT 1 test-while-idle: true test-on-borrow: false test-on-return: false mybatis: mapper-locations: - classpath:mapper/*.xml eureka: client: service-url: defaultZone: http://localhost:7900/eureka/ # tm配置 tx-lcn: client: manager-address: 127.0.0.1:8070
启动类:
Controller:
@Controller
public class LcnController {
@Autowired
ProducerInfoMapper producerInfoMapper;
@Autowired
RestTemplate restTemplate;
@RequestMapping("/addProducer")
@Transactional(rollbackFor = Exception.class)
@LcnTransaction
public @ResponseBody String addProducer(){
JSONObject json = new JSONObject();
String id = SnowFlake.nextId()+"";// utils 里的雪花算法
json.put("id",id);
System.out.println("获得Id : "+id);
ProducerInfo producerInfo = new ProducerInfo();
producerInfo.setId(id);
producerInfo.setName("li");
restTemplate.postForEntity("http://localhost:8090/addConsumer",json,String.class);
// int i = 1/0;
producerInfoMapper.insert(producerInfo);
return "addProducer插入成功";
}
}
测试数据库 表就两个字段:
标签:class exception port address res style 获得 fast model
原文地址:https://www.cnblogs.com/lifan12589/p/14722894.html