基于springboot的ssm的增删改查。配置文件不用写,要理解。
说明:这里需要把下面链接中的案例非常熟练,原理都搞明白
(1)application.properties配置文件
application.properties是一个配置文件,用于配置Spring Boot应用程序的各种属性和设置。
文件通常位于项目的src/main/resources目录下。在这个文件中,可以设置数据库连接信息、日志级别、端口号、缓存配置等等。Spring Boot会自动加载这个文件,并根据其中的配置来初始化应用程序的各种组件和功能。通过修改application.properties文件,可以轻松地自定义和调整应用程序的行为。
(2)pom.xml配置文件
pom.xml是Maven项目的配置文件,用于管理项目的依赖项、构建配置和插件等。它位于项目的根目录下(最外面),是Maven项目的核心文件之一。
在pom.xml中,可以定义项目的基本信息,如项目名称、版本号、作者等。同时,还可以指定项目所依赖的外部库和框架,包括它们的版本号和范围。Maven会根据这些依赖信息自动下载所需的库文件,并将它们添加到项目的类路径中。
此外,pom.xml还可以配置项目的构建过程,包括编译选项、打包方式、测试配置等。通过配置插件,还可以在构建过程中执行额外的操作,如运行单元测试、生成文档、打包发布等。
总之,pom.xml是Maven项目的核心配置文件,它定义了项目的依赖关系、构建配置和插件等,使得项目的构建和管理变得简单和可重复。
(3)实体类Category(增加一个包:com.how2java.springboot.pojo,然后创建实体类Category)
(4)CategoryMapper接口
增加一个包:com.how2java.springboot.mapper,然后创建接口CategoryMapper。
使用注解@Mapper 表示这是一个Mybatis Mapper接口。
使用@Select注解表示调用findAll方法会去执行对应的sql语句。
package com.how2java.springboot.mapper;
import java.util.List;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;
import com.how2java.springboot.pojo.Category;
@Mapper
public interface CategoryMapper {
@Select("select * from category_ ")
List<Category> findAll();
}
(5) CategoryController类
增加一个包:com.how2java.springboot.web,然后创建CategoryController 类。
1. 接受listCategory映射
2. 然后获取所有的分类数据
3. 接着放入Model中
4. 跳转到listCategory.jsp中
package com.how2java.springboot.web;
import java.util.List;
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 com.how2java.springboot.mapper.CategoryMapper;
import com.how2java.springboot.pojo.Category;
@Controller
public class CategoryController {
@Autowired CategoryMapper categoryMapper;
@RequestMapping("/listCategory")
public String listCategory(Model m) throws Exception {
List<Category> cs=categoryMapper.findAll();
m.addAttribute("cs", cs);
return "listCategory";
}
}
首先,使用@Controller
注解将该类标记为一个Spring MVC的Controller。然后,使用@Autowired
注解将CategoryMapper
对象自动注入到categoryMapper
属性中。
在CategoryController
类中,定义了一个listCategory
方法,该方法处理请求路径为"/listCategory"的请求。该方法的参数是一个Model
对象,用于封装数据。
在listCategory
方法中,调用categoryMapper
的findAll
方法,获取所有的Category对象,并将其存储在一个名为"cs"的属性中,通过model.addAttribute
方法将"cs"属性添加到Model
对象中。
最后,返回一个字符串"listCategory",表示视图的名称,Spring MVC会根据该名称找到对应的视图进行展示。
(6)listCategory.jsp
用jstl遍历从CategoryController 传递过来的集合:cs
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<table align='center' border='1' cellspacing='0'>
<tr>
<td>id</td>
<td>name</td>
</tr>
<c:forEach items="${cs}" var="c" varStatus="st">
<tr>
<td>${c.id}</td>
<td>${c.name}</td>
</tr>
</c:forEach>
</table>
当使用<c:forEach>
标签进行循环时,它会遍历一个集合或数组,并在每次迭代中将当前元素赋值给一个指定的变量。
在这个例子中,<c:forEach>
标签的items
属性设置为${cs}
,${cs}
是从Controller传递过来的Category对象列表。这意味着<c:forEach>
将会遍历${cs}
中的每一个Category对象。
<c:forEach>
标签中的var
属性设置为c
,这表示在每次迭代中,当前的Category对象将会被赋值给变量c
。
<c:forEach>
标签中的varStatus
属性设置为st
,这表示在每次迭代中,还会额外提供一个LoopTagStatus
对象,用于获取当前迭代的索引、计数等信息。
在循环体中,使用${c.id}
和${c.name}
分别获取当前迭代的Category对象的id和name,并将它们显示在表格的相应单元格中。
(1)基于上一个知识点的扩展
(2)对进行修改pom.xml,增加对PageHelper的支持
(3)PageHelperConfig配置文件
新增加一个包 com.how2java.springboot.config, 然后添加一个类PageHelperConfig ,其中进行PageHelper相关配置。
注解@Configuration 表示PageHelperConfig 这个类是用来做配置的。
注解@Bean 表示启动PageHelper这个拦截器。
offsetAsPageNum:设置为true时,会将RowBounds第一个参数offset当成pageNum页码使用.
p.setProperty("offsetAsPageNum", "true");
rowBoundsWithCount:设置为true时,使用RowBounds分页会进行count查询.
p.setProperty("rowBoundsWithCount", "true");
reasonable:启用合理化时,如果pageNum<1会查询第一页,如果pageNum>pages会查询最后一页。
p.setProperty("reasonable", "true");
代码:
package com.how2java.springboot.config;
import java.util.Properties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import com.github.pagehelper.PageHelper;
@Configuration
public class PageHelperConfig {
@Bean
public PageHelper pageHelper() {
PageHelper pageHelper = new PageHelper();
Properties p = new Properties();
p.setProperty("offsetAsPageNum", "true");
p.setProperty("rowBoundsWithCount", "true");
p.setProperty("reasonable", "true");
pageHelper.setProperties(p);
return pageHelper;
}
}
这段代码是一个配置类,用于配置PageHelper插件的属性。
首先,通过@Configuration注解将该类标记为一个配置类,表示它用于配置Spring应用程序的一些组件。
然后,使用@Bean注解将一个PageHelper对象声明为一个Spring Bean,并将其注册到Spring容器中。
在pageHelper()方法中,创建一个PageHelper对象,并通过Properties对象设置一些属性。这些属性包括:
- "offsetAsPageNum":将offset参数作为PageNum使用,即是否将offset参数当作页码使用。
- "rowBoundsWithCount":是否进行count查询,即是否在执行分页查询时,同时执行一次count查询获取总记录数。
- "reasonable":是否进行合理化查询,即是否对不合理的分页参数进行自动修正,默认为false。
最后,通过调用pageHelper对象的setProperties()方法,将设置好的属性应用到PageHelper对象中。
通过这个配置类,可以在Spring应用程序中使用PageHelper插件,并配置一些属性来定制分页查询的行为。当使用分页查询时,PageHelper插件会根据配置的属性来进行分页处理,并返回分页结果。
(4)修改CategoryMapper接口
增加CRUD方法的支持。 其实就是调用不同的SQL语句。
package com.how2java.springboot.mapper;
import java.util.List;
import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;
import com.how2java.springboot.pojo.Category;
@Mapper
public interface CategoryMapper {
@Select("select * from category_ ")
List<Category> findAll();
@Insert(" insert into category_ ( name ) values (#{name}) ")
public int save(Category category);
@Delete(" delete from category_ where id= #{id} ")
public void delete(int id);
@Select("select * from category_ where id= #{id} ")
public Category get(int id);
@Update("update category_ set name=#{name} where id=#{id} ")
public int update(Category category);
}
(5)修改CategoryController类
其实也就是为CategoryController添加: 增加、删除、获取、修改映射,还有分页查询映射
package com.how2java.springboot.web;
import java.util.List;
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.RequestParam;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.how2java.springboot.mapper.CategoryMapper;
import com.how2java.springboot.pojo.Category;
@Controller
public class CategoryController {
@Autowired CategoryMapper categoryMapper;
@RequestMapping("/addCategory")
public String listCategory(Category c) throws Exception {
categoryMapper.save(c);
return "redirect:listCategory";
}
@RequestMapping("/deleteCategory")
public String deleteCategory(Category c) throws Exception {
categoryMapper.delete(c.getId());
return "redirect:listCategory";
}
@RequestMapping("/updateCategory")
public String updateCategory(Category c) throws Exception {
categoryMapper.update(c);
return "redirect:listCategory";
}
@RequestMapping("/editCategory")
public String listCategory(int id,Model m) throws Exception {
Category c= categoryMapper.get(id);
m.addAttribute("c", c);
return "editCategory";
}
@RequestMapping("/listCategory")
public String listCategory(Model m,@RequestParam(value = "start", defaultValue = "0") int start,@RequestParam(value = "size", defaultValue = "5") int size) throws Exception {
PageHelper.startPage(start,size,"id desc");
List<Category> cs=categoryMapper.findAll();
PageInfo<Category> page = new PageInfo<>(cs);
m.addAttribute("page", page);
return "listCategory";
}
}
(6)修改listCategory.jsp页面
通过page.getList遍历当前页面的Category对象。
在分页的时候通过page.pageNum获取当前页面,page.pages获取总页面数。
注:page.getList会返回一个泛型是Category的集合。
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<div align="center">
</div>
<div style="width:500px;margin:20px auto;text-align: center">
<table align='center' border='1' cellspacing='0'>
<tr>
<td>id</td>
<td>name</td>
<td>编辑</td>
<td>删除</td>
</tr>
<c:forEach items="${page.list}" var="c" varStatus="st">
<tr>
<td>${c.id}</td>
<td>${c.name}</td>
<td><a href="editCategory?id=${c.id}">编辑</a></td>
<td><a href="deleteCategory?id=${c.id}">删除</a></td>
</tr>
</c:forEach>
</table>
<br>
<div>
<a href="?start=1">[首 页]</a>
<a href="?start=${page.pageNum-1}">[上一页]</a>
<a href="?start=${page.pageNum+1}">[下一页]</a>
<a href="?start=${page.pages}">[末 页]</a>
</div>
<br>
<form action="addCategory" method="post">
name: <input name="name"> <br>
<button type="submit">提交</button>
</form>
</div>
(7)editCategory.jsp(修改分类的页面)
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8" isELIgnored="false"%>
<div style="margin:0px auto; width:500px">
<form action="updateCategory" method="post">
name: <input name="name" value="${c.name}"> <br>
<input name="id" type="hidden" value="${c.id}">
<button type="submit">提交</button>
</form>
</div>
(8)重启测试访问
访问地址:http://127.0.0.1:8080/listCategory
简单的Spring Boot + SSM(SpringMVC + Spring + MyBatis)的工作原理:
- 客户端发送请求到前端控制器(DispatcherServlet)。
- 前端控制器接收请求,并将请求转发给Spring MVC的处理器映射器(HandlerMapping)。
- 处理器映射器根据请求的URL映射到相应的处理器(Controller)。
- 处理器执行业务逻辑,并返回一个ModelAndView对象。
- 前端控制器将ModelAndView对象传递给视图解析器(ViewResolver)。
- 视图解析器解析ModelAndView对象中的视图名称,找到对应的视图模板。
- 视图模板生成HTML页面,并返回给前端控制器。
- 前端控制器将HTML页面返回给客户端。
在这个过程中,Spring Boot负责启动和配置Spring MVC、Spring和MyBatis框架,以及提供一些常用的功能和便利的配置。
Spring MVC负责处理HTTP请求和响应,将请求映射到相应的处理器并执行业务逻辑,然后将结果返回给客户端。
Spring负责管理和组织应用程序中的各个组件,包括控制器、服务、数据访问对象等。
MyBatis负责处理数据库操作,包括查询、插入、更新等,通过配置映射文件和SQL语句,将Java对象和数据库表进行映射。