当前位置:   article > 正文

mybatis-plus基础查询语句_mybatis-plus findall代码

mybatis-plus findall代码

目录

集成

创建本地数据库

初始化项目

创建spring-boot项目,在pom文件添加所需依赖

配置yml文件

为mapper接口创建全局代理对象

 在pojo目录下创建User映射类 

定义公共接口,并继承BaseMapper

测试

常用mybatis-plus增删改查方法

mybatis-plus(select查询)

selectList查询全部数据

 selectBatchIds根据集合查询数据

selectByMap根据指定条件查询

queryWrapper构造器基础查询

 queryWrapper构造器nested嵌套查询

queryWrapper构造器inSql关联查询

 queryWrapper构造器selectMaps指定字段查询

 queryWrapper构造器过滤null条件查询

queryWrapper构造器groupBy分组查询

queryWrapper构造器selectObjs获取第一列数据查询

insert插入数据

updateById更新数据

 delete删除数据

 queryWrapper构造器其他查询


集成

依赖包地址https://mp.baomidou.com/

创建本地数据库

创建本地数据库,并创建用户表,假如我创建了一个user表,id设置为自增的主键

初始化项目

创建spring-boot项目,在pom文件添加所需依赖

  1. <project
  2. xmlns="http://maven.apache.org/POM/4.0.0"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4. xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
  5. <modelVersion>4.0.0</modelVersion>
  6. <groupId>com.jt.vip</groupId>
  7. <artifactId>spring_boot_mybatis</artifactId>
  8. <version>0.0.1-SNAPSHOT</version>
  9. <parent>
  10. <groupId>org.springframework.boot</groupId>
  11. <artifactId>spring-boot-starter-parent</artifactId>
  12. <version>2.4.6</version>
  13. <relativePath /> <!-- lookup parent from repository -->
  14. </parent>
  15. <dependencies>
  16. <dependency>
  17. <groupId>org.springframework.boot</groupId>
  18. <artifactId>spring-boot-starter-web</artifactId>
  19. </dependency>
  20. <dependency>
  21. <groupId>org.springframework.boot</groupId>
  22. <artifactId>spring-boot-starter-test</artifactId>
  23. <scope>test</scope>
  24. </dependency>
  25. <!--为属性赋值,添加依赖 -->
  26. <dependency>
  27. <groupId>org.springframework.boot</groupId>
  28. <artifactId>spring-boot-configuration-processor</artifactId>
  29. <optional>true</optional>
  30. </dependency>
  31. <!--添加热部署插件 -->
  32. <dependency>
  33. <groupId>org.springframework.boot</groupId>
  34. <artifactId>spring-boot-devtools</artifactId>
  35. </dependency>
  36. <!--引入插件lombok 自动的set/get/构造方法插件 -->
  37. <dependency>
  38. <groupId>org.projectlombok</groupId>
  39. <artifactId>lombok</artifactId>
  40. </dependency>
  41. <!--引入数据库驱动 -->
  42. <dependency>
  43. <groupId>mysql</groupId>
  44. <artifactId>mysql-connector-java</artifactId>
  45. <scope>runtime</scope>
  46. </dependency>
  47. <!--springBoot数据库连接 -->
  48. <dependency>
  49. <groupId>org.springframework.boot</groupId>
  50. <artifactId>spring-boot-starter-jdbc</artifactId>
  51. </dependency>
  52. <dependency>
  53. <groupId>com.baomidou</groupId>
  54. <artifactId>mybatis-plus-boot-starter</artifactId>
  55. <version>3.4.3.1</version>
  56. </dependency>
  57. </dependencies>
  58. <build>
  59. <plugins>
  60. <plugin>
  61. <groupId>org.springframework.boot</groupId>
  62. <artifactId>spring-boot-maven-plugin</artifactId>
  63. </plugin>
  64. </plugins>
  65. </build>
  66. </project>

配置yml文件

  1. server:
  2. port: 80
  3. servlet:
  4. context-path: /
  5. spring:
  6. datasource:
  7. #高版本配置如下 com.mysql.cj.jdbc.Driver
  8. #低版本配置如下com.mysql.jdbc.Driver
  9. driver-class-name: com.mysql.cj.jdbc.Driver
  10. url: jdbc:mysql://IP地址:3306/你的数据库名?serverTimezone=GMT%2B8&useUnicode=true&characterEncoding=utf8&autoReconnect=true&allowMultiQueries=true
  11. username: root
  12. password: 123456
  13. mybatis-plus:
  14. #定义别名包 resultType的映射可以省略包路径,这里修改为你的User文件的包路径
  15. type-aliases-package: com.example.demo.pojo
  16. #加载mapper的映射文件
  17. mapper-locations: classpath:/mybatis/mappers/*.xml
  18. #日志 替换成自己mapper包路径
  19. logging:
  20. level:
  21. '[com.example.demo.mapper]': debug

为mapper接口创建全局代理对象

  1. @SpringBootApplication
  2. @MapperScan("com.example.demo.mapper")
  3. public class Application {
  4. public static void main(String[] args) {
  5. SpringApplication.run(Application.class, args);
  6. }
  7. }

说明,@MapperScan("com.example.demo.mapper"),在启动类中,动态的为mapper的接口创建代理对象,免去之后所有mapper类中的@Mapper注解


 在pojo目录下创建User映射类 

  1. @Data
  2. @Accessors(chain = true)
  3. @NoArgsConstructor
  4. @AllArgsConstructor
  5. @TableName("user") //将对象和表进行关联
  6. public class User {
  7. @TableId(type = IdType.AUTO)
  8. private Integer id;
  9. @TableField("name")
  10. private String name;
  11. @TableField("sex")
  12. private String sex;
  13. @TableField("age")
  14. private Integer age;
  15. }

这里对注解做一个说明:

@TableName("user") 将User对象和user表进行关联
@TableId(type = IdType.AUTO)标识这个字段为主键,这里我设置为自增
@TableField("")关联字段映射

 如果User对象中的字段和表字段一致,注解可以省略不写,如图


定义公共接口,并继承BaseMapper

  1. public interface UserMapper extends BaseMapper<User>{
  2. //这里手动添加一个自己的测试方法,查询所有数据
  3. List<User>findAll();
  4. }

说明,BaseMapper需要一个泛型类型,这里是User类型,BaseMapper中包含我们所需的增删改查方法


测试

  1. @SpringBootTest
  2. class ApplicationTests {
  3. @Autowired
  4. private UserMapper userMapper;
  5. @Test
  6. void contextLoads() {
  7. List<User>list=userMapper.findAll();
  8. System.out.println(list);
  9. }
  10. }

常用mybatis-plus增删改查方法

mybatis-plus(select查询)


selectList查询全部数据

  1. @Test
  2. void test1() {
  3. List<User>list=userMapper.selectList(null);
  4. System.out.println(list);
  5. }


 selectBatchIds根据集合查询数据

  1. @Test
  2. void test2() {
  3. List<Integer>idList=new ArrayList<Integer>();
  4. idList.add(1);
  5. idList.add(2);
  6. List<User>list=userMapper.selectBatchIds(idList);
  7. System.out.println(list);
  8. }

相当于  SELECT id,name,sex,age FROM user WHERE id IN ( ? , ? ) 


selectByMap根据指定条件查询

  1. @Test
  2. void test3() {
  3. Map<String, Object>columnMap=new HashMap<String, Object>();
  4. columnMap.put("name", "张三");
  5. columnMap.put("age", 4000);
  6. List<User>list=userMapper.selectByMap(columnMap);
  7. System.out.println(list);
  8. }

 相当于  SELECT id,name,sex,age FROM user WHERE name = ? AND age = ?

 


queryWrapper构造器基础查询

  1. //条件构造器内容查询
  2. @Test
  3. void test4() {
  4. QueryWrapper<User>queryWrapper=new QueryWrapper<User>();
  5. queryWrapper.like("name", "三");
  6. queryWrapper.eq("sex", "男");
  7. List<User>list=userMapper.selectList(queryWrapper);
  8. System.out.println(list);
  9. }

 相当于  SELECT id,name,sex,age FROM user WHERE (name LIKE ? AND sex = ?)


 queryWrapper构造器nested嵌套查询

 例如 我想查询(age<5000 或者 性别是男)并且姓张的人

  1. @Test
  2. void test6() {
  3. QueryWrapper<User>queryWrapper=new QueryWrapper<User>();
  4. queryWrapper
  5. .nested(i ->i.lt("age", 5000).or().eq("sex", "男"))
  6. .likeRight("name", "张");
  7. List<User>list=userMapper.selectList(queryWrapper);
  8. System.out.println(list);
  9. }

i ->i.lt("age", 5000)是lambda表达式

 相当于   SELECT id,name,sex,age FROM user WHERE ((age < ? OR sex = ?) AND name LIKE ?)


queryWrapper构造器inSql关联查询

例如 我想查询 age<5000并且和李四是相同性别的数据

  1. @Test
  2. void test5() {
  3. QueryWrapper<User>queryWrapper=new QueryWrapper<User>();
  4. queryWrapper.lt("age", 5000);
  5. queryWrapper.inSql("sex", "select sex from user where name='李四'");
  6. List<User>list=userMapper.selectList(queryWrapper);
  7. System.out.println(list);
  8. }

相当于  SELECT id,name,sex,age FROM user WHERE (age < ? AND sex IN (select sex from user where name='李四')) 


 queryWrapper构造器selectMaps指定字段查询

  1. @Test
  2. void test10() {
  3. QueryWrapper<User>queryWrapper=new QueryWrapper<User>();
  4. queryWrapper.select("name","age").eq("name", "张三");
  5. List<Map<String, Object>>list=userMapper.selectMaps(queryWrapper);
  6. System.out.println(list);
  7. }

 相当于  SELECT name,age FROM user WHERE (name = ?)


 queryWrapper构造器过滤null条件查询

  如下三种

  1. @Test
  2. void test7() {
  3. String name="张三";
  4. QueryWrapper<User>queryWrapper=new QueryWrapper<User>();
  5. queryWrapper.eq(!StringUtils.isEmpty(name),"name",name);
  6. List<User>list=userMapper.selectList(queryWrapper);
  7. System.out.println(list);
  8. }
  9. @Test
  10. void test8() {
  11. User user=new User();
  12. user.setAge(3000);
  13. //如果对象中的某个属性不为空,则拼接user。使用对象作为条件,只能使用等号运算符
  14. QueryWrapper<User>queryWrapper=new QueryWrapper<User>(user);
  15. List<User>list=userMapper.selectList(queryWrapper);
  16. System.out.println(list);
  17. }
  18. @Test
  19. void test9() {
  20. Map<String, Object>columnMap=new HashMap<String, Object>();
  21. columnMap.put("name", "张三");
  22. columnMap.put("age", null);
  23. QueryWrapper<User>queryWrapper=new QueryWrapper<User>();
  24. queryWrapper.allEq(columnMap,true);//true无论是否为空,都拼接该条件,如果为false,不拼接
  25. List<User>list=userMapper.selectList(queryWrapper);
  26. System.out.println(list);
  27. }

queryWrapper构造器groupBy分组查询

比如我想查询性别男的最大和最小年龄,性别女的最大和最小年龄

  1. @Test
  2. void test11() {
  3. QueryWrapper<User>queryWrapper=new QueryWrapper<User>();
  4. queryWrapper.select("max(age) max_age","min(age) min_age")
  5. .groupBy("sex")
  6. .having("sex in({0},{1})", "男","女"); //这里的0,1就是为后面的男女占位,防止注入攻击
  7. List<Map<String, Object>>list=userMapper.selectMaps(queryWrapper);
  8. System.out.println(list);
  9. }

相当于  SELECT max(age) max_age,min(age) min_age FROM user GROUP BY sex HAVING sex in(?,?)


queryWrapper构造器selectObjs获取第一列数据查询

  1. @Test
  2. void test12() {
  3. QueryWrapper<User>queryWrapper=new QueryWrapper<User>();
  4. queryWrapper.gt("age", 2000);
  5. List<Object>list=userMapper.selectObjs(queryWrapper);
  6. System.out.println(list);
  7. }

相当于  SELECT id,name,sex,age FROM user WHERE (age > ?)


insert插入数据

  1. @Test
  2. void test14() {
  3. User user=new User();
  4. user.setName("围裙妈妈");
  5. user.setAge(50);
  6. user.setSex("女");
  7. int x=userMapper.insert(user);
  8. System.out.println(x);
  9. }

相当于  INSERT INTO user ( name, sex, age ) VALUES ( ?, ?, ? ) 


updateById更新数据

  1. @Test
  2. void test15() {
  3. User user=new User();
  4. user.setId(100).setName("围裙妈妈").setAge(50).setSex("女");
  5. int x=userMapper.updateById(user);
  6. System.out.println(x);
  7. }

 或者根据条件更新

  1. @Test
  2. void test16() {
  3. User user=new User();
  4. user.setName("小头爸爸").setAge(60).setSex("女");
  5. QueryWrapper<User>updateWrapper=new QueryWrapper<User>();
  6. updateWrapper.eq("name", "大头儿子");
  7. int x=userMapper.update(user, updateWrapper);
  8. System.out.println(x);
  9. }

 delete删除数据

  1. @Test
  2. void test17() {
  3. int x=userMapper.deleteById(1000);
  4. System.out.println(x);
  5. }

或者批量删除

  1. @Test
  2. void test18() {
  3. List<Integer>list=new ArrayList<Integer>();
  4. list.add(1100);
  5. list.add(1200);
  6. int x=userMapper.deleteBatchIds(list);
  7. System.out.println(x);
  8. }

 或者根据条件删除

  1. @Test
  2. void test19() {
  3. QueryWrapper<User>queryWrapper=new QueryWrapper<User>();
  4. queryWrapper.eq("age", 100000);
  5. int x=userMapper.delete(queryWrapper);
  6. System.out.println(x);
  7. }

或者 根据条件删除

  1. @Test
  2. void test20() {
  3. Map<String, Object>columnMap=new HashMap<String, Object>();
  4. columnMap.put("name", "花花");
  5. int x=userMapper.deleteByMap(columnMap);
  6. System.out.println(x);
  7. }

相当于  DELETE FROM user WHERE name = ? 


 queryWrapper构造器其他查询

queryWrapper.like("name", "三");模糊查询   %三%
queryWrapper.likeLeft(column, val);左模糊
queryWrapper.likeRight(column, val);右模糊
queryWrapper.in("id", 1,2,3,4,5);查询指定的几个
queryWrapper.select("name","","","");查询指定字段
queryWrapper.eq("sex", "男");sex为男的条件
queryWrapper.gt(column, val);大于
queryWrapper.lt(column, val);小于
queryWrapper.ge(column, val);大于等于
queryWrapper.le(column, val);小于等于
queryWrapper.between(column, val1, val2);两者之间
queryWrapper.orderByDesc(column, columns);降序
queryWrapper.orderByAsc(columns)升序
selectCount(queryWrapper)根据条件查询总数
selectOne(queryWrapper);查询单条数据(需保证只要一条)

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/从前慢现在也慢/article/detail/164167
推荐阅读
相关标签
  

闽ICP备14008679号