1.掌握相关知识
2.编写存储过程,这里不展示数据库表的数据
3从数据库得到多少列就要建立多少私有方法
import lombok.AllArgsConstructor;
import lombok.Data;
import org.apache.ibatis.annotations.Options;
import java.time.Duration;
import java.time.LocalDateTime;
@Data
@AllArgsConstructor
public class User {
private String ProductDescription;
private String ResourceName;
private Integer InPut;
private String NGCode;
private Double NGPre;
public User() {
}
}
4.创建接口我这里直接调用存储过程
import demomybatis.User;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;
import java.util.List;
@Mapper
public interface MapperUser {
@Select("EXEC ResourcePODPOGQuery_WZH")
List<User>list();
}
5.配置application.properties看我上一期文章 我这里使用的是SqlServer数据库
6.编写Task类
package demomybatis.Test;
import demomybatis.Mapper.MapperUser;
import demomybatis.User;
import org.apache.http.HttpStatus;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
@Component
public class Task {
@Autowired
private MapperUser mapperUser;
@Scheduled(initialDelay=1000, fixedRate=3600000) //第一次延迟1秒后执行,之后按fixedRate的规则每5秒执行一次
public void execute() {
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
System.out.println("执行定时任务: " + df.format(new Date()));
try {
test();
} catch (Exception e) {
// 适当的错误处理
e.printStackTrace();
}
}
public void test() throws Exception {
List<User> userList = mapperUser.list();
if (userList.isEmpty()) {
System.out.println("没有用户数据或机台良率良好");
return; // 如果列表为空,则直接返回,避免不必要的操作
}
StringBuilder sb = new StringBuilder();
userList.forEach(user -> {
sb.append("\n").append(user.getProductDescription()).append("\t")
.append(user.getResourceName()).append("\t"+"投入:")
.append(user.getInPut()).append("\t")
.append(user.getNGCode()).append("不良率:")
.append(user.getNGPre()).append("%");
});
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String jsonContent = sb.toString();
sendText( df.format(new Date()) + "\自定义标题头部\n" + jsonContent);
}
//markdown
public void sendText(String content) throws IOException {
String json = STR."{\"msgtype\": \"markdown\", \"markdown\": {\"content\": \"\{content}\", \"mentioned_list\": [18009547925]}}";
send(json);
}
public String send(String textMsg) throws IOException {
CloseableHttpClient httpClient = HttpClients.createDefault();
HttpPost httpPost = new HttpPost("你的企业微信机器人WebHook");
httpPost.addHeader("Content-Type", "application/json; charset=utf-8");
StringEntity se = new StringEntity(textMsg, "utf-8");
httpPost.setEntity(se);
CloseableHttpResponse response = httpClient.execute(httpPost);
if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
String result = EntityUtils.toString(response.getEntity(), "utf-8");
// 检查 result 是否为空或空白
if (result == null || result.trim().isEmpty()) {
System.out.println("消息内容为空,不发送微信机器人消息");
// 关闭资源
httpClient.close();
response.close();
return "消息内容为空,未发送";
} else {
System.out.println("发送微信机器人消息成功: " + result);
// 假设这里有一个发送消息给微信机器人的方法,比如 sendToWeChatBot(result);
// sendToWeChatBot(result);
// 关闭资源
httpClient.close();
response.close();
return result; // 或者返回其他表示成功的字符串
}
} else {
System.out.println("发送微信机器人消息失败");
// 关闭资源(通常应该在 finally 块中处理,但这里为了简洁而放在这里)
httpClient.close();
if (response != null) {
response.close();
}
return "发送微信机器人消息失败";
}
}
}
7 你可能需要的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>3.3.2</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>org.example</groupId>
<artifactId>DemoMybatis</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>DemoMybatis</name>
<description>DemoMybatis</description>
<url/>
<licenses>
<license/>
</licenses>
<developers>
<developer/>
</developers>
<scm>
<connection/>
<developerConnection/>
<tag/>
<url/>
</scm>
<properties>
<java.version>22</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>3.0.3</version>
</dependency>
<dependency>
<groupId>com.microsoft.sqlserver</groupId>
<artifactId>mssql-jdbc</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>com.mysql</groupId>
<artifactId>mysql-connector-j</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter-test</artifactId>
<version>3.0.3</version>
<scope>test</scope>
</dependency>
<!-- 客户端配置-->
<dependency>
<groupId>com.github.binarywang</groupId>
<artifactId>weixin-java-cp</artifactId>
<version>4.6.0</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<excludes>
<exclude>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</exclude>
</excludes>
</configuration>
</plugin>
</plugins>
</build>
</project>
最后在Idea自动生成的组件类运行代码,还需要两个注解
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.scheduling.annotation.EnableScheduling;
@SpringBootApplication
@EnableScheduling
public class DemoMybatisApplication {
public static void main(String[] args) {
SpringApplication.run(DemoMybatisApplication.class, args);
}
}
向企业微信发送消息那一段是我炒的另外一位博主的