首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式碼之家 >> 編程語言 >  >> C語言 >> C++ >> 關於C++

EasyUI + Spring + MyBatis 後台管理系統完整範例:軟體部署模組

     編輯:關於C++

專案架構

  • 前端:EasyUI (jQuery UI 框架),DataGrid 分頁、對話框編輯
  • 後端:Spring MVC + MyBatis,RESTful 風格 Controller
  • 資料庫:MySQL,軟體資訊表 deploy_software_info

Controller 層

@Controller
@RequestMapping("/deploySoftware")
public class DeploySoftwareController extends BaseController {

    @Autowired
    private DeploySoftwareService deploySoftwareService;

    // 頁面跳轉
    @RequestMapping("/list")
    public String softwareInfoList() {
        return "/deploySoftware/deploySoftwareInfoList";
    }

    // 分頁查詢 (EasyUI DataGrid 格式)
    @RequestMapping("/getSoftwareInfoList")
    @ResponseBody
    public Pagination getSoftwareInfoList(DataGridModel model, String softName) {
        return deploySoftwareService.getSoftwareInfoList(softName, model.getPage(), model.getRows());
    }

    // 編輯頁面
    @RequestMapping("/toEditSoftwareInfo/{id}")
    public String toEditSoftwareInfo(@PathVariable("id") Integer id, Model model) {
        DeploySoftwareInfo info = deploySoftwareService.getSoftwareInfoById(id);
        model.addAttribute("deploySoftwareInfo", info);
        return "/deploySoftware/editSoftwareInfo";
    }

    // 新增/修改
    @RequestMapping(value = "/editSoftwareInfo", method = {RequestMethod.POST, RequestMethod.GET})
    @ResponseBody
    public String editSoftwareInfo(DeploySoftwareInfo info, HttpServletRequest request) {
        Message msg = new Message();
        try {
            if (info.getId() != null) {
                DeploySoftwareInfo upd = new DeploySoftwareInfo();
                upd.setId(info.getId());
                upd.setSoftName(StringUtils.trim(info.getSoftName()));
                upd.setInstallPath(StringUtils.trim(info.getInstallPath()));
                upd.setSoftDesc(info.getSoftDesc());
                msg = deploySoftwareService.updateSoftwareInfo(upd);
            } else {
                DeploySoftwareInfo add = new DeploySoftwareInfo();
                add.setSoftName(StringUtils.trim(info.getSoftName()));
                add.setInstallPath(StringUtils.trim(info.getInstallPath()));
                add.setSoftDesc(info.getSoftDesc());
                msg = deploySoftwareService.addSoftwareInfo(add);
            }
            return msg.getContent();
        } catch (Exception e) {
            e.printStackTrace();
            return "error";
        }
    }

    // 批次刪除
    @RequestMapping(value = "/delDeploySoftwareInfo/{ids}", method = {RequestMethod.POST, RequestMethod.GET})
    @ResponseBody
    public String delDeploySoftwareInfo(@PathVariable("ids") List ids) {
        try {
            deploySoftwareService.delDeploySoftwareInfo(ids);
            return "success";
        } catch (Exception e) {
            e.printStackTrace();
            return "error";
        }
    }
}

Service 層

@Service
@Transactional
public class DeploySoftwareServiceImpl implements DeploySoftwareService {

    @Autowired
    private DeploySoftwareInfoMapper mapper;

    @Override
    public Pagination getSoftwareInfoList(String softName, int pageNo, int pageSize) {
        String like = (softName != null) ? "%" + softName + "%" : null;
        PageBounds pb = new PageBounds(pageNo, pageSize, null, true); // PageHelper 分頁
        PageList list = mapper.getSoftwareInfoList(like, pb);
        return new Pagination(pageNo, pageSize, list.getPaginator().getTotalCount(), list);
    }

    @Override
    public DeploySoftwareInfo getSoftwareInfoById(Integer id) {
        return mapper.getSoftwareInfoById(id);
    }

    @Override
    public Message addSoftwareInfo(DeploySoftwareInfo info) {
        Message msg = new Message();
        if (mapper.hasExists(info.getSoftName()) > 0) {
            msg.setType(Type.error);
            msg.setContent(info.getSoftName() + " 已存在");
        } else {
            mapper.addSoftwareInfo(info);
            msg.setType(Type.success);
            msg.setContent("success");
        }
        return msg;
    }

    @Override
    public Message updateSoftwareInfo(DeploySoftwareInfo info) {
        Message msg = new Message();
        DeploySoftwareInfo old = mapper.getSoftwareInfoById(info.getId());
        if (info.getSoftName().equals(old.getSoftName()) || mapper.hasExists(info.getSoftName()) == 0) {
            mapper.updateSoftwareInfo(info);
            msg.setType(Type.success);
            msg.setContent("success");
        } else {
            msg.setType(Type.error);
            msg.setContent(info.getSoftName() + " 已存在");
        }
        return msg;
    }

    @Override
    public void delDeploySoftwareInfo(List ids) {
        mapper.delDeploySoftwareInfo(ids);
    }
}

Mapper 介面

@Repository("deploySoftwareInfoMapper")
public interface DeploySoftwareInfoMapper {

    @Select("SELECT * FROM deploy_software_info WHERE 1=1 AND soft_name LIKE #{likeSoftName}")
    @Results(id = "DeploySoftwareInfoResult", value = {
        @Result(column = "id", property = "id", javaType = Integer.class),
        @Result(column = "soft_name", property = "softName"),
        @Result(column = "install_path", property = "installPath"),
        @Result(column = "soft_desc", property = "softDesc"),
        @Result(property = "deploySoftwareDetailList", column = "id",
                javaType = List.class, many = @Many(select = "getDetailsBySwId"))
    })
    PageList getSoftwareInfoList(@Param("likeSoftName") String like, PageBounds pb);

    @Select("SELECT * FROM deploy_software_info WHERE id = #{id}")
    @ResultMap("DeploySoftwareInfoResult")
    DeploySoftwareInfo getSoftwareInfoById(@Param("id") Integer id);

    @Insert("INSERT INTO deploy_software_info (soft_name, install_path, soft_desc) VALUES (#{softName}, #{installPath}, #{softDesc})")
    @Options(useGeneratedKeys = true, keyProperty = "id")
    int addSoftwareInfo(DeploySoftwareInfo info);

    @Update("UPDATE deploy_software_info SET soft_name=#{softName}, install_path=#{installPath}, soft_desc=#{softDesc} WHERE id=#{id}")
    boolean updateSoftwareInfo(DeploySoftwareInfo info);

    @Select("SELECT COUNT(1) FROM deploy_software_info WHERE soft_name = #{softName}")
    int hasExists(@Param("softName") String softName);

    @Delete("DELETE FROM deploy_software_info WHERE id IN (#{item})")
    void delDeploySoftwareInfo(List ids);
}

MyBatis XML 對應 (若不使用註解)




    
        
        
        
        
        
            
            
            
            
            
            
            
            
            
        
    

    

    

    
        INSERT INTO deploy_software_info (soft_name, install_path, soft_desc) VALUES (#{softName}, #{installPath}, #{softDesc})
    

    
        UPDATE deploy_software_info SET soft_name=#{softName}, install_path=#{installPath}, soft_desc=#{softDesc} WHERE id=#{id}
    

    

    
        DELETE FROM deploy_software_info WHERE id IN
        #{item}
    

關鍵技術點

  1. PageHelper:MyBatis 分頁插件,PageBounds 自動攔截 SQL 加 LIMIT
  2. @Transactional:服務層方法級事務管理
  3. ResultMap:解決欄位名稱映射 (下劃線 → 駝峰),支援關聯集合查詢
  4. foreach:動態 SQL 批次刪除 IN 子句
  5. Message 統一回應:前端 EasyUI 解析 success/error 字串顯示提示
  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式碼之家 All Rights Reserved