欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

mybatis-plus?查詢傳入?yún)?shù)Map,返回List<Map>方式

 更新時(shí)間:2021年12月20日 11:17:12   作者:yfx000  
這篇文章主要介紹了mybatis-plus?查詢傳入?yún)?shù)Map,返回List<Map>方式,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教

mybatis-plus 查詢傳入?yún)?shù)Map,返回List<Map>

原因有時(shí)實(shí)體類屬性不夠用,又不想寫自定義VO了,所以用map,這樣直接返回前臺(tái)用起來也很方便

1、mapper.xml

注意是resultType 不是resultMap 否則報(bào)錯(cuò)

<select id="getOrder" parameterType="hashMap" resultType="java.util.Map">
    SELECT * FROM  order 
    <where>  
 <if test="orderId != null  and orderId != ''"> and order_id = #{orderId}</if>          
    </where>
</select>

2、mapper.java

List<Map<String, Object>> getOrder(Map<String,Object> map);

3、service 組裝查詢條件

public List<Map<String, Object>> getOrder(String storeId) {
    Map<String,Object> map=new HashMap<String,Object>();
    map.put("orderId",orderId);
    return storeApiOrderMapper.getOrder(map);
}

mybatis-plus 基本使用

首先我們需要?jiǎng)?chuàng)建一個(gè)數(shù)據(jù)庫表

用于演示MyBatis-Plus的基本用法。

CREATE TABLE `user` (
    `id` varchar(32) NOT NULL,
    `username` varchar(32) DEFAULT '',
    `password` varchar(32) DEFAULT '',
    PRIMARY KEY (`id`)
);

然后創(chuàng)建一個(gè)Spring Boot項(xiàng)目

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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>org.kaven</groupId>
    <artifactId>mybatis-plus</artifactId>
    <version>1.0-SNAPSHOT</version>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.3.4.RELEASE</version>
        <relativePath/>
    </parent>
    <properties>
        <java.version>1.8</java.version>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-webflux</artifactId>
        </dependency>
        <dependency>
            <groupId>com.baomidou</groupId>
            <artifactId>mybatis-plus-boot-starter</artifactId>
            <version>3.4.0</version>
        </dependency>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>5.1.49</version>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
        </dependency>
    </dependencies>
    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>
</project>
spring:
  application:
    name: mybatis-plus
  datasource:
    driver-class-name: com.mysql.jdbc.Driver
    username: root
    password: 123456
    url: jdbc:mysql://127.0.0.1:3306/test?characterEncoding=utf-8&useSSL=false
server:
  port: 8085
  
logging:
  level:
    root: warn
    com.kaven.mybatisplus.dao: trace
  pattern:
    console: '%p%m%n'

實(shí)體類User:

package com.kaven.mybatisplus.entity;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
import lombok.Data;
@TableName("user")
@Data
public class User {
    @TableId
    private String id;
    @TableField("username")
    private String username;
    @TableField("password")
    private String password;
    /**
     * 使用 @TableField(exist = false) ,表示該字段在數(shù)據(jù)庫中不存在 ,所以不會(huì)插入到數(shù)據(jù)庫中
     * 使用 transient 、 static 修飾的屬性也不會(huì)插入數(shù)據(jù)庫中
     */
    @TableField(exist = false)
    private String phone;
}

Mapper接口UserMapper:

package com.kaven.mybatisplus.dao;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.kaven.mybatisplus.entity.User;
import org.springframework.stereotype.Component;
@Component
public interface UserMapper extends BaseMapper<User> {
}

UserMapper需要繼承MyBatis-Plus的BaseMapper接口。

BaseMapper接口源碼如下,其實(shí)就是定義了一些數(shù)據(jù)庫表的CRUD方法。

package com.baomidou.mybatisplus.core.mapper;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import java.io.Serializable;
import java.util.Collection;
import java.util.List;
import java.util.Map;
import org.apache.ibatis.annotations.Param;
public interface BaseMapper<T> extends Mapper<T> {
    int insert(T entity);
    int deleteById(Serializable id);
    int deleteByMap(@Param("cm") Map<String, Object> columnMap);
    int delete(@Param("ew") Wrapper<T> wrapper);
    int deleteBatchIds(@Param("coll") Collection<? extends Serializable> idList);
    int updateById(@Param("et") T entity);
    int update(@Param("et") T entity, @Param("ew") Wrapper<T> updateWrapper);
    T selectById(Serializable id);
    List<T> selectBatchIds(@Param("coll") Collection<? extends Serializable> idList);
    List<T> selectByMap(@Param("cm") Map<String, Object> columnMap);
    T selectOne(@Param("ew") Wrapper<T> queryWrapper);
    Integer selectCount(@Param("ew") Wrapper<T> queryWrapper);
    List<T> selectList(@Param("ew") Wrapper<T> queryWrapper);
    List<Map<String, Object>> selectMaps(@Param("ew") Wrapper<T> queryWrapper);
    List<Object> selectObjs(@Param("ew") Wrapper<T> queryWrapper);
    <E extends IPage<T>> E selectPage(E page, @Param("ew") Wrapper<T> queryWrapper);
    <E extends IPage<Map<String, Object>>> E selectMapsPage(E page, @Param("ew") Wrapper<T> queryWrapper);
}

啟動(dòng)類:

package com.kaven.mybatisplus;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
@MapperScan(basePackages = "com.kaven.mybatisplus.dao")
public class AppRun {
    public static void main(String[] args) {
        SpringApplication.run(AppRun.class , args);
    }
}

@MapperScan(basePackages = "com.kaven.mybatisplus.dao")這個(gè)一定要加上。

這樣就構(gòu)建好了項(xiàng)目,使用MyBatis-Plus可以不用寫Mapper.xml配置文件,是不是賊方便。

我們先在數(shù)據(jù)庫中添加幾行數(shù)據(jù),方便演示。

在這里插入圖片描述

我們來演示幾個(gè)基本的查詢方法

package com.kaven.mybatisplus.dao;
import com.kaven.mybatisplus.entity.User;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@RunWith(SpringRunner.class)
@SpringBootTest
public class UserMapperTest {
    @Autowired
    private UserMapper userMapper;
    @Test
    public void selectList(){
        // 條件設(shè)置為null , 就是沒有條件,即查詢所有數(shù)據(jù)
        List<User> userList = userMapper.selectList(null);
        userList.forEach(System.out::println);
    }
    @Test
    public void selectById(){
        // 根據(jù)Id查詢
        User user = userMapper.selectById("1");
        System.out.println(user);
    }
    @Test
    public void selectBatchIds(){
        // 根據(jù)Id列表進(jìn)行批查詢
        List<String> idList = Arrays.asList("1" , "2" , "3");
        List<User> userList = userMapper.selectBatchIds(idList);
        userList.forEach(System.out::println);
    }
    @Test
    public void selectByMap(){
        // 根據(jù)<屬性 , 值>來進(jìn)行匹配查詢 , 多個(gè)<屬性 , 值>會(huì)通過and方式來查詢
        Map<String , Object> map = new HashMap<>();
        // 這里是數(shù)據(jù)庫的列名 , 而不是實(shí)體類的屬性名
        map.put("username" , "kaven");
        map.put("password" , "kaven");
        List<User> userList = userMapper.selectByMap(map);
        userList.forEach(System.out::println);
    }
}

運(yùn)行結(jié)果:

在這里插入圖片描述

再演示更新方法。

package com.kaven.mybatisplus.dao;
import com.kaven.mybatisplus.entity.User;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
@RunWith(SpringRunner.class)
@SpringBootTest
public class UserMapperUpdateTest {
    @Autowired
    private UserMapper userMapper;
    @Test
    public void updateById(){
        // 根據(jù)Id進(jìn)行更新
        User user = userMapper.selectById("1");
        user.setPassword("itkaven");
        int rows = userMapper.updateById(user);
        System.out.println(userMapper.selectById(user.getId()));
    }
}

在這里插入圖片描述

其實(shí)還有一個(gè)update方法,但它需要一個(gè)條件,條件也可以設(shè)置為null,但這樣會(huì)更新所有的數(shù)據(jù),這里先不演示,之后的博客再進(jìn)行演示說明,兩個(gè)更新方法的定義如下。

    /**
     * 根據(jù) ID 修改
     *
     * @param entity 實(shí)體對象
     */
    int updateById(@Param(Constants.ENTITY) T entity);
    /**
     * 根據(jù) whereEntity 條件,更新記錄
     *
     * @param entity        實(shí)體對象 (set 條件值,可以為 null)
     * @param updateWrapper 實(shí)體對象封裝操作類(可以為 null,里面的 entity 用于生成 where 語句)
     */
    int update(@Param(Constants.ENTITY) T entity, @Param(Constants.WRAPPER) Wrapper<T> updateWrapper);

再演示幾個(gè)刪除方法

package com.kaven.mybatisplus.dao;
import org.junit.Assert;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.transaction.annotation.Transactional;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@RunWith(SpringRunner.class)
@SpringBootTest
public class UserMapperDeleteTest {
    @Autowired
    private UserMapper userMapper;
    @Test
    @Transactional
    public void deleteById(){
        // 根據(jù)Id進(jìn)行刪除
        int rows = userMapper.deleteById("1");
        Assert.assertEquals(rows , 1);
    }
    @Test
    @Transactional
    public void deleteByMap(){
        // 根據(jù)<屬性 , 值>進(jìn)行匹配刪除
        Map<String , Object> map = new HashMap<>();
        map.put("username" , "607");
        map.put("password" , "607");
        int rows = userMapper.deleteByMap(map);
        Assert.assertEquals(rows , 1);
    }
    @Test
    @Transactional
    public void deleteBatchIds(){
        // 根據(jù)Id列表進(jìn)行批刪除
        List<String> idList = Arrays.asList("1" , "2" , "3");
        int rows = userMapper.deleteBatchIds(idList);
        Assert.assertEquals(rows , 3);
    }
}

結(jié)果如下:

在這里插入圖片描述

這里也還有一個(gè)delete方法,也需要一個(gè)條件,所以也不進(jìn)行演示了。

再演示插入方法

package com.kaven.mybatisplus.dao;
import com.kaven.mybatisplus.entity.User;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
@RunWith(SpringRunner.class)
@SpringBootTest
public class UserMapperInsertTest {
    @Autowired
    private UserMapper userMapper;
    @Test
    public void insert(){
        // 直接實(shí)體插入
        User user = new User();
        user.setId("4");
        user.setUsername("hn");
        user.setPassword("hn");
        userMapper.insert(user);
    }
}

結(jié)果如下:

在這里插入圖片描述

這就是MyBatis-Plus的基本使用了。

以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。

相關(guān)文章

最新評論