歡迎訪問我的個人博客:苦酒
思途實訓-day02
上午
杜老師帶我們寫出了剩下的兩個模塊,分別是controller和serivce
- Departmentcontroller.java
package com.situ.company.department.controller;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.situ.company.department.model.DepartmentModel;
import com.situ.company.department.service.DepartmentService;
@RestController
@RequestMapping("department")
public class DepartmentController
{
@Autowired
private DepartmentService service;
@RequestMapping("insert")
public int insert(DepartmentModel model)
{
return service.insert(model);
}
@RequestMapping("delect")
public int delect(DepartmentModel model)
{
return service.delect(model);
}
@RequestMapping("updata")
public int updata(DepartmentModel model)
{
return service.updata(model);
}
@RequestMapping("selectmodel")
public DepartmentModel selectmodel(DepartmentModel model)
{
return service.selectModel(model);
}
@RequestMapping("selectList")
public List<DepartmentModel> select(DepartmentModel model)
{
return service.selectList(model);
}
}
- DepartmentSerivce.java
package com.situ.company.department.service;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.situ.company.department.mapper.DepartmentMapper;
import com.situ.company.department.model.DepartmentModel;
@Service
public class DepartmentService
{
@Autowired
private DepartmentMapper mapper;
public int insert(DepartmentModel model)
{
DepartmentModel mdb = selectModel(model);
if(mdb == null)
{
return mapper.insert(model);
}
else
{
return -1;
}
}
public int delect(DepartmentModel model)
{
return mapper.Delete(model);
}
public int updata(DepartmentModel model)
{
return mapper.updata(model);
}
public DepartmentModel selectModel(DepartmentModel model)
{
return mapper.selectModel(model);
}
public List<DepartmentModel> selectList(DepartmentModel model)
{
String name = model.getName();
if(name == null)//此處的null必須變成“”,否則下面的setName拼接成的爲%null% 查詢的是name中含有name的字符
{
name = "";
}
model.setName('%' + name + '%');
return mapper.selectList(model);
}
}
- Model模塊是建立私有化的數據類型,相當於建立數據庫中的對應數據。
- Mapper模塊在Model基礎上進行數據庫(sql)語句的編輯(增刪改查)
- Service模塊在Mapper基礎上進行一些邏輯化的操作。例如:id(主鍵)和code(非主鍵),但我想讓code的值是唯一的,這時候需要用到Service模塊的的操作,代碼如下:
public int insert(DepartmentModel model)
{
DepartmentModel mdb = selectModel(model);
if(mdb == null)
{
return mapper.insert(model);
}
else
{
return -1;
}
}
- 最後是Controller模塊,這個模塊對應的是網頁端,給網頁服務的。
@RequestMapping("insert")
這個地方的insert和網頁的請求一樣,不能寫錯。
3.
下午
老師講解使用網頁端來進行數據庫的增刪改查
- 剛開始,老師直接使用在網址中輸入的命令
http://127.0.0.1:8080/company/department/insert?code=1&name=hls&tel=17852031547
- 然後直接轉到網頁端,新建一個html文件,文件代碼如下:
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
<script type="text/javascript" src="/company/base/js/jquery-3.3.1.min.js"></script>
<style>
body {
background-image:url('9.jpg');
height: 100%;
width: 100%;
background-size: cover;
position: absolute;
overflow: hidden;
}
#login{
width:400px;
height:280px;
position:absolute;
left: 50%;
top: 40%;
margin-left:-200px;
margin-top:-140px;
border:1px;
align:center;
}
#form{
width:300px;
height:160px;
position:relative;
left:50%;
top:40%;
margin-left:-150px;
margin-top:-80px;
}
#img{
width:300px;
height:160px;
position:relative;
left:55%;
top:29.4%;
margin-left:-150px;
margin-top:-80px;
}
#bu1{
background-color: black;
width:50px;
height:40px;
}
#bu2{
background-color: black;
width:50px;
height:40px;
}
#bu3{
background-color: black;
width:100px;
height:40px;
}
</style>
</head>
<body>
<div id = "img">
<img src="8.jpg" width="165" height="60" />
</div>
<div id = "login">
<div id = "form">
<form>
<h3 align="center" style="color:green" >部門註冊系統</h3>
* 賬 號: <input type='text' name = 'code'><br><br>
* 名 稱: <input type='text' name = 'name'><br><br>
* 電 話: <input type='text' name = 'tel'><br>
<br>
<input id = "bu1" style = "color: white" type="button" value="註冊" onclick="test()">
<input id = "bu2" style = "color: white" type="button" value="重置" onclick="test1()">
<input id = "bu3" style = "color: white" type="button" value="成員註冊頁面" onclick="test2()">
</form>
</div>
</div>
</body>
<script type="text/javascript">
function test1()
{
window.location.href="http://127.0.0.1:8080/company/web/department.html";
}
function test()
{
var code = $("input[name='code']").val();
var name = $("input[name='name']").val();
var tel = $("input[name='tel']").val();
//htpp://127.0.0.1:8080/company/department/insert?code=a1&name=a2&pass=a3
$.ajax({
url:'/company/department/insert',
//data:'code='+code+'&name='+name+'&pass='+pass, //從網頁端到java發送的請求參數
data:{code:code,name:name,tel:tel},
dataType:'json', //返回的數據類型 java返回至網頁端
type:'post', //請求的方式
success:function(d)
{
console.log(d)
}
})
}
function test2(){
window.open('http://127.0.0.1:8080/company/web/Employee.html');
}
</script>
</html>
這樣就可以實施網頁端的操作。
晚上
鞏固網頁端的知識,自己查詢了HTML和CSS的一些操作,做出來了一個小小的界面
感覺杜老師真好,有問必答,太敬業了。
明天差不多要開始真正的做界面和邏輯化了,溜了,溜了!