杨校老师课堂之Java EE框架实训宠归于好项目开发(三)

2020-12-21 11:28:57 浏览数 (1)

类型模块
1. 类别的实体类
代码语言:javascript复制
package cn.javabs.pet.entity;

/**
 * 类别  实体类
 */
public class Type {
	// 类别编号
    private  Integer typeId;
    // 类别名称 
    private  String  name;

    @Override
    public String toString() {
        return "Type{"  
                "typeId="   typeId  
                ", name='"   name   '''  
                '}';
    }

    public Integer getTypeId() {
        return typeId;
    }

    public void setTypeId(Integer typeId) {
        this.typeId = typeId;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name == null  ? null : name;
    }
}
2. service层 接口
代码语言:javascript复制
package cn.javabs.pet.service;

import cn.javabs.pet.entity.Type;

import java.util.List;

/**
 * 类别的 业务逻辑层接口
 */
public interface TypeService {

    /**
     * 查询所有类别,考虑后不需要 分页
     * @return
     */
    List<Type>  findAll();

    /**
     *  通过id进行查询
     * @param typeId 分类id
     * @return
     */
    Type findTypeById(int typeId);


    /**
     *  添加类别
     * @param type
     * @return
     */
    int add(Type type);

    /**
     * 通过id进行删除
     * @param typeId
     * @return
     */
    int delete(int typeId);

    /**
     * 修改类别
     * @param type
     * @return
     */
    int update(Type type);
}
3. service层实现类
代码语言:javascript复制
package cn.javabs.pet.service;

import cn.javabs.pet.entity.Type;
import cn.javabs.pet.mapper.TypeMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class TypeServiceImpl implements  TypeService{


    // 自动装配
    @Autowired
    private TypeMapper typeMapper;

    /**
     * 查询所有类别,考虑后不需要 分页
     *
     * @return
     */
    @Override
    public List<Type> findAll() {
        List<Type> list = typeMapper.getList();
        System.out.println("list: "   list);
        return list;

    }

    /**
     * 通过id进行查询
     *
     * @param typeId 分类id
     * @return
     */
    @Override
    public Type findTypeById(int typeId) {
        return typeMapper.getTypeById(typeId);
    }

    /**
     * 添加类别
     *
     * @param type
     * @return
     */
    @Override
    public int add(Type type) {
        return typeMapper.add(type);
    }

    /**
     * 通过id进行删除
     *
     * @param typeId
     * @return
     */
    @Override
    public int delete(int typeId) {
        return typeMapper.del(typeId);
    }

    /**
     * 修改类别
     *
     * @param type
     * @return
     */
    @Override
    public int update(Type type) {
        return typeMapper.update(type);
    }
}
4. mapper接口
代码语言:javascript复制
package cn.javabs.pet.mapper;

import cn.javabs.pet.entity.Type;
import org.apache.ibatis.annotations.Select;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository
public interface TypeMapper {

    @Select("select * from type order by typeId desc")
    List<Type> getList();

    @Select("select * from type where typeId = #{typeId}")
    Type getTypeById(int typeId);

    int add(Type type);

    int del(int typeId);

    int update(Type type);
}
mapper映射文件
代码语言:javascript复制
<!DOCTYPE mapper
        PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-mapper.dtd">

<mapper namespace="cn.javabs.pet.mapper.TypeMapper">

    <!--添加类别-->

    <insert id="add" parameterType="Type">
        insert  into  type(typeId,name) values (#{typeId},#{name})
    </insert>

    <!--删除类别-->
    <delete id="del" parameterType="int">
        delete  from  type where typeId = #{typeId}
    </delete>

    <!--修改类别-->
    <update id="update" parameterType="Type">
        update type set name = #{name} where typeId = #{typeId}
    </update>

</mapper>
数据库-类型表
代码语言:javascript复制
use  pet;

create table type{

  typeId  int primary key auto_increment,
  
  name varchar(20) not null

}
控制器Controller
代码语言:javascript复制
package cn.javabs.pet.controller;

import cn.javabs.pet.entity.Type;
import cn.javabs.pet.service.TypeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@Controller
public class TypeController {

    @Autowired
    private TypeService typeService;

    /**
     * 查询所有分类
     * @param request
     * @return
     */
    @GetMapping("typeList")
    public String typeList(HttpServletRequest request){
        request.setAttribute("flag",4);
        List<Type> typeList = typeService.findAll();
        System.out.println("typeList:"   typeList);
        request.setAttribute("typeList",typeList);
        return  "admin/type_list.jsp";
    }

    /**
     * 添加类别
     * @param request
     * @param type
     * @return
     */
    @PostMapping("typeSave")
    public String typeSave(HttpServletRequest request,Type type){
        request.setAttribute("flag",4);
        typeService.add(type);
        return "redirect:typeList?flag=4";

    }

    /**
     * 删除类别
     * @param request
     * @param id
     * @return
     */
    @GetMapping("typeDelete")
    public String   typeDelete(HttpServletRequest request,int id){
        request.setAttribute("flag",4);
        typeService.delete(id);
        return "redirect:typeList?flag=4";
    }

    /**
     * 数据回显
     * @param request
     * @param id
     * @return
     */
    @GetMapping("typeEdit")
    public String  typeEdit(HttpServletRequest request,int id){
        request.setAttribute("flag",4);
        Type type = typeService.findTypeById(id);
        request.setAttribute("type",type);
        return "admin/type_edit.jsp";
    }
    
    @PostMapping("typeUpdate")
    public String typeUpdate(HttpServletRequest request,Type type){
        request.setAttribute("flag",4);
        typeService.update(type);
        return "redirect:typeList?flag=4";
    }
}
类型列表页
代码语言:javascript复制
<%@ page language="java" contentType="text/html; charset=UTF-8" %>
<!DOCTYPE html>
<html>
<head>
<title>类别列表</title>
<meta charset="utf-8"/>
<link rel="stylesheet" href="${pageContext.request.contextPath}/admin/css/bootstrap.css"/>
</head>
<body>
<div class="container-fluid">

	<%@include file="header.jsp"%>
	
	<br>
	
	<div>
		<form class="form-inline" method="post" action="typeSave">
			<input type="text" class="form-control" id="input_name" name="name" placeholder="输入类目名称" required="required" style="width: 500px">
			<input type="submit" class="btn btn-warning" value="添加类目"/>
		</form>
	</div>
	
	<br>

	<table class="table table-bordered table-hover">

	<tr>
		<th width="5%">ID</th>
		<th width="10%">名称</th>
		<th width="10%">操作</th>
	</tr>
	
	<c:forEach var="type" items="${typeList}">
         <tr>
         	<td><p>${type.typeId}</p></td>
         	<td><p>${type.name}</p></td>
			<td>
				<a class="btn btn-primary" href="typeEdit?id=${type.typeId}">修改</a>
				<a class="btn btn-danger" href="typeDelete?id=${type.typeId}">删除</a>
			</td>
       	</tr>
     </c:forEach>
     
</table>

</div>
</body>
</html>
类型修改页
代码语言:javascript复制
<%@ page language="java" contentType="text/html; charset=UTF-8" %>
<!DOCTYPE html>
<html>
<head>
<title>类目编辑</title>
<meta charset="utf-8"/>
<link rel="stylesheet" href="${pageContext.request.contextPath}/admin/css/bootstrap.css"/>
</head>
<body>
<div class="container-fluid">

	<%@include file="header.jsp"%>

	<br><br>
	
	<form class="form-horizontal" action="typeUpdate" method="post">
		<input type="hidden" name="typeId" value="${type.typeId}">
		<div class="form-group">
			<label for="input_name" class="col-sm-1 control-label">类目名称</label>
			<div class="col-sm-6">
				<input type="text" class="form-control" id="input_name" name="name" value="${type.name}" required="required">
			</div>
		</div>
		<div class="form-group">
			<div class="col-sm-offset-1 col-sm-10">
				<button type="submit" class="btn btn-success">提交修改</button>
			</div>
		</div>
	</form>
	
	<span style="color:red;">${msg}</span>
	
</div>	
</body>
</html>

0 人点赞