1. pom
org.springframework.boot
spring-boot-starter-web
org.springframework.boot
spring-boot-starter-test
test
org.junit.vintage
junit-vintage-engine
org.projectlombok
lombok
mysql
mysql-connector-java
5.1.47
javax.servlet
javax.servlet-api
javax.servlet
jstl
com.github.pagehelper
pagehelper-spring-boot-starter
1.2.3
tk.mybatis
mapper-spring-boot-starter
2.1.5
org.springframework
spring-jdbc
5.2.3.RELEASE
com.alibaba
druid-spring-boot-starter
1.1.10
junit
junit
4.12
org.junit.jupiter
junit-jupiter-api
5.5.2
io.springfox
springfox-swagger2
2.6.1
io.springfox
springfox-swagger-ui
2.6.1
org.thymeleaf
thymeleaf
3.0.11.RELEASE
org.hibernate
hibernate-validator
6.0.9.Final
org.springframework.boot
spring-boot-test
org.springframework
spring-test
5.2.3.RELEASE
compile
org.springframework.boot
spring-boot-maven-plugin
true
org.mybatis.generator
mybatis-generator-maven-plugin
1.3.7
true
true
src/main/resources/generatorConfig.xml
tk.mybatis
mapper
4.0.3
2. 配置文件
(a) application.properties
###datasource
spring.datasource.url = jdbc:mysql://152.136.27.48:3306/d_xiaokai?useUnicode=true&characterEncoding=utf8&serverTimezone=GMT%2B8&useSSL=false
spring.datasource.username = root
spring.datasource.password = 123456
spring.datasource.driverClassName = com.mysql.jdbc.Driver
spring.datasource.max-active=20
spring.datasource.max-idle=8
spring.datasource.min-idle=8
spring.datasource.initial-size=10
#SpringBoot项目解决全局响应返回中文乱码问题
spring.http.encoding.force=true
spring.http.encoding.charset=UTF-8
spring.http.encoding.enabled=true
server.tomcat.uri-encoding=UTF-8
(b) application.yml
#指定端口号
server:
port: 8001
##配置mmysql数据源
spring:
datasource:
# url: jdbc:mysql://152.136.27.48:3306/d_xiaokai?serverTimezone=GMT%2B8
# username: root
# password: 123456
# driver-class-name: com.mysql.jdbc.Driver
#配置数据连接池
type: com.alibaba.druid.pool.DruidDataSource
initialSize: 5
minIdle: 5
maxActive: 20
maxWait: 60000
timeBetweenEvictionRunsMillis: 60000
minEvictableIdleTimeMillis: 300000
validationQuery: SELECT 1 FROM DUAL
testWhileIdle: true
testOnBorrow: true
testOnReturn: false
poolPreparedStatements: true
# 配置监控统计拦截的filters,去掉后监控界面sql无法统计,'wall'用于防火墙
filters: stat,wall
maxPoolPreparedStatementPerConnectionSize: 20
useGlobalDataSourceStat: true
connectionProperties: druid.stat.mergeSql=true;druid.stat.slowSqlMillis=500
#配置MYBATIS
mybatis:
#指定映射文件的位置
mapper-locations: classpath:mapper/*.xml
#指定实体类包的位置
type-aliases-package: com.tina.springboot.springboot.entity
configuration:
map-underscore-to-camel-case: true #下划线转驼峰
#配置分页插件
pagehelper:
helperDialect: mysql
reasonable: true #跳转分页合理化参数
supportMethodsArguments: true
params: count=countSql
#打印sql
logging:
level:
com.tina.springboot.springboot.mapper : debug
(c)generatorConfig.xml
3. 实体类
Depart
package com.tina.springboot.springboot.entity;
import lombok.Data;
import java.io.Serializable;
import javax.persistence.*;
@Table(name = "t_depart")
@Data
public class Depart implements Serializable {
@Id
@Column(name = "dep_id")
private String depId;
@Column(name = "dep_name")
private String depName;
/**
* 是否删除 删除为0 ,不删除为1, 默认为1
*/
@Column(name = "del_flag")
private String delFlag;
public Depart(String depId, String depName, String delFlag) {
this.depId = depId;
this.depName = depName;
this.delFlag = delFlag;
}
public Depart() {
}
}
Employee
package com.tina.springboot.springboot.entity;
import lombok.Data;
import java.io.Serializable;
import java.util.Date;
import javax.persistence.*;
@Table(name = "t_employee")
@Data
public class Employee implements Serializable {
@Id
@Column(name = "emp_id")
private String empId;
@Column(name = "emp_name")
private String empName;
private String gender;
private String email;
@Column(name = "create_time")
private String createTime;
@Column(name = "dep_id")
private String depId;
/**
* 是否删除 0是删除 1 是不删除 默认是1
*/
@Column(name = "del_flag")
private String delFlag;
public Employee(String empId, String empName, String gender, String email, String createTime, String depId, String delFlag) {
this.empId = empId;
this.empName = empName;
this.gender = gender;
this.email = email;
this.createTime = createTime;
this.depId = depId;
this.delFlag = delFlag;
}
public Employee() {
}
}
4. dao
package com.tina.springboot.springboot.mapper;
import com.tina.springboot.springboot.entity.Depart;
import tk.mybatis.mapper.common.Mapper;
import java.util.List;
public interface DepartMapper extends Mapper {
List selectDepExceptDel();
}
package com.tina.springboot.springboot.mapper;
import com.tina.springboot.springboot.entity.Employee;
import com.tina.springboot.springboot.resp.EmployeeResp;
import tk.mybatis.mapper.common.Mapper;
import java.util.List;
public interface EmployeeMapper extends Mapper {
List selectEmpsWithDep();
EmployeeResp selectEmpById(String empId);
}
5. service
package com.tina.springboot.springboot.service.Impl;
import com.tina.springboot.springboot.entity.Depart;
import com.tina.springboot.springboot.entity.Employee;
import com.tina.springboot.springboot.exception.Code;
import com.tina.springboot.springboot.exception.EntityResp;
import com.tina.springboot.springboot.mapper.DepartMapper;
import com.tina.springboot.springboot.mapper.EmployeeMapper;
import com.tina.springboot.springboot.resp.EmployeeResp;
import com.tina.springboot.springboot.service.EmployeeService;
import com.tina.springboot.springboot.utils.CreatedTime;
import org.junit.Assert;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import javax.annotation.Resource;
import java.util.List;
import java.util.UUID;
/**
* @program: springboot-mybatis-plugins
* @description
* @author: tina.liu
* @create: 2020-02-03 17:55
**/
@Service
public class EmployeeServiceImpl implements EmployeeService {
@Resource
private EmployeeMapper employeeMapper;
@Resource
private DepartMapper departMapper;
@Override////根据查询员工的信息和部门
public List selectEmpsWithDep() {
List employeeRespList = employeeMapper.selectEmpsWithDep();
return employeeRespList;
}
@Override////根据id查询员工的信息及所属的部门
public EntityResp selectEmpById(String empId) {
EmployeeResp employeeResp = employeeMapper.selectEmpById(empId);
// 添加所有部分
List departs = this.departMapper.selectDepExceptDel();
employeeResp.setDepartList(departs);
return new EntityResp<>(employeeResp, Code.SERVER_SUCCESS);
}
@Override//// 根据id删除员工的信息
public EntityResp
package com.tina.springboot.springboot.service.Impl;
import com.tina.springboot.springboot.entity.Depart;
import com.tina.springboot.springboot.exception.Code;
import com.tina.springboot.springboot.exception.EntityResp;
import com.tina.springboot.springboot.mapper.DepartMapper;
import com.tina.springboot.springboot.req.DepartReq;
import com.tina.springboot.springboot.req.UpdateDepartReq;
import com.tina.springboot.springboot.service.DepartService;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.util.List;
import java.util.UUID;
/**
* @program: springboot-mybatis-plugins
* @description
* @author: tina.liu
* @create: 2020-02-03 18:35
**/
@Service
public class DepartServiceImpl implements DepartService {
@Resource
private DepartMapper departMapper;
@Override
public void insert(Depart depart) {
departMapper.insertSelective(depart);
}
@Override
public Depart selectById(String s) {
return departMapper.selectByPrimaryKey(s);
}
@Override////查询员工表中所有的部门
public EntityResp selectDepName() {
List departs = departMapper.selectDepExceptDel();
return new EntityResp<>(departs, Code.SERVER_SUCCESS);
}
@Override////根据ID删除部门表中的部门
public EntityResp deleteDepById(String depId) {
int effected = departMapper.deleteByPrimaryKey(depId);
return new EntityResp<>(effected,Code.SERVER_SUCCESS);
}
@Override//新增部门表的信息
public EntityResp AddDepart(DepartReq req) {
Depart depart = new Depart();
String depId= UUID.randomUUID().toString().substring(0, 5);
depart.setDepId(depId);
depart.setDelFlag("1");
depart.setDepName(req.getDepName());
int effected = departMapper.insertSelective(depart);
return new EntityResp<>(effected,Code.SERVER_SUCCESS);
}
@Override////修改部门表中的部门名
public EntityResp updateDepName(UpdateDepartReq depName) {
Depart depart = new Depart();
BeanUtils.copyProperties(depName,depart);
int effected= departMapper.updateByPrimaryKeySelective(depart);
return new EntityResp<>(effected,Code.SERVER_SUCCESS);
}
}//类的大括号
5, 自动生成的映射文件
6. controller
package com.tina.springboot.springboot.controller;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.tina.springboot.springboot.entity.Employee;
import com.tina.springboot.springboot.exception.Code;
import com.tina.springboot.springboot.exception.EntityResp;
import com.tina.springboot.springboot.req.DeleteReq;
import com.tina.springboot.springboot.resp.EmployeeResp;
import com.tina.springboot.springboot.service.EmployeeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;
import javax.validation.Valid;
import java.util.List;
/**
* @program: springboot-mybatis-plugins
* @description
* @author: tina.liu
* @create: 2020-02-03 17:56
**/
@RestController
@Api(value = "Employee模块")
@RequestMapping("/test")
@CrossOrigin
@tk.mybatis.spring.annotation.MapperScan(basePackages= "com.tina.springboot.springboot.mapper") //扫秒指定位置的mapper接口
public class EmployeeController {
@Autowired
private EmployeeService employeeService;
//批量删除员工的信息
@PostMapping(value = "deleteEmpByIds")
@ApiOperation(value = "批量删除员工的信息")
public EntityResp deleteEmpByIds(@RequestBody DeleteReq req){
return employeeService.deleteEmpByIds(req.getIds());
}
// 添加员工 //对邮箱进行校验,有效方可以数据方可以储存,后台数据库会对邮箱的唯一性自动校验
@PostMapping(value = "/addEmp")
@ApiOperation(value = "添加员工")
public EntityResp addEmp(@Valid @RequestBody Employee employee,
BindingResult bindingResult){
if(bindingResult.hasErrors()){
return new EntityResp<>("邮箱进行JSR303校验失败", Code.SERVER_OTHER_ERROR);
}else {
return employeeService.addEmp(employee);
}
}
////修改员工信息及部门信息 并添加valid进行校验
@PutMapping(value = "/updateEmp")
@ApiOperation(value = "//修改员工信息及部门信息")
public EntityResp updateEmp(@Valid @RequestBody Employee employee,
BindingResult bindingResult){
if(bindingResult.hasErrors()){
return new EntityResp<>("对邮箱进行JSR303校验失败",Code.SERVER_OTHER_ERROR);
}else{
return employeeService.updateEmp(employee);
}
}
//查询员工的信息和部门
@GetMapping(value = "/selectEmpsWithDep")
@ApiOperation(value = "查询员工的信息和部门")
public EntityResp> selectEmpsWithDep(@RequestParam(value = "pages",defaultValue = "1") Integer pages,
@RequestParam(value ="rows",defaultValue = "5") Integer rows){
//将当前页码和每页数量传入进入
PageHelper.startPage(pages,rows);
List employeeRespList = employeeService.selectEmpsWithDep();
//将数据库返回的数据封装到分页插件中去
PageInfo employeeRespPageInfo = new PageInfo<>(employeeRespList,5);
return new EntityResp>(employeeRespPageInfo, Code.SERVER_SUCCESS);
}
//根据id查询员工的信息及所属的部门
@GetMapping(value = "/selectEmpById/{empId}")
@ApiOperation(value = "根据id查询员工的信息及所属的部门")
public EntityResp selectEmpById(@PathVariable(value = "empId") String empId){
return employeeService.selectEmpById(empId);
}
// 根据id删除员工的信息
@DeleteMapping(value = "/deleteEmpById/{empId}")
@ApiOperation(value = "根据id删除员工的信息")
public EntityResp deleteEmpById(@PathVariable(value = "empId") String empId){
return employeeService.deleteEmpById(empId);
}
}//类的大括号
package com.tina.springboot.springboot.controller;
import com.tina.springboot.springboot.exception.EntityResp;
import com.tina.springboot.springboot.req.DepartReq;
import com.tina.springboot.springboot.req.UpdateDepartReq;
import com.tina.springboot.springboot.service.DepartService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import tk.mybatis.spring.annotation.MapperScan;
/**
* @program: springboot-mybatis-plugins
* @description
* @author: tina.liu
* @create: 2020-02-03 18:36
**/
@Controller
@ResponseBody
@CrossOrigin
@MapperScan(basePackages= "com.tina.springboot.springboot.mapper") //扫秒指定位置的mapper接口
public class DepartController {
@Autowired
private DepartService departService;
//查询员工表中所有的部门
@GetMapping(value = "/selectDepName")
@ApiOperation(value = "查询员工表中所有的部门")
public EntityResp selectDepName(){
return departService.selectDepName();
}
//根据ID删除部门表中的部门
@DeleteMapping(value = "/deleteDepById/{depId}")
@ApiOperation(value = "根据ID删除部门表中的部门")
public EntityResp deleteDepById(@PathVariable(value = "depId") String depId){
return departService.deleteDepById(depId);
}
//新增部门表的信息
@PostMapping(value = "/AddDepart")
@ApiOperation(value = "新增部门表的信息")
public EntityResp AddDepart(@RequestBody DepartReq req ){
return departService.AddDepart(req);
}
//修改部门表中的部门名
@PutMapping(value = "/updateDepName")
@ApiOperation(value = "修改部门表中的部门名")
public EntityResp updateDepName(@RequestBody UpdateDepartReq req){
return departService.updateDepName(req);
}
}//类的大括号
7. 测试类
(a)config
@Configuration
public class getSqlSession {
@Autowired
private SqlSessionFactory sqlSessionFactory;
@Autowired
SqlSession sqlSession;
@Bean
public SqlSessionTemplate sqlSessionTemplate(){
return new SqlSessionTemplate(sqlSessionFactory, ExecutorType.BATCH);
}
}
(b) test
@MapperScan(basePackages = "com.tina.springboot.springboot.mapper") //扫秒指定位置的mapper接口
@RunWith(SpringJUnit4ClassRunner.class) //junit单元测试
@SpringBootTest(classes = SpringbootMybatisPluginsApplication.class) //指定启动类
public class TestDepart {
@Autowired
private DepartService departService;
@Autowired
private DepartMapper departMapper;
@Autowired
SqlSession sqlSession;
@Test
public void test1() {
EmployeeMapper employeeMapper = (EmployeeMapper) sqlSession.getMapper(EmployeeMapper.class);
for (int i = 0; i < 10; i++) {
String id = UUID.randomUUID().toString().substring(0, 8);
Employee employee = new Employee(id, id + "bobo", "男", id + "@qq.com", CreatedTime.getTime(), "2", "1");
employeeMapper.insertSelective(employee);
}
System.out.println("添加完成");
}
//测试Depart的通用Mapper的方法
@Test
public void TEST() {
List departs = departMapper.selectAll();
System.out.println("depart 查询所有的结果集合为" + ":" + departs);
Depart depart = departMapper.selectByPrimaryKey("1");
System.out.println("depart的结果为" + ":" + depart);
}
@Test
public void test2() {
Depart depart = new Depart();
depart.setDepId("1");
Depart result = departMapper.selectOne(depart);
System.out.println("查询的结果为:" + result);
}
@Test
public void test3() {
List departLists = departMapper.selectDepExceptDel();
System.out.println("查询的结果为:" + departLists);
}
}//类的大括号