通过idea实现springboot集成mybatys

news2024/7/6 18:19:33

概述

使用springboot 集成 mybatys后,通过http请求接口,使得通过http请求可以直接直接操作数据库; 完成后端功能框架;前端是准备上小程序,调用https的请求接口用。简单实现后端框架;

详细

springboot 集成 mybatys项目demo

通过idea实现springboot集成mybatys;通过http请求操作mysql数据库

先将science.zip导入idea;

image.png

在mysql中运行test.sql;创建测试用数据库;数据库test用户名密码root/root

image.png

项目中只实现了一个对象User;通过controller接受http请求实现对user的操作;

实现过程:

编辑application.properties

spring.datasource.url = jdbc:mysql://127.0.0.1:3306/test
spring.datasource.username = root
spring.datasource.password = root
spring.datasource.driverClassName = com.mysql.cj.jdbc.Driver
实现springboot启动类
package com.liji.science;
 
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
 
@SpringBootApplication
public class ScienceApplication {
 
    public static void main(String[] args) {
        SpringApplication.run(ScienceApplication.class, args);
    }
 
}

实现controller

package com.liji.science.controller;
 
import com.liji.science.bean.User;
import com.liji.science.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
 
import java.util.List;
 
@RestController
public class UserController {
 
    @Autowired
    private UserService userService;
 
    @RequestMapping("/listUser")
    public  List<User> listUser(Model model) {
        List<User> users = userService.findAll();
        model.addAttribute("users", users);
        return users;
    }
 
    /*@RequestMapping("/toAdd")
    public String toAdd() {
        return "/addUser";
    }
 
    @RequestMapping("/add")
    public String addUser(User user) {
        userService.addUser(user);
        return "redirect:/listUser";
    }
 
    @RequestMapping("/toUpdate")
    public String toUpdate(Model model, int id) {
        User user = userService.findById(id);
        model.addAttribute("user", user);
        return "/updateUser";
    }
 
    @RequestMapping("/update")
    public String updateUser(User user) {
        userService.updateUser(user);
        return "redirect:/listUser";
    }
 
    @RequestMapping("/delete")
    public String deleteUser(int id) {
        userService.deleteUser(id);
        return "redirect:/listUser";
    }*/
 
}

service实现

package com.liji.science.mapper;
 
import com.liji.science.bean.User;
import org.apache.ibatis.annotations.*;
 
import java.util.List;
 
@Mapper
public interface UserMapper {
    /**
     * 全部用户查询
     * @return
     */
    @Select("SELECT ID,NAME,PASSWORD,AGE FROM USER")
    List<User> findAll();
 
    /**
     * 新增用户
     */
    @Insert("INSERT INTO USER(NAME,PASSWORD,AGE)VALUES(#{name}, #{password}, #{age})")
    void addUser(User user);
 
    /**
     * 修改用户
     */
    @Update("UPDATE USER SET NAME=#{name}, PASSWORD=#{password}, AGE=#{age} WHERE ID=#{id}")
    void updateUser(User user);
 
    /**
     * 删除用户
     */
    @Delete("DELETE FROM USER WHERE ID=#{id}")
    void deleteUser(int id);
 
    @Select("SELECT ID,NAME,PASSWORD,AGE FROM USER WHERE ID=#{id}")
    User findById(@Param("id")int id);
}
创建实体类
package com.liji.science.bean;
 
public class User {
    private int id;
    private String name;
    private String password;
    private int age;
 
    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;
    }
 
    public String getPassword() {
        return password;
    }
 
    public void setPassword(String password) {
        this.password = password;
    }
 
    public int getAge() {
        return age;
    }
 
    public void setAge(int age) {
        this.age = age;
    }
}

idea启动成功;

idea启动成功;

image.png

范文用户列表

image.png

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/974692.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

qt中子窗口最小化后再恢复显示窗口区域显示为全白色

问题&#xff1a; qt中子窗口最小化后再恢复显示窗口区域显示为全白色&#xff0c;如下图&#xff1a; 原因&#xff1a; 恢复显示后窗口为及时刷新。 解决办法&#xff1a; 重写showEvent函数&#xff0c;如下&#xff1a; void MyClass::showEvent(QShowEvent *event) {se…

OS | 第5章 插叙:进程API

OS | 第5章 插叙&#xff1a;进程API 文章目录 OS | 第5章 插叙&#xff1a;进程API5.1 fork()系统调用代码过程分析 5.2 wait()系统调用5.3 exec() 系统调用执行过程 为什么这样设计API&#xff1f;shell执行过程shell 重定向pipe()>>>>> 欢迎关注公众号【三戒…

YOLOv5:解读metrics.py

YOLOv5&#xff1a;解读metrics.py 前言前提条件相关介绍metrics.pyfitnesssmoothbox_iouConfusionMatrix ★ ★ \bigstar\bigstar ★★bbox_iou ★ ★ \bigstar\bigstar ★★compute_apap_per_class&#xff08;难度&#xff1a; ⋆ ⋆ ⋆ ⋆ ⋆ \star\star\star\star\star ⋆…

openpnp - 底部相机高级矫正后,底部相机看不清吸嘴的解决方法

文章目录 openpnp - 底部相机高级矫正后,底部相机看不清吸嘴的解决方法概述解决思路备注补充 - 新问题 - N1吸嘴到底部相机十字中心的位置差了很多END openpnp - 底部相机高级矫正后,底部相机看不清吸嘴的解决方法 概述 自从用openpnp后, 无论版本(dev/test), 都发现一个大概…

mac建议装双系统吗,详细分析苹果电脑双系统的利弊

mac建议装双系统吗&#xff0c;Mac电脑上安装双系统有哪些利弊呢&#xff0c;一起来看看吧&#xff01; 苹果Mac电脑安装双系统利&#xff1a; 1、用来办公更加方便&#xff1a;苹果系统功能也是很强大的&#xff0c;但是用来办公非常不方便&#xff0c;是由于一些常用的exe软…

Error: The project seems to require yarn but it‘s not installed.

把之前做过的vue项目拷贝到新电脑上&#xff0c;运行启动命令后发现报了如下错误&#xff1a; 我是这么解决的&#xff1a; 是因为项目中存在yarn.lock 文件&#xff0c;先把这个文件删除掉。 把这个文件删除后&#xff0c;执行如下命令&#xff1a; npm install -g yarn 下…

Docker Desktop 设置镜像环境变量

点击run 展开Optional settings container name &#xff1a;容器名称 Ports&#xff1a;根据你需要的端口进行输入&#xff0c;不输入则默认 后面这个 比如我这个 5432 Volumes&#xff1a;卷&#xff0c;也就是做持久化 需要docker 数据保存的地方 Environment variables…

TCP的滑动窗口协议有什么用?

分析&回答 滑动窗口协议&#xff1a; TCP协议的使用维持发送方/接收方缓冲区 缓冲区是 用来解决网络之间数据不可靠的问题&#xff0c;例如丢包&#xff0c;重复包&#xff0c;出错&#xff0c;乱序 在TCP协议中&#xff0c;发送方和接受方通过各自维护自己的缓冲区。通…

react16之前diff算法的理解和总结

此篇文章所讨论的是 React 16 以前的 Diff 算法。而 React 16 启用了全新的架构 Fiber&#xff0c;相应的 Diff 算法也有所改变&#xff0c;本片不详细讨论Fiber。 fiber架构是为了支持react进行可中断渲染&#xff0c;降低卡顿&#xff0c;提升流畅度。 react16之前的版本&…

什么是standard cell (标准单元) ?

参考文章&#xff1a; 聊一聊芯片后端的标准单元-standard cell - 知乎 (zhihu.com) standard cell中的7T和9T中的"T"指的是什么&#xff1f;或者是什么的缩写&#xff1f; - Layout讨论区 - EETOP 创芯网论坛 (原名&#xff1a;电子顶级开发网) - 数字后端基本概念介…

qemu-system-x86_64 命令创建虚拟机,报gtk initialization failed的

因为是ssh命令行启动&#xff0c;增加--nographic # /opt/debug/bin/qemu-system-aarch64 -machine virt-6.2 -qmp tcp:localhost:1238,server,nowait --nographic configure accelerator virt-6.2 start machine init start cpu init start add rom file: virtio-net-pci…

GIT实战篇,教你如何使用GIT可视化工具

系列文章目录 手把手教你安装Git&#xff0c;萌新迈向专业的必备一步 GIT命令只会抄却不理解&#xff1f;看完原理才能事半功倍&#xff01; 快速上手GIT命令&#xff0c;现学也能登堂入室 GIT实战篇&#xff0c;教你如何使用GIT可视化工具 系列文章目录一、GIT有哪些常用工具…

漆包线工厂云MES解决方案

漆包线行业老板痛点&#xff1a; 1.漆包线比较传统的行业&#xff0c;一般都是靠人工去管理&#xff0c;老板想及时知道工厂的生产&#xff0c;销售、出入库、库存情况&#xff1b; 2.型号多称重打印易错&#xff0c;没有系统前 &#xff1a;称重打印&#xff0c;出入库&#x…

解决github连接不上的问题

改 hosts 我们在浏览器输入 GitHub 的网址时&#xff0c;会向 DNS 服务器发送一个请求&#xff0c;获取到 GitHub 网站所在的服务器 IP 地址&#xff0c;从而进行访问。 就像你是一名快递员&#xff0c;在送快递前要先找中间人询问收件人的地址。而 DNS 就是这个告诉你目标地址…

java开发之个微机器人的二次开发

简要描述&#xff1a; 取消消息接收 请求URL&#xff1a; http://域名地址/cancelHttpCallbackUrl 请求方式&#xff1a; POST 请求头Headers&#xff1a; Content-Type&#xff1a;application/json 参数&#xff1a; 参数名类型说明codestring1000成功&#xff0c;1…

顶尖211“小清华”!强过985,不要错过它!

一、学校及专业介绍 西安电子科技大学&#xff08;Xidian University&#xff09;&#xff0c;简称“西电” &#xff0c;位于陕西省西安市&#xff0c;是中央部属高校&#xff0c;直属于教育部&#xff0c;为全国重点大学&#xff0c;位列国家“双一流”“211工程”&#xff…

宝塔面板开心版出问题升级到正版的解决方案,有效解决TypeError: ‘NoneType‘ object is not subscriptable

服务器之前图开心装了个宝塔面板的开心版&#xff0c;前几天突然出现问题&#xff0c;报错TypeError: ‘NoneType’ object is not subscriptable。没法正常打开软件商店&#xff0c;也没法修复和升级系统&#xff0c;很烦躁。因为里面很多业务还在跑&#xff0c;实在不想重装。…

呜呜呼呼无无话

姓名和手机号脱敏 function nameDesen(value) {if (!value) return return value.substring(0, 1) new Array(value.length).join(*) } const bklnameDesen(宝矿力) console.log(bkl) //宝**function telephoneDesen(value) {if (!value) return value value.toString()ret…

伦敦金投资中挂单的优势

在伦敦金交易中&#xff0c;其实挂单具有无可比拟的优势&#xff1f;首先&#xff0c;如果我们不是全职的投资者&#xff0c;我们在交易时间内可能有其他的工作&#xff0c;那么挂单就可以帮助我们捕捉到市场的机会&#xff0c;即便我们不一定会坐在电脑面前&#xff0c;也可以…

Vue中的指令

指令 指令 (Directives) 是带有 v- 前缀的特殊 attribute。指令 attribute 的值预期是单个 JavaScript 表达式。指令的职责是&#xff0c;当表达式的值改变时&#xff0c;将其产生的连带影响&#xff0c;响应式地作用于 DOM。 常用指令预期简短介绍v-showany显示隐藏元素&…