iis服务器助手广告广告
返回顶部
首页 > 资讯 > 精选 >SpringBoot整合MyBatis的示例分析
  • 170
分享到

SpringBoot整合MyBatis的示例分析

2023-06-15 00:06:51 170人浏览 独家记忆
摘要

这篇文章主要介绍了SpringBoot整合mybatis的示例分析,具有一定借鉴价值,感兴趣的朋友可以参考下,希望大家阅读完这篇文章之后大有收获,下面让小编带着大家一起了解一下。1.整合MyBatis操作前面一篇提到了springBoot整

这篇文章主要介绍了SpringBoot整合mybatis的示例分析,具有一定借鉴价值,感兴趣的朋友可以参考下,希望大家阅读完这篇文章之后大有收获,下面让小编带着大家一起了解一下。

1.整合MyBatis操作

前面一篇提到了springBoot整合基础的数据源JDBC、Druid操作,实际项目中更常用的还是MyBatis框架,而SpringBoot整合MyBatis进行CRUD也非常方便。

下面从配置模式、注解模式、混合模式三个方面进行说明MyBatis与SpringBoot的整合。

1.1.配置模式

MyBatis配置模式是指使用mybatis配置文件的方式与SpringBoot进行整合,相对应的就有mybatis-config.xml(用于配置驼峰命名,也可以省略这个文件)、XxxMapper.xml文件。

主要步骤为:

  • 导入mybatis官方starter

  • 编写mapper接口。标准@Mapper注解

  • 编写sql映射文件并绑定mapper接口

在application.yaml中指定Mapper配置文件的位置,以及指定全局配置文件的信息 (建议;配置在mybatis.configuration中,可以省略mybatis-config.xml文件)

下面是具体整合配置步骤:

①引入相关依赖pom.xml配置:

pom.xml

<dependencies>        <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.1.4</version>        </dependency>        <dependency>            <groupId>mysql</groupId>            <artifactId>Mysql-connector-java</artifactId>            <scope>runtime</scope>        </dependency>        <dependency>            <groupId>org.springframework.boot</groupId>            <artifactId>spring-boot-devtools</artifactId>            <scope>runtime</scope>            <optional>true</optional>        </dependency>        <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>    </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>

②编写对应Mapper接口:

@Mapper  //这个注解表示了这个类是一个mybatis的mapper接口类@Repositorypublic interface UserMapper {    //@Select("select * from user")    List<User> findAllUsers();    //@Insert("insert into user(id, username, passWord) values (#{id}, #{username}, #{password})")    void insert(User user);    //@Update("update user set username = #{username}, password = #{password} where id = #{id}")    void update(User user);    //@Delete("delete from user where id = #{id}")    void deleteById(Integer id);}

③在resources下创建对应的mapper文件,对应domain类,数据库表单如下:

User类:

@Datapublic class User {    private Integer id;    private String username;    private String password;}

数据库user表:

SpringBoot整合MyBatis的示例分析

UserMapper.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的唯一标识:一般使用domain的全路径名+Mapper来命名--><mapper namespace="com.fengye.springboot_mybatis.mapper.UserMapper">    <!--id:方法表示,一般配置对应的方法;        resultType:表示该方法有返回,返回需要封装到对应实体的类型-->    <select id="findAllUsers" resultType="com.fengye.springboot_mybatis.entity.User">        select * from user    </select>    <insert id="insert" parameterType="com.fengye.springboot_mybatis.entity.User">        insert into user(id, username, password) values (#{id}, #{username}, #{password})    </insert>    <update id="update" parameterType="com.fengye.springboot_mybatis.entity.User">        update user set username = #{username}, password = #{password} where id = #{id}    </update>    <delete id="deleteById" parameterType="Integer">        delete from user where id = #{id}    </delete></mapper>

④对应配置application.yml文件:

application.yml

server:  port: 8083spring:  datasource:    username: root    password: admin    #假如时区报错,增加时区配置serverTimezone=UTC    url: jdbc:mysql://localhost:3306/mybatis02_0322?serverTimezone=UTC&useUnicode=true&characterEncoding=utf-8    driver-class-name: com.mysql.cj.jdbc.Drivermybatis:  #config-location: classpath:mybatis/mybatis-config.xml  使用了configuration注解则无需再指定mybatis-config.xml文件  mapper-locations: classpath:mybatis/mapper    @Insert("insert into city(name, state, country) values (#{name}, #{state}, #{country})")    @Options(useGeneratedKeys = true, keyProperty = "id")    public void insert(City city);    @Update("update city set name = #{name}, state = #{state}, country = #{country} where id = #{id}")    public void update(City city);    @Delete("delete from city where id = #{id}")    public void deleteById(Long id);}

③编写Service层、Controller层:

Service相关:

public interface CityService {    City findCityById(Long id);    void insert(City city);    void update(City city);    void deleteById(Long id);}@Servicepublic class CityServiceImpl implements CityService {    @Autowired    private CityMapper cityMapper;    @Override    public City findCityById(Long id) {        return cityMapper.getCityById(id);    }    @Override    public void insert(City city) {        cityMapper.insert(city);    }    @Override    public void update(City city) {        cityMapper.update(city);    }    @Override    public void deleteById(Long id) {        cityMapper.deleteById(id);    }}

Controller相关:

@RestController@RequestMapping("/city/api")public class CityController {    @Autowired    private CityService cityService;    @RequestMapping("/findCityById/{id}")    public City findCityById(@PathVariable("id") Long id){        return cityService.findCityById(id);    }    @PostMapping("/insert")    public String insert(City city){        cityService.insert(city);        return "insert ok";    }    @PostMapping("/update")    public String update(City city){        cityService.update(city);        return "update ok";    }    @GetMapping("/delete/{id}")    public String delete(@PathVariable("id") Long id){        cityService.deleteById(id);        return "delete ok";    }}

④对应使用Postman接口进行测试

简单模拟接口POST/GET请求即可:

SpringBoot整合MyBatis的示例分析

1.3.混合模式

在实际项目开发中涉及很多复杂业务及连表查询SQL,可以配合使用注解与配置模式,达到最佳实践的目的。

实际项目操作步骤:

  • 引入mybatis-starter

  • 配置application.yaml中,指定mapper-location位置即可

  • 编写Mapper接口并标注@Mapper注解

  • 简单方法直接注解方式

  • 复杂方法编写mapper.xml进行绑定映射

  • 主启动类上使用@MapperScan("com.fengye.springboot_mybatis.mapper") 简化Mapper接口,包下所有接口就可以不用标注@Mapper注解

具体配置如下:

@SpringBootApplication//主启动类上标注,在XxxMapper中可以省略@Mapper注解@MapperScan("com.fengye.springboot_mybatis.mapper")public class SpringbootMybatisApplication {    public static void main(String[] args) {        SpringApplication.run(SpringbootMybatisApplication.class, args);    }}@Repositorypublic interface CityMapper {    @Select("select * from city where id = #{id}")    public City getCityById(Long id);        @Insert("insert into city(name, state, country) values (#{name}, #{state}, #{country})")    @Options(useGeneratedKeys = true, keyProperty = "id")    public void insert(City city);    @Update("update city set name = #{name}, state = #{state}, country = #{country} where id = #{id}")    public void update(City city);    @Delete("delete from city where id = #{id}")    public void deleteById(Long id);}

springboot是什么

springboot一种全新的编程规范,其设计目的是用来简化新Spring应用的初始搭建以及开发过程,SpringBoot也是一个服务于框架的框架,服务范围是简化配置文件。

感谢你能够认真阅读完这篇文章,希望小编分享的“SpringBoot整合MyBatis的示例分析”这篇文章对大家有帮助,同时也希望大家多多支持编程网,关注编程网精选频道,更多相关知识等着你来学习!

--结束END--

本文标题: SpringBoot整合MyBatis的示例分析

本文链接: https://www.lsjlt.com/news/276454.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

本篇文章演示代码以及资料文档资料下载

下载Word文档到电脑,方便收藏和打印~

下载Word文档
猜你喜欢
  • SpringBoot整合MyBatis的示例分析
    这篇文章主要介绍了SpringBoot整合MyBatis的示例分析,具有一定借鉴价值,感兴趣的朋友可以参考下,希望大家阅读完这篇文章之后大有收获,下面让小编带着大家一起了解一下。1.整合MyBatis操作前面一篇提到了SpringBoot整...
    99+
    2023-06-15
  • springboot与mybatis整合的示例分析
    这篇文章主要介绍了springboot与mybatis整合的示例分析,具有一定借鉴价值,感兴趣的朋友可以参考下,希望大家阅读完这篇文章之后大有收获,下面让小编带着大家一起了解一下。整合MyBatis新建Spring Boot项目,或以Cha...
    99+
    2023-05-30
    springboot mybatis
  • SpringBoot整合MybatisPlus的示例分析
    这篇文章给大家分享的是有关SpringBoot整合MybatisPlus的示例分析的内容。小编觉得挺实用的,因此分享给大家做个参考,一起跟随小编过来看看吧。创建个SpringBoot项目勾选生所需的依赖:我把application的后缀改为...
    99+
    2023-06-20
  • Spring整合Mybatis思路的示例分析
    这篇文章主要介绍Spring整合Mybatis思路的示例分析,文中介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们一定要看完!引入相关依赖SpringMyabtismysqlMybatsi-spring如何整合?Spring:项目管理框...
    99+
    2023-06-29
  • springboot整合swagger问题的示例分析
    小编给大家分享一下springboot整合swagger问题的示例分析,相信大部分人都还不怎么了解,因此分享这篇文章给大家参考一下,希望大家阅读完这篇文章后大有收获,下面让我们一起去了解一下吧!一.前言解决了一个困扰很久的问题。自己搭建的一...
    99+
    2023-06-14
  • Springboot整合knife4j与shiro的示例分析
    小编给大家分享一下Springboot整合knife4j与shiro的示例分析,相信大部分人都还不怎么了解,因此分享这篇文章给大家参考一下,希望大家阅读完这篇文章后大有收获,下面让我们一起去了解一下吧!一、介绍knife4j增强版本的Swa...
    99+
    2023-06-20
  • SpringBoot整合MyBatis-Plus的示例代码
    目录前言源码环境开发工具 SQL脚本 正文单工程POM文件(注意) application.properties(注意)自定义配置(注意)实体类(注意)...
    99+
    2024-04-02
  • Java基础之SpringBoot整合knife4j的示例分析
    这篇文章给大家分享的是有关Java基础之SpringBoot整合knife4j的示例分析的内容。小编觉得挺实用的,因此分享给大家做个参考,一起跟随小编过来看看吧。插件的特点非常简洁清爽的UI设计,接口的快速搜索。支持个性化设置,个性化设置包...
    99+
    2023-06-15
  • SSM框架整合之Spring+SpringMVC+MyBatis的示例分析
    这篇文章主要介绍了SSM框架整合之Spring+SpringMVC+MyBatis的示例分析,具有一定借鉴价值,感兴趣的朋友可以参考下,希望大家阅读完这篇文章之后大有收获,下面让小编带着大家一起了解一下。1、基本概念1.1、SpringSp...
    99+
    2023-06-15
  • SpringBoot整合Redis案例分析
    这篇文章主要介绍了SpringBoot整合Redis案例分析的相关知识,内容详细易懂,操作简单快捷,具有一定借鉴价值,相信大家阅读完这篇SpringBoot整合Redis案例分析文章都会有所收获,下面我们一起来看看吧。Springboot整...
    99+
    2023-06-19
  • springboot整合quartz实例分析
    这篇文章主要讲解了“springboot整合quartz实例分析”,文中的讲解内容简单清晰,易于学习与理解,下面请大家跟着小编的思路慢慢深入,一起来研究和学习“springboot整合quartz实例分析”吧!一、quartz简介Quart...
    99+
    2023-06-29
  • 整合jQueryMobile+AngularJs的示例分析
    整合jQueryMobile+AngularJs的示例分析,很多新手对此不是很清楚,为了帮助大家解决这个难题,下面小编将为大家详细讲解,有这方面需求的人可以来学习下,希望你能有所收获。两者都是不错的JS编程...
    99+
    2024-04-02
  • springboot 整合canal实现示例解析
    目录前言环境准备一、快速搭建canal服务搭建步骤1、服务器使用docker快速安装一个mysql并开启binlog日志2、上传canal安装包并解压3、进入到第二步解压后的文件目录...
    99+
    2024-04-02
  • SpringBoot整合Mybatis Plus多数据源的实现示例
    目录导读添加依赖application.properties 2种方式创建DataSource Master配置,使用druid连接池 Slave配置 启动类演示导读   有一个这样...
    99+
    2024-04-02
  • spring boot整合JMS的示例分析
    这篇文章将为大家详细讲解有关spring boot整合JMS的示例分析,小编觉得挺实用的,因此分享给大家做个参考,希望大家阅读完这篇文章后可以有所收获。一、安装ActiveMQ具体的安装步骤,请参考我的另一篇文章:https://www.j...
    99+
    2023-05-30
    spring boot jms
  • java框架整合的示例分析
    java框架整合的示例分析,相信很多没有经验的人对此束手无策,为此本文总结了问题出现的原因和解决方法,通过这篇文章希望你能解决这个问题。框架整合:Springmvc + Mybatis + Shiro(权限) + REST(服务) + We...
    99+
    2023-06-05
  • springboot整合freemarker代码自动生成器的示例分析
    这篇文章给大家分享的是有关springboot整合freemarker代码自动生成器的示例分析的内容。小编觉得挺实用的,因此分享给大家做个参考,一起跟随小编过来看看吧。技术架构页面是用 Vue ,element-ui开发;网络请求是 Axi...
    99+
    2023-06-15
  • Springboot整合mybatis开启二级缓存的实现示例
    目录前言mybatis 一级缓存和二级缓存的概念pom引入依赖application.properties 文件配置mapper.xml 文件配置cache-ref完整示例代码踩坑参...
    99+
    2024-04-02
  • Springboot整合Dubbo之代码集成和发布的示例分析
    这篇文章主要介绍了Springboot整合Dubbo之代码集成和发布的示例分析,具有一定借鉴价值,感兴趣的朋友可以参考下,希望大家阅读完这篇文章之后大有收获,下面让小编带着大家一起了解一下。具体如下:1. boot-dubbo-api相关打...
    99+
    2023-05-30
    springboot dubbo
  • SpringBoot整合aws的示例代码
    业务需求 将本地的一些文件保存到aws上 引入依赖 创建client 工具类 引入依赖 <dependency> ...
    99+
    2024-04-02
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作