栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

2022年6月9号(前后端的数据交互技术项目七:回顾这个学期的所学):梦幻神游入三国-回望自己这个学期的所学SSM框架总结起来四个字:删 查 增 改 简称(CRUP)。

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

2022年6月9号(前后端的数据交互技术项目七:回顾这个学期的所学):梦幻神游入三国-回望自己这个学期的所学SSM框架总结起来四个字:删 查 增 改 简称(CRUP)。

2022年6月9号(前后端的数据交互技术项目七:回顾这个学期的所学):梦幻神游入三国-回望自己这个学期的所学SSM框架总结起来四个字:删 查 增 改 简称(CRUP)。 接收到任务:今天的目标是完成单张表的(CRUP)将数据库的内容展示在页面上:首先完成这项任务要用到那些知识的要点呢! 第一部分:回顾一下所学框架的一些概念和基础的定义:学这个的目的是为了日后的更好的工作.
  1. MyBatis框架。作用于对数据访问层的操作 对数据库的数据增删改查 这里包含多张表的增删改查.
  2. Spring框架。这是一个模块 作用在 Mvc三层的各个领域.
  3. SpringMvc框架.是Spring框架的一个模块 作用在控制层上.
  4. Ajax技术。作用在前后端的数据交互上主要运用到的几个重要的参数信息.
  5. Json数据解析。将控制层发送过来的增删改查的数据在Mvc转换成Json格式,然后在Html页面中对数据进行解析.
  6. Html Css JavaScript Jquery 这是前端的入门基础也是java程序员一定要会的操作.
  7. MySql 这是一门课程要想学更好 又要自己更加的深入的去了解信息。

第二部分:分析Mvc三层架构的执行流程代码是如何执行的呢?本项目采用的是注解开发的项目的. 理清思路:才能更好的解决问题. 方案一:如果采用的是普通的Java代码开发. 控制层.作用控制项目的整个运行流程:@1 接收浏览器发过来的消息,将消息发送到Mvc中的三层中层层返回最后回到控制层. @2 控制层将最后数据库中的增删改查的数据返回到页面中 这里要用到Ajax+Json两课的灵活运用. 业务逻辑层:处理一些业务逻辑的信息 这里有 一个接口  在接口中定义的方法在实现类中去实现接口中没有实现的方法. 数据访问层:处理一些数据库信息 对数据库的信息增删改查:这里有 一个接口  在接口中定义的方法在实现类中去实现接口中没有实现的方法.

 

方案二:如果采用的是SSM框架的不用注解的方式用配置文件的方式 控制层:作用控制项目的整个运行流程:@1 接收浏览器发过来的消息,将消息发送到Mvc中的三层中层层返回最后回到控制层. @2 控制层将最后数据库中的增删改查的数据返回到页面中 这里要用到Ajax+Json两课的灵活运用. 业务逻辑层:处理一些业务逻辑的信息 这里有 一个接口  在接口中定义的方法在实现类中去实现接口中没有实现的方法. 数据访问层:处理一些数据库信息 对数据库的信息增删改查:这里有 一个接口  在接口中定义的方法在实现类中去实现接口中没有实现的方法.这里要运用到的是三个框架的配置文件信息. 方案三:如果采用的是SSM框架的用注解的方式开发. 控制层:作用控制项目的整个运行流程:@1 接收浏览器发过来的消息,将消息发送到Mvc中的三层中层层返回最后回到控制层. @2 控制层将最后数据库中的增删改查的数据返回到页面中 这里要用到Ajax+Json两课的灵活运用. 业务逻辑层.处理一些业务逻辑的信息 这里有 一个接口  在接口中定义的方法在实现类中去实现接口中没有实现的方法. 数据访问层.这里只需要定义接口文件 利用注解的开发对数据 的增伤改查操作 注解一般以@ 开头
第三部分:分析增删改查定义接口的原理:
  1. 如果用户要查整张表的信息要做以下分析

    1 表中查到的是一条记录 那你又可以这样做 在java中定义了 Map集合 Map   

  2.   2 如果查到的是两条记录或者是多条记录 外表用到是list集合 里面是MAP集合 

  3.     3所以多条记录利用的是 list中调用map集合 List> getCarList()

    如果用户查询表的一个字段 中有多条记录

    1  多条记录利用List集合 一个字段利用的是String存放 2所以使用的是list中调用String List

  4. 如果用户要增加一条记录 在这条记录中 包含多个字段 如果用户要修改一条一条记录 包含多个字段 如果用户要删除一条记录 包含多个字段 返回值 为 int类型

  5. 如果用户要增加一条记录 在这条记录中 包含多个字段

  6. 1首先服务器要接收用户增加数据的字段名 利用 @RequestParam 将每个字段存放起来

  7. 2 然后利用map集合的方式将增加的多个字段 封装起来转入到mvc三层流程图中

  8. 3 因为控制层将增加的数据封装成了Map集合方式 所以 在mvc三层的所有接口中定义 int 方法名称(Map map)

  9. 如果用户查询表的一个字段 中有多条记录

    1多条记录利用List集合 一个字段利用的是String存放

  10. 2所以使用的是list中调用String List 如果用户要增加一条记录 在这条记录中 包含多个字段 如果用户要修改一条一条记录 包含多个字段 如果用户要删除一条记录 包含多个字段 返回值 为 int类型 如果用户要增加一条记录 在这条记录中 包含多个字段

  11. 1首先服务器要接收用户增加数据的字段名 利用 @RequestParam 将每个字段存放起来

  12. 2 然后利用map集合的方式将增加的多个字段 封装起来转入到mvc三层流程图中

  13. 3 因为控制层将增加的数据封装成了Map集合方式 所以 在mvc三层的所有接口中定义 int 方法名称(Map map)

  14. 如果用户要删除一条记录 包含多个字段

    int del(String ids); 删除为多条记录 int del(int id); 删除为一条记录 返回值为int类型 更据id删除 所以服务器器要接收到浏览器发过来的id更据主键id删除数据库指定的数据记录在第几条

  15. 如果用户查询表的一个字段 中有多条记录

    1多条记录利用List集合 一个字段利用的是String存放

  16. 2所以使用的是list中调用String List 如果用户要增加一条记录 在这条记录中 包含多个字段 如果用户要修改一条一条记录 包含多个字段 如果用户要删除一条记录 包含多个字段 返回值 为 int类型 如果用户要增加一条记录 在这条记录中 包含多个字段

  17. 1首先服务器要接收用户增加数据的字段名 利用 @RequestParam 将每个字段存放起来

  18. 2 然后利用map集合的方式将增加的多个字段 封装起来转入到mvc三层流程图中

  19. 3 因为控制层将增加的数据封装成了Map集合方式 所以 在mvc三层的所有接口中定义 int 方法名称(Map map)

    如果用户要修改一条一条记录 包含多个字段 同理在SSM框架中修改数据

    1 首先用户要将你要修改的是哪条记录的参数发到服务器中 然后服务器利用mvc三层原理最终将其转到数据库中

  20. 2 我查到的每一条记录 的存放方式是 Map

  21. 3 所以 返回的类型为 Map 方法名称(转入的参数) Map getInfoById(int id);

  22. 4 将修改信息如何展示在页面上这里需要你的前端基础

  23. 5 将浏览器修改后的信息返回到服务器 中到数据库中完成数据的修改

  24. 6 将修改的信息利用Map封装起来 所以 返回的类型为 int方法名称(转入的参数) int updateInfo(Map map); 思考你要修改的是一条记录 而一条记录利用的是 Map 存放

  25. 返回一条记录的值为 int 类型 所以接口定义下面方案 int updateInfo(Map map);


第四部分:Mvc三层架构增删改查执行流程. 查询功能
  1. 在查询的数据库中的表中肯定是多条记录 每个条记录中有多个字段 因此使用Java存放的方式是

  2. 在这里我使用的是MVC三层架构的普通方式介绍

  3. 因此你要在数据反问层的接口中定义方法

  4. List> getStudentList();}或者是ListgestudentList();}

  5. 同理在业务逻辑思层中:

  6. List> getStudentList();}或者是ListgestudentList();}

  7. 那么如何将控制层 业务逻辑层 数据反问层 连接起来呢!

  8. 在控制层中: ITeacherService service = new TeacherServiceImp();

  9. 在业务逻辑层中

  10. ITeacherDao dao=new TeacherDaoImp();

  11. 然后层层返回 到控制层

  12. List> teacherList = service.getTeacherList();

  13. Map map = new HashMap<>();

  14. map.put("list", teacherList);

  15. new ObjectMapper().writeValue(response.getWriter(), map);

  16. 发到HTML页面进行JSON解析。

  17. JSON解析后会将数据展示在页面。

删除功能
  1. 写出SQL语句

  2. delete from emp where id=?

  3. 分析SQL语句在Java中利用什么方式存放起来的

  4. 在增删改中返回值为int类型值。删除是根据编号删除的因此控制器要将浏览器发来的ID接收 利用是是resquest对象

  5. 在控制层调用业务逻辑层的方法利用。

  6. IEmpService service=new EmpServiceImp()

  7. 同理在业务逻辑层是实现类中

  8. IEmpDao dao = new EmpDaoImp();

  9. 在业务逻辑层接口中定义方法利用int del(int Id)

  10. 同理在数据访问层接口中定义方法

  11. int del(int Id)

  12. int delete service.delete(ids);if(delete>0){response.sendRedirect("workList.html"); }

  13. 利用重定向的方式

增加功能
  1. SQL语句当我点击按钮跳转到一个新的页面用于增加数据的信息。

  2. 当用户增加成功要进行页面的查询操作。

  3. insert into emp values(?,?,?,?,?,?)",

  4. 返回值为int类型 在没条记录中但对应的属性和值。

  5. 在业务逻辑层接口中定义方法 int add(Map map);

  6. 同理在数据访问层

  7. int add(Map map);

  8. 在控制层中

  9. Map parameterMap = request.getParameterMap();

  10. int add = service.add(parameterMap);

  11. if (add > 0) {

  12. //添加成功,跳回主页面

  13. response.sendRedirect("index.html");}

修改功能
  1. 首先第一部要获得你要修改那一条数据的id

  2. 第二步你要用jQuery的属性选择器将数据显示在页面上

  3. 第三步 你要将修改后的页面数据信息发回到服务器 经历MVC 三层架构 将修改后上,信息在数据库中更新

  4. 第四步将更新后的数据刷新到页面上查看。

  5. 总结遇到修改不用慌

  6. 先分清楚流程是什么 利用的技术是 Ajax+JSON解析。当我要修改的的记录编号ID 为1001001 时候

  7. 首先浏览器要将ID编号发送给服务器 在控制层中

  8. 将浏览器发过来的ID等于1001001接收

  9. 利用的是resquest 对象 然后将ID 参数发送到 业务逻辑层 在到数据访问层

  10. 为了更好的理解一般是从数据访问层的接口中定义方法

  11. Map getInfoStudentById(int id);

  12. 在数据反问层实现类中实现

  13. DBUtil.jt.queryForMap("select * from student where id=?", new Object[]{id});}

  14. 这里数据反问层使用的方案是

  15. Ajax+Json+Mvc+JdbcTemplate+Servlect+Html+JavaScript+Jquery+Mysql

  16. 同理在业务实现类的接口

  17. Map getInfoStudentById(int id);

  18. 在业务逻辑层接口的实现类中实现。

  19. 在控制层中调用以下方法

  20. Map infoStudentById = service.getInfoStudentById(Integer.parseInt(id));

  21. //我要获得 年龄 姓名 电话 地址

  22. List age = service.getAge();

  23. List name = service.getName();

  24. List phone = service.getPhone();

  25. List address = service.getAddress();

  26. 第二步步分上面的数据利用什么方式封装起来

  27. Map map = new HashMap<>();

  28. map.put("infoStudentById", infoStudentById);

  29. map.put("age", age);

  30. map.put("name", name);

  31. map.put("phone", phone);

  32. map.put("address", address);

  33. new ObjectMapper().writeValue(response.getWriter(), map);

  34. 比如在浏览器中要在input标签中显示性别 年龄如何实现呢!

  35. $("input[name=age]").val(json.infoStudentById.age)//将数据库中的年龄展示在页面中

  36. (json.infoStudentById.sex = "男") {

  37. $("input[value='男']").attr("checked", true)

  38. } else {

  39. $("input[value='女']").attr("checked", true)

  40. }

    用户单机修改按钮将修改后的数据发送到数据库中去步骤
  41. 浏览器中页面完成修改后数据库也要更新

  42. 因此根据MVC三层架构思想

  43. 控制器要接收到浏览器要修改的页面数据信息

  44. 在控制层的第一步

  45. Map parameterMap = request.getParameterMap();

  46. String id = (String) request.getSession().getAttribute("id");

  47. 接下来从数据访问层的接口中定义方法

  48. int updateStudent(Map map);

  49. 同理在业务逻辑层中的接口中写下以下方法

  50. int updateStudent(Map map);

  51. 最终控制器回执行下面的内容

  52. int student = service.updateStudent(parameterMap, Integer.parseInt(id));

  53. if(student>0){

  54. System.out.println("恭喜用户修改信息已完成");

  55. response.sendRedirect("workList1.html");

  56. }

  57. 最后修改页面完成。仔细分析发现也不是那么难吧!珞


项目代码: 前端代码



    
    Title






    
    图书管理系统的首页查询页面
    
编号 书籍编号 书籍名称 价格 用户浏览量 阅读人 网上点击量 删除 增加 修改>
第二张表的增删改查



    
    Title
    



    增加数据页面
    
书籍编号
书籍名称
书籍价格
书籍数量
阅读的作者
点击量

 




    
    Title
    





    
    修改数据页面
    
书籍编号
书籍名称
书籍价格
书籍数量
阅读的作者
点击量

 

package Com.BookSystem.Controller;

import Com.BookSystem.Service.IBookSystemService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


@Controller
public class ControllerBookSystem {
    @Autowired
    IBookSystemService service;

//    定义方法查询书籍

    @RequestMapping("list.do")
    @ResponseBody
    public Object ListS() {
        List> list = service.getListS();
        return list;
    }

    //    定义方法删除数据
    @RequestMapping("del.do")
    public String del(@RequestParam int id) {
        int i = service.del(id);

        return "index.html";
    }
//    定义方法增加数据

    
    @RequestMapping("add.do")
    public String add(@RequestParam int bookid,
                      @RequestParam String scribe,
                      @RequestParam int price,
                      @RequestParam int number,
                      @RequestParam String redingwriter,
                      @RequestParam int dianjinumber) {
        System.out.println("开始增加数据信息");
//         
        Map map = new HashMap<>();
        map.put("bookid", bookid);
        map.put("scribe", scribe);
        map.put("price", price);
        map.put("number", number);
        map.put("redingwriter", redingwriter);
        map.put("dianjinumber", dianjinumber);
        int i = service.add(map);
        return "index.html";
    }

    //    定义方修改页面
    @RequestMapping("update1.do")
    public String update1(@RequestParam int id, HttpSession session) {
        session.setAttribute("id", id);
        return "update.html";
    }

    //定义方法
    @ResponseBody
    @RequestMapping("Update2.do")
    public Object update2(HttpSession session) {
        int id = (int) session.getAttribute("id");
        Map map = service.update2(id);
        return map;
    }


    @RequestMapping("update3.do")
    public String update3(@RequestParam int bookid,
                          @RequestParam String scribe,
                          @RequestParam int price,
                          @RequestParam int number,
                          @RequestParam String redingwriter,
                          @RequestParam int dianjinumber, HttpSession session) {
//         
        Map map = new HashMap<>();
        int id = (int) session.getAttribute("id");
        map.put("bookid", bookid);
        map.put("scribe", scribe);
        map.put("price", price);
        map.put("number", number);
        map.put("redingwriter", redingwriter);
        map.put("dianjinumber", dianjinumber);
        map.put("id", id);
        int i = service.update3(map);
        return "index.html";
    }


    //操作第二张表的数据
    @RequestMapping("BookList.do")
    @ResponseBody
    public Object BookList() {
        List> BookList = service.getBookList();
        return BookList;
    }

    //定义方法删除数据记录
    @RequestMapping("DelBook.do")
    public String DelBook(@RequestParam int id) {
        int i = service.getDeleteBook(id);
        return "index1.html";
    }

    //    定义方法增加数据记录
//    转入增加的参数
    @RequestMapping("CarAdd.do")
    public String CarAdd(@RequestParam String name,
                         @RequestParam int age,
                         @RequestParam Object datetime,
                         @RequestParam int CarId,
                         @RequestParam String address,
                         @RequestParam String addressCar) {
        Map map = new HashMap<>();
        map.put("name",name );
        map.put(" age", age );
        map.put("DateTime", datetime);
        map.put("CarId",CarId );
        map.put("address",address );
        map.put("addressCar", addressCar);
        int i=service.addCar(map);
        return "index1.html";
    }

//    修改数据的记录页面
    @RequestMapping("UpdateCar1.do")
    public String UpdateCar1(@RequestParam int id,HttpSession session){
        session.setAttribute("id",id);

        return "update1.html";
    }

//    定义方法
    @RequestMapping("UpdateCar2.do")
    @ResponseBody
    public  Object UpdateCar2(HttpSession session){
     int id = (int) session.getAttribute("id");
     Map map = service.SelectCar2(id);
     return map;
    }

//定义方法完成修改
    @RequestMapping("UpdateCar3.do")
    public String UpdateCar3(@RequestParam String name,
                         @RequestParam int age,
                         @RequestParam String datetime,
                         @RequestParam int CarId,
                         @RequestParam String address,
                         @RequestParam String addressCar,
                             HttpSession session) {
        Map map = new HashMap<>();
        map.put("name",name );
        map.put(" age", age );
        map.put("DateTime", datetime);
        map.put("CarId",CarId );
        map.put("address",address );
        map.put("addressCar", addressCar);
        int id = (int) session.getAttribute("id");
        map.put("id",id);
        int i=service.UpdateCar3(map);
        return "index1.html";
    }



}

 

package Com.BookSystem.Service;

import java.util.List;
import java.util.Map;


public interface IBookSystemService {
    List> getListS();

    int del(int id);

    int add(Map map);

    Map update2(int id);

    int update3(Map map);

    List> getBookList();

    int getDeleteBook(int id);

    int addCar(Map map);

    Map SelectCar2(int id);

    int UpdateCar3(Map map);
}

 

package Com.BookSystem.Service;

import Com.BookSystem.Dao.IBookSystemDao;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;


@Service(value = "sService")
public class BookSystemServiceImp  implements  IBookSystemService {
    @Autowired
    IBookSystemDao dao;

    @Override
    public List> getListS() {
        return dao.getListS();
    }

    @Override
    public int del(int id) {
        return dao.del(id);
    }

    @Override
    public int add(Map map) {
        return dao.add(map);
    }

    @Override
    public Map update2(int id) {
        return dao.update2(id);
    }

    @Override
    public int update3(Map map) {
        return dao.update3(map);
    }

    @Override
    public List> getBookList() {
        return dao.getBookList();
    }

    @Override
    public int getDeleteBook(int id) {
        return dao.deleteBook(id);
    }

    @Override
    public int addCar(Map map) {
        return dao.addCar(map);
    }

    @Override
    public Map SelectCar2(int id) {
        return dao.selectCar2(id);
    }

    @Override
    public int UpdateCar3(Map map) {
        return dao.updateCar3(map);
    }
}
package Com.BookSystem.Dao;

import org.apache.ibatis.annotations.*;

import java.util.List;
import java.util.Map;


public interface IBookSystemDao {

    @Select("select * from  booksystem")
    List> getListS();

    @Delete("delete  from  booksystem where id=#{id} ")
    int del(@Param("id") int id);

    @Insert("INSERT INTO `demossmtest`.`booksystem`(`bookid`, `scribe`, `price`, `number`, `redingwriter`, `dianjinumber`) VALUES (#{bookid}, #{scribe}, #{price}, #{number}, #{redingwriter}, #{dianjinumber})")
    int add(Map map);

    
    @Select("SELECT * FROM booksystem WHERe id=#{id}")
    Map update2(@Param("id") int id);

    @Update("UPDATE `demossmtest`.`booksystem` SET bookid = #{bookid}, scribe =#{scribe},price = #{price}, number =#{number} ,redingwriter = #{redingwriter}, `dianjinumber` = #{dianjinumber} WHERe id = #{id}")
    int update3(Map map);

    //    操作第二张表
    @Select("SELECT * FROM cardidsystem")
    List> getBookList();

    @Delete("delete  from  cardidsystem where id=#{id} ")
    int deleteBook(@Param("id") int id);

    @Insert("INSERT INTO `demossmtest`.`cardidsystem`(name, age, dateime, CarId, address, addressCar) VALUES (#{name}, #{ age},#{dateime},#{CarId},#{ address},#{addressCar})")
    int addCar(Map map);


    @Select("SELECT * FROM cardidsystem WHERe id=#{id}")
    Map selectCar2(@Param("id") int id);


    @Update("UPDATE cardidsystem SET name=#{name}, age=#{age}, dateime=#{dateime}, CarId=#{CarId}, address=#{address}, addressCar=#{addressCar} where id=#{id}")
    int updateCar3(Map map);
}

 

 

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/963755.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号