使用逆向工程
引入jar包
package com.gds.test;
import org.mybatis.generator.api.MyBatisGenerator;
import org.mybatis.generator.config.Configuration;
import org.mybatis.generator.config.xml.ConfigurationParser;
import org.mybatis.generator.exception.InvalidConfigurationException;
import org.mybatis.generator.exception.XMLParserException;
import org.mybatis.generator.internal.DefaultShellCallback;
import java.io.File;
import java.io.IOException;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
/**
* @Author 龚道松
* @Date 2019/6/30 9:48
* @Wersion 1.0
**/
public class MBGTest {
public static void main(String[] args) throws Exception{
List warnings = new ArrayList();
boolean overwrite = true;
File configFile = new File("mbg.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);
}
}
推荐使用spring的单元测试
package com.gds.test;
import com.alibaba.druid.support.spring.stat.SpringStatUtils;
import com.gds.bean.Department;
import com.gds.bean.Employee;
import com.gds.dao.DepartmentMapper;
import com.gds.dao.EmployeeMapper;
import org.apache.ibatis.session.SqlSession;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import javax.naming.Name;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;
/**
* @Author 龚道松
* @Date 2019/6/30 11:32
* @Wersion 1.0
* 推荐使用Spring的项目就可以使用Spring的单元测试,可以自动注入我们的组件
* 1,导入springTest,模块
* 2,使用注解@ContextConfiguration指定spring配置文件位置
* 3,直接autowired要用到的组件即可
**/
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = {"classpath:applicationContext.xml"})
public class MapperTest {
@Autowired
SqlSession sqlSession;
@Autowired
EmployeeMapper employeeMapper;
@Autowired
DepartmentMapper departmentMapper;
@Test
public void testCrud() {
System.out.println(departmentMapper);
List employees = employeeMapper.selectByExample(null);
System.out.println(employees);
// 插入部门
/* departmentMapper.insert(new Department(null, "开发部"));
departmentMapper.insert(new Department(null, "测试部"));*/
// departmentMapper.updateByPrimaryKey(new Department(6, "后勤部"));
// departmentMapper.updateByPrimaryKey(new Department(7, "前台"));
// departmentMapper.updateByPrimaryKey(new Department(8, "web"));
// departmentMapper.deleteByPrimaryKey(1);
/*List list = new ArrayList<>();
Department select = departmentMapper.selectByPrimaryKey(2);
Department select1 = departmentMapper.selectByPrimaryKey(3);
list.add(select);
list.add(select1);
System.out.println(list);
Employee employee = employeeMapper.selectByPrimaryKeyWithDept(1);
System.out.println(employee);*/
// 部门插入
// departmentMapper.insertSelective(new Department(null, "研发部"));
// employeeMapper.insertSelective(new Employee(null,"zhangsan","M","[email protected] ",3));
// 测试员工插入
// employeeMapper.insert(new Employee(null,"李四","m","[email protected] ",2));
// 批量插入多个员工,使用可以实现批量操作的sqlsession
// for循环查就不是一个批量的插入
/*for (){
employeeMapper.insert(new Employee(null,"M","[email protected] ",2));
}*/
// 批量插入多个员工,使用可以实现批量操作的sqlsession
/* EmployeeMapper mapper = sqlSession.getMapper(EmployeeMapper.class);
for (int i = 0; i<1000; i++){
String uid = UUID.randomUUID().toString().substring(0, 5)+i;
mapper.insertSelective(new Employee(null,uid,"M",uid+"@qq.com",3));
}*/
}
}
分页查询
mybatis.config.xml配置文件
添加的分页插件
com.github.pagehelper
pagehelper
5.1.8
service方法
package com.gds.service;
import com.gds.bean.Employee;
import com.gds.dao.EmployeeMapper;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestParam;
import java.util.List;
@Service
public class EmployeeService {
@Autowired
EmployeeMapper employeeMapper;
// 查询所有员工
public List getAll(){
return employeeMapper.selectByExampleWithDept(null);
}
}
controller
package com.gds.controller;
import com.gds.bean.Employee;
import com.gds.service.EmployeeService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import java.util.List;
/**
* @Author 龚道松
* @Date 2019/7/1 20:34
* @Wersion 1.0
* 处理员工CRUD
**/
@Controller
public class EmployeeCotroller {
@Autowired
EmployeeService employeeService;
@RequestMapping("/emps")
public String getEmps(@RequestParam(value = "pn",defaultValue = "1") Integer pn, Model model){
//引入分页插件pagehelper,传入页码,以及每页显示数据
PageHelper.startPage(pn,5);
List emps = employeeService.getAll();
// f分装了分页信息,包括查询的数据,传入连续显示的页数
PageInfo pageInfo = new PageInfo(emps,5);
model.addAttribute("pageinfo",pageInfo);
return "list";
}
}
spring提供的测试
package com.gds.test;
import com.gds.bean.Employee;
import com.github.pagehelper.PageInfo;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mock.web.MockHttpServletRequest;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.context.web.WebAppConfiguration;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.MockMvcBuilder;
import org.springframework.test.web.servlet.MvcResult;
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
import org.springframework.web.context.WebApplicationContext;
import java.util.List;
/**
* @Author 龚道松
* @Date 2019/7/1 21:23
* @Wersion 1.0
* 使用Spring提供的测试请求功能,测试CRUD请求的正确性
**/
@RunWith(SpringJUnit4ClassRunner.class)
@WebAppConfiguration
@ContextConfiguration(locations = {"classpath:applicationContext.xml","classpath:springmvc.xml"})
public class MvcTest {
// 传入springMVC的ioc
@Autowired
WebApplicationContext context;
// 虚拟mvc请求,获取处理结果
MockMvc mockMvc;
@Before
public void initMockMvc(){
mockMvc = MockMvcBuilders.webAppContextSetup(context).build();
}
// 测试分页的方法
@Test
public void testPage() throws Exception {
// 模拟请求,拿到返回值
MvcResult result = mockMvc.perform(MockMvcRequestBuilders.get("/emps").param("pn", "5")).andReturn();
// 请求成功后。请求域中会有pageinfo,取出pageinfo验证
MockHttpServletRequest request = result.getRequest();
PageInfo pi = (PageInfo) request.getAttribute("pageinfo");
System.out.println("当前页码"+pi.getPageNum());
System.out.println("总页码"+pi.getPages());
System.out.println("总记录数"+pi.getTotal());
System.out.println("在页面需要连续显示的页码");
int[] nums = pi.getNavigatepageNums();
for (int i : nums){
System.out.print(""+i);
}
// 获取员工数据
List list = pi.getList();
for (Employee employee : list){
System.out.println("ID:"+employee.getdId()+"NAME:"+employee.getEmpName()+"");
}
}
}
mvctest
javax.servlet
javax.servlet-api
4.0.1
provided
list.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
员工列表
<%
pageContext.setAttribute("APP_PATH",request.getContextPath());
%>
<%–引入jquery–%>
<%–引入样式–%>
按钮
按钮样式
初步的list表单页面
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
员工列表
<%
pageContext.setAttribute("APP_PATH",request.getContextPath());
%>
去数据库信息
效果图
pageinfo的类
查询用ajax改造
添加jackson依赖(返回json字符串支持)
com.fasterxml.jackson.core
jackson-databind
2.9.8
提示信息表
package com.gds.bean;
import java.util.HashMap;
import java.util.Map;
/**
* @Author 龚道松
* @Date 2019/7/2 9:46
* @Wersion 1.0
**/
public class Msg {
// 状态码 100-成功,200-失败
private Integer code;
// 提示信息
private String msg;
// 要返回给浏览器的数据
private Map extend = new HashMap();
public static Msg success(){
Msg result = new Msg();
result.setCode(100);
result.setMsg("处理成功!");
return result;
}
public static Msg fail(){
Msg result = new Msg();
result.setCode(200);
result.setMsg("处理失败!");
return result;
}
public Msg add(String key, Object value){
this.getExtend().put(key,value);
return this;
}
public Map getExtend() {
return extend;
}
public void setExtend(Map extend) {
this.extend = extend;
}
public Integer getCode() {
return code;
}
public void setCode(Integer code) {
this.code = code;
}
public String getMsg() {
return msg;
}
public void setMsg(String msg) {
this.msg = msg;
}
}
修改后的index页面
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
Index
<%
pageContext.setAttribute("APP_PATH",request.getContextPath());
%>
#
empName
gender
email
deptName
操作
问题
mybatis配置文件
index.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
Index
<%
pageContext.setAttribute("APP_PATH",request.getContextPath());
%>
#
empName
gender
email
deptName
操作
员工新增按钮添加模态框
效果图
校验用户名
清空表单
清空
后端校验引依赖
修改之前的方法
更新员工信息用静态控件
报错信息
put请求失效的原因
配置put过滤器
当个删除
全选/全不选功能
后来创建的按钮就用下面方式绑事件
案例地址: https://github.com/gongdaosong/ssm-CRUD.git