【发布时间】:2020-01-06 01:47:00
【问题描述】:
我想做什么:在html中输入文本(例如:11:00:00)。然后将该输入值与数据库中的column Time进行比较,并显示与11:00:00相关的数据。我想我必须将输入文本从 Html 传输到 Controller,保存到一个变量中,然后在 Repository 文件中,我使用语法“Select ... from ... Where time = XXX”,但我不知道如何做...
我在 SQL server 中的表是 product,有 4 列:id、TAG00(我的温度数据)、Date,时间。
感谢您的帮助。
AppController.java
public class AppController {
@Autowired
private ProductService service;
@Autowired
private ProductRepository prorepo;
@RequestMapping("/ChartBar")
public String viewBar(Model model) {
List<Object[]> listData = service.listData(20);
model.addAttribute("listData", listData);
List<Object[]> listTime = service.listTime("20");
model.addAttribute("listTime", listTime);
return "ChartBar";
}
}
ProductRepository.java
public interface ProductRepository extends JpaRepository<Product, Long> {
@Query(value="SELECT tag00 FROM Product",nativeQuery =true)
public List<Object[]> findByTag00(@Param("tag00")float tag00);
@Query(value="SELECT time FROM Product",nativeQuery =true)
public List<Object[]> findByTime(@Param("time")String time);}
Product.java
@Entity
public class Product {
public int id;
public float tag00;
public String date;
public String time;
protected Product() {
}
protected Product(int id, float tag00, String date, String time) {
super();
this.id = id;
this.tag00 = tag00;
this.date = date;
this.time = time;
}
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public float getTAG00() {
return tag00;
}
public void setTAG00(float tag00) {
this.tag00 = tag00;
}
public String getDate() {
return date;
}
public void setDate(String date) {
this.date = date;
}
public String getTime() {
return time;
}
public void setTime(String time) {
this.time = time;
}
}
ChartBar.html 我使用 Graph (ChartJS) 来显示数据库中的数据和时间。
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:th="http://www.thymeleaf.org">
<div style="position:relative;left:50px;top:5px;" > <!-- Position:
relative(tuong quan theo left,right,bottom,top), absolute,fixed -->
<a href="/">Home</a>
</div>
<script
src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.8.0/Chart.min.js">
</script>
<link rel="stylesheet"href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css">
<script src="https://cdn.jsdelivr.net/npm/chartjs-plugin-datalabels@0.5.0">
</script> <!-- thu vien dung de hien thi gia tri tren bieu do -->
<div class="container">
<canvas id="ChartBar"></canvas>
</div>
<div class="container">
<canvas id="myChart1" width ="350" height="350"></canvas>
</div>
<script th:inline="javascript">
let myChart1 = document.getElementById('myChart1').getContext('2d');
// Global Options
Chart.defaults.global.defaultFontFamily = 'Lato';
Chart.defaults.global.defaultFontSize = 18;
Chart.defaults.global.defaultFontColor = '#777';
let massPopChart = new Chart(myChart1, {
type:'bar', // bar, horizontalBar, pie, line, doughnut, radar, polarArea
data:{
//labels: [1,3,5,7];
labels:/*[[${listTime}]]*/,<!--my Time data-->
datasets:[{
label:'Temperature',
//data: [2,4,6,8];
data:/*[[${listData}]]*/, <!--my Temperature data-->
backgroundColor:'rgba(255, 99, 132, 0.6)',
fill: false,
borderWidth:1,
borderColor:'rgba(255, 0, 0, 0.6)', //thay doi mau cho Line
hoverBorderWidth:1,
hoverBorderColor:'#111',
pointRadius: 3
}]
},
options: {
legend : {
display: false,
},
responsive : true ,
maintainAspectRatio: false,
plugins: { //plugin dung de hien thi gia tri len bieu do
datalabels: {
display: function(context) {
return context.dataIndex % 1;
},
backgroundColor: function(context) {
return context.dataset.backgroundColor;
},
backgroundColor: 'rgba(255, 255, 255,0)',
borderRadius: 2,
anchor : 'end',
align : 'top',
color: 'black',
font: {
weight: 'bold'
},
formatter: Math.round
}
},
scales: {//scales dung de cai dat option cho cot X,Y
yAxes: [{
ticks: {
beginAtZero: true,
stepSize: 0.1,
min: 24,
max: 25,
fontColor : 'blue'
},
}],
xAxes: [{
ticks: {
fontColor: 'blue'
},
}]
}
}
});
【问题讨论】:
标签: sql-server spring-boot spring-data-jpa chart.js thymeleaf