mybatis plus作用就是可以少些sql,实现对数据一系列操作的功能
首先查询所有数据
根据指定的时间查询
根据时间范围查询,时间倒序
其中QueryWrapper叫做条件构造器
sql表结构,直接放到sql工具执行就好
/* SQLyog Ultimate v10.00 Beta1 MySQL - 5.7.22-log : Database - yourheart_temp ********************************************************************* */ /*!40101 SET NAMES utf8 */; /*!40101 SET SQL_MODE=''*/; /*!40014 SET @OLD_UNIQUE_CHECKS=@@UNIQUE_CHECKS, UNIQUE_CHECKS=0 */; /*!40014 SET @OLD_FOREIGN_KEY_CHECKS=@@FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS=0 */; /*!40101 SET @OLD_SQL_MODE=@@SQL_MODE, SQL_MODE='NO_AUTO_VALUE_ON_ZERO' */; /*!40111 SET @OLD_SQL_NOTES=@@SQL_NOTES, SQL_NOTES=0 */; CREATE DATABASE /*!32312 IF NOT EXISTS*/`yourheart_temp` /*!40100 DEFAULT CHARACTER SET latin1 */; USE `yourheart_temp`; /*Table structure for table `bank_resume` */ DROP TABLE IF EXISTS `bank_resume`; CREATE TABLE `bank_resume` ( `id` int(11) NOT NULL AUTO_INCREMENT COMMENT '主键', `bank_end` varchar(255) DEFAULT NULL COMMENT '银行卡后四位', `bank_name` longtext COMMENT '银行名称', `bank_funds` varchar(255) DEFAULT NULL COMMENT '银行卡余额', `bank_use` longtext COMMENT '该笔金额用途', `update_resume_times` varchar(255) DEFAULT NULL COMMENT '余额变动最新时间', `resume_user` varchar(255) DEFAULT NULL COMMENT '消费人', `bank_resume_status` char(1) DEFAULT NULL, PRIMARY KEY (`id`) ) ENGINE=InnoDB AUTO_INCREMENT=245 DEFAULT CHARSET=utf8; /*!40101 SET SQL_MODE=@OLD_SQL_MODE */; /*!40014 SET FOREIGN_KEY_CHECKS=@OLD_FOREIGN_KEY_CHECKS */; /*!40014 SET UNIQUE_CHECKS=@OLD_UNIQUE_CHECKS */; /*!40111 SET SQL_NOTES=@OLD_SQL_NOTES */;
pom文件
<?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.7.0</version> <relativePath/> <!-- lookup parent from repository --> </parent> <groupId>com</groupId> <artifactId>mysql</artifactId> <version>0.0.1-SNAPSHOT</version> <name>mysql-service</name> <description>Mysql project for Spring Boot</description> <properties> <java.version>1.8</java.version> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-mongodb</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-redis</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-devtools</artifactId> <scope>runtime</scope> <optional>true</optional> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>5.1.6</version> </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> <dependency> <groupId>com.baomidou</groupId> <artifactId>mybatis-plus-boot-starter</artifactId> <version>3.5.1</version> </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> </project>View Code
代码部分
spring.profiles.active=common
logging.level.com.mysql=debug logging.level.web=debug spring.devtools.add-properties=false spring.datasource.driver-class-name=com.mysql.jdbc.Driver spring.datasource.url=jdbc:mysql://127.0.0.1:3306/yourheart_temp?useUnicode=true&characterEncoding=utf-8&useSSL=false spring.datasource.username=root spring.datasource.password=root spring.datasource.hikari.connection-timeout=600000 spring.datasource.hikari.maximum-pool-size=500 spring.datasource.hikari.minimum-idle=20 mybatis.configuration.map-underscore-to-camel-case=true mybatis-plus.configuration.log-impl=org.apache.ibatis.logging.stdout.StdOutImpl
mybatis-plus.configuration.log-impl作用是日志打印
package com.mysql; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.boot.autoconfigure.mongo.MongoAutoConfiguration; @SpringBootApplication(exclude={MongoAutoConfiguration.class}) public class MysqlServiceApplication { public static void main(String[] args) { SpringApplication.run(MysqlServiceApplication.class, args); } }
package com.mysql.bean; import lombok.Data; /** * @Description: * @Author: Yourheart * @Create: 2023/3/28 16:04 */ @Data public class BankResume { private Integer id; /** * 银行卡后四位 */ private String bankEnd; /** * 银行名称 */ private String bankName; /** * 银行卡余额 */ private String bankFunds; /** * 该笔金额用途 */ private String bankUse; /** * 余额变动最新时间 */ private String updateResumeTimes; /** * 消费人 */ private String resumeUser; /** * 消费记录状态 */ private String bankResumeStatus; }
package com.mysql.mapper; import com.baomidou.mybatisplus.core.mapper.BaseMapper; import com.mysql.bean.BankResume; import org.apache.ibatis.annotations.Mapper; import org.apache.ibatis.annotations.Param; import java.util.List; /** * @Description: * @Author: Yourheart * @Create: 2023/3/28 16:05 */ @Mapper public interface BankResumeMapper extends BaseMapper<BankResume> { List<BankResume> selectByTimeRange(@Param("startTime") String startTime, @Param("endTime") String endTime); }
package com.mysql; import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; import com.mysql.bean.BankResume; import com.mysql.mapper.BankResumeMapper; import lombok.extern.slf4j.Slf4j; import org.junit.jupiter.api.Test; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; import java.util.List; @SpringBootTest @Slf4j class MysqlTests { @Autowired private BankResumeMapper bankResumeMapper; @Test void contextLoads() { //查询所有 // List<BankResume> bankResumeList = bankResumeMapper.selectList(null); // bankResumeList.forEach(a->{ // log.info("a:{}",a.getBankName()); // }); //根据指定的时间查询 // QueryWrapper<BankResume> wrapper = new QueryWrapper<>(); // QueryWrapper<BankResume> eq = wrapper.eq("update_resume_times", "2023-03-27 00:05:21"); // BankResume bankResume = bankResumeMapper.selectOne(eq); // log.info("bankResume:{}",bankResume); //根据时间范围查询,时间倒序 String startTime="2023-03-18 20:38:34"; String endTime="2023-03-27 00:05:21"; QueryWrapper<BankResume> queryWrapper = new QueryWrapper<BankResume>() .orderByDesc("update_resume_times"); queryWrapper.between("update_resume_times", startTime, endTime); List<BankResume> bankResumeList = bankResumeMapper.selectList(queryWrapper); bankResumeList.forEach(a->{ log.info("a:{}",a.getUpdateResumeTimes()); }); } }
这里就实现mybatis plus的简单使用,下面实现多数据源的操作部分
先发效果图
以上是从两个不同的数据库中查询的数据
代码部分
<!--mybatis plus提供的多数据源依赖--> <dependency> <groupId>com.baomidou</groupId> <artifactId>dynamic-datasource-spring-boot-starter</artifactId> <version>3.5.1</version> </dependency>
配置文件
spring.datasource.dynamic.primary=master spring.datasource.dynamic.strict=false spring.datasource.dynamic.datasource.master.driver-class-name=com.mysql.jdbc.Driver spring.datasource.dynamic.datasource.master.url=jdbc:mysql://localhost:3306/yourheart_temp spring.datasource.dynamic.datasource.master.username=root spring.datasource.dynamic.datasource.master.password=root spring.datasource.dynamic.datasource.slave.driver-class-name=com.mysql.jdbc.Driver spring.datasource.dynamic.datasource.slave.url=jdbc:mysql://localhost:3306/yourheart spring.datasource.dynamic.datasource.slave.username=root spring.datasource.dynamic.datasource.slave.password=root
package com.mysql.mapper; import com.baomidou.dynamic.datasource.annotation.DS; import com.baomidou.mybatisplus.core.mapper.BaseMapper; import com.mysql.bean.BankResume; import org.apache.ibatis.annotations.Mapper; import org.apache.ibatis.annotations.Param; import java.util.List; /** * @Description: * @Author: Yourheart * @Create: 2023/3/29 18:17 */ @Mapper @DS("slave") //使用另外一个数据源 public interface BankResumeListMapper extends BaseMapper<BankResume> { List<BankResume> selectByTimeRange(@Param("startTime") String startTime, @Param("endTime") String endTime); }
package com.mysql.mapper; import com.baomidou.mybatisplus.core.mapper.BaseMapper; import com.mysql.bean.BankResume; import org.apache.ibatis.annotations.Mapper; import org.apache.ibatis.annotations.Param; import java.util.List; /** * @Description: * @Author: Yourheart * @Create: 2023/3/28 16:05 */ @Mapper public interface BankResumeMapper extends BaseMapper<BankResume> { List<BankResume> selectByTimeRange(@Param("startTime") String startTime, @Param("endTime") String endTime); }
测试方法
package com.mysql; import com.mysql.bean.BankResume; import com.mysql.mapper.BankResumeMapper; import lombok.extern.slf4j.Slf4j; import org.junit.jupiter.api.Test; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; import java.util.List; @SpringBootTest @Slf4j class MysqlTests { @Autowired private BankResumeMapper bankResumeMapper; @Test void contextLoads() { //查询所有 List<BankResume> bankResumeList = bankResumeMapper.selectList(null); bankResumeList.forEach(a->{ log.info("a:{}",a.getBankName()); }); } }
package com.mysql; import com.mysql.bean.BankResume; import com.mysql.mapper.BankResumeListMapper; import lombok.extern.slf4j.Slf4j; import org.junit.jupiter.api.Test; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; import java.util.List; /** * @Description: * @Author: Yourheart * @Create: 2023/3/29 18:18 */ @SpringBootTest @Slf4j public class MysqlOtherTests { @Autowired private BankResumeListMapper bankResumeListMapper; @Test public void test(){ List<BankResume> bankResumeList = bankResumeListMapper.selectList(null); bankResumeList.forEach(a->{ log.info("a:{}",a.getBankName()); }); } }
以上就是使用@DS实现多数据源的方案
标签:mysql,spring,简单,plus,datasource,org,mybatis,import,com From: https://www.cnblogs.com/q202105271618/p/17268549.html