SpringBoot整合其他框架
1、springboot整合Redis
实际上只是在 idea 上建立 springboot 项目时,选中 NoSQL -> spring data redis 模板即可,springboot 会自动帮我们把需要的依赖引入。然后就可以直接在新建的项目里面使用 Redis了。默认配置是连接的本地 127.0.0.1 的 Redis 服务器,当然我们也可以改它的配置。
可参考:https://blog.csdn.net/weixin_56395837/article/details/121484260
2、springboot整合mybatis
新建一个 springboot 项目,比如 order-service,如下:
选择项目所需依赖,如下:
生成的项目的 pom.xml 文件大致如下:
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion>
<parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.6.6</version> <relativePath/> <!-- lookup parent from repository --> </parent>
<groupId>com.example</groupId> <artifactId>order-service</artifactId> <version>0.0.1-SNAPSHOT</version> <name>order-service</name> <description>Demo project for Spring Boot</description>
<properties> <java.version>1.8</java.version> </properties>
<dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-jdbc</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> <version>2.2.2</version> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <scope>runtime</scope> </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> </plugin> </plugins> </build> </project>
给配置文件如 application.yaml 添加以下配置:
server: port: 8081 spring: datasource: username: root password: 123456 url: jdbc:mysql://localhost:3306/cloud_order?useUnicode=true&characterEncoding=utf-8&useSSL=true&serverTimezone=UTC driver-class-name: com.mysql.jdbc.Driver mybatis: mapper-locations: classpath:mapping/*Mapper.xml
创建包controller、entity、mapper、service。在 resources下创建 mapping 文件夹,用于存放 mapper 文件。
项目目录结构如下:
假设表数据如下:
项目入口 OrderServiceApplication.java 如下:
package com.example.orderservice; import org.mybatis.spring.annotation.MapperScan; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; @MapperScan("com.example.orderservice.mapper") @SpringBootApplication public class OrderServiceApplication { public static void main(String[] args) { SpringApplication.run(OrderServiceApplication.class, args); } }
orderMapper.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"> <mapper namespace="com.example.orderservice.mapper.OrderMapper"> <select id="findById" resultType="com.example.orderservice.entity.Order"> select * from tb_order where id = #{id} </select> </mapper>
entity/Order 实体类内容:
package com.example.orderservice.entity; public class Order { private Long id; private Long price; private String name; private Integer num; private Long userId; public Long getId() { return id; } public void setId(Long id) { this.id = id; } public Long getPrice() { return price; } public void setPrice(Long price) { this.price = price; } public String getName() { return name; } public void setName(String name) { this.name = name; } public Integer getNum() { return num; } public void setNum(Integer num) { this.num = num; } public Long getUserId() { return userId; } public void setUserId(Long userId) { this.userId = userId; } @Override public String toString() { return "Order{" + "id=" + id + ", price=" + price + ", name='" + name + '\'' + ", num=" + num + ", userId=" + userId + '}'; } }
OrderController.java 类文件内容:
package com.example.orderservice.controller; import com.example.orderservice.entity.Order; import com.example.orderservice.service.OrderService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; @RestController @RequestMapping("order") public class OrderController { @Autowired private OrderService orderService; @GetMapping("{orderId}") public Order queryOrderByUserId(@PathVariable("orderId") Long orderId) { // 根据id查询订单并返回 return orderService.queryOrderById(orderId); } }
OrderService.java 类文件内容:
package com.example.orderservice.service; import com.example.orderservice.entity.Order; import com.example.orderservice.mapper.OrderMapper; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import org.springframework.web.client.RestTemplate; @Service public class OrderService { @Autowired OrderMapper orderMapper; public Order queryOrderById(Long orderId) { Order order = orderMapper.findById(orderId); return order; } }
OrderMapper.java 类文件内容:
package com.example.orderservice.mapper; import com.example.orderservice.entity.Order; import org.apache.ibatis.annotations.Select; import org.springframework.stereotype.Repository; @Repository public interface OrderMapper { public Order findById(Long id); }
启动 springboot 项目,访问 http://localhost:8081/order/101 即可查询到对应订单信息,大致如下:
实际上整合 mybatis 有时候可能都不需要写 mapper.xml 文件,直接在 mapper.java 类中通过注解写 SQL 也行,比如 @Select("select * from tb_order where id = #{id}"),这样更方便快捷,但是有时候不够灵活,所以还是推荐使用 mapper.xml 文件的方式。
可参考:https://blog.csdn.net/iku5200/article/details/82856621