diff --git a/eladmin-system/src/main/java/me/zhengjie/modules/student/domain/Student.java b/eladmin-system/src/main/java/me/zhengjie/modules/student/domain/Student.java new file mode 100644 index 0000000..a1fd21a --- /dev/null +++ b/eladmin-system/src/main/java/me/zhengjie/modules/student/domain/Student.java @@ -0,0 +1,57 @@ +/* +* Copyright 2019-2020 Zheng Jie +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ +package me.zhengjie.modules.student.domain; + +import lombok.Data; +import cn.hutool.core.bean.BeanUtil; +import io.swagger.annotations.ApiModelProperty; +import cn.hutool.core.bean.copier.CopyOptions; +import javax.persistence.*; +import javax.validation.constraints.*; +import java.io.Serializable; + +/** +* @website https://el-admin.vip +* @description / +* @author x +* @date 2020-09-04 +**/ +@Entity +@Data +@Table(name="t_student") +public class Student implements Serializable { + + @Id + @Column(name = "id") +// @UpdateTimestamp + @ApiModelProperty(value = "id") + @GeneratedValue(strategy = GenerationType.IDENTITY) // 设置主键自增策略 + private Integer id; + + @Column(name = "name",nullable = false) + @NotBlank + @ApiModelProperty(value = "姓名") + private String name; + + @Column(name = "age",nullable = false) + @NotNull + @ApiModelProperty(value = "年龄") + private Integer age; + + public void copy(Student source){ + BeanUtil.copyProperties(source,this, CopyOptions.create().setIgnoreNullValue(true)); + } +} \ No newline at end of file diff --git a/eladmin-system/src/main/java/me/zhengjie/modules/student/repository/StudentRepository.java b/eladmin-system/src/main/java/me/zhengjie/modules/student/repository/StudentRepository.java new file mode 100644 index 0000000..822c795 --- /dev/null +++ b/eladmin-system/src/main/java/me/zhengjie/modules/student/repository/StudentRepository.java @@ -0,0 +1,28 @@ +/* +* Copyright 2019-2020 Zheng Jie +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ +package me.zhengjie.modules.student.repository; + +import me.zhengjie.modules.student.domain.Student; +import org.springframework.data.jpa.repository.JpaRepository; +import org.springframework.data.jpa.repository.JpaSpecificationExecutor; + +/** +* @website https://el-admin.vip +* @author x +* @date 2020-09-04 +**/ +public interface StudentRepository extends JpaRepository, JpaSpecificationExecutor { +} \ No newline at end of file diff --git a/eladmin-system/src/main/java/me/zhengjie/modules/student/rest/StudentController.java b/eladmin-system/src/main/java/me/zhengjie/modules/student/rest/StudentController.java new file mode 100644 index 0000000..50a7abc --- /dev/null +++ b/eladmin-system/src/main/java/me/zhengjie/modules/student/rest/StudentController.java @@ -0,0 +1,87 @@ +/* +* Copyright 2019-2020 Zheng Jie +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ +package me.zhengjie.modules.student.rest; + +import me.zhengjie.annotation.Log; +import me.zhengjie.modules.student.domain.Student; +import me.zhengjie.modules.student.service.StudentService; +import me.zhengjie.modules.student.service.dto.StudentQueryCriteria; +import org.springframework.data.domain.Pageable; +import lombok.RequiredArgsConstructor; +import org.springframework.http.HttpStatus; +import org.springframework.http.ResponseEntity; +import org.springframework.security.access.prepost.PreAuthorize; +import org.springframework.validation.annotation.Validated; +import org.springframework.web.bind.annotation.*; +import io.swagger.annotations.*; +import java.io.IOException; +import javax.servlet.http.HttpServletResponse; + +/** +* @website https://el-admin.vip +* @author x +* @date 2020-09-04 +**/ +@RestController +@RequiredArgsConstructor +@Api(tags = "student管理") +@RequestMapping("/api/student") +public class StudentController { + + private final StudentService studentService; + + @Log("导出数据") + @ApiOperation("导出数据") + @GetMapping(value = "/download") + @PreAuthorize("@el.check('student:list')") + public void download(HttpServletResponse response, StudentQueryCriteria criteria) throws IOException { + studentService.download(studentService.queryAll(criteria), response); + } + + @GetMapping + @Log("查询student") + @ApiOperation("查询student") + @PreAuthorize("@el.check('student:list')") + public ResponseEntity query(StudentQueryCriteria criteria, Pageable pageable){ + return new ResponseEntity<>(studentService.queryAll(criteria,pageable),HttpStatus.OK); + } + + @PostMapping + @Log("新增student") + @ApiOperation("新增student") + @PreAuthorize("@el.check('student:add')") + public ResponseEntity create(@Validated @RequestBody Student resources){ + return new ResponseEntity<>(studentService.create(resources),HttpStatus.CREATED); + } + + @PutMapping + @Log("修改student") + @ApiOperation("修改student") + @PreAuthorize("@el.check('student:edit')") + public ResponseEntity update(@Validated @RequestBody Student resources){ + studentService.update(resources); + return new ResponseEntity<>(HttpStatus.NO_CONTENT); + } + + @Log("删除student") + @ApiOperation("删除student") + @PreAuthorize("@el.check('student:del')") + @DeleteMapping + public ResponseEntity delete(@RequestBody Integer[] ids) { + studentService.deleteAll(ids); + return new ResponseEntity<>(HttpStatus.OK); + } +} \ No newline at end of file diff --git a/eladmin-system/src/main/java/me/zhengjie/modules/student/service/StudentService.java b/eladmin-system/src/main/java/me/zhengjie/modules/student/service/StudentService.java new file mode 100644 index 0000000..016fd67 --- /dev/null +++ b/eladmin-system/src/main/java/me/zhengjie/modules/student/service/StudentService.java @@ -0,0 +1,83 @@ +/* +* Copyright 2019-2020 Zheng Jie +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ +package me.zhengjie.modules.student.service; + +import me.zhengjie.modules.student.domain.Student; +import me.zhengjie.modules.student.service.dto.StudentDto; +import me.zhengjie.modules.student.service.dto.StudentQueryCriteria; +import org.springframework.data.domain.Pageable; +import java.util.Map; +import java.util.List; +import java.io.IOException; +import javax.servlet.http.HttpServletResponse; + +/** +* @website https://el-admin.vip +* @description 服务接口 +* @author x +* @date 2020-09-04 +**/ +public interface StudentService { + + /** + * 查询数据分页 + * @param criteria 条件 + * @param pageable 分页参数 + * @return Map + */ + Map queryAll(StudentQueryCriteria criteria, Pageable pageable); + + /** + * 查询所有数据不分页 + * @param criteria 条件参数 + * @return List + */ + List queryAll(StudentQueryCriteria criteria); + + /** + * 根据ID查询 + * @param id ID + * @return StudentDto + */ + StudentDto findById(Integer id); + + /** + * 创建 + * @param resources / + * @return StudentDto + */ + StudentDto create(Student resources); + + /** + * 编辑 + * @param resources / + */ + void update(Student resources); + + /** + * 多选删除 + * @param ids / + */ + void deleteAll(Integer[] ids); + + /** + * 导出数据 + * @param all 待导出的数据 + * @param response / + * @throws IOException / + */ + void download(List all, HttpServletResponse response) throws IOException; +} \ No newline at end of file diff --git a/eladmin-system/src/main/java/me/zhengjie/modules/student/service/dto/StudentDto.java b/eladmin-system/src/main/java/me/zhengjie/modules/student/service/dto/StudentDto.java new file mode 100644 index 0000000..4d3e841 --- /dev/null +++ b/eladmin-system/src/main/java/me/zhengjie/modules/student/service/dto/StudentDto.java @@ -0,0 +1,38 @@ +/* +* Copyright 2019-2020 Zheng Jie +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ +package me.zhengjie.modules.student.service.dto; + +import lombok.Data; +import java.io.Serializable; + +/** +* @website https://el-admin.vip +* @description / +* @author x +* @date 2020-09-04 +**/ +@Data +public class StudentDto implements Serializable { + + /** id */ + private Integer id; + + /** 姓名 */ + private String name; + + /** 年龄 */ + private Integer age; +} \ No newline at end of file diff --git a/eladmin-system/src/main/java/me/zhengjie/modules/student/service/dto/StudentQueryCriteria.java b/eladmin-system/src/main/java/me/zhengjie/modules/student/service/dto/StudentQueryCriteria.java new file mode 100644 index 0000000..192b14b --- /dev/null +++ b/eladmin-system/src/main/java/me/zhengjie/modules/student/service/dto/StudentQueryCriteria.java @@ -0,0 +1,41 @@ +/* +* Copyright 2019-2020 Zheng Jie +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ +package me.zhengjie.modules.student.service.dto; + +import lombok.Data; +import java.util.List; +import me.zhengjie.annotation.Query; + +/** +* @website https://el-admin.vip +* @author x +* @date 2020-09-04 +**/ +@Data +public class StudentQueryCriteria{ + + /** 精确 */ + @Query + private Integer id; + + /** 模糊 */ + @Query(type = Query.Type.INNER_LIKE) + private String name; + + /** 精确 */ + @Query + private Integer age; +} \ No newline at end of file diff --git a/eladmin-system/src/main/java/me/zhengjie/modules/student/service/impl/StudentServiceImpl.java b/eladmin-system/src/main/java/me/zhengjie/modules/student/service/impl/StudentServiceImpl.java new file mode 100644 index 0000000..7b76924 --- /dev/null +++ b/eladmin-system/src/main/java/me/zhengjie/modules/student/service/impl/StudentServiceImpl.java @@ -0,0 +1,105 @@ +/* +* Copyright 2019-2020 Zheng Jie +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ +package me.zhengjie.modules.student.service.impl; + +import me.zhengjie.modules.student.domain.Student; +import me.zhengjie.utils.ValidationUtil; +import me.zhengjie.utils.FileUtil; +import lombok.RequiredArgsConstructor; +import me.zhengjie.modules.student.repository.StudentRepository; +import me.zhengjie.modules.student.service.StudentService; +import me.zhengjie.modules.student.service.dto.StudentDto; +import me.zhengjie.modules.student.service.dto.StudentQueryCriteria; +import me.zhengjie.modules.student.service.mapstruct.StudentMapper; +import org.springframework.stereotype.Service; +import org.springframework.transaction.annotation.Transactional; +import org.springframework.data.domain.Page; +import org.springframework.data.domain.Pageable; +import me.zhengjie.utils.PageUtil; +import me.zhengjie.utils.QueryHelp; +import java.util.List; +import java.util.Map; +import java.io.IOException; +import javax.servlet.http.HttpServletResponse; +import java.util.ArrayList; +import java.util.LinkedHashMap; + +/** +* @website https://el-admin.vip +* @description 服务实现 +* @author x +* @date 2020-09-04 +**/ +@Service +@RequiredArgsConstructor +public class StudentServiceImpl implements StudentService { + + private final StudentRepository studentRepository; + private final StudentMapper studentMapper; + + @Override + public Map queryAll(StudentQueryCriteria criteria, Pageable pageable){ + Page page = studentRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder),pageable); + return PageUtil.toPage(page.map(studentMapper::toDto)); + } + + @Override + public List queryAll(StudentQueryCriteria criteria){ + return studentMapper.toDto(studentRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder))); + } + + @Override + @Transactional + public StudentDto findById(Integer id) { + Student student = studentRepository.findById(id).orElseGet(Student::new); + ValidationUtil.isNull(student.getId(),"Student","id",id); + return studentMapper.toDto(student); + } + + @Override + @Transactional(rollbackFor = Exception.class) + public StudentDto create(Student resources) { + return studentMapper.toDto(studentRepository.save(resources)); + } + + @Override + @Transactional(rollbackFor = Exception.class) + public void update(Student resources) { + Student student = studentRepository.findById(resources.getId()).orElseGet(Student::new); + ValidationUtil.isNull( student.getId(),"Student","id",resources.getId()); + student.copy(resources); + studentRepository.save(student); + } + + @Override + public void deleteAll(Integer[] ids) { + for (Integer id : ids) { + studentRepository.deleteById(id); + } + } + + @Override + public void download(List all, HttpServletResponse response) throws IOException { + List> list = new ArrayList<>(); + for (StudentDto student : all) { + Map map = new LinkedHashMap<>(); + map.put("姓名", student.getName()); + map.put("年龄", student.getAge()); + list.add(map); + } + FileUtil.downloadExcel(list, response); + } +} \ No newline at end of file diff --git a/eladmin-system/src/main/java/me/zhengjie/modules/student/service/mapstruct/StudentMapper.java b/eladmin-system/src/main/java/me/zhengjie/modules/student/service/mapstruct/StudentMapper.java new file mode 100644 index 0000000..5abf39b --- /dev/null +++ b/eladmin-system/src/main/java/me/zhengjie/modules/student/service/mapstruct/StudentMapper.java @@ -0,0 +1,32 @@ +/* +* Copyright 2019-2020 Zheng Jie +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ +package me.zhengjie.modules.student.service.mapstruct; + +import me.zhengjie.base.BaseMapper; +import me.zhengjie.modules.student.domain.Student; +import me.zhengjie.modules.student.service.dto.StudentDto; +import org.mapstruct.Mapper; +import org.mapstruct.ReportingPolicy; + +/** +* @website https://el-admin.vip +* @author x +* @date 2020-09-04 +**/ +@Mapper(componentModel = "spring", unmappedTargetPolicy = ReportingPolicy.IGNORE) +public interface StudentMapper extends BaseMapper { + +} \ No newline at end of file diff --git a/src/views/student/index.vue b/src/views/student/index.vue new file mode 100644 index 0000000..432fce0 --- /dev/null +++ b/src/views/student/index.vue @@ -0,0 +1,108 @@ + + + + + diff --git a/student/index.vue b/student/index.vue new file mode 100644 index 0000000..432fce0 --- /dev/null +++ b/student/index.vue @@ -0,0 +1,108 @@ + + + + + diff --git a/student/student.js b/student/student.js new file mode 100644 index 0000000..467263a --- /dev/null +++ b/student/student.js @@ -0,0 +1,27 @@ +import request from '@/utils/request' + +export function add(data) { + return request({ + url: 'api/student', + method: 'post', + data + }) +} + +export function del(ids) { + return request({ + url: 'api/student/', + method: 'delete', + data: ids + }) +} + +export function edit(data) { + return request({ + url: 'api/student', + method: 'put', + data + }) +} + +export default { add, edit, del }