竞赛信息管理系统——SSM1:https://developer.aliyun.com/article/1521817
五、竞赛信息模块
1、定义竞赛信息实体类
@Data public class Competition { private int competition_id; private String competition_name; private String competition_description; private String publication_date; private String submission_deadline; private String sponsor; private String venue; }
2、实现CompetitionMapper接口文件
@Mapper public interface CompetitionMapper { //添加竞赛信息 int addCompetitionMap(Competition competition); //根据竞赛名称查询竞赛信息 Competition getCompetitionByName(@Param("competition_name")String competition_name); //查询所有的竞赛信息 List<Competition> getAllCompetitions(); //删除指定id的竞赛信息,返回受影响的行数 int deleteCompetition(@Param("competition_id")Integer competition_id); //删除id集合中的所有竞赛信息,返回受影响的行数 int deleteCompetitions(@Param("competition_ids") List<Integer> competition_ids); //查询指定id的竞赛信息 Competition getCompetitionById(@Param("competition_id")Integer competition_id); //修改竞赛信息,返回受影响的行数 int updateCompetition(Competition competition); //查询指定的竞赛信息 List<Competition> list(@Param("competition_name") String competition_name,@Param("publication_date") String publication_date, @Param("submission_deadline") String submission_deadline,@Param("limit") Integer limit,@Param("offset") Integer offset); //查询指定竞赛信息的数目 int getCount(@Param("competition_name") String competition_name,@Param("publication_date") String publication_date, @Param("submission_deadline") String submission_deadline); }
3、编写CompetitionMapper.xml文件
编写sql语句对数据库中的Competition表进行相关操作。
<?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.example.competition.mapper.CompetitionMapper"> <insert id="addCompetitionMap"> insert into competition_info(competition_name,competition_description,publication_date, submission_deadline,sponsor,venue) values(#{competition_name},#{competition_description},#{publication_date},#{submission_deadline}, #{sponsor},#{venue}) </insert> <update id="updateCompetition"> update competition_info set competition_name=#{competition_name},competition_description=#{competition_description}, publication_date=#{publication_date},submission_deadline=#{submission_deadline}, sponsor=#{sponsor},venue=#{venue} where competition_id=#{competition_id} </update> <delete id="deleteCompetition"> delete from competition_info where competition_id=#{competition_id}; </delete> <delete id="deleteCompetitions"> delete from competition_info where competition_id in <foreach collection="competition_ids" item="competition_id" separator="," open="(" close=")"> #{competition_id} </foreach> </delete> <select id="getCompetitionByName" resultType="com.example.competition.bean.Competition"> select * from competition_info where competition_name=#{competition_name} </select> <select id="getAllCompetitions" resultType="com.example.competition.bean.Competition"> select * from competition_info </select> <select id="getCompetitionById" resultType="com.example.competition.bean.Competition"> select * from competition_info where competition_id=#{competition_id} </select> <select id="list" resultType="com.example.competition.bean.Competition"> select * from competition_info <where> <if test="competition_name!=null"> competition_name like concat('%',#{competition_name},'%') </if> <if test="publication_date!=null"> and publication_date=#{publication_date} </if> <if test="submission_deadline!=null"> and submission_deadline=#{submission_deadline} </if> </where> limit #{limit} offset #{offset} </select> <select id="getCount" resultType="java.lang.Integer"> select count(*) from competition_info <where> <if test="competition_name!=null"> competition_name like concat('%',#{competition_name},'%') </if> <if test="publication_date!=null"> and publication_date=#{publication_date} </if> <if test="submission_deadline!=null"> and submission_deadline=#{submission_deadline} </if> </where> </select> </mapper>
4、定义CompetitionService类
@Service public class CompetitionService { @Autowired private CompetitionMapper competitionMapper; public int addCompetition(Competition competition){ return competitionMapper.addCompetitionMap(competition); } public Competition getCompetitionByName(String competition_name) { return competitionMapper.getCompetitionByName(competition_name); } public List<Competition> getAllCompetitions(){ return competitionMapper.getAllCompetitions(); } public int deleteCompetition(Integer competition_id){ return competitionMapper.deleteCompetition(competition_id); } public int deleteCompetitions(List<Integer> competition_ids){ return competitionMapper.deleteCompetitions(competition_ids); } public Competition getCompetitionById(Integer competition_id){ return competitionMapper.getCompetitionById(competition_id); } public int updateCompetition(Competition competition){ return competitionMapper.updateCompetition(competition); } public List<Competition> list(String competition_name, String publication_date, String submission_deadline,Integer limit,Integer offset){ return competitionMapper.list(competition_name, publication_date, submission_deadline, limit, offset); } public int getCount(String competition_name, String publication_date, String submission_deadline){ return competitionMapper.getCount(competition_name, publication_date, submission_deadline); } }
6、CompetitionController处理请求
首先将CompetitionService注入进来,方便后续进行调用。
@Autowired CompetitionService competitionService;
a、添加竞赛信息
在添加竞赛信息时,首先判断竞赛信息是否为空,接着对竞赛名称进行唯一性校验,然后对竞赛信息的时间格式进行判断,都满足后将竞赛信息添加到数据库。
//添加竞赛返回受影响的行数,如果是时间格式不符,则返回-1 @RequestMapping("/addcompetition") public int addCompetition(Competition competition){ int res = 0; if(competition == null){ return res; } //确保竞赛名的唯一性 if(competitionService.getCompetitionByName(competition.getCompetition_name()) != null){ return res; } DateUtil.isValid(competition.getPublication_date()); //确保插入的时间格式正确 if( !DateUtil.isValid(competition.getPublication_date()) || !DateUtil.isValid(competition.getSubmission_deadline())){ return -1; } res = competitionService.addCompetition(competition); return res; }
前端代码实现:
<script> //进行添加操作 function add(){ var competition_name = jQuery("#competition_name"); var competition_description = jQuery("#competition_description"); var publication_date= jQuery("#publication_date"); var submission_deadline = jQuery("#submission_deadline"); var venue = jQuery("#venue"); var sponsor = jQuery("#sponsor"); //非空校验 if(jQuery.trim(competition_name.val()) === ""){ alert("请先输入竞赛名称"); competition_name.focus(); return false; } if(jQuery.trim(competition_description.val()) === ""){ alert("请先输入竞赛描述"); competition_description.focus(); return false; } if(jQuery.trim(publication_date.val()) === ""){ alert("请先输入发布时间"); publication_data.focus(); return false; } if(jQuery.trim(submission_deadline.val()) === ""){ alert("请先输入截止时间"); submission_deadline.focus(); return false; } if(jQuery.trim(sponsor.val()) === ""){ alert("请先输入赞助商"); sponsor.focus(); return false; } if(jQuery.trim(venue.val()) === ""){ alert("请先输入地点"); venue.focus(); return false; } jQuery.ajax({ url:"addcompetition", type:"POST", data:{ "competition_name":competition_name.val(), "competition_description":competition_description.val(), "publication_date":publication_date.val(), "submission_deadline":submission_deadline.val(), "sponsor":sponsor.val(), "venue":venue.val() }, success:function(result){ if(result != null ){ if(result.data > 0){ alert("添加成功!"); if(confirm("是否继续添加?")){ location.href = location.href; }else{ location.href = "list.html"; } }else if(result.data == -1){ alert("时间格式输入不正确,请重新输入"); }else{ alert("添加失败") } } } }); } </script>
b、修改竞赛信息
修改竞赛信息时,竞赛名称是不能被修改的,然后对修改后的竞赛信息的时间格式进行判断,满足后数据库的竞赛格式进行修改。
//修改竞赛信息 @RequestMapping("/updatecompetition") public int updateCompetition(Competition competition){ int res = 0; if(competition == null){ return res; } DateUtil.isValid(competition.getPublication_date()); //确保插入的时间格式正确 if( !DateUtil.isValid(competition.getPublication_date()) || !DateUtil.isValid(competition.getSubmission_deadline())){ return -1; } res = competitionService.updateCompetition(competition); return res; }
前端代码实现:
首先需要获取到url中的竞赛信息的竞赛编号。
//获取到url中的指定参数 function getParamvalue(key){ var url = location.search; if(url != ""){ url = url.substr(1);//获取到?后的字符串即查询字符串 var kvs = url.split("&"); for(var i = 0;i < kvs.length;i++){ var kv = kvs[i].split("="); if(kv[0] == key){ return kv[1]; } } }else{ return ""; } }
然后将要修改的竞赛信息展示出来。
//将要修改的用户信息展示出来 function getUserInfo(){ competition_id=getParamvalue("competition_id"); jQuery.ajax({ url:"getcompetitionbyid", type:"GET", data:{"competition_id":competition_id,}, success:function(result){ if(result != null && result.data != null ){ var competition = result.data; jQuery("#competition_name").val(competition.competition_name); jQuery("#competition_description").val(competition.competition_description); jQuery("#publication_date").val(competition.publication_date); jQuery("#submission_deadline").val(competition.submission_deadline); jQuery("#sponsor").val(competition.sponsor); jQuery("#venue").val(competition.venue); }else{ alert("请求错误!") } } }) }
对竞赛信息进行修改。
function myUpdate(){ var competition_name = jQuery("#competition_name"); var competition_description = jQuery("#competition_description"); var publication_date= jQuery("#publication_date"); var submission_deadline = jQuery("#submission_deadline"); var venue = jQuery("#venue"); var sponsor = jQuery("#sponsor"); //非空校验 if(jQuery.trim(competition_name.val()) === ""){ alert("请先输入竞赛名称"); competition_name.focus(); return false; } if(jQuery.trim(competition_description.val()) === ""){ alert("请先输入竞赛描述"); competition_description.focus(); return false; } if(jQuery.trim(publication_date.val()) === ""){ alert("请先输入发布时间"); publication_data.focus(); return false; } if(jQuery.trim(submission_deadline.val()) === ""){ alert("请先输入截止时间"); submission_deadline.focus(); return false; } if(jQuery.trim(sponsor.val()) === ""){ alert("请先输入赞助商"); sponsor.focus(); return false; } if(jQuery.trim(venue.val()) === ""){ alert("请先输入地点"); venue.focus(); return false; } jQuery.ajax({ url:"updatecompetition", type:"POST", data:{ "competition_id":competition_id, "competition_name":competition_name.val(), "competition_description":competition_description.val(), "publication_date":publication_date.val(), "submission_deadline":submission_deadline.val(), "sponsor":sponsor.val(), "venue":venue.val() }, success:function (result){ if(result != null ){ if(result.data > 0){ alert("修改成功!"); location.href = "list.html"; }else if(result.data === -1){ alert("时间格式输入不正确,请重新输入"); }else{ alert("修改失败") } } } }) }
c、删除单条竞赛信息
根据竞赛信息的id来对竞赛信息进行删除。
//删除指定id的竞赛信息 @RequestMapping("/deletecompetition") public int deleteCompetition(Integer competition_id){ int res = 0; if(competition_id == null || competition_id <= 0){ return res; } res = competitionService.deleteCompetition(competition_id); return res; }
前端代码实现:
function deleteCompetition(competition_id){ if(confirm("确认删除本条竞赛信息吗?一旦删除,数据不可恢复!")){ jQuery.ajax({ url:"deletecompetition", type:"POST", data:{"competition_id":competition_id}, success:function (result){ if(result != null && result.data > 0){ alert("删除成功"); //页面刷新 location.href = location.href; }else{ alert("删除失败"); } } }); } }
d、删除多条竞赛信息
将要删除的竞赛信息的id存入一个集合中进行删除。
//删除指定id集合的竞赛信息 @RequestMapping("/deletecompetitions") public int deleteCompetitions( @RequestParam List<Integer> competition_ids){ int res = 0; if(competition_ids == null){ return res; } res = competitionService.deleteCompetitions(competition_ids); return res; }
前端代码实现:
function deleteCompetitions(){ if(confirm("确认删除所选的竞赛信息吗?一旦删除,数据不可恢复!")){ var competition_ids =""; jQuery("#info").find("tr").each(function (i){ if(jQuery(this).find("th:first").find("input").prop("checked")===true){ competition_ids +=jQuery(this).find("th:first").find("input").attr("id")+","; } }); if(competition_ids !== ""){ jQuery.ajax({ url:"deletecompetitions", type:"POST", data:{"competition_ids":competition_ids}, success:function (result){ if(result != null && result.data > 0){ alert("删除成功"); //页面刷新 location.href = location.href; }else{ alert("删除失败"); } } }) } } }
e、查询并分页展示所有的竞赛信息
可以对所有的竞赛信息进行竞赛名称、发布时间和截止时间进行查询,如果未输入任何查询信息就展示出所有的竞赛信息,默认从第一页开始,每页展示10条竞赛信息。
//分页查询竞赛信息 @RequestMapping("/list") public HashMap<String,Object> list(String competition_name, String publication_date, String submission_deadline,Integer index,Integer size){ if(index == null || index <= 0){ index = 1; } if(size == null || size <= 0){ size = 10; } if(!StringUtils.hasLength(competition_name)){ competition_name = null; } if(!StringUtils.hasLength(publication_date)){ publication_date = null; } if(!StringUtils.hasLength(submission_deadline)){ submission_deadline = null; } int offset = (index - 1) * size; HashMap<String,Object> result = new HashMap<>(); List<Competition> list = competitionService.list(competition_name,publication_date,submission_deadline,size,offset); int count = competitionService.getCount(competition_name,publication_date,submission_deadline); result.put("list",list); result.put("count",count); return result; }
前端代码实现:
首先初始化页面,获取到竞赛信息,默认从第一页进行查询。
//定义全局变量 var competition_name=""; var publication_date=""; var submission_deadline=""; var pages=0;//总页数 var count=0;//总条数 var index=1;//当前页数 var size=10;//每页显示的用户数目 //获取到url中的指定参数 function getParamvalue(key){ var url = location.search; if(url !== ""){ url = url.substr(1);//获取到?后的字符串即查询字符串 var kvs = url.split("&"); for(var i = 0;i < kvs.length;i++){ var kv = kvs[i].split("="); if(kv[0] === key){ return decodeURIComponent(kv[1]); } } }else{ return ""; } } function firstPage(){ location.href="list.html?index=1&competition_name="+competition_name+"&publication_date="+publication_date+"&submission_deadline="+submission_deadline; } function lastPage(){ location.href="list.html?index="+pages+"&competition_name="+competition_name+"&publication_date="+publication_date+"&submission_deadline="+submission_deadline; } function nextPage(){ if(index >= pages){ alert("已经是最后一页了!"); }else{ index=parseInt(index)+1; location.href="list.html?index="+index+"&competition_name="+competition_name+"&publication_date="+publication_date+"&submission_deadline="+submission_deadline; } } function prePage(){ if(index <=1){ alert("已经是第一页了!"); }else{ index=parseInt(index)-1; location.href="list.html?index="+index+"&competition_name="+competition_name+"&publication_date="+publication_date+"&submission_deadline="+submission_deadline; } } //获取竞赛信息列表 function getList(){ jQuery.ajax({ url:"list", type:"GET", data:{ "competition_name":competition_name, "publication_date":publication_date, "submission_deadline":submission_deadline, "index":index, "size":size }, success:function(result){ if(result != null && result.data != null) { var listDiv = ""; count = result.data.count;//获取到总的用户数 pages = Math.ceil(parseInt(count) / size); jQuery("#pageinfo").html("共" + count + "条数据,共" + pages + "页"); if (result.data.list.length>0) { for (var i = 0; i < result.data.list.length; i++) { var competition = result.data.list[i]; listDiv += '<tr>'; listDiv += '<th>'; listDiv += '<input id="' + competition.competition_id+ '" type="checkbox"></th>'; listDiv += '<th>' + competition.competition_id + '</th>'; listDiv += '<th>' + competition.competition_name + '</th>'; listDiv += '<th>' + competition.competition_description + '</th>'; listDiv += '<th>' + competition.publication_date + '</th>'; listDiv += '<th>' + competition.submission_deadline + '</th>'; listDiv += '<th>' + competition.sponsor + '</th>'; listDiv += '<th>' + competition.venue + '</th>'; listDiv += '<th>'; listDiv += '<a class="btn btn-default btn-sm" href="update.html?competition_id=' + competition.competition_id + '">修改</a>'; listDiv += '<a class="btn btn-default btn-sm" href="javascript:deleteCompetition(' + competition.competition_id + ');">删除</a>'; listDiv += '</th></tr>'; } jQuery("#info").html(listDiv); } } } }); } //初始化页面 function init(){ competition_name=getParamvalue("competition_name"); publication_date=getParamvalue("publication_date"); submission_deadline=getParamvalue("submission_deadline"); index=getParamvalue("index"); if(index == null || index === ""){ index=1; } getList(); }
如果要进行条件查询,先获取到查询信息,再进行分页查询。
//查询功能 function myQuery(){ competition_name = jQuery.trim(jQuery("#competition_name").val()); publication_date = jQuery.trim(jQuery("#publication_date").val()); submission_deadline = jQuery.trim(jQuery("#submission_deadline").val()); getList(); firstPage(); }
六、效果展示
登陆页面:
注册页面:
竞赛信息页面:
添加竞赛信息页面:
修改竞赛信息页面: