测试代码生成完毕

master
土豆兄弟 4 years ago
parent 31bb7035a1
commit a4ada727c3

@ -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));
}
}

@ -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<Student, Integer>, JpaSpecificationExecutor<Student> {
}

@ -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<Object> 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<Object> 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<Object> 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<Object> delete(@RequestBody Integer[] ids) {
studentService.deleteAll(ids);
return new ResponseEntity<>(HttpStatus.OK);
}
}

@ -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<String,Object>
*/
Map<String,Object> queryAll(StudentQueryCriteria criteria, Pageable pageable);
/**
*
* @param criteria
* @return List<StudentDto>
*/
List<StudentDto> 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<StudentDto> all, HttpServletResponse response) throws IOException;
}

@ -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;
}

@ -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;
}

@ -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<String,Object> queryAll(StudentQueryCriteria criteria, Pageable pageable){
Page<Student> page = studentRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder),pageable);
return PageUtil.toPage(page.map(studentMapper::toDto));
}
@Override
public List<StudentDto> 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<StudentDto> all, HttpServletResponse response) throws IOException {
List<Map<String, Object>> list = new ArrayList<>();
for (StudentDto student : all) {
Map<String,Object> map = new LinkedHashMap<>();
map.put("姓名", student.getName());
map.put("年龄", student.getAge());
list.add(map);
}
FileUtil.downloadExcel(list, response);
}
}

@ -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<StudentDto, Student> {
}

@ -0,0 +1,108 @@
<template>
<div class="app-container">
<!--工具栏-->
<div class="head-container">
<div v-if="crud.props.searchToggle">
<!-- 搜索 -->
<label class="el-form-item-label">id</label>
<el-input v-model="query.id" clearable placeholder="id" style="width: 185px;" class="filter-item" @keyup.enter.native="crud.toQuery" />
<label class="el-form-item-label">姓名</label>
<el-input v-model="query.name" clearable placeholder="姓名" style="width: 185px;" class="filter-item" @keyup.enter.native="crud.toQuery" />
<label class="el-form-item-label">年龄</label>
<el-input v-model="query.age" clearable placeholder="年龄" style="width: 185px;" class="filter-item" @keyup.enter.native="crud.toQuery" />
<rrOperation :crud="crud" />
</div>
<!--如果想在工具栏加入更多按钮可以使用插槽方式 slot = 'left' or 'right'-->
<crudOperation :permission="permission" />
<!--表单组件-->
<el-dialog :close-on-click-modal="false" :before-close="crud.cancelCU" :visible.sync="crud.status.cu > 0" :title="crud.status.title" width="500px">
<el-form ref="form" :model="form" :rules="rules" size="small" label-width="80px">
<el-form-item label="id" prop="id">
<el-input v-model="form.id" style="width: 370px;" />
</el-form-item>
<el-form-item label="姓名" prop="name">
<el-input v-model="form.name" :rows="3" type="textarea" style="width: 370px;" />
</el-form-item>
<el-form-item label="年龄" prop="age">
<el-input v-model="form.age" style="width: 370px;" />
</el-form-item>
</el-form>
<div slot="footer" class="dialog-footer">
<el-button type="text" @click="crud.cancelCU"></el-button>
<el-button :loading="crud.cu === 2" type="primary" @click="crud.submitCU"></el-button>
</div>
</el-dialog>
<!--表格渲染-->
<el-table ref="table" v-loading="crud.loading" :data="crud.data" size="small" style="width: 100%;" @selection-change="crud.selectionChangeHandler">
<el-table-column type="selection" width="55" />
<el-table-column prop="id" label="id" />
<el-table-column prop="name" label="姓名" />
<el-table-column prop="age" label="年龄" />
<el-table-column v-permission="['admin','student:edit','student:del']" label="操作" width="150px" align="center">
<template slot-scope="scope">
<udOperation
:data="scope.row"
:permission="permission"
/>
</template>
</el-table-column>
</el-table>
<!--分页组件-->
<pagination />
</div>
</div>
</template>
<script>
import crudStudent from '@/api/student'
import CRUD, { presenter, header, form, crud } from '@crud/crud'
import rrOperation from '@crud/RR.operation'
import crudOperation from '@crud/CRUD.operation'
import udOperation from '@crud/UD.operation'
import pagination from '@crud/Pagination'
const defaultForm = { id: null, name: null, age: null }
export default {
name: 'Student',
components: { pagination, crudOperation, rrOperation, udOperation },
mixins: [presenter(), header(), form(defaultForm), crud()],
cruds() {
return CRUD({ title: 'student', url: 'api/student', sort: 'id,desc', crudMethod: { ...crudStudent }})
},
data() {
return {
permission: {
add: ['admin', 'student:add'],
edit: ['admin', 'student:edit'],
del: ['admin', 'student:del']
},
rules: {
id: [
{ required: true, message: 'id不能为空', trigger: 'blur' }
],
name: [
{ required: true, message: '姓名不能为空', trigger: 'blur' }
],
age: [
{ required: true, message: '年龄不能为空', trigger: 'blur' }
]
},
queryTypeOptions: [
{ key: 'id', display_name: 'id' },
{ key: 'name', display_name: '姓名' },
{ key: 'age', display_name: '年龄' }
]
}
},
methods: {
// false
[CRUD.HOOK.beforeRefresh]() {
return true
}
}
}
</script>
<style scoped>
</style>

@ -0,0 +1,108 @@
<template>
<div class="app-container">
<!--工具栏-->
<div class="head-container">
<div v-if="crud.props.searchToggle">
<!-- 搜索 -->
<label class="el-form-item-label">id</label>
<el-input v-model="query.id" clearable placeholder="id" style="width: 185px;" class="filter-item" @keyup.enter.native="crud.toQuery" />
<label class="el-form-item-label">姓名</label>
<el-input v-model="query.name" clearable placeholder="姓名" style="width: 185px;" class="filter-item" @keyup.enter.native="crud.toQuery" />
<label class="el-form-item-label">年龄</label>
<el-input v-model="query.age" clearable placeholder="年龄" style="width: 185px;" class="filter-item" @keyup.enter.native="crud.toQuery" />
<rrOperation :crud="crud" />
</div>
<!--如果想在工具栏加入更多按钮可以使用插槽方式 slot = 'left' or 'right'-->
<crudOperation :permission="permission" />
<!--表单组件-->
<el-dialog :close-on-click-modal="false" :before-close="crud.cancelCU" :visible.sync="crud.status.cu > 0" :title="crud.status.title" width="500px">
<el-form ref="form" :model="form" :rules="rules" size="small" label-width="80px">
<el-form-item label="id" prop="id">
<el-input v-model="form.id" style="width: 370px;" />
</el-form-item>
<el-form-item label="姓名" prop="name">
<el-input v-model="form.name" :rows="3" type="textarea" style="width: 370px;" />
</el-form-item>
<el-form-item label="年龄" prop="age">
<el-input v-model="form.age" style="width: 370px;" />
</el-form-item>
</el-form>
<div slot="footer" class="dialog-footer">
<el-button type="text" @click="crud.cancelCU"></el-button>
<el-button :loading="crud.cu === 2" type="primary" @click="crud.submitCU"></el-button>
</div>
</el-dialog>
<!--表格渲染-->
<el-table ref="table" v-loading="crud.loading" :data="crud.data" size="small" style="width: 100%;" @selection-change="crud.selectionChangeHandler">
<el-table-column type="selection" width="55" />
<el-table-column prop="id" label="id" />
<el-table-column prop="name" label="姓名" />
<el-table-column prop="age" label="年龄" />
<el-table-column v-permission="['admin','student:edit','student:del']" label="操作" width="150px" align="center">
<template slot-scope="scope">
<udOperation
:data="scope.row"
:permission="permission"
/>
</template>
</el-table-column>
</el-table>
<!--分页组件-->
<pagination />
</div>
</div>
</template>
<script>
import crudStudent from '@/api/student'
import CRUD, { presenter, header, form, crud } from '@crud/crud'
import rrOperation from '@crud/RR.operation'
import crudOperation from '@crud/CRUD.operation'
import udOperation from '@crud/UD.operation'
import pagination from '@crud/Pagination'
const defaultForm = { id: null, name: null, age: null }
export default {
name: 'Student',
components: { pagination, crudOperation, rrOperation, udOperation },
mixins: [presenter(), header(), form(defaultForm), crud()],
cruds() {
return CRUD({ title: 'student', url: 'api/student', sort: 'id,desc', crudMethod: { ...crudStudent }})
},
data() {
return {
permission: {
add: ['admin', 'student:add'],
edit: ['admin', 'student:edit'],
del: ['admin', 'student:del']
},
rules: {
id: [
{ required: true, message: 'id不能为空', trigger: 'blur' }
],
name: [
{ required: true, message: '姓名不能为空', trigger: 'blur' }
],
age: [
{ required: true, message: '年龄不能为空', trigger: 'blur' }
]
},
queryTypeOptions: [
{ key: 'id', display_name: 'id' },
{ key: 'name', display_name: '姓名' },
{ key: 'age', display_name: '年龄' }
]
}
},
methods: {
// false
[CRUD.HOOK.beforeRefresh]() {
return true
}
}
}
</script>
<style scoped>
</style>

@ -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 }
Loading…
Cancel
Save