现有三个class:class1,class2,class3;class1中包含List<class2>,class2中包含List<class2>,现要将这三个class作为table展示,请给出SpringBoot下具体的java、JS、jsp代码
时间: 2024-05-01 21:24:01 浏览: 127
基于(siger)开发的SpringBoot项目,java获取服务器信息+源代码+文档说明
以下是一个简单的示例代码,仅供参考:
Java代码:
```java
@Entity
@Table(name = "class1")
public class Class1 {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
@OneToMany(mappedBy = "class1", cascade = CascadeType.ALL)
private List<Class2> class2List;
// getters and setters
}
@Entity
@Table(name = "class2")
public class Class2 {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
@ManyToOne
@JoinColumn(name = "class1_id")
private Class1 class1;
@OneToMany(mappedBy = "class2", cascade = CascadeType.ALL)
private List<Class3> class3List;
// getters and setters
}
@Entity
@Table(name = "class3")
public class Class3 {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
@ManyToOne
@JoinColumn(name = "class2_id")
private Class2 class2;
// getters and setters
}
@Repository
public interface Class1Repository extends JpaRepository<Class1, Long> {
}
@Service
public class Class1Service {
@Autowired
private Class1Repository class1Repository;
public List<Class1> getAllClass1() {
return class1Repository.findAll();
}
}
```
JS代码:
```javascript
$(document).ready(function() {
$.ajax({
url: "/class1",
type: "GET",
dataType: "json",
success: function(data) {
var table = "<table>";
table += "<tr><th>Class1 ID</th><th>Class2 ID</th><th>Class3 ID</th></tr>";
for (var i = 0; i < data.length; i++) {
var class1 = data[i];
for (var j = 0; j < class1.class2List.length; j++) {
var class2 = class1.class2List[j];
for (var k = 0; k < class2.class3List.length; k++) {
var class3 = class2.class3List[k];
table += "<tr><td>" + class1.id + "</td><td>" + class2.id + "</td><td>" + class3.id + "</td></tr>";
}
}
}
table += "</table>";
$("#table-container").html(table);
},
error: function() {
alert("Error occurred.");
}
});
});
```
JSP代码:
```jsp
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Table Example</title>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
<script src="/js/table.js"></script>
</head>
<body>
<div id="table-container"></div>
</body>
</html>
```
需要注意的是,这里仅提供了一个简单示例,实际开发中还需要根据具体需求进行修改和完善。
阅读全文