1.首先把准备工作搞好
把mysql数据库里面的表和测试数据创建好
每个加入3条测试数据
2.创建一个sptingboot工程 注意:要用到maven
选择这个点击下一步
记得java版本要选择8
选择这四个就可以了
记得架包要导入mysql和mybatis和boot的整合
不会的看我的
然后编写yaml 没有的创建一个
还有分页的
pagehelper: helperDialect: mysql reasonable: true supportMethodsArguments: false params: count=countSql
还有 properties这样保险一点
然后创建包和接口
控制层 mapper接口service 实体类(要get set 无参 有参)
总体下来就这个样
别忘了在总程序类添加
@MapperScan("com.springboot.mapper")注解 里面写mapper包的地址
然后把页面准备好就可以写控制层了就是controller
附上我的
package com.springboot.controller;
import com.github.pagehelper.PageInfo;
import com.springboot.pojo.clubs;
import com.springboot.pojo.players;
import com.springboot.service.playersService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;
/**
* Keafmd
*
* @ClassName: playersController
* @Description:
* @author: 牛哄哄的中山南柯
* @date: 2022/11/21 17:15
*/
@Controller
public class playersController {
@Autowired
private playersService playersService;
@RequestMapping("/")
public String toIndex(){
return"redirect:/players/1";
}
@RequestMapping(value = "/players/{pageNum}",method = RequestMethod.GET)
public String access_control_deviceGettype(HttpSession session, Model model,
@RequestParam(defaultValue = "0") Integer cid,
@RequestParam(defaultValue = "") String name, @PathVariable("pageNum") Integer pageNum){
//获取分页信息
PageInfo<players> page= playersService.getAllplayers(pageNum,cid,name);
List<clubs> type= playersService.getClubs();
//将分页数据和共享到请求域中
session.setAttribute("type",type);
model.addAttribute("cId",cid);
model.addAttribute("name",name);
model.addAttribute("page",page);
return "index";
}
@RequestMapping("/players/to/add")
public String toadd(){
return "add";
}
@RequestMapping("/players/add")
public String access_control_deviceAdd(Model model, players players, HttpServletResponse response ) throws IOException {
PrintWriter out=response.getWriter();
playersService.playersAdd(players);
out.print(1);
out.flush();
out.close();
return "index";
}
@RequestMapping("/players/delete")
public String playersDelete(Integer id, HttpServletResponse response) throws IOException {
PrintWriter out=response.getWriter();
playersService.playersDelete(id);
out.print(1);
out.flush();
out.close();
return "index";
}
@RequestMapping("/players/to/update/{id}")
public String toUpdate( Model model,@PathVariable("id") Integer id){
players players=playersService.getIdplayers(id);
model.addAttribute("players",players);
return "update";
}
@RequestMapping("/players/to/update/update/go")
public String update(Model model,players players){
playersService.update(players);
return "redirect:/players/1";
}
}
这个新增和删除的有点问题 还没改 但是能用 参考参考
然后就写service接口
package com.springboot.service;
import com.github.pagehelper.PageInfo;
import com.springboot.pojo.clubs;
import com.springboot.pojo.players;
import java.util.List;
public interface playersService {
/**
*查询分页信息
* @param pageNum
* @param cid
* @param name
* @return
*/
PageInfo<players>getAllplayers(Integer pageNum, Integer cid, String name);
/**
* 查询所有球队
* @return
*/
List<clubs> getClubs();
/**
* 新增球员
* @param players
*/
void playersAdd(players players);
/**
* 删除
* @param id
*/
void playersDelete(Integer id);
/**
* 根据id查询信息
* @param id
* @return
*/
players getIdplayers(Integer id);
/**
* 更新球员信息
* @param players
*/
void update(players players);
}
然后创建接口实现类
package com.springboot.service.impl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.springboot.mapper.playersMapper;
import com.springboot.pojo.clubs;
import com.springboot.pojo.players;
import com.springboot.service.playersService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
/**
* Keafmd
*
* @ClassName: playersImpl
* @Description:
* @author: 牛哄哄的中山南柯
* @date: 2022/11/21 17:21
*/
@Service
public class playersImpl implements playersService {
@Autowired
private playersMapper playersMapper;
public PageInfo<players> getAllplayers(Integer pageNum, Integer cid, String name) {
//开启分页功能
PageHelper.startPage(pageNum,2);
//查询所有的员工信息
List<players> list=playersMapper.getAllplayers(cid,name);
//获取分页相关数据
PageInfo<players>page=new PageInfo<players>(list,5);
return page;
}
public List<clubs> getClubs() {
return playersMapper.getClubs();
}
public void playersAdd(players players) {
playersMapper.playersAdd(players);
}
public void playersDelete(Integer id) {
playersMapper.playersDelete(id);
}
@Override
public players getIdplayers(Integer id) {
return playersMapper.getIdplayers(id);
}
@Override
public void update(players players) {
playersMapper.update(players);
}
}
这边分页查询是用了mybatis的分页插件 大家不需要去知道他的原理 知道 怎么用的就行 pageNum是表示第几页 2是每页展示几条
然后写mapper接口
package com.springboot.mapper;
import com.springboot.pojo.clubs;
import com.springboot.pojo.players;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import java.util.List;
@Mapper
public interface playersMapper {
/**
* 根据条件查询
* @param cid
* @param name
* @return
*/
List<players> getAllplayers(@Param("cid") Integer cid, @Param("name") String name);
/**
* 查询所有球队
* @return
*/
List<clubs> getClubs();
/**
* 新增
* @param players
*/
void playersAdd(players players);
/**
*
* @param id
*/
void playersDelete(@Param("id") Integer id);
/**
* 根据id查询信息
* @param id
* @return
*/
players getIdplayers(@Param("id") Integer id);
/**
* 更新球员信息
* @param players
*/
void update(players players);
}
别忘了加@Mapper注解 免得系统找不到
然后就可以写mybatis.xml了
这个xml你们可以创一个模板
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.springboot.mapper.playersMapper">
<select id="getAllplayers" resultType="players">
SELECT * from players join clubs on players.cid=clubs.cid
<where>
<if test="cid != 0">
players.cid=#{cid}
</if>
<if test="name !=''">
and players.pname like '%${name}%'
</if>
</where>
ORDER BY birthday DESC
</select>
<select id="getClubs" resultType="clubs">
select * from clubs
</select>
<insert id="playersAdd">
insert into players(pname,birthday,height,weight,position,cid)values(#{pname},#{birthday},#{height},#{weight},#{position},#{cid})
</insert>
<delete id="playersDelete">
delete from players where pid=#{id}
</delete>
<select id="getIdplayers" resultType="players">
SELECT * from players join clubs on players.cid=clubs.cid
where players.pid=#{id}
</select>
<update id="update">
update players
<set >
<if test="pname != null and pname != '' " >
pname = #{pname},
</if>
<if test="birthday != null" >
birthday= #{birthday},
</if>
<if test="height != null" >
height = #{height},
</if>
<if test="weight != null" >
weight= #{weight},
</if>
<if test="position != null" >
position= #{position},
</if>
<if test="cid != null" >
cid= #{cid}
</if>
</set>
where players.pid = #{pid}
</update>
</mapper>
怎么知道自己写的slq对上方法呢 id对应mapper接口方法的方法名
建议大家写sql的时候可以去数据库里面试试看看有没有写错
这样数据就一层一层得返回到控制层了
控制层再把数据放在请求域中 再跳到页面 页面 获取就可以了
最后再附上
html页面
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>Title</title>
<script src="https://code.jquery.com/jquery-3.1.1.min.js"></script>
</head>
<body style="text-align: center">
<form th:action="@{/players/1}" method="get">
<table align="center">
<tr>
<th colspan="8">
<h1>美国职业篮球联盟(NBA)球员信息</h1>
</th>
</tr>
<tr>
<th></th>
<th>
球员姓名<input type="text" name="name" th:value="${name}">
</th>
<th>
<select name="cid">
<option value="0" >请选择</option>
<span th:each="clubs:${session.type}">
<option th:text="${clubs.cname}" th:value="${clubs.cid}" th:if="${clubs.cid==cId}" selected></option>
<option th:text="${clubs.cname}" th:value="${clubs.cid}" th:if="${clubs.cid!=cId}"></option>
</span>
</select>
</th>
<th>
<input type="submit" value="查询">
</th>
<th>
</th>
<th>
</th>
<th>
</th>
<th>
<a th:href="@{to/add}">添加</a>
</th>
</tr>
<tr>
<th>球员编号</th>
<th>球员名称</th>
<th>出生时间(yyyy-MM-dd)</th>
<th>球员身高(单位:cm)</th>
<th>球员体重(单位:Kg)</th>
<th>球员位置</th>
<th>所属球队</th>
<th>相关操作</th>
</tr>
<tr th:if="${page.list.size()==0}">
<th colspan="8">
<h1>没有找到任何信息!</h1>
</th>
</tr>
<tr th:each="players:${page.list}">
<th th:text="${players.pid}"></th>-->
<th th:text="${players.pname}"></th>
<th th:text="${#dates.format(players.birthday,'yyyy-MM--dd')}"></th>
<th th:text="${players.height}"></th>
<th th:text="${players.weight}"></th>
<th th:text="${players.position}"></th>
<th th:text="${players.cname}"></th>
<th>
<a th:onclick="|x(${players.pid})|" href="#">删除</a>
<a th:href="@{'to/update/'+${players.pid}}">更新</a>
</th>
</tr>
</table>
<div>
<a th:if="${page.hasPreviousPage}" th:href="@{'/players/1?cid='+${cId}+'&name='+${name}}">首页</a>
<a th:if="${page.hasPreviousPage}" th:href="@{'/players/'+${page.prePage}+'?cid='+${cId}+'&name='+${name}}">上一页</a>
<span th:each="num : ${page.navigatepageNums}">
<a th:if="${page.pageNum == num}" style="color: red;" th:href="@{'/players/'+${num}+'?cid='+${cId}+'&name='+${name}}" th:text="'['+${num}+']'"></a>
<a th:if="${page.pageNum != num}" th:href="@{'/players/'+${num}+'?cid='+${cId}+'&name='+${name}}" th:text="${num}"></a>
</span>
<a th:if="${page.hasNextPage}" th:href="@{'/players/'+${page.nextPage}+'?cid='+${cId}+'&name='+${name}}">下一页</a>
<a th:if="${page.hasNextPage}" th:href="@{'/players/'+${page.pages}+'?cid='+${cId}+'&name='+${name}}">末页</a>
</div>
</form>
</body>
</html>
<script>
$(function (){
//设置表格的背景颜色
$("table tr").css("background","pink");
//设置双数行的背景颜色
$("table tr:even").css("background","lightskyblue");
});
function x(nb) {
var id=nb;
if (confirm('要删除吗')){
$.ajax({
url:"/players/delete",
type:"get",
data:{"id":id},
dataType: "text",
status: function(data){
if (data!=null){
location.href='/';
}
}
});
history.go(0);
}
}
</script>
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>Title</title>
<script src="https://code.jquery.com/jquery-3.1.1.min.js"></script>
</head>
<body>
<table align="center" style="border: 1px solid pink">
<form action="#">
<tr>
<th>
<h1>添加球员信息</h1>
</th>
</tr>
<tr>
<th>
球员姓名<input type="text" name="pname">
</th>
</tr>
<tr>
<th>
出生时间<input type="date" name="birthday">(yyyy-MM-dd)
</th>
</tr>
<tr>
<th>
球员身高<input type="text" name="height">(单位:cm)
</th>
</tr>
<tr>
<th>
球员体重<input type="text" name="weight">(单位:Kg)
</th>
</tr>
<tr>
<th>
球员位置:
<input type="radio" value="控球后卫" name="position">控球后卫
<input type="radio" value="得分后卫" name="position">得分后卫
<input type="radio" value="小前锋" name="position">小前锋
<input type="radio" value="大前锋" name="position">大前锋
<input type="radio" value="中锋" name="position">中锋
</th>
</tr>
<tr>
<th>
所属球队 <select name="cid">
<option value="" selected>请选择</option>
<option th:each="clubs:${session.type}" th:text="${clubs.cname}" th:value="${clubs.cid}"></option>
</select>
</th>
</tr>
<tr>
<th>
<input type="submit" value="保存" id="submit">
<input type="reset" value="重置">
<input type="button" onclick="location.href='/'" value="返回">
</th>
</tr>
</form>
</table>
</body>
</html>
<script>
$('#submit').click(function () {
var pname = $('[name=pname]').val();
var birthday = $('[name=birthday]').val();
var height = $('[name=height]').val();
var weight = $('[name=weight]').val();
var position = $('[name=position]').val();
var cid =$('[name=cid]').val();
if (pname==''||pname==null){
alert("球员名称不能为空");
return;
}else
if (birthday==''||birthday==null){
alert("出生时间不能为空");
return;
}else if (height==null||height==''){
alert("球员身高不能为空");
return;
}else if (weight==null||weight==''){
alert("球员体重不能为空");
return;
}else if (position==null||position==''){
alert("球员位置不能为空");
return;
}else {
alert('添加成功');
$.ajax({
url: "/players/add",
type: "post",
data: {"pname":pname,"birthday":birthday,"height":height,"weight":weight,"position":position,"cid":cid},
dataType: "text",
success: function (data) {
if (data==1){
location.href='/'
}
}
});
}
})
</script>
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<body>
<table align="center" style="border: 1px solid pink">
<form action="update/go">
<tr>
<th>
<h1>添加球员信息</h1>
<input type="hidden" name="pid" th:value="${players.pid}">
</th>
</tr>
<tr>
<th>
球员姓名<input type="text" name="pname" th:value="${players.pname}">
</th>
</tr>
<tr>
<th>
出生时间<input type="date" name="birthday" th:value="${players.birthday}">(yyyy-MM-dd)
</th>
</tr>
<tr>
<th>
球员身高<input type="text" name="height" th:value="${players.height}">(单位:cm)
</th>
</tr>
<tr>
<th>
球员体重<input type="text" name="weight" th:value="${players.weight}">(单位:Kg)
</th>
</tr>
<tr>
<th>
球员位置:
<input type="radio" value="控球后卫" name="position" th:field="${players.position}">控球后卫
<input type="radio" value="得分后卫" name="position" th:field="${players.position}">得分后卫
<input type="radio" value="小前锋" name="position" th:field="${players.position}">小前锋
<input type="radio" value="大前锋" name="position"th:field="${players.position}">大前锋
<input type="radio" value="中锋" name="position" th:field="${players.position}">中锋
</th>
</tr>
<tr>
<th>
<select name="cid">
<option value="0" >请选择</option>
<span th:each="clubs:${session.type}">
<option th:text="${clubs.cname}" th:value="${clubs.cid}" th:if="${clubs.cid==players.cid}" selected></option>
<option th:text="${clubs.cname}" th:value="${clubs.cid}" th:if="${clubs.cid!=players.cid}"></option>
</span>
</select>
</select>
</th>
</tr>
<tr>
<th>
<input type="submit" value="更新" id="submit">
<input type="reset" value="重置">
<input type="button" onclick="location.href='/'" value="返回">
</th>
</tr>
</form>
</table>
</body>
</body>
</html>
第一次发博客 大家支持一下 如有不足请告知 写的不是很好