SpringBoot使用Mybatis 快速入门

本文涉及的产品
云数据库 RDS MySQL,集群版 2核4GB 100GB
推荐场景:
搭建个人博客
RDS MySQL Serverless 基础系列,0.5-2RCU 50GB
RDS MySQL Serverless 高可用系列,价值2615元额度,1个月
简介: SpringBoot使用Mybatis 快速入门

先说一下常用数据库连接库, 对比一下子

  • 原始java访问数据库,开发流程麻烦, 需要如下步骤
1、JDBC 注册驱动/加载驱动
    Class.forName("com.mysql.jdbc.Driver")
2、建立连接
    Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/dbname","root","root");
3、创建Statement
4、执行SQL语句
5、处理结果集
6、关闭连接,释放资源

2、apache dbutils框架

JDBC要简单点, 对JDBC的一个封装。官网:DbUtils – JDBC Utility Component

3、jpa框架

spring-data-jpa 也是做【对象-关系表】之间的映射关系的,并将实体对象持久化到数据库中。

jpa在复杂查询的时候性能不是很好

4、Hiberante   解释:ORM:对象关系映射Object Relational Mapping

企业大都喜欢使用hibernate, 例如做一些内部OA系统, 快捷方便, 不需要特别灵活的业务可以使用

5、Mybatis框架

互联网行业通常使用mybatis,不提供对象和关系模型的直接映射,半ORM, 灵活度很高

接入Mybatis 连接mysql 增加数据获取增加后的数据id

  • 在启动类 xxxApplication.java里面添加扫描注解
@MapperScan("com.example.demo.mapper") // 包名 + dao层目录名称 我这是mapper
  • 增加依赖项 pom.xml
<!--        mybatis-->
<dependency>
    <groupId>org.mybatis.spring.boot</groupId>
    <artifactId>mybatis-spring-boot-starter</artifactId>
    <version>2.1.1</version>
</dependency>
<!--mysql 连接-->
<dependency>
    <groupId>mysql</groupId>
    <artifactId>mysql-connector-java</artifactId>
    <version>8.0.17</version>
</dependency>
<!-- 引入第三方数据源 -->
<dependency>
    <groupId>com.alibaba</groupId>
    <artifactId>druid</artifactId>
    <version>1.1.6</version>
</dependency>
  • 配置数据库连接 application.properties
#mybatis.type-aliases-package=net.xdclass.base_project.domain
#可以自动识别
spring.datasource.driver-class-name =com.mysql.cj.jdbc.Driver
useUnicode=true&characterEncoding=utf-8
spring.datasource.url=jdbc:mysql://localhost:3306/u_test?useUnicode=true&characterEncoding=utf-8
spring.datasource.username =root
spring.datasource.password =123456
#如果不使用默认的数据源 (com.zaxxer.hikari.HikariDataSource)
#spring.datasource.type =com.alibaba.druid.pool.DruidDataSource
spring.datasource.type =com.alibaba.druid.pool.DruidDataSource
# sql输出
mybatis.configuration.log-impl=org.apache.ibatis.logging.stdout.StdOutImpl

加载配置,注入到sqlSessionFactory等都是springBoot帮我们完成

  • 编写mapper 也就是dao  UserMapper.java
package com.example.demo.mapper;
import com.example.demo.bean.UserBean;
import org.apache.ibatis.annotations.*;
import java.util.List;
//@Mapper 因为直接在入口文件做扫描了 所以不需要写
public interface UserMapper {
    @Insert("insert into users(name, bio) values(#{name}, #{bio})")
    @Options(useGeneratedKeys = true, keyProperty = "id", keyColumn = "id") // keyProperty java对象的属性 keyColumn 数据库的键
    int insert(UserBean user);
}
  • 编写逻辑层 Service.
  1. UserService (interface)
package com.example.demo.service;
import com.example.demo.bean.UserBean;
/**
 * 用户业务逻辑
 */
public interface UserService {
    public int add(UserBean user);
}
  1. impl/UserServiceImpl 编写UserService的实现
package com.example.demo.service.impl;
import com.example.demo.bean.UserBean;
import com.example.demo.controller.User;
import com.example.demo.mapper.UserMapper;
import com.example.demo.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
@Service // 标注是Service
public class UserServiceImpl implements UserService {
    @Autowired
    private UserMapper userMapper;
    @Override
    public int add(UserBean user) {
        userMapper.insert(user);
        return user.getId();
    }
}
  • 编写控制器 UserController.java
package com.example.demo.controller;
import com.example.demo.bean.UserBean;
import com.example.demo.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class UserController {
    @Autowired
    private UserService userService;
    @GetMapping("/api3/v1/add")
    public int add()
    {
        UserBean user = new UserBean();
        user.setBio("这是一个新签名");
        user.setName("憧憬");
        return userService.add(user);
//        return 1;
    }
}
  • 效果
相关实践学习
如何在云端创建MySQL数据库
开始实验后,系统会自动创建一台自建MySQL的 源数据库 ECS 实例和一台 目标数据库 RDS。
全面了解阿里云能为你做什么
阿里云在全球各地部署高效节能的绿色数据中心,利用清洁计算为万物互联的新世界提供源源不断的能源动力,目前开服的区域包括中国(华北、华东、华南、香港)、新加坡、美国(美东、美西)、欧洲、中东、澳大利亚、日本。目前阿里云的产品涵盖弹性计算、数据库、存储与CDN、分析与搜索、云通信、网络、管理与监控、应用服务、互联网中间件、移动服务、视频服务等。通过本课程,来了解阿里云能够为你的业务带来哪些帮助 &nbsp; &nbsp; 相关的阿里云产品:云服务器ECS 云服务器 ECS(Elastic Compute Service)是一种弹性可伸缩的计算服务,助您降低 IT 成本,提升运维效率,使您更专注于核心业务创新。产品详情: https://www.aliyun.com/product/ecs
目录
相关文章
|
4天前
|
Java 关系型数据库 MySQL
1、Mybatis-Plus 创建SpringBoot项目
这篇文章是关于如何创建一个SpringBoot项目,包括在`pom.xml`文件中引入依赖、在`application.yml`文件中配置数据库连接,以及加入日志功能的详细步骤和示例代码。
|
6天前
|
前端开发 Java 数据库连接
一天十道Java面试题----第五天(spring的事务传播机制------>mybatis的优缺点)
这篇文章总结了Java面试中的十个问题,包括Spring事务传播机制、Spring事务失效条件、Bean自动装配方式、Spring、Spring MVC和Spring Boot的区别、Spring MVC的工作流程和主要组件、Spring Boot的自动配置原理和Starter概念、嵌入式服务器的使用原因,以及MyBatis的优缺点。
|
5天前
|
数据库
elementUi使用dialog的进行信息的添加、删除表格数据时进行信息提示。删除或者添加成功的信息提示(SpringBoot+Vue+MybatisPlus)
这篇文章介绍了如何在基于SpringBoot+Vue+MybatisPlus的项目中使用elementUI的dialog组件进行用户信息的添加和删除操作,包括弹窗表单的设置、信息提交、数据库操作以及删除前的信息提示和确认。
elementUi使用dialog的进行信息的添加、删除表格数据时进行信息提示。删除或者添加成功的信息提示(SpringBoot+Vue+MybatisPlus)
|
5天前
|
Java 数据库 Spring
MyBatisPlus分页插件在SpringBoot中的使用
这篇文章介绍了如何在Spring Boot项目中配置和使用MyBatis-Plus的分页插件,包括创建配置类以注册分页拦截器,编写测试类来演示如何进行分页查询,并展示了测试结果和数据库表结构。
MyBatisPlus分页插件在SpringBoot中的使用
|
5天前
|
Java 测试技术 数据库
mybatisPlus在Springboot中的使用
这篇文章详细介绍了如何在Spring Boot项目中集成和使用MyBatis-Plus框架,包括依赖配置、数据库设置、项目结构、实体类定义、启动类配置、Mapper接口编写以及通过单元测试进行的增删改查操作示例。
mybatisPlus在Springboot中的使用
|
5天前
|
Java 数据库连接 mybatis
基于SpringBoot+MyBatis的餐饮点餐系统
本文介绍了一个基于SpringBoot和MyBatis开发的餐饮点餐系统,包括系统的主控制器`IndexController`的代码实现,该控制器负责处理首页、点餐、登录、注册、订单管理等功能,适用于毕业设计项目。
13 0
基于SpringBoot+MyBatis的餐饮点餐系统
|
5天前
|
XML SQL JavaScript
在vue页面引入echarts,图表的数据来自数据库 springboot+mybatis+vue+elementui+echarts实现图表的制作
这篇文章介绍了如何在Vue页面中结合SpringBoot、MyBatis、ElementUI和ECharts,实现从数据库获取数据并展示为图表的过程,包括前端和后端的代码实现以及遇到的问题和解决方法。
在vue页面引入echarts,图表的数据来自数据库 springboot+mybatis+vue+elementui+echarts实现图表的制作
|
7天前
|
SQL Java 数据库连接
springboot+mybatis+shiro项目中使用shiro实现登录用户的权限验证。权限表、角色表、用户表。从不同的表中收集用户的权限、
这篇文章介绍了在Spring Boot + MyBatis + Shiro项目中,如何使用Shiro框架实现登录用户的权限验证,包括用户、角色和权限表的设计,以及通过多个表查询来收集和验证用户权限的方法和代码实现。
springboot+mybatis+shiro项目中使用shiro实现登录用户的权限验证。权限表、角色表、用户表。从不同的表中收集用户的权限、
|
20天前
|
XML Java 数据库连接
Spring Boot集成MyBatis
主要系统的讲解了 Spring Boot 集成 MyBatis 的过程,分为基于 xml 形式和基于注解的形式来讲解,通过实际配置手把手讲解了 Spring Boot 中 MyBatis 的使用方式,并针对注解方式,讲解了常见的问题已经解决方式,有很强的实战意义。在实际项目中,建议根据实际情况来确定使用哪种方式,一般 xml 和注解都在用。
|
23天前
|
SQL Java 数据库连接
springboot~mybatis-pagehelper原理与使用
【7月更文挑战第15天】MyBatis-PageHelper是用于MyBatis的分页插件,基于MyBatis的拦截器机制实现。它通过在SQL执行前动态修改SQL语句添加LIMIT子句以支持分页。使用时需在`pom.xml`添加依赖并配置方言等参数。示例代码: PageHelper.startPage(2, 10); List&lt;User&gt; users = userMapper.getAllUsers(); PageInfo&lt;User&gt; pageInfo = new PageInfo&lt;&gt;(users); 这使得分页查询变得简单且能获取总记录数等信息。