准备工作
上图mapper类型错了,不是class,是interface,修正过后的图片,如下所示
修正如下
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost:3306/webm
spring.datasource.username=www123
spring.datasource.password=www123
mybatis.configuration.log-impl=org.apache.ibatis.logging.stdout.StdOutImplmybatis.configuration.map-underscore-to-camel-case=true
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;@Data
@NoArgsConstructor
@AllArgsConstructor
public class Result {private Integer code;//响应码,1 代表成功; 0 代表失败private String msg; //响应信息 描述字符串private Object data; //返回的数据//增删改 成功响应public static Result success(){return new Result(1,"success",null);}//查询 成功响应public static Result success(Object data){return new Result(1,"success",data);}//失败响应public static Result error(String msg){return new Result(0,msg,null);}
}
数据库准备
-- 部门管理
create table dept(id int unsigned primary key auto_increment comment '主键ID',name varchar(10) not null unique comment '部门名称',create_time datetime not null comment '创建时间',update_time datetime not null comment '修改时间'
) comment '部门表';insert into dept (id, name, create_time, update_time) values(1,'学工部',now(),now()),(2,'教研部',now(),now()),(3,'咨询部',now(),now()), (4,'就业部',now(),now()),(5,'人事部',now(),now());-- 员工管理(带约束)
create table emp (id int unsigned primary key auto_increment comment 'ID',username varchar(20) not null unique comment '用户名',password varchar(32) default '123456' comment '密码',name varchar(10) not null comment '姓名',gender tinyint unsigned not null comment '性别, 说明: 1 男, 2 女',image varchar(300) comment '图像',job tinyint unsigned comment '职位, 说明: 1 班主任,2 讲师, 3 学工主管, 4 教研主管, 5 咨询师',entrydate date comment '入职时间',dept_id int unsigned comment '部门ID',create_time datetime not null comment '创建时间',update_time datetime not null comment '修改时间'
) comment '员工表';INSERT INTO emp(id, username, password, name, gender, image, job, entrydate,dept_id, create_time, update_time) VALUES(1,'jinyong','123456','金庸',1,'1.jpg',4,'2000-01-01',2,now(),now()),(2,'zhangwuji','123456','张无忌',1,'2.jpg',2,'2015-01-01',2,now(),now()),(3,'yangxiao','123456','杨逍',1,'3.jpg',2,'2008-05-01',2,now(),now()),(4,'weiyixiao','123456','韦一笑',1,'4.jpg',2,'2007-01-01',2,now(),now()),(5,'changyuchun','123456','常遇春',1,'5.jpg',2,'2012-12-05',2,now(),now()),(6,'xiaozhao','123456','小昭',2,'6.jpg',3,'2013-09-05',1,now(),now()),(7,'jixiaofu','123456','纪晓芙',2,'7.jpg',1,'2005-08-01',1,now(),now()),(8,'zhouzhiruo','123456','周芷若',2,'8.jpg',1,'2014-11-09',1,now(),now()),(9,'dingminjun','123456','丁敏君',2,'9.jpg',1,'2011-03-11',1,now(),now()),(10,'zhaomin','123456','赵敏',2,'10.jpg',1,'2013-09-05',1,now(),now()),(11,'luzhangke','123456','鹿杖客',1,'11.jpg',5,'2007-02-01',3,now(),now()),(12,'hebiweng','123456','鹤笔翁',1,'12.jpg',5,'2008-08-18',3,now(),now()),(13,'fangdongbai','123456','方东白',1,'13.jpg',5,'2012-11-01',3,now(),now()),(14,'zhangsanfeng','123456','张三丰',1,'14.jpg',2,'2002-08-01',2,now(),now()),(15,'yulianzhou','123456','俞莲舟',1,'15.jpg',2,'2011-05-01',2,now(),now()),(16,'songyuanqiao','123456','宋远桥',1,'16.jpg',2,'2007-01-01',2,now(),now()),(17,'chenyouliang','123456','陈友谅',1,'17.jpg',NULL,'2015-03-21',NULL,now(),now());
部门管理
查询
测试
package com.web.management.controller;import com.web.management.pojo.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;@Slf4j
@RestController
public class DeptController {@GetMapping("/depts")public Result list(){log.info("查询全部部门数据");return Result.success();}
}
localhost:8080/depts
实现
pojo-controller-service-serviceimpl-mapper
pojo
package com.web.management.pojo;import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;import java.time.LocalDateTime;@Data
@AllArgsConstructor
@NoArgsConstructor
public class Dept {private Integer id; //IDprivate String name; //部门名称private LocalDateTime createTime; //创建时间private LocalDateTime updateTime; //修改时间}
controller
package com.web.management.controller;import com.web.management.pojo.Dept;
import com.web.management.pojo.Result;
import com.web.management.service.DeptService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;import java.util.List;@Slf4j
@RestController
public class DeptController {@Autowiredprivate DeptService deptService;@GetMapping("/depts")public Result list(){log.info("查询全部部门数据");//调用service查询部门数据List<Dept> deptList = deptService.list();return Result.success(deptList);}
}
service
package com.web.management.service;import com.web.management.pojo.Dept;
import org.springframework.stereotype.Service;import java.util.List;public interface DeptService {
// 查询全部部门数据List<Dept> list();
}
serviceimpl
package com.web.management.service.impl;import com.web.management.mapper.DeptMapper;
import com.web.management.pojo.Dept;
import com.web.management.service.DeptService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;import java.util.List;@Service
public class DeptServiceImpl implements DeptService {@Autowiredprivate DeptMapper deptMapper;@Overridepublic List<Dept> list() {return deptMapper.list();}
}
mapper
package com.web.management.mapper;import com.web.management.pojo.Dept;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;import java.util.List;@Mapper
public interface DeptMapper {@Select("select * from dept")List<Dept> list();
}
可以利用标红报错这个功能快速创建。
删除
实现
pojo-controller-service-serviceimpl-mapper
pojo
package com.web.management.pojo;import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;import java.time.LocalDateTime;@Data
@AllArgsConstructor
@NoArgsConstructor
public class Dept {private Integer id; //IDprivate String name; //部门名称private LocalDateTime createTime; //创建时间private LocalDateTime updateTime; //修改时间}
controller
@DeleteMapping("/depts/{id}")public Result delete(@PathVariable Integer id){deptService.delete(id);log.info("成功删除{}",id);return Result.success();}
service
// 删除部门void delete(Integer id);
serviceimpl
@Overridepublic void delete(Integer id) {deptMapper.delete(id);}
mapper
@Delete("delete from dept where id = #{id}")void delete(Integer id);
新增
实现
pojo-controller-service-serviceimpl-mapper
pojo
package com.web.management.pojo;import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;import java.time.LocalDateTime;@Data
@AllArgsConstructor
@NoArgsConstructor
public class Dept {private Integer id; //IDprivate String name; //部门名称private LocalDateTime createTime; //创建时间private LocalDateTime updateTime; //修改时间}
controller
@PostMapping("/depts")public Result add(@RequestBody Dept dept){log.info("新增成功");deptService.add(dept);return Result.success();}
service
void add(Dept dept);
serviceimpl-补全属性
@Overridepublic void add(Dept dept) {dept.setCreateTime(LocalDateTime.now());dept.setUpdateTime(LocalDateTime.now());deptMapper.insert(dept);}
mapper
@Insert("insert into dept(name, create_time, update_time) values(#{name},#{createTime},#{updateTime})")void insert(Dept dept);
员工管理
分页查询
pojo
package com.web.management.pojo;import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;import java.util.List;
@Data
@NoArgsConstructor
@AllArgsConstructor
public class PageBean {private long total;//总记录数private List rows;//数据列表
}
package com.web.management.pojo;import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;import java.time.LocalDate;
import java.time.LocalDateTime;@Data
@AllArgsConstructor
@NoArgsConstructor
public class Emp {private Integer id; //IDprivate String username; //用户名private String password; //密码private String name; //姓名private Short gender; //性别 , 1 男, 2 女private String image; //图像urlprivate Short job; //职位 , 1 班主任 , 2 讲师 , 3 学工主管 , 4 教研主管 , 5 咨询师private LocalDate entrydate; //入职日期private Integer deptId; //部门IDprivate LocalDateTime createTime; //创建时间private LocalDateTime updateTime; //修改时间
}
controller
package com.web.management.controller;import com.web.management.pojo.PageBean;
import com.web.management.pojo.Result;
import com.web.management.service.EmpService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;@Slf4j
@RestController
public class EmpController {@Autowiredprivate EmpService empService;@GetMapping("/emps")public Result pagebean(@RequestParam(defaultValue = "1") int page,@RequestParam(defaultValue = "10") int pageSize){log.info("查询的页数是{},一共{}页",page,pageSize);PageBean pageBean = empService.page(page,pageSize);return Result.success(pageBean);}
}
service
package com.web.management.service;import com.web.management.pojo.PageBean;
import org.springframework.stereotype.Service;public interface EmpService {PageBean page(Integer page, Integer pageSize);}
serviceimpl-补全属性,记得一定要自动注入mapper
package com.web.management.service.impl;import com.web.management.mapper.EmpMapper;
import com.web.management.pojo.Emp;
import com.web.management.pojo.PageBean;
import com.web.management.service.EmpService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;import java.util.List;@Service
public class EmpServiceImpl implements EmpService {@Autowiredprivate EmpMapper empMapper;@Overridepublic PageBean page(Integer page, Integer pageSize) {//查询所有记录数Long total = empMapper.total();//分页查询Integer start = (page - 1) * pageSize;List<Emp> empList = empMapper.page(start, pageSize);PageBean pageBean = new PageBean(total,empList);return pageBean;}
}
mapper
package com.web.management.mapper;import com.web.management.pojo.Emp;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;import java.util.List;@Mapper
public interface EmpMapper {@Select("select count(*) from emp")Long total();@Select("select * from emp limit #{start},#{pageSize}")List<Emp> page(Integer start, Integer pageSize);}
根据条件查询
pojo
package com.web.management.pojo;import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;import java.util.List;
@Data
@NoArgsConstructor
@AllArgsConstructor
public class PageBean {private long total;//总记录数private List rows;//数据列表
}
package com.web.management.pojo;import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;import java.time.LocalDate;
import java.time.LocalDateTime;@Data
@AllArgsConstructor
@NoArgsConstructor
public class Emp {private Integer id; //IDprivate String username; //用户名private String password; //密码private String name; //姓名private Short gender; //性别 , 1 男, 2 女private String image; //图像urlprivate Short job; //职位 , 1 班主任 , 2 讲师 , 3 学工主管 , 4 教研主管 , 5 咨询师private LocalDate entrydate; //入职日期private Integer deptId; //部门IDprivate LocalDateTime createTime; //创建时间private LocalDateTime updateTime; //修改时间
}
controller
package com.web.management.controller;import com.web.management.pojo.PageBean;
import com.web.management.pojo.Result;
import com.web.management.service.EmpService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;import java.time.LocalDate;
import java.time.LocalDateTime;@Slf4j
@RestController
public class EmpController {@Autowiredprivate EmpService empService;@GetMapping("/emps")public Result pagebean(@RequestParam(defaultValue = "1") int page,@RequestParam(defaultValue = "10") int pageSize,String name, Short gender,@DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate begin,@DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate end){log.info("查询的页数是{},一共{}页",page,pageSize,name,gender,begin,end);PageBean pageBean = empService.page(page,pageSize,name,gender,begin,end);return Result.success(pageBean);}
}
service
package com.web.management.service;import com.web.management.pojo.PageBean;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.stereotype.Service;import java.time.LocalDate;public interface EmpService {PageBean page(Integer page, Integer pageSize,String name, Short gender,LocalDate begin, LocalDate end);}
serviceimpl-补全属性,记得一定要自动注入mapper
package com.web.management.service.impl;import com.web.management.mapper.EmpMapper;
import com.web.management.pojo.Emp;
import com.web.management.pojo.PageBean;
import com.web.management.service.EmpService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;import java.time.LocalDate;
import java.util.List;@Service
public class EmpServiceImpl implements EmpService {@Autowiredprivate EmpMapper empMapper;@Overridepublic PageBean page(Integer page, Integer pageSize, String name, Short gender, LocalDate begin, LocalDate end) {//查询所有记录数Long total = empMapper.total();//分页查询Integer start = (page - 1) * pageSize;List<Emp> empList = empMapper.li(start, pageSize,name,gender,begin,end);PageBean pageBean = new PageBean(total,empList);return pageBean;}
}
mapper
package com.web.management.mapper;import com.web.management.pojo.Emp;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;import java.time.LocalDate;
import java.util.List;@Mapper
public interface EmpMapper {@Select("select count(*) from emp")Long total();public List<Emp> li(Integer start, Integer pageSize, String name, Short gender, LocalDate begin, LocalDate end);}
xml文件
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapperPUBLIC "-//mybatis.org//DTD Mapper 3.0//EN""https://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.web.management.mapper.EmpMapper"><select id="li" resultType="com.web.management.pojo.Emp">select *from emp<where><if test="name != null and name != ''">name like concat('%',#{name},'%')</if><if test="gender != null">and gender = #{gender}</if><if test="begin != null and end != null">and entrydate between #{begin} and #{end}</if></where>order by update_time desc</select></mapper>
这里有个疑问,下列sql语句在navicat中可以实现,但是在idea中显示语法错误,有大佬知道为什么吗
SELECT * FROM emp
WHERE name LIKE CONCAT('%','张','%') AND gender = 1 and entrydate BETWEEN '2000-01-01' and '2015-01-01'
order BY entrydate desc LIMIT 1,1