1.配置相关:
server.port=8080
spring.datasource.url=jdbc:mysql://localhost:3306/db_mybatis?useUnicode=true&characterEncoding=UTF-8&useSSL=false&serverTimezone=Asia/Shanghai
spring.datasource.username=root
spring.datasource.password=root
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.jackson.date-format=yyyy-MM-dd HH:mm:ss
spring.jackson.time-zone=GMT+8
mybatis.type-aliases-package=cn.sjxy.springboot.mybatis_study.domain
mybatis.mapper-locations=classpath:mapper/*.xml
mybatis.configuration.map-underscore-to-camel-case=true
2.主配置类
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import springfox.documentation.swagger2.annotations.EnableSwagger2;
@EnableSwagger2
@MapperScan(value = {"cn.sjxy.springboot.mybatis_study.mapper"})
@SpringBootApplication
public class MybatisStudyApplication {
public static void main(String[] args) {
SpringApplication.run(MybatisStudyApplication.class, args);
}
}
3.pom文件
1.8
2.0.3
1.2.5
3.3.9
2.7.0
org.springframework.boot
spring-boot-starter-web
org.springframework.boot
spring-boot-starter-test
test
org.mybatis.spring.boot
mybatis-spring-boot-starter
2.0.1
tk.mybatis
mapper-spring-boot-starter
${mapper.starter.version}
com.github.pagehelper
pagehelper-spring-boot-starter
${pageHelper.starter.version}
com.alibaba
fastjson
1.2.58
mysql
mysql-connector-java
runtime
org.projectlombok
lombok
true
io.springfox
springfox-swagger2
2.7.0
io.springfox
springfox-swagger-ui
2.7.0
org.apache.httpcomponents
httpclient
4.5.9
org.apache.httpcomponents
httpcore
4.4.11
org.springframework.boot
spring-boot-maven-plugin
org.apache.maven.plugins
maven-surefire-plugin
2.18.1
true
4.swagger2配置,综合我上一篇的swagger2进行配置
5.实体类
@Data可以自动生成get,set,tostring方法
需要安装一个插件
@ApiModel(“员工信息”)是对实体类的描述
@Data
@ApiModel("员工信息")
public class Employee {
private int id;
private String name;
private Integer age;
private int deptId;
private Department department;
}
@Data
@ApiModel("部门信息")
public class Department {
private int id;
private String name;
private List employee;
}
6.dao层编写
public interface EmployeeMapper {
//查询emp表所有信息,默认的结果集是 EMployee这个实体类
@Select("select * from emp")
List findAll();
//查询emp表中的所有信息,包括了员工对应的部门信息(多对一)
//使用one进行映射
@Select("select * from emp")
@Results(value = {
//查询的主表是emp,需要查询的附属信息在dept表中,这里不能用外连接而是用查询的方法调用。
//就是将emp表中与dept表中对应的字段作为参数传给findDept方法,column就是传的emp表中的字段
@Result(property = "department", column = "dept_id",one = @One(select = "cn.sjxy.springboot.mybatis_study.mapper.DepartmentMapper.findDept"))
})
List findAllWithDept();
//查询一个员工的信息包括所在部门(一对一)
@Select(" SELECT " +
" * " +
" FROM" +
" emp" +
" where" +
" id = #{id} ")
@Results(value = {
@Result(property = "department", column = "dept_id", one = @One(select = "cn.sjxy.springboot.mybatis_study.mapper.DepartmentMapper.findDept"))
})
Employee findById(@Param("id") int id);
//通过部门编号查询员工
@Select(" select " +
" * " +
" from " +
" emp " +
" where dept_id = #{dept_id}")
// @Results(value = {
// @Result(property = "department", column = "dept_id", one=@One(select = "cn.sjxy.springboot.mybatis_study.mapper.DepartmentMapper.findDept"))
// })
List findByDeptId(@Param("dept_id")int dept_id);
//通过编号删除员工
@Delete("DELETE" +
" FROM " +
" emp " +
" WHERE " +
" id=#{id}")
void deleteById(@Param("id") int id);
//插入员工
@Insert(" INSERT " +
" INTO " +
" emp(name,age,dept_id) " +
" VALUES(#{name}, #{age}, #{deptId}) ")
void insert(Employee employee);
//更新员工信息
@Update(" UPDATE " +
" emp " +
" SET name=#{name}, age = #{age} " +
" WHERE " +
" id = #{id}")
void update(Employee employee);
}
public interface DepartmentMapper {
//通过部门id查询部门信息
@Select(" select * from dept where id = #{id}")
List findDept(@Param("id") int id);
//查询所有部门信息,包括部门下的员工信息(一对多)
@Select(" select " +
" * " +
" from " +
"dept")
@Results(value = {
@Result(property = "id", column = "id"),
//查询的主表是dept,需要从emp表中取得相应的信息,就需要将dept对应于emp表的字段作为column的值传给方法findByDeptId
@Result(property = "employee", column = "id",
many = @Many(select =
"cn.sjxy.springboot.mybatis_study.mapper.EmployeeMapper.findByDeptId"))
})
List findDeptWithEmp();
}
7.service层
@Service
public class DepartmentService {
@Resource
private DepartmentMapper departmentMapper;
public List findDeptWithEmp(){
return departmentMapper.findDeptWithEmp();
}
}
*/
@Service
public class EmployeeService {
@Resource
EmployeeMapper employeeMapper;
public List findAll(){
System.out.println(employeeMapper.findByDeptId(1));
return employeeMapper.findAll();
}
public List findAllWithDept(){
return employeeMapper.findAllWithDept();
}
public Employee findById(int id){
return employeeMapper.findById(id);
}
public void deleteById(int id){
employeeMapper.deleteById(id);
}
public void update(Employee employee){
employeeMapper.update(employee);
}
public void insert(Employee employee){
employeeMapper.insert(employee);
}
}
8.controller层
@RestController
@RequestMapping("/api/emp")
@Api(tags = "员工相关")
public class EmpController {
@Resource
private EmployeeService employeeService;
@ApiOperation(value = "查询所有员工")
@GetMapping("/findAll")
public Result findAll(){
return new Result<>(employeeService.findAll()) ;
}
@ApiOperation(value = "查询所有员工及其部门")
@GetMapping("/findAllWithDept")
public Result findAllWithDept(){
return new Result<>(employeeService.findAllWithDept());
}
@ApiOperation(value = "查询员工信息")
@ApiImplicitParam(paramType = "query", name = "id", dataType = "int", value = "用户id",required = true, defaultValue = "1")
@GetMapping("/findById")
public Result findById(@RequestParam("id")int id){
return new Result(employeeService.findById(id));
}
@ApiOperation(value = "删除员工")
@GetMapping("/deleteById")
public void deleteById(@RequestParam("id")int id){
employeeService.deleteById(id);
}
@ApiOperation(value = "修改员工")
@GetMapping("/update")
public void update(Employee employee){
employeeService.update(employee);
}
@ApiOperation(value = "添加员工")
@GetMapping("/insert")
public void insert(Employee employee){
employeeService.insert(employee);
}
}
@RestController
@RequestMapping("/api/dept")
@Api(tags = "部门相关")
public class DepartmentController {
@Resource
private DepartmentService departmentService;
@ApiOperation(value = "部门信息")
@GetMapping("/findDeptWithEmp")
public Result findDeptWithEmp(){
return new Result<>(departmentService.findDeptWithEmp());
}
}