< ?xml version = "1.0" encoding = "UTF-8" ?>
< ! DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
< mapper namespace = "com.java1234.mapper.SmallTypeMapper" >
< resultMap id = "smallTypeResult" type = "com.java1234.entity.SmallType" >
< association property = "bigType" column = "bigTypeId" select = "com.java1234.mapper.BigTypeMapper.findById" >
< /association>
< /resultMap>
< select id = "list" parameterType = "Map" resultMap = "smallTypeResult" >
select * from t_smallType
< where>
< if test = "name!=null and name!='' " >
and name like concat( '%' ,
< /if>
< /where>
< if test = "start!=null and pageSize!=null" >
limit
< /if>
< /select>
< select id = "getTotal" parameterType = "Map" resultType = "Long" >
select count( *) from t_smallType
< where>
< if test = "name!=null and name!='' " >
and name like concat( '%' ,
< /if>
< /where>
< /select>
< insert id = "add" parameterType = "com.java1234.entity.SmallType" >
insert into t_smallType values( null,
< /insert>
< update id = "update" parameterType = "com.java1234.entity.SmallType" >
update t_smallType
< set>
< if test = "name!=null and name!=''" >
name =
< /if>
< if test = "remark!=null and remark!=''" >
remark =
< /if>
< if test = "bigType.id!=null" >
bigTypeId =
< /if>
< /set>
where id =
< /update>
< /mapper>
package com.java1234.mapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.java1234.entity.BigType;
import com.java1234.entity.SmallType;
import java.util.List;
import java.util.Map;
/**
* 商品小类Mapper接口
*/
public interface SmallTypeMapper extends BaseMapper< SmallType> {
List< SmallType> list( Map< String, Object> map) ;
Long getTotal( Map< String, Object> map) ;
/**
* 添加商品小类
* @param smallType
* @return
*/
public Integer add( SmallType smallType) ;
/**
* 修改商品小类
* @param smallType
* @return
*/
public Integer update( SmallType smallType) ;
}
package com.java1234.service.impl;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.java1234.entity.SmallType;
import com.java1234.mapper.SmallTypeMapper;
import com.java1234.service.ISmallTypeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
import java.util.Map;
/**
* 商品小类Service实现类
*/
@Service( "smallTypeService" )
public class ISmallTypeServiceImpl extends ServiceImpl< SmallTypeMapper, SmallType> implements ISmallTypeService {
@Autowired
private SmallTypeMapper smallTypeMapper;
@Override
public List< SmallType> list( Map< String, Object> map) {
return smallTypeMapper.list( map) ;
}
@Override
public Long getTotal( Map< String, Object> map) {
return smallTypeMapper.getTotal( map) ;
}
@Override
public Integer add( SmallType smallType) {
return smallTypeMapper.add( smallType) ;
}
@Override
public Integer update( SmallType smallType) {
return smallTypeMapper.update( smallType) ;
}
}
package com.java1234.service;
import com.baomidou.mybatisplus.extension.service.IService;
import com.java1234.entity.Order;
import com.java1234.entity.SmallType;
import java.util.List;
import java.util.Map;
/**
* 商品大类Service接口
*/
public interface ISmallTypeService extends IService< SmallType> {
List< SmallType> list( Map< String, Object> map) ;
Long getTotal( Map< String, Object> map) ;
/**
* 添加商品小类
* @param smallType
* @return
*/
public Integer add( SmallType smallType) ;
/**
* 修改商品小类
* @param smallType
* @return
*/
public Integer update( SmallType smallType) ;
}
package com.java1234.controller.admin;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.java1234.entity.PageBean;
import com.java1234.entity.R;
import com.java1234.entity.SmallType;
import com.java1234.service.ISmallTypeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
* 管理端-商品小类Controller控制器
* @author java1234_小锋
* @site www.java1234.com
* @company 南通小锋网络科技有限公司
* @create 2022 -02-14 6 :54
*/
@RestController
@RequestMapping( "/admin/smallType" )
public class AdminSmallTypeController {
@Autowired
private ISmallTypeService smallTypeService;
/**
* 根据条件分页查询
* @param pageBean
* @return
*/
@RequestMapping( "/list" )
public R list( @RequestBody PageBean pageBean) {
System.out.println( pageBean) ;
Map< String,Object> map = new HashMap<> ( ) ;
map.put( "name" ,pageBean.getQuery( ) .trim( )) ;
map.put( "start" ,pageBean.getStart( )) ;
map.put( "pageSize" ,pageBean.getPageSize( )) ;
List< SmallType> list = smallTypeService.list( map) ;
Long total = smallTypeService.getTotal( map) ;
Map< String,Object> resultMap = new HashMap<> ( ) ;
resultMap.put( "smallTypeList" ,list) ;
resultMap.put( "total" ,total) ;
return R.ok( resultMap) ;
}
/**
* 添加或者修改
* @param smallType
* @return
*/
@PostMapping( "/save" )
public R save( @RequestBody SmallType smallType) {
if( smallType.getId( ) == null || smallType.getId( ) == -1) {
smallTypeService.add( smallType) ;
} else{
smallTypeService.update( smallType) ;
}
return R.ok( ) ;
}
/**
* 删除
* @param id
* @return
*/
@GetMapping( "/delete/{id}" )
public R delete( @PathVariable( value = "id" ) Integer id ) {
smallTypeService.removeById( id) ;
return R.ok( ) ;
}
/**
* 根据商品大类id,查询所有数据 下拉框用到
* @return
*/
@RequestMapping( "/listAll/{bigTypeId}" )
public R listAll( @PathVariable( value = "bigTypeId" ) Integer bigTypeId) {
Map< String,Object> map = new HashMap<> ( ) ;
map.put( "smallTypeList" ,smallTypeService.list( new QueryWrapper< SmallType> ( ) .eq( "bigTypeId" ,bigTypeId)) ) ;
return R.ok( map) ;
}
@GetMapping( "/getBigTypeIdBySmallTypeId/{id}" )
public R getBigTypeIdBySmallTypeId( @PathVariable( value = "id" ) Integer id ) {
Map< String,Object> map = new HashMap<> ( ) ;
Integer bigTypeId = smallTypeService.getById( id) .getBigTypeId( ) ;
System.out.println( "bigTypeId=" +bigTypeId) ;
map.put( "bigTypeId" ,bigTypeId) ;
return R.ok( map) ;
}
}
< template>
< el-dialog
:model-value= "dialogVisible"
:title= "dialogTitle"
width = "30%"
@close= "handleClose"
>
< el-form ref = "formRef" :model= "form" label-width= "100px" :rules= "rules" >
< el-form-item label = "小类名称" prop = "name" >
< el-input v-model= "form.name" > < /el-input>
< /el-form-item>
< el-form-item label = "所属大类" >
< el-select v-model= "form.bigType.id" class = "m-2" placeholder = "请选择..." >
< el-option
v-for= "item in bigTypeSlectOptions"
:key= "item.id"
:label= "item.name"
:value= "item.id"
>
< /el-option>
< /el-select>
< /el-form-item>
< el-form-item label = "小类描述" prop = "remark" >
< el-input
v-model= "form.remark"
:rows= "4"
type = "textarea"
/>
< /el-form-item>
< /el-form>
< template
< span class = "dialog-footer" >
< el-button @click= "handleClose" > 取消< /el-button>
< el-button type = "primary" @click= "handleConfirm"
> 确认< /el-button
>
< /span>
< /template>
< /el-dialog>
< /template>
< script setup>
import { defineEmits,ref ,defineProps,watch} from 'vue'
import axios from "@/util/axios" ;
import { ElMessage } from "element-plus" ;
const props = defineProps( {
dialogTitle:{
type:String,
default:'' ,
required:true
} ,
dialogValue:{
type:Object,
default:( ) = > { }
}
} )
const form = ref( {
id:-1,
name:'' ,
remark:'' ,
bigType:{
id:""
}
} )
watch(
( ) = > props.dialogValue,
( ) = > {
console.log( "dialogValue222=" +props.dialogValue) ;
form.value = props.dialogValue;
} ,
{ deep:true,immediate:true}
)
const emits = defineEmits( [ 'update:modelValue' ,'initSmallTypeList' ] )
const formRef = ref( null)
const handleClose = ( ) = > {
formRef.value.resetFields( ) ;
emits( 'update:modelValue' ,false)
}
const rules = ref( {
name:[
{
required: true,
message: '请输入商品小类名称!' ,
}
] ,
remark:[
{
required: true,
message: '请输入商品小类描述!' ,
} ,
]
} )
const bigTypeSlectOptions = ref( [ ] )
const initBigTypeSelectList = async( ) = > {
console.log( 'xxx' )
const res = await axios.post( "admin/bigType/listAll" ) ;
bigTypeSlectOptions.value = res.data.bigTypeList;
}
initBigTypeSelectList( ) ;
const handleConfirm = ( ) = > {
formRef.value.validate( async ( valid) = > {
if ( valid) {
try{
if( form.value.bigType.id== "" ) {
ElMessage.error( "请选择商品大类" ) ;
return ;
}
let result = await axios.post( "admin/smallType/save" ,form.value) ;
let data = result.data;
if( data.code== 0 ) {
ElMessage.success( "执行成功" ) ;
formRef.value.resetFields( ) ;
emits( "initSmallTypeList" )
handleClose( ) ;
} else{
ElMessage.error( data.msg) ;
}
} catch( err) {
console.log( "error:" +err)
ElMessage.error( '系统运行出错,请联系管理员' ) ;
}
} else {
return false
}
} )
}
< /script>
< style scoped>
< /style>
< template>
< el-card>
< el-row :gutter= "20" class = "header" >
< el-col :span= "7" >
< el-input placeholder = "请输入商品小类名称..." clearable v-model= "queryForm.query" > < /el-input>
< /el-col>
< el-button type = "primary" :icon= "Search" @click= "initSmallTypeList" > 搜索< /el-button>
< el-button type = "primary" @click= "handleDialogValue()" > 添加商品小类< /el-button>
< /el-row>
< el-table :data= "tableData" stripe style = "width: 100%" >
< el-table-column prop = "id" label = "#ID" width = "80" />
< el-table-column prop = "name" label = "商品小类名称" width = "200" />
< el-table-column prop = "bigType" :formatter= "bigTypeFormatter" label = "所属大类" width = "200" >
< /el-table-column>
< el-table-column prop = "remark" label = "商品小类描述" />
< el-table-column prop = "action" label = "操作" width = "300" >
< template v-slot= "scope" >
< el-button type = "primary" :icon= "Edit" @click= "handleDialogValue(scope.row)" > < /el-button>
< el-button type = "danger" :icon= "Delete" @click= "handleDelete(scope.row.id)" > < /el-button>
< /template>
< /el-table-column>
< /el-table>
< el-pagination
v-model:currentPage= "queryForm.pageNum"
:page-sizes= "[10, 20, 30, 40,50]"
:page-size= "queryForm.pageSize"
:small= "small"
:disabled= "disabled"
:background= "background"
layout = "total, sizes, prev, pager, next, jumper"
:total= "total"
@size-change= "handleSizeChange"
@current-change= "handleCurrentChange"
>
< /el-pagination>
< /el-card>
< Dialog v-model= "dialogVisible" :dialogTitle= "dialogTitle" @initSmallTypeList= "initSmallTypeList" :dialogValue= "dialogValue" />
< /template>
< script setup>
import { Search,Edit,Delete } from '@element-plus/icons-vue'
import { ref } from 'vue'
import axios from '@/util/axios'
import { getServerUrl} from "@/config/sys" ;
import Dialog from './components/dialog'
import { ElMessageBox,ElMessage} from 'element-plus'
const queryForm = ref( {
query:'' ,
pageNum:1,
pageSize:10
} )
const total = ref( 0 )
const tableData = ref( [
] )
const dialogValue = ref( { } )
const dialogTitle = ref( '' )
const initSmallTypeList = async( ) = > {
console.log( 'xxx' )
const res = await axios.post( "admin/smallType/list" ,queryForm.value) ;
tableData.value = res.data.smallTypeList;
total.value = res.data.total;
}
initSmallTypeList( ) ;
const dialogVisible = ref( false)
const handleSizeChange = ( pageSize) = > {
queryForm.value.pageNum = 1 ;
queryForm.value.pageSize = pageSize;
initSmallTypeList( ) ;
}
const handleCurrentChange = ( pageNum) = > {
queryForm.value.pageNum = pageNum;
initSmallTypeList( ) ;
}
const handleDialogValue = ( row) = > {
if( row) {
dialogValue.value = JSON.parse( JSON.stringify( row)) ;
dialogTitle.value = "商品小类修改"
} else{
dialogValue.value = {
bigType:{
id:""
}
}
dialogTitle.value = "商品小类添加"
}
dialogVisible.value = true;
}
const handleDelete = ( id) = > {
ElMessageBox.confirm(
'您确定要删除这条记录吗?' ,
'系统提示' ,
{
confirmButtonText: '确定' ,
cancelButtonText: '取消' ,
type: 'warning' ,
}
)
.then( async( ) = > {
console.log( "id=" +id)
let res = await axios.get( "admin/smallType/delete/" +id) ;
if( res.data.code== 0 ) {
ElMessage( {
type: 'success' ,
message: '删除成功!' ,
} ) ;
initSmallTypeList( ) ;
} else{
ElMessage( {
type: 'error' ,
message: res.data.msg,
} ) ;
}
} )
.catch(( ) = > {
} )
}
const bigTypeFormatter = ( row) = > {
return row.bigType.name
}
< /script>
< style lang = "scss" scoped>
.header{
padding-bottom: 16px;
box-sizing: border-box;
}
.el-pagination{
padding-top: 15px;
box-sizing: border-box;
}
< /style>