安装依赖
这里有个版本的报错,循环依赖的问题,大家可以去具体查下,我这是sp3,所以要选择高点的版本,否则启动会报错
<!--mybatis起步依赖-->
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.2.2</version>
</dependency>
<!--mysql驱动-->
<dependency>
<groupId>com.mysql</groupId>
<artifactId>mysql-connector-j</artifactId>
<scope>runtime</scope>
</dependency>
<!--分页插件-->
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper-spring-boot-starter</artifactId>
<version>1.4.1</version>
</dependency>
看到这个,就说明安装成功了
application.propertis配置文件
server.port=9090
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost:3306/springapp01
spring.datasource.username=root
spring.datasource.password=123456
mybatis.configuration.log-impl=org.apache.ibatis.logging.stdout.StdOutImpl
mybatis.configuration.map-underscore-to-camel-case=true
- controller
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.*;
import java.time.LocalDate;
import java.util.List;
/**
* 员工管理Controller
*/
@RestController
@Slf4j
@RequestMapping("/emps")
public class EmpController {
@Autowired
private EmpService empService;
@GetMapping
public Result page(
String name,
Short gender,
@DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate begin,
@DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate end,
@RequestParam(defaultValue = "1") Integer page,
@RequestParam(defaultValue = "10") Integer pageSize){
log.info("{},{}.{},{},page:{},pageSize:{}",name,gender,begin,end,page, pageSize);
//PageBean pageBean = empService.page(page, pageSize);
PageBean pageBean = empService.pageHelper(name,gender,begin,end,page, pageSize);
return Result.success(pageBean);
}
/**
* 批量删除员工信息
* delete from emp where id in (1,2,3)
*/
@DeleteMapping("/{ids}")
public Result delete(@PathVariable List<Integer> ids) {
log.info("批量删除操作 {}", ids);
empService.delete(ids);
return Result.success();
}
}
- services
基础类
/**
* 员工管理
*/
public interface EmpService {
PageBean page(Integer page, Integer pageSize);
PageBean pageHelper(String name, Short gender, LocalDate begin,LocalDate end, Integer page, Integer pageSize);
void delete(List<Integer> ids);
}
实现类
package com.itheima.service.impl;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
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 {
@Autowired
private EmpMapper empMapper;
//自己实现的page方法
@Override
public PageBean page(Integer page, Integer pageSize) {
Long count = empMapper.count();
Integer start = (page - 1) * pageSize;
List<Emp> page1 = empMapper.page(start, pageSize);
return new PageBean(count, page1);
}
@Override
public PageBean pageHelper(String name, Short gender, LocalDate begin, LocalDate end, Integer page, Integer pageSize){
//设置分页参数
PageHelper.startPage(page, pageSize);
List<Emp> empList = empMapper.listAll(name, gender, begin,end);
Page<Emp> p = (Page<Emp>) empList;
return new PageBean(p.getTotal(), p.getResult());
}
@Override
public void delete(List<Integer> ids){
empMapper.delete(ids);
}
}
- mapper
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.itheima.mapper.EmpMapper">
<select id="listAll" resultType="com.itheima.pojo.Emp">
select * from emp
<where>
<if test="name != null">
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>
<delete id="delete">
delete from emp where id in
<foreach collection="ids" item="id" open="(" close=")" separator=",">
#{id}
</foreach>
</delete>
</mapper>