首页 > 其他分享 >mybatis plus的简单使用

mybatis plus的简单使用

时间:2023-04-02 21:33:05浏览次数:43  
标签:mysql spring 简单 plus datasource org mybatis import com

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

相关文章

  • 有关哈希表简单的散列函数实现-Java实现
    其实现不难,所以直接贴代码:1packagedataSrtuct;23importjava.util.ArrayList;4importjava.util.LinkedList;56publicclassHashTab{7publicstaticvoidmain(String[]args){8hashTablehashT=newhashTable(10);9......
  • .net6 制作elementplus离线文档
    1、新建net6项目设置配置信息<ProjectSdk="Microsoft.NET.Sdk.Web"><PropertyGroup><TargetFramework>net6.0</TargetFramework><Nullable>enable</Nullable><ImplicitUsings>enable</ImplicitUsings>......
  • springboot集成mybatis-plus
    springboot项目先导入相关依赖mybatis-plus相关依赖<dependency><groupId>com.baomidou</groupId><artifactId>mybatis-plus-boot-starter</artifactId><version>3.0.5</version></dependency><dependency><gr......
  • mybatis OGNL表达式,xml查询中使用java的变量值
    1、参考struts2ongl访问静态变量与访问枚举Ognl表达式基本原理和使用方法2、java类packageorg.jeecg.mgt.cms.constant;publicclassEAd{publicstaticfinalIntegershow1=1;publicstaticfinalclassAA{publicstaticfinalIntegershow2......
  • Mybatis-动态SQL
    什么是动态SQL?动态SQL是MyBatis的强大特性之一。顾名思义,就是会动的SQL,即是能够灵活的根据某种条件拼接出完整的SQL语句。这种类似于MySQL中的casewhenthenelsethenend....这种语法,能够根据某种条件动态的拼接出需要的SQL。至于Mybatis如何实现动态SQL呢,Mybatis提供了......
  • selenium简单使用
    fromseleniumimportwebdriverfromselenium.webdriver.chrome.serviceimportServicefromselenium.webdriver.common.byimportByfromselenium.webdriver.support.uiimportSelectfromselenium.webdriver.common.action_chainsimportActionChainsimporttime......
  • 用OpenCv-Python自带的LBPH识别器实现简单人脸识别(上)
    用OpenCv-Python自带的LBPH识别器实现简单人脸识别(上)引言:本文开发环境为:Windows10+phchram+Anaconda5.2(Python3.6)+Opencv4.5.5,用opencv-contrib原生的API完成了人脸识别的功能,其可以任意添加人脸ID数据,但其效果比较差(勉强能用),如果日后有时间的话,给大家出一期(挖坑)利......
  • 用OpenCv-Python自带的LBPH识别器实现简单人脸识别(下)
    介绍本文附录了通过LBPH实现简单人脸识别的源代码,分类效果并不是很好,供个人学习使用。人脸录入.pyimportcv2cap=cv2.VideoCapture(0)flag=1num=0while(cap.isOpened()):ret_flag,Vshow=cap.read()cv2.imshow("Capture_Test",Vshow)k=cv2.w......
  • 使用 MybatisPlusCore 自带的雪花算法生成不重复数字
    这里不介绍雪花算法的实现原理,可以自行搜索查阅网上的资料。这里主要介绍雪花算法的使用场景,如何调用第三方类库MybatisPlusCore自带的方法来使用雪花算法。雪花算法的主要使用场景,就是生成不重复的数字,作为数据库表的主键使用。你可能会使用uuid作为主键,但是其占用16个......
  • 校周赛-我左看右看上看下看 原来每个排列都不简单
    题目描述ABCDEFGBABCDEFCBABCDEDCBABCDEDCBABC这是一个5行7列的图形,请找出这个图形的规律,并输出一个n行m列的图形。输入格式输入一行,包含两个整数n和m,分别表示你要输出的图形的行数的列数。输出格式输出n行,每个m个字符,为你的图形。样例......