首页 > 其他分享 >springboot之MybatisPlus

springboot之MybatisPlus

时间:2024-04-03 10:59:13浏览次数:13  
标签:return springboot userMapper list user MybatisPlus public String

文章目录

一、ORM

简单来说ORM就是一个能够帮我们把java中Bean类映射到数据库中。

在这里插入图片描述

使用mybatis-plus。

  • 配置架包
<!--      MyBatisPlus依赖   -->
        <dependency>
            <groupId>com.baomidou</groupId>
            <artifactId>mybatis-plus-boot-starter</artifactId>
            <version>3.4.2</version>
        </dependency>
<!--       mysql依赖-->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>5.1.47</version>
        </dependency>

<!--       数据连接池增加数据查询效率-->
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>druid-spring-boot-starter</artifactId>
            <version>1.2.20</version>
        </dependency>

下载以后需要配置application.properties

spring.datasource.type=com.alibaba.druid.pool.DruidDataSource
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.url= jdbc:mysql://localhost:3306/"你的数据库名"?useSSL=false&characterEncoding=utf8
spring.datasource.username="你的账号"
spring.datasource.password="你的密码"
mybatis-plus.configuration.log-impl=org.apache.ibatis.logging.stdout.StdOutImpl

在启动函数增加注解@MapperScan("Mapper的路径")
在这里插入图片描述

相关注解
在这里插入图片描述

二、mybatis实际操作

  • 首先创建好对应的数据库
create database hello;

CREATE TABLE User (  
    id INT PRIMARY KEY,  
    username VARCHAR(255) NOT NULL,  
    password VARCHAR(255) NOT NULL,  
    birthday VARCHAR(10)  -- 假设日期格式为 YYYY-MM-DD,长度为10  
);


INSERT INTO User (id, username, password, birthday) VALUES  
(1, 'Alice', 'password123', '1990-01-01'),  
(2, 'Bob', 'pass456word', '1992-05-10');
  • 在mapper下创建一个基于Bean为主的mapper接口!
    UserMapper

  • 在UserMapper里面声明方法。记得加@Mapper

@Mapper
public interface UserMapper {

    //查询所有用户
    @Select("select * from user")
    public List<User> find();
}
  • 在controller写查询接口,其中要使用刚才在UserMapper写好的接口。
	//使用Autowired注解注入接口
	@Autowired
    private UserMapper userMapper;
	//使用与数据库字段对应的Bean接受数据User。
    @GetMapping("/user")
    public List query(){
        List<User> list = userMapper.find();
        System.out.println(list);
        return list;
    }
  • User的代码
	private int id;
    private String username;
    private String password;
    private String birthday;
	
	//get 
	//set
	//使用鼠标右键generate生成

调用我们的写好的query()接口,就能在控制台看到我们的查询结果了。
在这里插入图片描述
在这里插入图片描述
一些方法的参考
在这里插入图片描述
controller文件如下

@RestController
public class UserController {
    @Autowired
    private UserMapper userMapper;

    @GetMapping("/user")
    public List query(){
        List<User> list = userMapper.find();
        System.out.println(list);
        return list;
    }
    @PostMapping("/user/insert")
    public String insert(User user){
        int i = userMapper.insert(user);
        if (i > 0){
            return "successful";
        }
        else{
            return "failed";
        }
    }

    @DeleteMapping("/user/delete/{id}")
    public String delete(@PathVariable int id){
        int j = userMapper.delete(id);
        if (j > 0){
            return "delete successfully";
        }else{
            return "delete fail";
        }
    }
	

    @PutMapping("/user/update")
    public String update(User user){
		//传入进来的user的id要和库里面匹配,不然会报错,毕竟是update
        int k = userMapper.update(user);
        if (k > 0){
            return "update successfully";
        }else{
            return "update fail";
        }

    }
}

三、mybatis-plus

Mapper文件如下

@Mapper
public interface UserMapper2 extends BaseMapper<User> {

}

Controller文件如下

@RestController
public class UserController2 {

    @Autowired
    private UserMapper2 userMapper2;

    @GetMapping("/user2")
    public List query(){
        List<User> list= userMapper2.selectList(null);
        System.out.println(list);
        return list;
    }

    @PostMapping("user2/insert")
    public String insert(User user){
        int i = userMapper2.insert(user);
        if (i > 0){
            return "successful";
        }
        else{
            return "failed";
        }
    }
}
  • 如果Bean名和表明不一致,可以通过增加注解设定对应的表@TableName

在这里插入图片描述

  • 指定列自增

在这里插入图片描述

  • 将Bean类中的属性映射到表列,如果不一致。
    在这里插入图片描述

标签:return,springboot,userMapper,list,user,MybatisPlus,public,String
From: https://blog.csdn.net/xdg15294969271/article/details/137298011

相关文章

  • springboot中的Lombok的使用
    1.Lombok       属于一种对实体类进行简化配置的功能操作,通过@Data实现实体类中get和set方法省略行为。2.测试方式  第一步:创建一个springboot的项目,并同时选择需要的依赖         第二步:创建bean实体类,同时使用@Data注解       第三步:测试,此......
  • springboot的自动装配
    SpringBoot自动装配       Spring框架提供了IOC的功能实现对所有的javabean进行装配,当时使用的xml文件       提供bean配置完成注入行为。       在SpringBoot中可以使用自动装配配置类替换之前xml文件方式。       使用@Configuration修饰一个类......
  • springboot234基于Spring Boot的疗养院管理系统的设计与实现
    具体演示视频链接:https://pan.baidu.com/s/1epOAnmfyRpfuI3eBR13WDA?pwd=8888毕业设计(论文)疗养院管理系统设计与实现摘要传统办法管理信息首先需要花费的时间比较多,其次数据出错率比较高,而且对错误的数据进行更改也比较困难,最后,检索数据费事费力。因此,在计算机上安......
  • springboot235基于SpringBoot的房屋交易平台的设计与实现
    具体演示视频链接:https://pan.baidu.com/s/1epOAnmfyRpfuI3eBR13WDA?pwd=8888本科毕业设计论文题目:房屋交易平台设计与实现系别:XX系(全称)专业:软件工程班级:软件工程15201学生姓名:学生学号:指导教师:导师1导师2摘要信息数据从传统到当代,是一直在变革当中,突......
  • springboot的常用标签
    SpringBoot中常用的注解       -------       类注解:       @RequestMapping  --方法映射       @ResponseBody    --JSON返回修饰       @RestController  --控制器JSON返回控制       ---------------      ......
  • 【附源码】JAVA计算机毕业设计智慧点餐系统(springboot+mysql+开题+论文)
    本系统(程序+源码)带文档lw万字以上 文末可获取一份本项目的java源码和数据库参考。系统程序文件列表开题报告内容研究背景随着信息技术的快速发展和互联网的普及,人们的生活方式发生了深刻的变化。特别是在餐饮行业,传统的点餐方式已经无法满足现代消费者对于便捷性、个性化......
  • 基于SpringBoot+Vue前后端分离的宠物领养管理系统的设计与实现+15000字毕业论文
    介绍用户端:首页:播放宠物视频,展示公告列表,介绍流浪宠物。宠物领养:用户搜索想要领养宠物,申请领养,查看自己领养的宠物。流浪宠物救助:用户能够看到需要救助的流浪宠物,并能够新增新的流浪宠物信息。宠物喂养点:用户能够看到需要喂养的流浪宠物的地点,并展示出地点环境。丢失宠物......
  • springboot校园台球厅人员与设备管理系统
    在Internet高速发展的今天,我们生活的各个领域都涉及到计算机的应用,其中包括校园台球厅人员与设备管理系统的网络应用,在外国管理系统已经是很普遍的方式,不过国内的管理网站可能还处于起步阶段。校园台球厅人员与设备管理系统具有校园台球厅人员与设备信息管理功能的选择。校园台......
  • MybatisPlus存储非List<Long>类型
    错误信息:java.lang.RuntimeException:FailedtodeserializeJSONtoList<Long> 使用mybatisplus的时候,对应数据库的实体类有个字段如下:@TableField(typeHandler=JacksonTypeHandler.class)privateList<String>authImages;需要存储图片列表的地址,["aaa.png","bbb.png&q......
  • 【附源码】计算机毕业设计音乐豆瓣(java+springboot+mysql+mybatis+论文)
    本系统(程序+源码)带文档lw万字以上  文末可领取本课题的JAVA源码参考系统程序文件列表系统的选题背景和意义音乐豆瓣是一个以音乐为主题的社交网站,用户可以在网站上分享自己喜欢的音乐、评论和推荐音乐作品,还可以与其他用户进行交流和互动。音乐豆瓣的目的是为了让更多的......