How to query the incoming parameter Map by mybatis-plus, and return the List
This article mainly introduces mybatis-plus how to query the incoming parameter Map, return List mode, the article introduces in great detail, has a certain reference value, interested friends must read!
Mybatis-plus query passes the parameter Map and returns List
Because sometimes the entity class attributes are not enough, and you don't want to write a custom VO, so use map, which is very convenient to return to the foreground.
1 、 mapper.xml
Note that it is resultType, not resultMap, or an error is reported.
SELECT * FROM order and order_id = # {orderId} 2, mapper.javaList getOrder (Map map); 3. Service assembly query condition public List getOrder (String storeId) {Map map=new HashMap (); map.put ("orderId", orderId); return storeApiOrderMapper.getOrder (map);} the basic use of mybatis-plus first we need to create a database table
Used to demonstrate the basic usage of MyBatis-Plus.
CREATE TABLE `user` (`id` varchar (32) NOT NULL, `username` varchar (32) DEFAULT'', `password` varchar (32) DEFAULT'', PRIMARY KEY (`id`)), and then create a Spring Boot project
The pom.xml and configuration are as follows:
4.0.0 org.kaven mybatis-plus 1.0-SNAPSHOT org.springframework.boot spring-boot-starter-parent 2.3.4.RELEASE 1.8 org.springframework.boot spring-boot-starter org.springframework.boot spring-boot-starter- Test org.springframework.boot spring-boot-starter-webflux com.baomidou mybatis-plus-boot-starter 3.4.0 mysql mysql-connector-java 5.1.49 org.projectlombok Lombok org.springframework.boot spring-boot-maven-plugin 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=falseserver: port: 8085 logging: level: root: warn com.kaven.mybatisplus.dao: trace pattern: console:'% p% m% n'
Entity class 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") @ Datapublic class User {@ TableId private String id; @ TableField ("username") private String username; @ TableField ("password") private String password / * * use @ TableField (exist = false), indicating that the field does not exist in the database, so it will not be inserted into the database * attributes decorated with transient and static will not be inserted into the database * / @ TableField (exist = false) private String phone;}
Mapper API UserMapper:
Package com.kaven.mybatisplus.dao;import com.baomidou.mybatisplus.core.mapper.BaseMapper;import com.kaven.mybatisplus.entity.User;import org.springframework.stereotype.Component;@Componentpublic interface UserMapper extends BaseMapper {}
UserMapper needs to inherit the BaseMapper interface of MyBatis-Plus.
The source code of the BaseMapper interface is as follows, which actually defines the CRUD methods of some database tables.
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 extends Mapper {int insert (T entity); int deleteById (Serializable id); int deleteByMap (@ Param ("cm") Map columnMap); int delete (@ Param ("ew") Wrapper wrapper) Int deleteBatchIds (@ Param ("coll") Collection