写在开头,该Demo借鉴了http://t.csdn.cn/zKUVm?在基础上做了深入探索!
目的:分享一个简单的SpringBoot项目,通过该项目了解SpringBoot的各层框架之间的关联。
具体的SpringBoo项目层级关联:参照http://t.csdn.cn/CdquD
controller层—>service层(serviceImpl实现Iservice接口)—>mapper层—>mapper.xml文件
bean层等同于entiy,实体类层。
该Demo的整体框架结构参照第一篇文章,newDemo(http://t.csdn.cn/5dbga),在此基础上我延伸出来了关于Leader类的相关增删改查的功能和实现,也是通过这个实现才理解了各个层级之间的关联。
项目结构如下图
?
首先是最外层的操作页面 index.html
代码如下
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>NewDemo</title>
</head>
<body>
<script src="https://code.jquery.com/jquery-3.1.1.min.js"></script>
<div id="header" style="background-color:#FFA500;">
<h2 style="margin-bottom:0;">Depart院系信息</h2>
<button style="margin: 20px auto;width: 120px;height: 45px;" onclick="getAll()">查看院系信息</button>
<button style="margin: 20px auto;width: 120px;height: 45px;" onclick="getRandAll()">随机查看单条院系信息</button>
</div>
<div id="header1" style="background-color:#00cdff;">
<h2 style="margin-bottom:0;">Leader领导信息</h2>
<button style="margin: 20px auto;width: 120px;height: 45px;" onclick="getLeaderAll()">查看领导信息</button>
<input class="a-b-c" type="text" placeholder="请输入姓名" style="width: 200px;height: 40px;margin-left: 40px;">
<button style="margin: 20px auto;width: 120px;height: 45px;" onclick="getAppointLeader()">查詢指定领导信息</button>
<input class="a-b-c-d" type="text" placeholder="请输入待删除的领导排名(数字)" style="width: 200px;height: 40px;margin-left: 40px;">
<button style="margin: 20px auto;width: 120px;height: 45px;" onclick="getDeleteLeader()">删除指定领导信息</button>
</div>
<div id="header2" style="background-color:#36dd91;">
<h2 style="margin-bottom:0;">新增Leader领导信息</h2>
<input id="name" type="text" placeholder="请输入姓名" style="width: 200px;height: 40px;">
<input id="ranking" type="text" placeholder="请输入顺位" style="width: 200px;height: 40px;">
<button style="margin: 20px auto;width: 120px;height: 45px;" onclick="getAddLeader()">新增领导信息</button>
</div>
<div id="header3" style="background-color:#45aebd;">
<h2 style="margin-bottom:0;">编辑Leader领导信息</h2>
<input id="editid" type="text" placeholder="请输入待修改的用户id" style="width: 50px;height: 40px;" onchange = "getLeaderdata()">
<input id="editname" type="text" placeholder="请输入姓名" style="width: 200px;height: 40px;">
<input id="editranking" type="text" placeholder="请输入顺位" style="width: 50px;height: 40px;">
<button style="margin: 20px auto;width: 120px;height: 45px;" onclick="getEditLeader()">编辑领导信息</button>
</div>
<div id="footer" style="background-color:#3ea3f4;clear:both;text-align:center;">
<div class="result" id="result"></div>
</div>
</body>
<script>
function getAll(){
$.ajax({
type: "get",
url: "depart/selectAll",
data: {
},
success:function (data) {
console.log(data)
$("#result").empty()
for(var i = 0;i < data.length;i++){
$("#result").append(JSON.stringify(data[i]) + "<br>")
}
},
});
}
function getRandAll(){
$.ajax({
type: "get",
url: "depart/selectId",
data: {
},
success:function (data) {
console.log(data)
$("#result").empty()
for(var i = 0;i < data.length;i++){
$("#result").append(JSON.stringify(data[i]) + "<br>")
}
},
});
}
function getLeaderAll(){
$.ajax({
type: "get",
url: "leader/selectAll",
data: {
},
success:function (data) {
console.log(data)
$("#result").empty()
for(var i = 0;i < data.length;i++){
$("#result").append(JSON.stringify(data[i]) + "<br>")
}
},
});
}
function getAppointLeader(){
var Name = $('.a-b-c').val();
// var Name = document.getElementById("leadername1").value;
alert(Name);
$.ajax({
type: "get",
url: "leader/" + Name,
// data: {
// },
success:function (data) {
console.log(data)
$("#result").empty()
for(var i = 0;i < data.length;i++){
$("#result").append(JSON.stringify(data[i]) + "<br>")
}
},
});
}
function getDeleteLeader(){
var rk = $('.a-b-c-d').val();
$.ajax({
type: "delete",
url: "leader/" + rk,
// data: {
// },
success:function (data) {
console.log(data)
alert(data)
// $("#result").empty()
// for(var i = 0;i < data.length;i++){
// $("#result").append(JSON.stringify(data[i]) + "<br>")
// }
},
});
}
function getAddLeader(){
var leaderName = document.getElementById("name").value;
var ranKing1 = document.getElementById("ranking").value;
var ranKing = parseInt(ranKing1);
// alert(leaderName+ranKing);
// var form = {
// leaderName: leaderName,
// ranKing: ranKing,
// };
this.form = {
leaderName: leaderName,
ranKing: ranKing,
};
alert(this.form);
$.ajax({
contentType : 'application/json',
type: "post",
url: "leader",
dataType:"json",
data:JSON.stringify(this.form),
// data: this.form,
success:function (data) {
console.log(data)
alert(data)
$("#result").empty()
for(var i = 0;i < data.length;i++){
$("#result").append(JSON.stringify(data[i]) + "<br>")
}
},
});
}
function getLeaderdata(){
var vid = document.getElementById("editid").value;
var id = parseInt(vid);
$.ajax({
type: "get",
url: "leader/selectid/" + id,
success:function (data) {
$("#result").empty()
$("#result").append(JSON.stringify(data) + "<br>")
// alert(data.leaderName);
// alert(data.ranKing);
$("#editname").attr("value",data.leaderName);
$("#editranking").attr("value",data.ranKing);
// for(var i = 0;i < data.length;i++){
// $("#result").append(JSON.stringify(data[i]) + "<br>")
// }
},
});
}
function getEditLeader(){
var id1 = document.getElementById("editid").value;
var leaderName = document.getElementById("editname").value;
var ranKing1 = document.getElementById("editranking").value;
var ranKing = parseInt(ranKing1);
var id = parseInt(id1);
// alert(leaderName+ranKing);
// var form = {
// leaderName: leaderName,
// ranKing: ranKing,
// };
this.form = {
id:id,
leaderName: leaderName,
ranKing: ranKing,
};
$.ajax({
contentType : 'application/json',
type: "put",
url: "leader",
dataType:"json",
data:JSON.stringify(this.form),
// data: this.form,
success:function (data) {
console.log(data)
alert(data)
$("#result").empty()
for(var i = 0;i < data.length;i++){
$("#result").append(JSON.stringify(data[i]) + "<br>")
}
},
});
}
</script>
</html>
然后是Controller层? ? ?
LeaderController
代码如下
package com.example.newdemo.controller;
import com.example.newdemo.bean.Leader;
import com.example.newdemo.service.LeaderService;
import com.example.newdemo.service.LeaderServiceImpl;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import java.util.List;
@RestController
@RequestMapping("/leader")
public class LeaderController {
@Resource
private LeaderService leaderService = new LeaderServiceImpl();
@GetMapping(value = "/selectAll")
public List<Leader> selectAll() {
List<Leader> list = leaderService.selectAll();
return list;
}
@GetMapping(value = "/{Name}")
public List<Leader> selectRanKing(@PathVariable("Name") String Name) {
List<Leader> list = leaderService.selectRanKing(Name);
return list;
}
@DeleteMapping(value = "/{rk}")
public int deleteRanKing(@PathVariable("rk") int rk) {
int iresult = leaderService.deleteRanKing(rk);
return iresult;
}
@PostMapping
public int addleader(@Validated @RequestBody Leader leader) {
int iresult = leaderService.insertLeader(leader);
return iresult;
}
@GetMapping(value = "/selectid/{id}")
public Leader selectid(@PathVariable("id") int id) {
Leader list = leaderService.selectid(id);
return list;
}
@PutMapping
public int editleader(@Validated @RequestBody Leader leader) {
int iresult = leaderService.editleader(leader);
return iresult;
}
}
再然后是Service层? 先入LeaderService再入LeaderServiceImpl
LeaderService
代码如下
package com.example.newdemo.service;
import com.example.newdemo.bean.Leader;
import java.util.List;
public interface LeaderService {
public List<Leader> selectAll();
public List<Leader> selectRanKing(String Name);
public int deleteRanKing(int rk);
public int insertLeader(Leader leader);
public Leader selectid(int id);
public int editleader(Leader leader);
}
LeaderServiceImpl
代码如下
package com.example.newdemo.service;
import com.example.newdemo.bean.Leader;
import com.example.newdemo.mapper.LeaderMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
@Service("leaderService")
public class LeaderServiceImpl implements LeaderService{
@Autowired
private LeaderMapper leaderMapper;
@Override
public List<Leader> selectAll() {
return leaderMapper.selectAll();
}
@Override
public List<Leader> selectRanKing(String Name) {
return leaderMapper.selectRanKing(Name);
}
@Override
public int deleteRanKing(int rk) {
return leaderMapper.deleteRanKing(rk);
}
@Override
public int insertLeader(Leader leader) {
return leaderMapper.insertLeader(leader);
}
@Override
public Leader selectid(int id) { return leaderMapper.selectid(id); }
@Override
public int editleader(Leader leader) {
return leaderMapper.editleader(leader);
}
}
Service层代码处理完成后转入到Mapper层,在Mapper层实现底层数据的操作,在Mapper层可简单的通过@Select@Insert@Update@Delete直接操作,也可以通过将底层操作封装到mapper.xml中,各有优缺点。该项目使用的是直接在Mapper中操作数据。
LeaderMapper
代码如下
package com.example.newdemo.mapper;
import com.example.newdemo.bean.Leader;
import org.apache.ibatis.annotations.*;
import java.util.List;
@Mapper
public interface LeaderMapper {
@Select({
"select",
"id, leaderName, ranKing",
"from leaderments"
})
List<Leader> selectAll();
@Select({
"select",
"id, leaderName, ranKing",
"from leaderments where leaderName = #{Name}"
})
List<Leader> selectRanKing(String Name);
@Delete({
"delete",
"from leaderments where ranKing = #{rk}"
})
int deleteRanKing(int rk);
@Insert({
"insert into leaderments (leaderName, ranKing) values(#{leaderName}, #{ranKing})"
})
int insertLeader(Leader leader);
@Select({
"select",
"id, leaderName, ranKing",
"from leaderments where id = #{id}"
})
Leader selectid(int id);
@Update({
"update leaderments set leaderName = #{leaderName}, ranKing = #{ranKing} where id = #{id}"
})
int editleader(Leader leader);
}
Bean层的Leader实体
Leader
代码如下
package com.example.newdemo.bean;
public class Leader {
private String id;
private String leaderName;
private Integer ranKing;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getLeaderName() {
return leaderName;
}
public void setLeaderName(String leaderName) {
this.leaderName = leaderName;
}
public Integer getRanKing() {
return ranKing;
}
public void setRanKing(Integer ranKing) {
this.ranKing = ranKing;
}
}
数据库表名称和结构见下图
?
|