最新下载
热门教程
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
MyBatis-Plus查询指定字段代码实现方法
时间:2020-12-23 编辑:袖梨 来源:一聚教程网
本篇文章小编给大家分享一下MyBatis-Plus查询指定字段代码实现方法,文章代码介绍的很详细,小编觉得挺不错的,现在分享给大家供大家参考,有需要的小伙伴们可以来看看。
首先创建一个数据库表,如下图所示:
然后创建一个Spring Boot项目,pom.xml和配置如下:
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: ITkaven@123 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' mybatis-plus: mapper-locations: classpath:mappers/*.xml
实体类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("age") private Integer age; /** * 使用 @TableField(exist = false) ,表示该字段在数据库中不存在 ,所以不会插入数据库中 * 使用 transient 、 static 修饰属性也不会插入数据库中 */ @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{}
启动类:
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")这个一定要加上。
我们先在数据库中添加几行数据,方便演示。
使用MyBatis-Plus 查询时指定字段有两种方法。
一:查询username包含字符k,并且age要小于35,只需要输出username、age即可。
package com.kaven.mybatisplus.dao; import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; 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.List; @RunWith(SpringRunner.class) @SpringBootTest public class UserMapperTest { @Autowired private UserMapper userMapper; @Test public void selectList(){ QueryWrapperuserQueryWrapper = new QueryWrapper<>(); // QueryWrapper userQueryWrapper = Wrappers.query(); 和上面一样的效果 userQueryWrapper.select("username", "age").like("username" , "k").lt("age" , 35); // userQueryWrapper.like("username" , "k").lt("age" , 35).select("username", "age"); // 把select()放在最后面也可以,但我一般喜欢放在最前面,和sql语法保持一致 List userList = userMapper.selectList(userQueryWrapper); userList.forEach(System.out::println); } }
结果如下:
很显然,结果是正确的。
当数据库表有很多列,并且需要输出大部分列时,用这种方法来指定字段查询就很繁琐,这也是不可避免的。
二:查询username包含字符k,并且age要大于等于25并且小于等于35、password不能为null,不需要输出password。
package com.kaven.mybatisplus.dao; import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; 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.List; @RunWith(SpringRunner.class) @SpringBootTest public class UserMapperTest { @Autowired private UserMapper userMapper; @Test public void selectList2(){ QueryWrapperuserQueryWrapper = new QueryWrapper<>(); // QueryWrapper userQueryWrapper = Wrappers.query(); 和上面一样的效果 userQueryWrapper.select(User.class , e->!e.getColumn().equals("password")).like("username" , "k") .between("age" , 25 , 35) .isNotNull("password"); List userList = userMapper.selectList(userQueryWrapper); userList.forEach(System.out::println); } }
结果如下:
结果也是正确的。
这两种方法各有优缺点,可以互补使用。
相关文章
- SpringBoot自定义bean绑定解析 10-24
- Javaweb工程运行报错HTTP Status 404解决教程 10-20
- JAVA获取jvm和操作系统相关信息方法 10-20
- BeanFactory和FactoryBean的区别讲解 10-20
- 微信小程序的宿主环境实现教程 10-10
- dispatchEvent解决重叠元素响应事件教程 10-10