搭建项目

项目简单效果,前端效果丑的一批,主要是后端功能实现:

springboot thymeleaf 增删改查 案例 springboot增删改查代码_spring

springboot增删改查

Gitee下载地址:

Gitee下载地址

新建文件选依赖

使用spring Initializr进行初始化。唯一的区别就是我们这里整合了mybatis框架。所以在勾选依赖的时候多选了几个。

springboot thymeleaf 增删改查 案例 springboot增删改查代码_spring_02


初始化完成后一定要观察你的pom.xml颜色,如果颜色为橙色,可以进入settings中搜索maven,设置配置文件。上面给出的博客都有解决办法。

springboot thymeleaf 增删改查 案例 springboot增删改查代码_spring boot_03

pom.xml配置项

pom.xml完整配置项,如果你刚刚是按照我的勾选的话,就只需要添加最后一个thymeleaf模板引擎依赖项就好了。

<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
	<modelVersion>4.0.0</modelVersion>
	<parent>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-parent</artifactId>
		<version>2.7.4</version>
		<relativePath/> <!-- lookup parent from repository -->
	</parent>
	<groupId>com.example</groupId>
	<artifactId>csdn_release</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<name>csdn_release</name>
	<description>csdn_release</description>
	<properties>
		<java.version>1.8</java.version>
	</properties>
	<dependencies>
		<!--jdbc依赖项,我们这里用的是mybatis了,这里没用了-->
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-data-jdbc</artifactId>
		</dependency>
		<!--springweb依赖项-->
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-web</artifactId>
		</dependency>
			<!--mybatis依赖项-->
		<dependency>
			<groupId>org.mybatis.spring.boot</groupId>
			<artifactId>mybatis-spring-boot-starter</artifactId>
			<version>2.2.2</version>
		</dependency>
			<!--热更新依赖-->
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-devtools</artifactId>
			<scope>runtime</scope>
			<optional>true</optional>
		</dependency>
		<!--mysql数据库依赖-->
		<dependency>
			<groupId>mysql</groupId>
			<artifactId>mysql-connector-java</artifactId>
			<scope>runtime</scope>
		</dependency>
		<!--一个@data注解的依赖,当然不止于,可以简化类操作-->
		<dependency>
			<groupId>org.projectlombok</groupId>
			<artifactId>lombok</artifactId>
			<optional>true</optional>
		</dependency>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-test</artifactId>
			<scope>test</scope>
		</dependency>
	<!--thymeleaf是适用于Web和独立环境的现代服务器端Java模板引擎,方便html中数据传递-->
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-thymeleaf</artifactId>
		</dependency>
	</dependencies>

	<build>
		<plugins>
			<plugin>
				<groupId>org.springframework.boot</groupId>
				<artifactId>spring-boot-maven-plugin</artifactId>
				<configuration>
					<excludes>
						<exclude>
							<groupId>org.projectlombok</groupId>
							<artifactId>lombok</artifactId>
						</exclude>
					</excludes>
				</configuration>
			</plugin>
		</plugins>
	</build>

</project>

application.properties配置文件

数据库配置文件,在resources下的application.properties中

#数据库配置文件
#修改服务器运行端口
server.port=9090  
#加一个统一前缀,当然可以不加,看需求
server.servlet.context-path=/springboot
#配置静态文件存放地址,可以是本项目中的resources文件夹中的templates,也可以是本机的盘区
spring.web.resources.static-locations=classpath:/templates ,file:E:/????/
#数据库地址,把3306/ 后面的class3改成你的数据库名
spring.datasource.url=jdbc:mysql://localhost:3306/class3?characterEncoding=utf8&serverTimezone=UTC
# 用户密码
spring.datasource.username=root
spring.datasource.password=1234
spring.datasource.name=defaultDataSource

# mybatis 对象包
# 此时可以新建一个包,用来存放实体类
mybatis.type-aliases-package=com.example.csdn_release.pojo
# sql语句映射文件
mybatis.mapper-locations=classpath:mappers/*.xml

# 打开控制台日志
logging.level.com.example.csdn_release=debug

查询所有用户

新建三个包,和pojo同级,叫dao、service、controller,在dao中新建一个PersonMapper接口类,然后继续在resources下新建一个目录叫做mappers(数据库语言映射文件),在这个包中新建一个PersonMapper.xml文件,与dao中 的PersonMapper接口类对应,一般一张表一个xml文件。在pojo新建一个实体类Person。,目录结构如下,我删除不需要的test文件夹了

springboot thymeleaf 增删改查 案例 springboot增删改查代码_spring_04

如果你的包名和我不同,那么得对应的修改包名

Person实体类

package com.example.csdn_release.pojo;

import lombok.Data;
//可以省略getset方法和toString方法
@Data
public class Person {
    private int id;
    private int age;
    private String name;
}

dao层PersonMapper

package com.example.csdn_release.dao;

import com.example.csdn_release.pojo.Person;
import org.apache.ibatis.annotations.Mapper;

import java.util.List;

@Mapper
public interface PersonMapper {
//    查询所有用户,定义一个方法就好了在mappers中的xml文件中实现
public List<Person> findAllPerson();

}

PersonMapper.xml文件

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<!--上面两行直接复制就好了-->
<!--这里的namespace命名空间对应于你的接口类,就可以绑定这个mapper的xml文件-->
<mapper namespace="com.example.csdn_release.dao.PersonMapper">

    <!-- 集合对象可以省略,此处的id对应于接口类中的方法,一定要一致,不然绑定不上
    resultMap是返回到结果类型,这里一定要注意是resultType而不是resultMap,不然控制台报错 Result Maps collection does not contain value for com.example.csdn_release.pojo.Person-->
    <select id="findAllPerson" resultType="com.example.csdn_release.pojo.Person">
        select * from person
    </select>

</mapper>

继续写service层,在service中新建一个PersonService接口类,里面定义一些方法,然后在在新建一个实现类PersonServiceImpl

PersonService接口

package com.example.csdn_release.service;

import com.example.csdn_release.pojo.Person;

import java.util.List;

public interface PersonService {
    //因为是有多个用户数据,所以采用集合的形式来保存
    public List<Person> findAllPerson();
}

PersonServiceImpl接口实现类

package com.example.csdn_release.service;

import com.example.csdn_release.dao.PersonMapper;
import com.example.csdn_release.pojo.Person;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class PersonServiceImpl implements PersonService{
//    定义一个PersonMapper类,用来掉取里面的方法
  @Autowired
    PersonMapper personMapper;
    @Override
    public List<Person> findAllPerson() {
        return personMapper.findAllPerson();
    }
}

继续写html页面

在templates中新建一个personlist.html用来显示所有的用户.前端的内容我就简单过了

<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>用户列表</title>
</head>
<body>
<!--一个基本的 html表格-->
<table border="1px" cellspacing="0px">
  <tr>
    <th th:width="40px">id</th>
    <th th:width="40px">年龄</th>
    <th th:width="150px">名字</th>
    <th >删除</th>
    <th>更新</th>
  </tr>
  <tr th:each="person:${personlist}">
    <td th:text="${person.id}"></td>
    <td th:text="${person.age}"></td>
    <td th:text="${person.name}"></td>
    <!--    通过a标签的href属性值发起deletepersonbyid请求被controller层拦截-->
    <td><a th:href="@{deletepersonbyid(id=${person.id})}">删除用户</a> </td>
    <td><a th:href="@{updatepersonbyid(id=${person.id},name=${person.name},age=${person.age})}">删除用户</a> </td>
  </tr>

</table>
</body>
</html>

继续写controller层

在controller中新建一个PersonController类

package com.example.csdn_release.controller;

import com.example.csdn_release.service.PersonService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
public class PersonContro {
    @Autowired
    PersonService personService;
//    拦截带有/personlist的请求
    @RequestMapping("personlist")
//    model可以为html页面传递参数
    public String personList(Model model){
//        传递参数
        model.addAttribute("personlist",personService.findAllPerson());
//        拦截到请求后将personlist.html的页面映射出来
        return "personlist.html";
    }
}

运行结果:

springboot thymeleaf 增删改查 案例 springboot增删改查代码_mybatis_05

删除用户

dao层

继续在Person中添加一个删除方法

public Integer deletePersonById(Integer id);

springboot thymeleaf 增删改查 案例 springboot增删改查代码_spring boot_06

mappers文件

在PersonMapper.xml中加一个删除语句

<!--    返回值为基本类型可以省略
parameterType;参数类型-->
    <delete id="deletePersonById"  parameterType="Integer">
        delete from person where id=#{id}
    </delete>

springboot thymeleaf 增删改查 案例 springboot增删改查代码_mybatis_07

service层

在PersonService接口类中加一个删除方法

public int deletePersonById(Integer id);

springboot thymeleaf 增删改查 案例 springboot增删改查代码_xml_08


在PersonServiceImp接口实现类中继承方法

@Override
  public int deletePersonById(Integer id) {
    return personMapper.deletePersonById(id);
  }

springboot thymeleaf 增删改查 案例 springboot增删改查代码_xml_09

controller层

新加一个删除方法来带调用service层的方法

@RequestMapping("deletepersonbyid")
    public String deletepersonbyid(int id){
       personService.deletePersonById(id);
        return "redirect:/personList";
    }

springboot thymeleaf 增删改查 案例 springboot增删改查代码_java_10

检查删除功能正常,继续写增加接口

增加用户

还是和上面的套路一样,先dao->service->controller

dao

dao 中的PersonMapper新增一个方法

public int addPerson(Person person);

然后mappers中的PersonMapper.xml写对应的SQL语句

<!--    如果参数是一个对象,不能使用person.name-->
    <insert id="addPerson"  parameterType="com.example.csdn_release.pojo.Person">

        insert into person(name,age) values (#{name},#{age})
    </insert>

service层

先是接口

public int addPerson(Person person);

接口实现类

@Override
    public int addPerson(Person person) {
        return personMapper.addPerson(person);
    }

controller层

PersonController中新增两个方法,一个是单独用来转发的,另一个是逻辑处理

@RequestMapping("addperson")
    public String addperson(){
        return "addperson.html";
    }
    @RequestMapping("addpersoncommit")
    public String addpersoncommit(Person person){
        personService.addPerson(person);
        return "redirect:/personlist";
    }

新建一个addperson.html页面

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
<form th:action="@{/addpersoncommit} " method="post">
  用户名:<input name="name"><br>
  年龄:<input name="age">
  <div><input type="submit" value="提交"></div>
</form>
</body>
</html>

更新用户

dao 中的PersonMapper新增一个方法

public int  updatePerson(Person person);

然后mappers中的PersonMapper.xml写对应的SQL语句

<update id="updatePerson" parameterType="com.example.csdn_release.pojo.Person">
        update person set name=#{name},age=#{age} where id=#{id}
    </update>

先是接口

public int updatePerson(Person person);

接口实现类

@Override
  public int updatePerson(Person person) {
    return personMapper.updatePerson(person);
  }

PersonController中新增两个方法,一个是单独用来转发的,另一个是逻辑处理

@RequestMapping("updatepersonbyid")
    public String updatepersonbyid(Model model,Person person){
        model.addAttribute("id",person.getId());
        model.addAttribute("name",person.getName());
        model.addAttribute("age",person.getAge());
        return "updateperson.html";
    }
    @RequestMapping("updatepersoncommit")
    public String updatepersoncommit(Person person){
        personService.updatePerson(person);
        return "redirect:/personlist";
    }

新建一个updateperson.html页面

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>Title</title>
</head>
<body>
<form th:action="@{/updatepersoncommit} " method="post">
  id:<input name="id" th:value="${id}">
  用户名:<input name="name" th:value="${name}"><br>
  年龄:<input name="age" th:value="${age}">
  <div><input type="submit" value="提交"></div>
</form>
</body>
</html>

总结

springboot整合mybatis实现单表的增删改查已经完成了,之后会继续更新博客实现多表查询、模糊查询、多条数据进行分页展示、springboot+vue实现简单的前后端分离增删改查。