8-12智能商贸,自主完成crud

1.课程介绍
1.需求分析
2.技术点支持

一,系统数据字典

SystemDictionaryType

@Entity
@Table(name = "systemdictionarydetail")
public class Systemdictionarydetail extends BaseDomain{

    //定义两个常量
    public static final String PRODUCT_BRAND = "productBrand";
    public static final String PRODUCT_UNIT = "productUnit";
    private String name;
    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "types_id")
    private Systemdictionarytype types;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Systemdictionarytype getTypes() {
        return types;
    }

    public void setTypes(Systemdictionarytype types) {
        this.types = types;
    }
}

query

public class SystemdictionarydetailQuery extends BaseQuery{
    private String name;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    @Override
    public Specification createSpecification() {
        Specification<Systemdictionarydetail> spe = Specifications.<Systemdictionarydetail>and().
                like(StringUtils.isNotBlank(name), "name", "%" + name + "%")
                .build();

        return spe;
    }
}

Repository

public interface SystemdictionarydetailRepository extends BaseRepository<Systemdictionarydetail,Long>{

    @Query("select d from Systemdictionarydetail d join d.types t where t.sn=?1")
    List<Systemdictionarydetail> findDetailsBySn(String sn);
}

service

public interface ISystemdictionarydetailService extends IBaseService<Systemdictionarydetail,Long> {

    /**
     * 根据编号查询对应的数据字典明细
     * @param sn   数据字典类型的编号
     * @return
     */
    List<Systemdictionarydetail> findDetailsBySn(String sn);
}
@Service
public class SystemdictionarydetailServiceImpl extends BaseServiceImpl<Systemdictionarydetail,Long> implements ISystemdictionarydetailService {

    @Autowired
    private SystemdictionarydetailRepository systemdictionarydetailRepository;

    @Override
    public List<Systemdictionarydetail> findDetailsBySn(String sn) {
        return systemdictionarydetailRepository.findDetailsBySn(sn);
    }
}

Controller

@Controller
@RequestMapping("/systemdictionarydetail")
public class SystemdictionarydetailController {
    @Autowired
    private ISystemdictionarydetailService systemdictionarydetailService;
    @RequestMapping("/index")
    public String index(){
        return "systemdictionarydetail/systemdictionarydetail";
    }

    //写一个方法查询数据 返回json --查数据库 findPage
    @RequestMapping("/page")
    @ResponseBody
    public UiPage page(SystemdictionarydetailQuery systemdictionarydetailQuery){
        //List<Systemdictionarydetail> systemdictionarydetails = systemdictionarydetailService.findAll();
        Page page = systemdictionarydetailService.findPageByQuery(systemdictionarydetailQuery);
        UiPage uipage = new UiPage(page);
        return uipage;
    }

    //方法 delete
    @RequestMapping("/delete")
    @ResponseBody
    public Map delete(Long id){
        Map mp = new HashMap();
        try {
            systemdictionarydetailService.delete(id);
            mp.put("success", true);
        } catch (Exception e) {
            e.printStackTrace();
            mp.put("success", false);
            mp.put("msg", e.getMessage());
        }
        return mp;

    }

    //保存方法-- 修改和新增保存
    @RequestMapping("/save")
    @ResponseBody
    public Map save(Systemdictionarydetail systemdictionarydetail){
        Map mp = new HashMap();
        try {
            systemdictionarydetailService.save(systemdictionarydetail);
            mp.put("success", true);
        } catch (Exception e) {
            e.printStackTrace();
            mp.put("success", false);
            mp.put("msg", e.getMessage());
        }
        return mp;

    }

    @RequestMapping("/update")
    @ResponseBody
    public Map update(@ModelAttribute("editSystemdictionarydetail") Systemdictionarydetail systemdictionarydetail){
        Map mp = new HashMap();
        try {
            //Systemdictionarydetail systemdictionarydetail = findOne(systemdictionarydetail.getId())
            systemdictionarydetailService.save(systemdictionarydetail);
            mp.put("success", true);
        } catch (Exception e) {
            e.printStackTrace();
            mp.put("success", false);
            mp.put("msg", e.getMessage());
        }
        return mp;

    }



    //删除方法
    @ModelAttribute("editSystemdictionarydetail")
    public Systemdictionarydetail beforeEdit(Long id, String cmd){
        Systemdictionarydetail systemdictionarydetail = null;
        if("update".equals(cmd) && id != null && !"".equals(id) ){
            systemdictionarydetail = systemdictionarydetailService.findOne(id);

        }
        return systemdictionarydetail;
    }
}

二,自主完成CRUD

使用代码生成器完成:
domain
query
repository
service
controller
还有前台页面

产品页面展示

<table id="productGrid" class="easyui-datagrid" data-options="fit:true,fixed:true,fitColumns:true,toolbar:'#tb',singleSelect:true";
       url="/product/page"
       iconCls="icon-save"
       rownumbers="true" pagination="true">
    <thead>
    <tr>
        <th width="20"  field="name"  >名称</th>
        <th width="20"  field="color"  data-options="formatter:formatColor"  >颜色</th>
        <th width="20"  field="smallpic"  data-options="formatter:formatImg"  >图片</th>
        <th width="20"  field="costprice"  >成本价</th>
        <th width="20"  field="saleprice"  >销售价</th>
        <th width="20"  field="types"  data-options="formatter:formatObj"  >类型</th>
        <th width="20"  field="unit"  data-options="formatter:formatObj" >单位</th>
        <th width="20"  field="brand"  data-options="formatter:formatObj" >品牌</th>
    </tr>
    </thead>
</table>

处理大图展示方法
function loadSuccess(data) {
//看是否进来了
alert(进来了)
var rows = data.rows;
for(var i=0;i<rows.length;i++){
var result = rows[i];
. e a s y u i . t o o l t i p . i n i t ( .easyui.tooltip.init( .easyui.tooltip.init((“img[src=’”+result.smallpic+"’]"), {
position: “rigth”,
content: “<div style=“width:600px;height:480px;”>
});
}
}
添加与修改页面

<div id="productDialog" class="easyui-dialog" data-options="closed:true,modal:true" title="功能操作" style="width:450px">
    <div style="padding:10px 60px 20px 40px">
        <form id="productForm" class="easyui-form" method="post" data-options="">
            <input type="hidden" id="productId" name="id" >
            <table cellpadding="5">
                <tr>
                    <td>名称:</td>
                    <td><input class="easyui-validatebox" type="text" name="name" data-options="required:true"></input></td>
                </tr>
                <tr>
                    <td>颜色:</td>
                    //注意颜色在回显的时候不用去写,,,type=color可以直接回显
                    <td><input class="easyui-validatebox" type="color" name="color"></input></td>
                </tr>
                <tr>
                    <td>成本价:</td>
                    <td><input class="easyui-validatebox" type="text" name="costprice"></input></td>
                </tr>
                <tr>
                    <td>销售价:</td>
                    <td><input class="easyui-validatebox" type="text" name="saleprice"></input></td>
                </tr>
                <tr>
                    <td>产品图片:</td>
                    <td>
                        <input name="fileImage" class="easyui-filebox" style="width:100%">
                    </td>
                </tr>
            </table>
        </form>
        <div style="text-align:center;padding:5px">
            <a href="javascript:void(0)" class="easyui-linkbutton" data-method="save">提交</a>
            <a href="javascript:void(0)" class="easyui-linkbutton" onclick="$('#productDialog').dialog('close')">取消</a>
        </div>
    </div>
</div>

产品修改时回显

edit:function () {
    //选中了某一条数据才删除
    var row = productGrid.datagrid("getSelected");
    if(row){
        //隐藏有data-save属性的元素
        $("*[data-save]").hide();
        //禁用有data-save属性的input元素的验证功能
        $("*[data-save] input").validatebox("disableValidation");

        productForm.form("clear");//清除数据
        productDialog.dialog("center").dialog('open');
        //解决单位,品牌,类型的回显问题
        if(row.unit){
            row["unit.id"] = row.unit.id;
        }
        if(row.brand){
            row["brand.id"] = row.brand.id;
        }
        if(row.types){
            row["types.id"] = row.types.id;
        }
        //为form加载数据
        productForm.form("load",row);
    }else{
        $.messager.alert('提示信息','请选择一行再进行修改!','info');
    }
},

保存

@RequestMapping("/save")
@ResponseBody
public Map<String,Object> save(Product product,HttpServletRequest req){
    return saveOrUpdate(product,req);
}

最后删除(注意删除时把图片也删除了)

//删除功能
@RequestMapping("/delete")
@ResponseBody
public Map<String,Object> delete(Long id,HttpServletRequest req){
    Map<String,Object> map = new HashMap<>();
    try{
        Product product = productService.findOne(id);
        productService.delete(id);
        // 删除图片的代码,写在delete方法之后
        String webapp = req.getServletContext().getRealPath("/");
        if (id != null && StringUtils.isNotBlank(product.getPic())) {
            File deleteFile = new File(webapp, product.getPic());
            if (deleteFile.exists()) {
                deleteFile.delete();
            }
            File deleteSmallFile = new File(webapp, product.getSmallPic());
            if (deleteSmallFile.exists()) {
                deleteSmallFile.delete();
            }
        }
        map.put(SUCCESS,true);
    }catch (Exception e){
        map.put(SUCCESS,false);
        map.put("msg",e.getMessage());
        e.printStackTrace();
    }
    return map;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值