javaEEday12-springAOP-快速入门
This commit is contained in:
+13
@@ -0,0 +1,13 @@
|
||||
package com.inmind;
|
||||
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
|
||||
@SpringBootApplication
|
||||
public class SpringbootAopQickstartApplication {
|
||||
|
||||
public static void main(String[] args) {
|
||||
SpringApplication.run(SpringbootAopQickstartApplication.class, args);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,77 @@
|
||||
package com.inmind.controller;
|
||||
|
||||
import com.inmind.pojo.Dept;
|
||||
import com.inmind.pojo.Result;
|
||||
import com.inmind.service.DeptService;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
@RestController
|
||||
@Slf4j//自动生成logback日志框架的对象log,在整个类中都可以直接使用logger
|
||||
@RequestMapping("/depts")//部门功能的统一URL前缀
|
||||
public class DeptController {
|
||||
|
||||
@Autowired//让spring容器进行依赖注入
|
||||
private DeptService deptService;
|
||||
|
||||
//logback日志框架
|
||||
//private static Logger logger = LoggerFactory.getLogger("com.inmind.controller.DeptController");
|
||||
|
||||
// @RequestMapping(value = "/depts",method = {RequestMethod.GET})
|
||||
// @GetMapping("/depts")
|
||||
@GetMapping
|
||||
public Result deptList(){
|
||||
//接收请求
|
||||
log.info("查询全部部门数据请求");
|
||||
//调用业务层的查询部门的功能
|
||||
List<Dept> deptList = deptService.deptList();
|
||||
return Result.success(deptList);
|
||||
}
|
||||
|
||||
// @RequestMapping(value = "/depts/{id}",method = {RequestMethod.DELETE})
|
||||
// @DeleteMapping("/depts/{id}")
|
||||
@DeleteMapping("/{id}")
|
||||
//PathVariable:路径参数的注解,用来将url地址上的id参数赋值给形参id
|
||||
public Result deleteDept(@PathVariable Integer id) throws Exception {
|
||||
log.info("删除部门:{}",id);
|
||||
|
||||
//调用业务层的删除部门功能
|
||||
deptService.deleteDept(id);
|
||||
return Result.success();
|
||||
}
|
||||
|
||||
//请求参数为json时{} [],要使用java的对象或者集合来接收
|
||||
// @PostMapping("/depts")
|
||||
@PostMapping
|
||||
public Result addDept(@RequestBody Dept dept) {
|
||||
log.info("新增部门{}",dept);
|
||||
//调用业务层的新增部门方法
|
||||
deptService.addDept(dept);
|
||||
return Result.success();
|
||||
}
|
||||
|
||||
/*
|
||||
根据部门id查询部门数据(点击编辑时,内容回显)
|
||||
*/
|
||||
@GetMapping("/{id}")
|
||||
public Result getDeptById(@PathVariable Integer id){
|
||||
log.info("查询部门:id--{}",id);
|
||||
//调用业务层,获取部门数据
|
||||
Dept dept = deptService.getDeptById(id);
|
||||
return Result.success(dept);
|
||||
}
|
||||
|
||||
/*
|
||||
根据id修改部门
|
||||
*/
|
||||
@PutMapping
|
||||
public Result update(@RequestBody Dept dept){
|
||||
log.info("更新部门:{}",dept);
|
||||
//调用业务层更新部门名称
|
||||
deptService.update(dept);
|
||||
return Result.success();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,27 @@
|
||||
package com.inmind.mapper;
|
||||
|
||||
import com.inmind.pojo.Dept;
|
||||
import org.apache.ibatis.annotations.*;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
@Mapper //让mybatis框架识别,并自动生成实现类,发送sql给mysql获取数据的
|
||||
public interface DeptMapper {
|
||||
@Select("select * from dept")
|
||||
List<Dept> deptList();
|
||||
|
||||
@Delete("delete from dept where id = #{id}")
|
||||
void deleteDept(Integer id);
|
||||
|
||||
@Insert("insert into dept (name, create_time, update_time) values " +
|
||||
"(#{name},#{createTime},#{updateTime})")
|
||||
void addDept(Dept dept);
|
||||
|
||||
//根据id查询部门
|
||||
@Select("select * from dept where id = #{id}")
|
||||
Dept getDeptById(Integer id);
|
||||
|
||||
//根据id更新部门名称
|
||||
@Update("update dept set name = #{name},update_time = #{updateTime} where id = #{id} ")
|
||||
void update(Dept dept);
|
||||
}
|
||||
@@ -0,0 +1,20 @@
|
||||
package com.inmind.pojo;
|
||||
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Data;
|
||||
import lombok.NoArgsConstructor;
|
||||
|
||||
import java.time.LocalDateTime;
|
||||
|
||||
/**
|
||||
* 部门实体类
|
||||
*/
|
||||
@Data
|
||||
@NoArgsConstructor
|
||||
@AllArgsConstructor
|
||||
public class Dept {
|
||||
private Integer id; //ID
|
||||
private String name; //部门名称
|
||||
private LocalDateTime createTime; //创建时间
|
||||
private LocalDateTime updateTime; //修改时间
|
||||
}
|
||||
@@ -0,0 +1,27 @@
|
||||
package com.inmind.pojo;
|
||||
|
||||
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);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,33 @@
|
||||
package com.inmind.service;
|
||||
|
||||
import com.inmind.pojo.Dept;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
public interface DeptService {
|
||||
|
||||
/**
|
||||
* 查询所有部门
|
||||
* @return 部门的集合
|
||||
*/
|
||||
List<Dept> deptList();
|
||||
|
||||
/**
|
||||
* 删除部门
|
||||
* @param id 部门id
|
||||
*/
|
||||
void deleteDept(Integer id) throws Exception;
|
||||
|
||||
/**
|
||||
* 新增部门
|
||||
* @param dept 部门信息
|
||||
*/
|
||||
void addDept(Dept dept);
|
||||
|
||||
|
||||
//根据id查询部门
|
||||
Dept getDeptById(Integer id);
|
||||
|
||||
//根据id更新部门
|
||||
void update(Dept dept);
|
||||
}
|
||||
@@ -0,0 +1,58 @@
|
||||
package com.inmind.service.impl;
|
||||
|
||||
import com.inmind.mapper.DeptMapper;
|
||||
import com.inmind.pojo.Dept;
|
||||
import com.inmind.service.DeptService;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
|
||||
import java.time.LocalDateTime;
|
||||
import java.util.List;
|
||||
|
||||
@Service
|
||||
public class DeptServiceImpl implements DeptService {
|
||||
|
||||
@Autowired
|
||||
private DeptMapper deptMapper;
|
||||
|
||||
|
||||
//查询部门列表
|
||||
@Override
|
||||
public List<Dept> deptList() {
|
||||
//调用deptMapper获取部门列表数据
|
||||
List<Dept> deptList = deptMapper.deptList();
|
||||
return deptList;
|
||||
}
|
||||
|
||||
//根据id删除部门
|
||||
@Transactional(rollbackFor = Exception.class)//让当前方法,处于事务管理下(在方法调用前,执行start transaction)
|
||||
@Override
|
||||
public void deleteDept(Integer id) throws Exception {
|
||||
|
||||
}
|
||||
|
||||
//新增部门
|
||||
@Override
|
||||
public void addDept(Dept dept) {
|
||||
//补充基础属性,创建时间和更新时间
|
||||
dept.setCreateTime(LocalDateTime.now());
|
||||
dept.setUpdateTime(LocalDateTime.now());
|
||||
deptMapper.addDept(dept);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Dept getDeptById(Integer id) {
|
||||
//调用Mapper查询部门数据即可
|
||||
Dept dept = deptMapper.getDeptById(id);
|
||||
return dept;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void update(Dept dept) {
|
||||
//补充更新时间数据
|
||||
dept.setUpdateTime(LocalDateTime.now());
|
||||
//调用mapper更新部门
|
||||
deptMapper.update(dept);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,19 @@
|
||||
server:
|
||||
port : 8080
|
||||
spring:
|
||||
application:
|
||||
name: aop-qickstart
|
||||
datasource:
|
||||
driver-class-name: com.mysql.cj.jdbc.Driver
|
||||
url: jdbc:mysql://localhost:3306/tlias
|
||||
username: root
|
||||
password: 1234
|
||||
servlet:
|
||||
multipart:
|
||||
max-file-size: 10MB
|
||||
max-request-size: 100MB
|
||||
mybatis:
|
||||
configuration:
|
||||
log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
|
||||
map-underscore-to-camel-case: true
|
||||
|
||||
+13
@@ -0,0 +1,13 @@
|
||||
package com.inmind;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.springframework.boot.test.context.SpringBootTest;
|
||||
|
||||
@SpringBootTest
|
||||
class SpringbootAopQickstartApplicationTests {
|
||||
|
||||
@Test
|
||||
void contextLoads() {
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user