1:先看一下项目结构
2:打开后的效果
3:引入所需依赖
<!-- Mybatis整合springboot-->
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>1.3.2</version>
</dependency>
<!-- MySQL驱动-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
4:application.yml配置,视图配置,数据源,端口号(默认不加都是8080)
server:
port: 8080
spring:
mvc:
view:
prefix: /
suffix: .html
# 配置数据源
datasource:
driver-class-name: com.mysql.cj.jdbc.Driver
username: root
password: 123456
url: jdbc:mysql://localhost:3306/jdbc_1?useUnicode=true&characterEncoding=utf-8&useSSL=false&serverTimezone=Asia/Shanghai
jackson:
# 格式化返回时间 yyyy-MM-dd HH:mm:ss
date-format: yyyy-MM-dd HH:mm:ss
time-zone: GMT+8
mybatis:
type-aliases-package: com.aaa.pojo
mapper-locations: classpath:mapper/*.xml
5:前端html页面
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<form method="post">
<p>id:<input type="text" id="id" name="id" readonly></p>
<p>name:<input type="text" id="stuname" name="stuname"></p>
<p>idcard:<input type="text" id="idcard" name="idcard" onblur="card()"></p>
<p>sex:<input type="text" id="sex" name="sex" readonly></p>
<p>birthday:<input type="text" id="birthday" name="birthday" readonly></p>
<p><button value="提交" onclick="tj()">提交</button></p>
</form>
<hr>
<table>
<tr>
<th>id</th>
<th>name</th>
<th>sex</th>
<th>idcard</th>
<th>birthday</th>
<th>操作</th>
</tr>
<tbody id="tb">
</tbody>
这里显示数据内容
</table>
<button onclick="Previous()">上一页</button>
当前页:<input type="text" id="page" readonly value="1">
一共多少页:<input type="text" id="pageall" readonly>
一共有多少条数据:<input type="text" id="pagesizeall" readonly>
每一页有几条数据:<select id="pagesize">
<option>5</option>
<option>4</option>
<option>3</option>
<option>2</option>
<option>1</option>
</select>
<button onclick="Next()">下一页</button>
<button onclick="zOne()">第一页</button>
<button onclick="dOne()">最后一页</button>
<script src="/jquery-3.5.1.js"></script>
</body>
</html>
6:pojo
映射数据库数据的工具类根据自行情况设置就好了!
如下是Result统一类,使用属于规范
public class Result {
private Integer code;
private String msg;
private Object data;
public Result(Integer code, String msg, Object data) {
this.code = code;
this.msg = msg;
this.data = data;
}
public Result() {
}
public static Result success(Object data) {
return new Result(200, "success", data);
}
public static Result fail() {
return new Result(400, "fail", null);
}
}
7:Controller
package com.aaa.controller;
import com.aaa.pojo.Emp;
import com.aaa.pojo.Result;
import com.aaa.service.MyService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;
@SuppressWarnings("all")
@RestController
@RequestMapping("/test/")
public class MyController {
@Autowired
MyService myService;
//limit分页
@RequestMapping("limit")
public Result Limit(int page, int pagesize) {
return Result.success(myService.selectLimit(page, pagesize));
}
//修改与增加
@PostMapping("update")
public Result Update(Emp emp) {
System.out.println("dasdsadsa" + emp);
return Result.success(myService.Byupdate(emp));
}
//删除
@RequestMapping("delete")
public Result Insert(int id) {
return Result.success(myService.Delete(id));
}
//根据id查询
@RequestMapping("selectbyid")
public Result Select(int id) {
return Result.success(myService.selecByid(id));
}
}
8:service
package com.aaa.service;
import com.aaa.mapper.Mymapper;
import com.aaa.pojo.Emp;
import com.aaa.pojo.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@SuppressWarnings("all")
@Service
public class MyServiceimpl implements MyService {
@Autowired
Mymapper mymapper;
@Override
public List<Emp> selectAll() {
return mymapper.selectAll();
}
@Override
public int Byupdate(Emp emp) {
// 如果身份证号重复不需添加返回3
List<Emp> emps = mymapper.selectByidCard(emp.getIdcard());
System.out.println(emps);
if (emps.size() > 0) {
System.out.println(emps.size());
return 3;
} else {
if (emp.getId() != 0) {
int update = mymapper.Update(emp);
return update;
} else {
int insert = mymapper.Insert(emp);
return insert;
}
}
}
@Override
public int Delete(int id) {
return mymapper.Delete(id);
}
@Override
public Emp selecByid(int id) {
return mymapper.SelectByid(id);
}
//查询分页内容
@Override
public Map selectLimit(int page, int pagesize) {
int bypage = (pagesize * page) - pagesize;
List<Emp> emps1 = mymapper.selectLimit(bypage, pagesize);
List<Emp> emps = mymapper.selectAll();
int pageAll = emps.size() / pagesize;
if (emps.size() % pagesize != 0) {
pageAll += 1;
}
HashMap hashMap = new HashMap();
hashMap.put("sizeAll", emps.size());
hashMap.put("emplist", emps1);
hashMap.put("pageAll", pageAll);
return hashMap;
}
}
9:mapper
package com.aaa.mapper;
import com.aaa.pojo.Emp;
import com.aaa.pojo.Result;
import org.apache.ibatis.annotations.Param;
import org.springframework.stereotype.Repository;
import java.util.List;
@Repository
public interface Mymapper {
List<Emp> selectAll();
int Update(Emp emp);
int Insert(Emp emp);
int Delete(int id);
Emp SelectByid(int id);
List<Emp> selectLimit(@Param("page") int page, @Param("pagesize") int pagesize);
List<Emp> selectByidCard(String idcard);
}
mapper的xmlsql语句有点多,就不展示了!毕竟也没啥东西
1:修改与添加写到了一个方法里面。通过前端传过来的值,到后端方法里判断一下有木有id就好了,毕竟添加不需要id,修改需要id写个判断调用不同的mapper方法就OK了!