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