1. 程式人生 > >實現學生管理系統

實現學生管理系統

姓名 進入 ddl com 管理系 max .sh value 需要

一.使用idea創建springboot項目及搭建


1.創建新項目

.技術分享圖片

2.

技術分享圖片

點擊Spring Initializr 點擊next

技術分享圖片書寫項目名點擊next

3.

技術分享圖片

技術分享圖片

技術分享圖片進行選項頁面,分別選擇熱部署,web開發,使用jpa和mysql,然後next,finish,創建完成

4.進入項目後分別配置application.properties 文件和pom.xml文件

application.properties

#數據源配置
spring.datasource.type=com.alibaba.druid.pool.DruidDataSource
spring.datasource.driverClassName=com.mysql.cj.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost:3306/stu?useSSL=false&useUnicode=true&characterEncoding=utf-8&autoReconnect=true&serverTimezone=Asia/Shanghai
spring.datasource.username=root
spring.datasource.password=root
spring.datasource.initialSize=20
spring.datasource.minIdle=50
spring.datasource.maxActive=500

#上下文配置
server.port=8888
server.servlet.context-path=/kude

#配置jpa
#幫我們自動生成表結構
spring.jpa.properties.hibernate.hbm2ddl.auto=update
spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.MySQL5InnoDBDialect
spring.jpa.show-sql= true
spring.jpa.properties.hibernate.enable_lazy_load_no_trans=true


pom.xml:

<!-- druid數據庫連接池-->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.1.10</version>
</dependency>

基本配置完成

二.後端書寫
  1.

技術分享圖片在java的com.example.kede目錄下創建如下四個文件夾。controller控制層,dao接口層,service服務層,entity實體層

實體層

先根據數據庫書寫實體類

數據庫如下

技術分享圖片

實體類成員變量

private Integer id;
private String name;
private String sex;
private Integer age;

書寫實體類時需加如下標簽

技術分享圖片

@Entity 代表為實體類

@Table(name = "student") 代表使用表為student
@Id    主鍵
@GeneratedValue(strategy = GenerationType.IDENTITY)   自增序列


dao層

dao層需要註意

技術分享圖片

繼承

JpaRepository<Student,Integer> 接口 兩個參數第一個為實體類型,第二個為主鍵類型

還需註意點如下自建方法 加入@Query標簽 name屬性為方法名;nativeQuery 為; true value中為自定義sql語句
技術分享圖片


Service層



service層中無需特別註意,需要註意的是其中的接口實現類
實現類中實現Service類 並在類前加入@Service標簽代表為service層
技術分享圖片


技術分享圖片 書寫dao層的類為成員變量並且加入自動註入標簽@Autowired

技術分享圖片

 

controller層

技術分享圖片

在controller中加入作者註釋
@author中加入作者名稱
加入@RestController註解代表是控制層可以將返回值轉換為json格式
同樣將Service層的類寫為成員變量並且加入@Autowired註解自動註入

技術分享圖片

這樣寫可以在url後無需加?寫參數,直接加入/即可

還需註意的有

@PostMapping使用post方法


三.前端書寫

前端使用ajax的方法請求,在數據加載完成前,先加載頁面

$.ajax({

url: ‘http://localhost:8888/kude/student/pagequery?page=‘+page,
success:function(result){
var rel = result.content;
tot = result.totalPages;
var str = "<table width=‘80%‘ align=‘center‘ border=‘1‘>"
+"<tr><td>ID</td>"+"<td>姓名</td>"+"<td>年齡</td>"+"<td>性別</td>"+"<td>操作</td>";
for(var i=0;i<rel.length;i++){
var stu = rel[i];
var id = stu.id;
str += "<tr><td>"+stu.id+"</td><td>"+stu.name+"</td><td>"+stu.age+
"</td><td>"+stu.sex+"</td><td><a href=‘update.html?id‘>編輯</a>&nbsp;<a href=‘http://localhost:8888/kude/student/delete/"+stu.id+"‘>刪除</a></td></tr>";
}

str += "</table>";
$("#show").html(str);
}

});

url代表請求地址

success代表成功後動作

function(result)result代表頁面返回的結果

將結果數組賦給rel 使用循環輸出返回的json對象stu[]

stu.id 即id。。。。。

需要註意的有

<a href="javascript:up_p();" >上一頁</a>中

跳函數需要在前面加入 javascript

$("#show").html(str);

使用jquery獲取div name屬性展示頁面

實現學生管理系統