how2j.cn

下载区
文件名 文件大小
thymeleaf.rar 9k
步骤 1 : 基于前面的知识点   
步骤 2 : 先运行,看到效果,再学习   
步骤 3 : 模仿和排错   
步骤 4 : 表结构和数据   
步骤 5 : application.properties   
步骤 6 : pom.xml   
步骤 7 : Category   
步骤 8 : CategoryMapper   
步骤 9 : CategoryController   
步骤 10 : PageHelperConfig   
步骤 11 : listCategory.html   
步骤 12 : editCategory.html   
步骤 13 : 重启测试   

步骤 1 :

基于前面的知识点

edit
本知识点是建立在上一个知识点可运行项目的基础上进行的改进,所以最好把上个知识点理解和消化了.
步骤 2 :

先运行,看到效果,再学习

edit
老规矩,先下载右上角的可运行项目,配置运行起来,确认可用之后,再学习做了哪些步骤以达到这样的效果。
运行 Application.java ,然后访问地址:

http://127.0.0.1:8080/thymeleaf/listCategory
先运行,看到效果,再学习
在确保可运行项目能够正确无误地运行之后,再严格照着教程的步骤,对代码模仿一遍。
模仿过程难免代码有出入,导致无法得到期望的运行结果,此时此刻通过比较正确答案 ( 可运行项目 ) 和自己的代码,来定位问题所在。
采用这种方式,学习有效果,排错有效率,可以较为明显地提升学习速度,跨过学习路上的各个槛。

推荐使用diffmerge软件,进行文件夹比较。把你自己做的项目文件夹,和我的可运行项目文件夹进行比较。
这个软件很牛逼的,可以知道文件夹里哪两个文件不对,并且很明显地标记出来
这里提供了绿色安装和使用教程:diffmerge 下载和使用教程
步骤 4 :

表结构和数据

edit
create database how2java; use how2java; CREATE TABLE category_ ( id int(11) NOT NULL AUTO_INCREMENT, name varchar(30), PRIMARY KEY (id) ) DEFAULT CHARSET=UTF8;
create database how2java;
use how2java;
CREATE TABLE category_ (
  id int(11) NOT NULL AUTO_INCREMENT,
  name varchar(30),
  PRIMARY KEY (id)
) DEFAULT CHARSET=UTF8;
步骤 5 :

application.properties

edit
增加数据库相关配置

#数据库
spring.datasource.url=jdbc:mysql://127.0.0.1:3306/how2java?characterEncoding=UTF-8
spring.datasource.username=root
spring.datasource.password=admin
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
#thymeleaf 配置 spring.thymeleaf.mode=HTML5 spring.thymeleaf.encoding=UTF-8 spring.thymeleaf.content-type=text/html #缓存设置为false, 这样修改之后马上生效,便于调试 spring.thymeleaf.cache=false #上下文 server.context-path=/thymeleaf #数据库 spring.datasource.url=jdbc:mysql://127.0.0.1:3306/how2java?characterEncoding=UTF-8 spring.datasource.username=root spring.datasource.password=admin spring.datasource.driver-class-name=com.mysql.jdbc.Driver
#thymeleaf 配置
spring.thymeleaf.mode=HTML5
spring.thymeleaf.encoding=UTF-8
spring.thymeleaf.content-type=text/html
#缓存设置为false, 这样修改之后马上生效,便于调试
spring.thymeleaf.cache=false
#上下文
server.context-path=/thymeleaf

#数据库
spring.datasource.url=jdbc:mysql://127.0.0.1:3306/how2java?characterEncoding=UTF-8
spring.datasource.username=root
spring.datasource.password=admin
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
修改pom.xml, 增加 jdbc,mybatis, pageHelper 的jar包
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.how2java</groupId> <artifactId>thymeleaf</artifactId> <version>0.0.1-SNAPSHOT</version> <name>thymeleaf</name> <description>thymeleaf</description> <packaging>war</packaging> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>1.5.9.RELEASE</version> </parent> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-thymeleaf</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-tomcat</artifactId> </dependency> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>3.8.1</version> <scope>test</scope> </dependency> <!-- servlet依赖. --> <dependency> <groupId>javax.servlet</groupId> <artifactId>javax.servlet-api</artifactId> </dependency> <dependency> <groupId>javax.servlet</groupId> <artifactId>jstl</artifactId> </dependency> <!-- tomcat的支持.--> <dependency> <groupId>org.apache.tomcat.embed</groupId> <artifactId>tomcat-embed-jasper</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-devtools</artifactId> <optional>true</optional> <!-- 这个需要为 true 热部署才有效 --> </dependency> <!-- mybatis --> <dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> <version>1.1.1</version> </dependency> <!-- mysql --> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>5.1.21</version> </dependency> <!-- pageHelper --> <dependency> <groupId>com.github.pagehelper</groupId> <artifactId>pagehelper</artifactId> <version>4.1.6</version> </dependency> </dependencies> <properties> <java.version>1.8</java.version> </properties> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project>
增加实体类
package com.how2java.springboot.pojo; public class Category { private int id; private String name; public int getId() { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } }
增加Mapper类
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); }
步骤 9 :

CategoryController

edit
增加控制类
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"; } }
步骤 10 :

PageHelperConfig

edit
配置 PageHealper
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; } }
步骤 11 :

listCategory.html

edit
增加和查询的页面 listCategory.html
<!DOCTYPE HTML> <html xmlns:th="http://www.thymeleaf.org"> <head> <title>hello</title> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /> </head> <body> <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> <tr th:each="c:${page.list}"> <td th:text="${c.id}"></td> <td th:text="${c.name}"></td> <td><a th:href="@{/editCategory(id=${c.id})}">编辑</a></td> <td><a th:href="@{/deleteCategory(id=${c.id})}">删除</a></td> </tr> </table> <br/> <div> <a th:href="@{/listCategory(start=0)}">[首 页]</a> <a th:href="@{/listCategory(start=${page.pageNum-1})}">[上一页]</a> <a th:href="@{/listCategory(start=${page.pageNum+1})}">[下一页]</a> <a th:href="@{/listCategory(start=${page.pages})}">[末 页]</a> </div> <br/> <form action="addCategory" method="post"> name: <input name="name"/> <br/> <button type="submit">提交</button> </form> </div> </body> </html>
步骤 12 :

editCategory.html

edit
修改页面
<!DOCTYPE HTML> <html xmlns:th="http://www.thymeleaf.org"> <head> <title>hello</title> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /> </head> <body> <div style="margin:0px auto; width:500px"> <form action="updateCategory" method="post"> name: <input name="name" th:value="${c.name}"/> <br/> <input name="id" type="hidden" th:value="${c.id}"/> <button type="submit">提交</button> </form> </div> </body> </html>
重新运行 Application.java 然后访问地址:

http://127.0.0.1:8080/thymeleaf/listCategory


HOW2J公众号,关注后实时获知最新的教程和优惠活动,谢谢。


问答区域    
2021-03-12 这里没看明白为什么可以分页
凯神




从这里看page 跟PageHelper那里都没有关系,为什么可以实现分页
@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";
    }

							


1 个答案

jh_315322
答案时间:2022-06-18
PageHelper.startPage(start,size,"id desc");这一行代码设置了分页的起始和大小,我觉得应该是spring根据我们的条件自动帮我们在查询数据库的时候把条件添加上去了吧,这个如果学习过ssm框架的分页的话应该会有所了解,那个mybatis操作数据库的部分比较多



回答已经提交成功,正在审核。 请于 我的回答 处查看回答记录,谢谢
答案 或者 代码至少填写一项, 如果是自己有问题,请重新提问,否则站长有可能看不到





2021-01-07 对数据库进行不了增删改查
代码改变世界




运行后可以可以查到数据库中的表中的数据,但是在页面上添加和修改的数据进不到数据库中页面显示是空白的删除的只是页面上的数据
package com.yg.plan.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.yg.plan.pojo.Plan;



@Mapper
public interface PlanMapper {
	@Select("select * from plan_ ")
    List<Plan> findAll();

	   @Insert(" insert into plan_ ( name ) values (#{name}) ")
	    public int save(Plan plan); 
	     
	    @Delete(" delete from plan_ where id= #{id} ")
	    public void delete(int id);
	         
	    @Select("select * from plan_ where id= #{id} ")
	    public Plan get(int id);
	       
	    @Update("update plan_ set text=#{text} where id=#{id} ")
	    public int update(Plan plan); 
}
-----------------------------------------------------------
package com.yg.plan.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.yg.plan.pojo.Plan;



@Mapper
public interface PlanMapper {
	@Select("select * from plan_ ")
    List<Plan> findAll();

	   @Insert(" insert into plan_ ( name ) values (#{name}) ")
	    public int save(Plan plan); 
	     
	    @Delete(" delete from plan_ where id= #{id} ")
	    public void delete(int id);
	         
	    @Select("select * from plan_ where id= #{id} ")
	    public Plan get(int id);
	       
	    @Update("update plan_ set text=#{text} where id=#{id} ")
	    public int update(Plan plan); 
}
--------------------------
package com.yg.plan.controller;

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.yg.plan.mapper.PlanMapper;
import com.yg.plan.pojo.Plan;

@Controller
public class PlanController {
    @Autowired PlanMapper planMapper;
      
    @RequestMapping("/addPlan")
    public String listPlan(Plan p) throws Exception {
    	planMapper.save(p);
        return "redirect:listPlan";
    }
    @RequestMapping("/deletePlan")
    public String deletePlan(Plan p) throws Exception {
        planMapper.delete(p.getId());
        return "redirect:listPlan";
    }
    @RequestMapping("/updatePlan")
    public String updatePlan(Plan p) throws Exception {
       planMapper.update(p);
        return "redirect:listPlan";
    }
    @RequestMapping("/editPlan")
    public String listPlan(int id,Model m) throws Exception {
        Plan p= planMapper.get(id);
        m.addAttribute("p", p);
        return "editPlan";
    }
     
    @RequestMapping("/listPlan")
    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<Plan> cs=planMapper.findAll();
        PageInfo<Plan> page = new PageInfo<>(cs);
        m.addAttribute("page", page);        
        return "listPlan";
    }
    
}
----------------
<!DOCTYPE HTML>
<html xmlns:th="http://www.thymeleaf.org">
<head>
    <title>每日计划</title>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
</head>
<body>

<div style="width:500px;margin:20px auto;text-align: center">
    <table align='center' border='1' cellspacing='0'>
        <tr>
            <td>id</td>
            <td>计划</td>
            <td>编辑</td>
            <td>删除</td>
        </tr>
        <tr th:each="p:${page.list}">
            <td th:text="${p.id}"></td>
            <td th:text="${p.text}"></td>
            <td><a th:href="@{/editPlan(id=${p.id})}">编辑</a></td>
            <td><a th:href="@{/deletePlan(id=${p.id})}">删除</a></td>
        </tr>
    </table>
    <br/>
    <div>
			<a th:href="@{/listPlan(start=0)}">[首  页]</a>
			<a th:href="@{/listPlan(start=${page.pageNum-1})}">[上一页]</a>
			<a th:href="@{/listPlan(start=${page.pageNum+1})}">[下一页]</a>
			<a th:href="@{/listPlan(start=${page.pages})}">[末  页]</a>
    </div>
    <br/>    
    <form action="addPlan" method="post">
      
    name: <input name="name"/> <br/>
    <button type="submit">提交</button>
      
    </form>
</div>

</body>
</html>
-------------------
<!DOCTYPE HTML>
<html xmlns:th="http://www.thymeleaf.org">
<head>
    <title>修改计划</title>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
</head>
<body>
	<div style="margin:0px auto; width:500px">
	  
			<form action="updatePlan" method="post">
			  
			计划: <input name="name" th:value="${p.text}"/> <br/>
			  
			<input name="id" type="hidden" th:value="${p.id}"/>
			<button type="submit">提交</button>
		  
		</form>
	</div>
</body>

</html>  
id	计划	编辑	删除
2		编辑	删除

[首 页] [上一页] [下一页] [末 页]

name: 

提交


1 个答案

代码改变世界
答案时间:2021-01-07
如果想修改表的字段PlanMapper.java中把name替换成新的字段名为什么会出现上面的问题啊



回答已经提交成功,正在审核。 请于 我的回答 处查看回答记录,谢谢
答案 或者 代码至少填写一项, 如果是自己有问题,请重新提问,否则站长有可能看不到





2020-07-09 我感觉页码有点bug
2020-05-24 无法分页
2020-02-06 listCategory.html中的分页跳转问题


提问太多,页面渲染太慢,为了加快渲染速度,本页最多只显示几条提问。还有 11 条以前的提问,请 点击查看

提问之前请登陆
提问已经提交成功,正在审核。 请于 我的提问 处查看提问记录,谢谢
关于 JAVA 框架-SpringBoot-CRUD和分页 的提问

尽量提供截图代码异常信息,有助于分析和解决问题。 也可进本站QQ群交流: 578362961
提问尽量提供完整的代码,环境描述,越是有利于问题的重现,您的问题越能更快得到解答。
对教程中代码有疑问,请提供是哪个步骤,哪一行有疑问,这样便于快速定位问题,提高问题得到解答的速度
在已经存在的几千个提问里,有相当大的比例,是因为使用了和站长不同版本的开发环境导致的,比如 jdk, eclpise, idea, mysql,tomcat 等等软件的版本不一致。
请使用和站长一样的版本,可以节约自己大量的学习时间。 站长把教学中用的软件版本整理了,都统一放在了这里, 方便大家下载: https://how2j.cn/k/helloworld/helloworld-version/1718.html

上传截图