今天我们要完成的项目功能为就业城市的查询和增加,因为就业城市是单表操作,设计到的类相对来说比较少,所以先完成
1.创建City实体类
package com.pojo;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.ToString;
import lombok.experimental.Accessors;
import java.util.Date;
@Data
@AllArgsConstructor
@NoArgsConstructor
@ToString
@Accessors(chain=true)
public class City {
private String id;
private String name;
private Date createtime;
private Integer numbers;
}
注解解析:
@Data:为类提供get、set、equals、hashCode、canEqual方法(一般都会写)
@AllArgsConstructor:为类提供有参构造方法
@NoArgsConstructor:为类提供无参构造方法
@ToString:为类提供tostring方法
@Accessors(chain=true):可以进行链式调用
2.编写CityMapper接口类
我这次抽取了一些所有实体类都要写的增删改查抽象方法以及分页相关的查询(分页查询以及查询所有条数),放在了BaseDao里面,以后调用的时候就比较方便了
package com.mapper;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import java.util.List;
@Mapper
//泛型1代表操作对象类型,泛型2代表主键类型
public interface BaseDao<T,K> {
void save(T t);
void update(T t);
void delete(K t);
T findById(K k);
List<T> findAll();
List<T> findByPage(@Param("start") Integer start,
@Param("rows") Integer rows);
Long findCounts();
}
package com.mapper;
import com.pojo.City;
import org.apache.ibatis.annotations.Mapper;
import java.util.List;
@Mapper
public interface CityMapper extends BaseDao {
List<City> findAllJSON();
City findById(int id);//通过id查询city,后期会用到
}
3.编写CityMapper.xml
这些都是较为基础的sql语句,一定要熟练掌握,闭着眼睛都要会
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.mapper.CityMapper">
<insert id="save" parameterType="com.pojo.City" keyProperty="id" useGeneratedKeys="true">
insert into t_city values(#{id},#{name},#{createtime},#{numbers})
</insert>
<select id="findAll" resultType="com.pojo.City">
select id,name,createtime,numbers from t_city
</select>
<select id="findAllJSON" resultType="com.pojo.City">
select id,name,createtime,numbers from t_city
</select>
<select id="findById" resultType="com.pojo.City">
select id,name,createtime,numbers from t_city where id = #{id}
</select>
<update id="update" parameterType="com.pojo.City">
update t_city set numbers = #{numbers} where id=#{id}
</update>
<delete id="delete" parameterType="string">
delete from t_city where id=#{id}
</delete>
</mapper>
4.编写CityController
由于这一个项目涉及到了ajax,所以特意编写了一个方法是返回json数据的,如果是利用ajax来进行向后台发请求的话,千万1不要忘记在方法前加 **@ResponseBody
**注解
package com.controller;
import com.pojo.City;
import com.service.CityService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import java.util.List;
@Controller
@RequestMapping("city")
public class CityController {
@Autowired
CityService cityService;
@RequestMapping("delete")
public String delete(String id){
cityService.delete(id);
return "redirect:/city/findAll";
}
@RequestMapping("save")
public String save(City city){
cityService.save(city);
return "redirect:/city/findAll";
}
@RequestMapping("findAll")
public String findAll(Model model){
List<City> citys = cityService.findAll();
model.addAttribute("citys",citys);
return "back/city/index";
}
@RequestMapping("findAllJSON")
@ResponseBody
public List<City> findAllJSON(){
return cityService.findAllJSON();
}
}
5.前端界面展示
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>
<%@page contentType="text/html; UTF-8" pageEncoding="UTF-8" isELIgnored="false" %>
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport"
content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@3.3.7/dist/css/bootstrap.min.css"/>
<script src="${pageContext.request.contextPath}/back/statics/js/jquery-1.8.3.min.js"></script>
</head>
<body>
<div class="container-fluid">
<div class="row">
<div class="col-sm-6 col-sm-offset-3">
<h1 class="text-center">城市管理</h1>
</div>
</div>
<div class="row">
<div class="col-sm-12">
<table class="table table-striped table-bordered table-hover" align="center">
<tr>
<th align='center' valian='middle'>编号</th>
<th align='center' valian='middle'>名称</th>
<th align='center' valian='middle'>录入时间</th>
<th align='center' valian='middle'>当前城市就业人数</th>
<th align='center' valian='middle'>操作</th>
</tr>
<c:forEach items="${requestScope.citys}" var="city">
<tr>
<td align='center' valian='middle'>${city.id}</td>
<td align='center' valian='middle'>${city.name}</td>
<td align='center' valian='middle'><fmt:formatDate value="${city.createtime}" pattern="yyyy年MM月dd日" /></td>
<td align='center' valian='middle'>${city.numbers}</td>
<td align='center' valian='middle'>
<a href="" class="btn btn-info btn-sm">修改</a>
<a href="${pageContext.request.contextPath}/city/delete?id=${city.id}" class="btn btn-danger btn-sm" >删除</a>
</td>
</tr>
</c:forEach>
</table>
</div>
</div>
<%--添加城市--%>
<div class="row">
<div class="col-sm-12">
<form class="form-inline" id="inputForm" action="${pageContext.request.contextPath}/city/save" method="post">
<div class="form-group">
<label for="name">城市名称:</label>
<input type="text" class="form-control" id="name" name="name" placeholder="输入城市名称">
</div>
<button type="submit" class="btn btn-success">添加城市信息</button>
</form>
</div>
</div>
</div>
</body>
<script>
$(function(){
//提交事件
$("#inputForm").submit(function () {
if (!$("#name").val()) {
alert('城市名称不能为空!!!');
return false;
}
});
})
</script>
6.测试
记得要在昨天的meau.jsp那里修改请求
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@page contentType="text/html; UTF-8" pageEncoding="UTF-8" isELIgnored="false" %>
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@3.3.7/dist/css/bootstrap.min.css"/>
<script src="${pageContext.request.contextPath}/back/statics/js/jquery-1.8.3.min.js"></script>
<style>
body,ul,li{
background:#eee;
}
.list-group-item{
background: #eee;
}
</style>
<body>
<ul class="list-group text-center" style="margin-top: 70px;">
<li class="list-group-item"><a href="${pageContext.request.contextPath}/student/findAll" target="content">学生管理</a>
</li>
<c:if test="${sessionScope.user.role=='admin'}">
<li class="list-group-item"><a href="${pageContext.request.contextPath}/group/findAll" target="content">小组管理</a>
</li>
<li class="list-group-item"><a href="${pageContext.request.contextPath}/clazz/findAll" target="content">班级管理</a>
</li>
<li class="list-group-item"><a href="${pageContext.request.contextPath}/tag/findAll" target="content">标签管理</a>
</li>
<li class="list-group-item"><a href="${pageContext.request.contextPath}/city/findAll" target="content">城市管理</a>
</li>
</c:if>
</ul>
</body>
噔噔噔噔,大功告成!