1)web更换为4.0,之前版本过低会出严重bug
2)引入所需依赖
引入依赖
com.github.pagehelper pagehelper5.3.0 org.springframework spring-webmvc5.2.15.RELEASE org.mybatis mybatis3.5.6 org.mybatis mybatis-spring2.0.6 mysql mysql-connector-java8.0.28 com.alibaba druid1.2.4 org.projectlombok lombok1.18.24 com.fasterxml.jackson.core jackson-databind2.13.2.2 javax.servlet javax.servlet-api4.0.1 org.springframework spring-jdbc5.2.15.RELEASE org.mybatis.generator mybatis-generator-core1.4.0 junit junit4.12 compile
3)spring配置文件
4)web配置文件
dispatcherServlet org.springframework.web.servlet.DispatcherServlet contextConfigLocation classpath:appliction.xml 1 dispatcherServlet / encodingFilter org.springframework.web.filter.CharacterEncodingFilter encoding utf-8 encodingFilter /*
5)使用generator快速生成代码
测试代码:
public class TestGenerator {
@Test
public void test02() throws Exception{
List warnings = new ArrayList();
boolean overwrite = true;
File configFile = new File("generator.xml");
ConfigurationParser cp = new ConfigurationParser(warnings);
Configuration config = cp.parseConfiguration(configFile);
DefaultShellCallback callback = new DefaultShellCallback(overwrite);
MyBatisGenerator myBatisGenerator = new MyBatisGenerator(config, callback, warnings);
myBatisGenerator.generate(null);
}
}
使用 generator生成了dao、entity、mapper文件
3.实现主页面(查询)service实体类层代码:
@Service
public class StudentServiceImpl implements StudentService {
//交予spring管理
@Autowired
private StudentMapper sm;
public PageInfo find(Integer currentPage, Integer pageSize,Map map) {
PageHelper.startPage(currentPage,pageSize);
List l = sm.findAll(map);
PageInfo pageInfo = new PageInfo(l);
return pageInfo;
}
}
controller操作层代码:
@RestController
@RequestMapping("student")
public class StudentController {
@Autowired
private StudentService ss;
//查询
@RequestMapping("select")
public CommonResult select(Integer currentPage, Integer pageSize, @RequestBody Map map){
PageInfo lsit = ss.find(currentPage, pageSize,map);
return new CommonResult(2000,"查询成功",lsit);
}
}
前端界面代码:
<%--
Created by IntelliJ IDEA.
User: Z
Date: 2022/6/15
Time: 15:23
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
Title
添加
退出
<%--搜索框--%>
查询
取 消
确 定
取 消
确 定
编辑
删除
<%--
分页组件
size-change:页面显示条数改变时触发的事件
current-change:页码改变时触发的事件
current-page:当前的页面
page-sizes:接受一个整型数组,数组元素为展示的选择每页显示个数的选项
page-size:每页显示的条数
--%>
4.添加
service层
//添加
public CommonResult add(Student s) {
int i = sm.insert(s);
return new CommonResult(2000,"添加成功",null);
}
controller操作层:
//添加
@RequestMapping("insert")
public CommonResult insert(Student s){
CommonResult add = ss.add(s);
return add;
}
5.修改
service层
//修改
public CommonResult upd(Student s) {
int i = sm.updateByPrimaryKey(s);
return new CommonResult(2000,"修改成功",null);
}
controller操作层:
//修改
@RequestMapping("update")
public CommonResult update(Student s){
CommonResult upd = ss.upd(s);
return upd;
}
6.删除
service层
//删除
public CommonResult del(Integer id) {
int i = sm.deleteByPrimaryKey(id);
return new CommonResult(2000,"删除成功",null);
}
controller操作层:
//删除
@RequestMapping("delete")
public CommonResult delete(Integer id){
CommonResult del = ss.del(id);
return del;
}


