0

0

SpringBoot怎么整合Mybatis与thymleft实现增删改查功能

王林

王林

发布时间:2023-05-11 16:19:06

|

1741人浏览过

|

来源于亿速云

转载

首先我们先创建项目 注意:创建springboot项目时一定要联网不然会报错

SpringBoot怎么整合Mybatis与thymleft实现增删改查功能

SpringBoot怎么整合Mybatis与thymleft实现增删改查功能

项目创建好后我们首先对 application.yml 进行编译

SpringBoot怎么整合Mybatis与thymleft实现增删改查功能

#指定端口号server: port: 8888#配置mysql数据源spring:  datasource:    driver-class-name: com.mysql.cj.jdbc.Driver    url: jdbc:mysql://localhost:3306/nba?serverTimezone=Asia/Shanghai    username: root    password: root#配置模板引擎 thymeleaf  thymeleaf:    mode: HTML5    cache: false    suffix: .html    prefix: classpath:/templates/mybatis:  mapper-locations: classpath:/mapper/*.xml  type-aliases-package: com.bdqn.springboot  #放包名

注意:在 :后一定要空格,这是他的语法,不空格就会运行报错

接下来我们进行对项目的构建 创建好如下几个包 可根据自己实际需要创建其他的工具包之类的

SpringBoot怎么整合Mybatis与thymleft实现增删改查功能

mapper:用于存放dao层接口

pojo:用于存放实体类

service:用于存放service层接口,以及service层实现类

web:用于存放controller控制层

接下来我们开始编写代码

首先是实体类,今天做的是一个两表的简单增删改查

package com.baqn.springboot.pojo;
import lombok.Data;
@Data
public class Clubs {
    private int cid;
    private String cname;
    private String city;
}
package com.baqn.springboot.pojo;
import lombok.Data;
@Data
public class Players {
    private int pid;
    private String pname;
    private String birthday;
    private int height;
    private int weight;
    private String position;
    private int cid;
    private String cname;
    private String city;
}

使用@Data注解可以有效减少实体类中的代码数量,缩减了对于get/set和toString的编写

然后是mapper层

package com.baqn.springboot.mapper;
import com.baqn.springboot.pojo.Players;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.springframework.stereotype.Repository;
import java.util.List;
@Mapper
@Repository
public interface PlayersMapper {
    /**
     * 查询所有
     * @return
     */
    List findAll();
    /**
     * 根据ID查询
     * @return
     */
    Players findById(Integer id);
    /**
     * 新增
     * @param players
     * @return
     */
    Integer add(Players players);
    /**
     * 删除
     * @param pid
     * @return
     */
    Integer delete(Integer pid);
    /**
     * 修改
     * @param players
     * @return
     */
    Integer update(Players players);
}

使用@mapper后,不需要在spring配置中设置扫描地址,通过mapper.xml里面的namespace属性对应相关的mapper类,spring将动态的生成Bean后注入到Servicelmpl中。

然后是service层

package com.baqn.springboot.service;
import com.baqn.springboot.pojo.Players;
import org.apache.ibatis.annotations.Param;
import java.util.List;
public interface PlayersService {
    List findAll();
    Players findById(Integer pid);
    Integer add(Players players);
    Integer delete(Integer pid);
    Integer update(Players players);
}
package com.baqn.springboot.service;
import com.baqn.springboot.mapper.PlayersMapper;
import com.baqn.springboot.pojo.Players;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
@Service
public class PlayersServiceImpl implements  PlayersService{
    @Autowired
    private PlayersMapper mapper;
    @Override
    public List findAll() {
        return mapper.findAll();
    }
    @Override
    public Players findById(Integer pid) {
        return mapper.findById(pid);
    }
    @Override
    public Integer add(Players players) {
        return mapper.add(players);
    }
    @Override
    public Integer delete(Integer pid) {
        return mapper.delete(pid);
    }
    @Override
    public Integer update(Players players) {
        return mapper.update(players);
    }
}

最后是web层的controller控制类

package com.baqn.springboot.web;
import com.baqn.springboot.pojo.Players;
import com.baqn.springboot.service.PlayersServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import java.util.List;
@Controller
public class PlayersController {
    @Autowired
    private PlayersServiceImpl service;
    @RequestMapping("/findAll")
    public String findAll(Model model) {
        List allList = service.findAll();
        model.addAttribute("allList",allList);
        return "index";
    }
    @RequestMapping("/findById/{pid}")
    public String findById(Model model,@PathVariable("pid") Integer pid) {
        Players list = service.findById(pid);
        //System.out.println("---------------"+list.toString());
        model.addAttribute("list",list);
        return "update.html";
    }
    @RequestMapping("/add")
    public String add(Model model, Players players){
        Integer count = service.add(players);
        if (count>0){
            return "redirect:/findAll";
        }
        return "add";
    }
    @RequestMapping("/delete/{pid}")
    public String delete(Model model,@PathVariable("pid") Integer pid){
        Integer count = service.delete(pid);
        if (count>0){
            return "redirect:/findAll";
        }
        return null;
    }
    @RequestMapping("/a1")
    public String a1(Model model, Players players){
        return "add.html";
    }
    @RequestMapping("/update")
    public String update(Model model,Players plays){
        Integer count = service.update(plays);
        if (count>0){
            return "redirect:/findAll";
        }
        return null;
    }
}

注意:a1方法仅仅是用于跳转页面,并没有其他作用,如果有更好的跳转方法可以给我留言哦

现在准备工作都做完了,可以开始编写SQL语句了

mapper.xml可以写在下面resources里面也可以写在上面的mapper层里

如果写在上面的话需要在pom里面写一个资源过滤器,有兴趣的话可以去百度

问小白
问小白

免费使用DeepSeek满血版

下载




    
    
    
        INSERT INTO `nba`.`players`(pname, birthday, height, weight, position, cid)
        VALUES (#{pname}, #{birthday}, #{height}, #{weight}, #{position}, #{cid});
    
    
        delete from players where pid = #{pid}
    
    
        UPDATE `nba`.`players`
        
            pname=#{pname},
            birthday=#{birthday},
            height=#{height},
            weight=#{weight},
            position=#{position},
            cid=#{cid}
        
        WHERE `pid` = #{pid};
    

注意:mapper.xml中的id里对应的是mapper层接口的方法,一定不能写错

到现在为止我们的后端代码就已经完全搞定了,前端页面如下

主页 index.html



    Title


美国职业篮球联盟(NBA)球员信息

新增
球员编号 球员名称 出生时间(yyyy-MM-dd) 球员身高(cm) 球员体重(kg) 球员位置 所属球队 相关操作
${list.height} 修改 删除

新增页 add.html



      
      Title


新增球员

球员名称:

出生日期:

球员升高:

球员体重:

球员位置: 控球后卫 得分后卫 小前锋 大前锋 中锋

所属球队:

修改页 update.html




  
  Title


    

修改球员信息


球员编号:
球员姓名:
出身日期:
球员身高:
球员体重:
球员位置:
所属球队:

数据库创建源码 -- 注意:我用的是MySQL数据库

create table clubs(
		cid int primary key auto_increment,
		cname varchar(50) not null,
		city varchar(50) not null
)
create table players(
		pid int primary key auto_increment,
		pname varchar(50) not null,
		birthday datetime not null,
		height int not null,
		weight int not null,
		position varchar(50) not null,
		cid int not null
)
alter  table  players  add  constraint  players_cid
foreign key(cid)  references  clubs(cid);
insert into clubs values
(1,'热火队','迈阿密'),
(2,'奇才队','华盛顿'),
(3,'魔术队','奥兰多'),
(4,'山猫队','夏洛特'),
(5,'老鹰队','亚特兰大')
insert into players values
(4,'多多','1989-08-08',213,186,'前锋',1),
(5,'西西','1987-10-16',199,162,'中锋',1),
(6,'南南','1990-01-23',221,184,'后锋',1)

最后给大家看一下页面展示

在地址栏输入:http://localhost:8888/findAll 进入到查询所有方法再跳转到idnex.html进行显示

SpringBoot怎么整合Mybatis与thymleft实现增删改查功能

点击新增跳转到新增页面

输入参数

SpringBoot怎么整合Mybatis与thymleft实现增删改查功能

然后点击保存 添加成功后跳转到idnex.html并显示数据

SpringBoot怎么整合Mybatis与thymleft实现增删改查功能

前端数据显示表面成功添加

点击修改 根据findById方法找到数据,并跳转到update.htnl页面进行显示

SpringBoot怎么整合Mybatis与thymleft实现增删改查功能

我们修改所属球队为 奇才队 点击保存

SpringBoot怎么整合Mybatis与thymleft实现增删改查功能

跳转到index.html页面并且数据成功修改

相关专题

更多
Python多线程合集
Python多线程合集

本专题整合了Python多线程相关教程,阅读专题下面的文章了解更多详细内容。

0

2026.01.21

java多线程相关教程合集
java多线程相关教程合集

本专题整合了java多线程相关教程,阅读专题下面的文章了解更多详细内容。

1

2026.01.21

windows激活码分享 windows一键激活教程指南
windows激活码分享 windows一键激活教程指南

Windows 10/11一键激活可以通过PowerShell脚本或KMS工具实现永久或长期激活。最推荐的简便方法是打开PowerShell(管理员),运行 irm https://get.activated.win | iex 脚本,按提示选择数字激活(选项1)。其他方法包括使用HEU KMS Activator工具进行智能激活。

1

2026.01.21

excel表格操作技巧大全 表格制作excel教程
excel表格操作技巧大全 表格制作excel教程

Excel表格操作的核心技巧在于 熟练使用快捷键、数据处理函数及视图工具,如Ctrl+C/V(复制粘贴)、Alt+=(自动求和)、条件格式、数据验证及数据透视表。掌握这些可大幅提升数据分析与办公效率,实现快速录入、查找、筛选和汇总。

2

2026.01.21

毒蘑菇显卡测试网站入口 毒蘑菇测试官网volumeshader_bm
毒蘑菇显卡测试网站入口 毒蘑菇测试官网volumeshader_bm

毒蘑菇VOLUMESHADER_BM测试网站网址为https://toolwa.com/vsbm/,该平台基于WebGL技术通过渲染高复杂度三维分形图形评估设备图形处理能力,用户可通过拖动彩色物体观察画面流畅度判断GPU与CPU协同性能;测试兼容多种设备,但中低端手机易卡顿或崩溃,高端机型可能因发热降频影响表现,桌面端需启用独立显卡并使用支持WebGL的主流浏览器以确保准确结果

4

2026.01.21

github中文官网入口 github中文版官网网页进入
github中文官网入口 github中文版官网网页进入

github中文官网入口https://docs.github.com/zh/get-started,GitHub 是一种基于云的平台,可在其中存储、共享并与他人一起编写代码。 通过将代码存储在GitHub 上的“存储库”中,你可以: “展示或共享”你的工作。 持续“跟踪和管理”对代码的更改。

3

2026.01.21

windows安全中心怎么关闭打开_windows安全中心操作指南
windows安全中心怎么关闭打开_windows安全中心操作指南

Windows安全中心可以通过系统设置轻松开关。 暂时关闭:打开“设置” -> “隐私和安全性” -> “Windows安全中心” -> “病毒和威胁防护” -> “管理设置”,将“实时保护”关闭。打开:同样路径将开关开启即可。如需彻底关闭,需在组策略(gpedit.msc)或注册表中禁用Windows Defender。

2

2026.01.21

C++游戏开发Unreal Engine_C++怎么用Unreal Engine开发游戏
C++游戏开发Unreal Engine_C++怎么用Unreal Engine开发游戏

虚幻引擎(Unreal Engine, 简称UE)是由Epic Games开发的一款功能强大的工业级3D游戏引擎,以高品质实时渲染(如Nanite和Lumen)闻名 。它基于C++语言,为开发者提供高效率的框架、强大的可视化脚本系统(蓝图)、以及针对PC、主机和移动端的完整开发工具,广泛用于游戏、电影制片等领域。

0

2026.01.21

Python GraphQL API 开发实战
Python GraphQL API 开发实战

本专题系统讲解 Python 在 GraphQL API 开发中的实际应用,涵盖 GraphQL 基础概念、Schema 设计、Query 与 Mutation 实现、权限控制、分页与性能优化,以及与现有 REST 服务和数据库的整合方式。通过完整示例,帮助学习者掌握 使用 Python 构建高扩展性、前后端协作友好的 GraphQL 接口服务,适用于中大型应用与复杂数据查询场景。

1

2026.01.21

热门下载

更多
网站特效
/
网站源码
/
网站素材
/
前端模板

精品课程

更多
相关推荐
/
热门推荐
/
最新课程
Redis6入门到精通超详细教程
Redis6入门到精通超详细教程

共47课时 | 5.3万人学习

关于我们 免责申明 举报中心 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送

Copyright 2014-2026 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号