SpringBoot+MyBatisPlus

MyBatis-Plus官网

MyBatis-Plus(简称 MP)是一个 MyBatis 的增强工具,在 MyBatis 的基础上只做增强不做改变,为简化开发、提高效率而生。

  • 创建springboot项目

一、在SpringBoot中集成MyBatisPlus

1.1、pom.xml 添加依赖

        
        
            mysql
            mysql-connector-java
            runtime
        
        
        
            com.baomidou
            mybatis-plus-boot-starter
            3.3.1.tmp
        
        
        
            org.projectlombok
            lombok
            1.18.10
            provided
        

1.2、application.properties 配置

# mysql 5 驱动不同 com.mysql.jsbc.Driver
# mysql 8 驱动不同 com.mysql.cj.jsbc.Driver、需要增加时区的配置
spring.datasource.username=root (数据库用户名)
spring.datasource.password=Aa123123.(数据库密码)

spring.datasource.url=jdbc:mysql://localhost:3306/mybatis_plus?useSSL=false&useUnicode=true&characterEncoding=utf-8&serverTimezone=GMT%2B8(数据库连接地址)
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver(数据源)
#配置日志
mybatis-plus.configuration.log-impl=org.apache.ibatis.logging.stdout.StdOutImpl
 

1.3、BootApplication添加@MapperScan注解

package com.example.plus;
 
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
 
@SpringBootApplication
@MapperScan("com.example.plus.dao") //扫描mapper文件夹
public class MybatisPlusApplication {
 
    public static void main(String[] args) {
        SpringApplication.run(MybatisPlusApplication.class, args);
    }
 
}

1.4、实体

常用注解:

  • @TableName("表名")当表名与实体类名不一致时,可以在实体类上加入@TableName()声明
  • @TableId声明属性为表中的主键(若属性名称不为默认id)
  • @TableFieId("字段") 当实体类属性与表字段不一致时,可以用来声明
package com.example.plus.entity;
 
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
import lombok.Data;
 
@Data//pom文件添加了lombok依赖,使用@Data 注解实现: 无参、 有参构造、 get、 set、 方法
@TableName("user") //当表名与实体类名不一致时,可以在实体类上加入@TableName()声明
public class User {
    @TableId //声明属性为表中的主键(若属性名称不为默认id)
    @TableId(type = IdType.AUTO) //主键自增,数据库字段也务必设置为自增长
    private Long id;
 
 //   @TableField("name") //当实体类属性与表字段不一致时,可以用来声明
 //   private String UserName;
    
    private String name;
    private Integer age;
    private String email;
}

1.5、UserMapper.java

package com.example.plus.dao;
 
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.example.plus.entity.User;
 
/**
 * 在对应的Mapper上继承基本的类baseMapper
 */
public interface UserMapper extends BaseMapper {
    //所有的CRUD已经编写完成 (增删改查)
    //不需要像以前的配置一些xml
}

1.6、Controller中使用Wrapper进行条件查询

package com.example.plus.controller;
 
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.plus.dao.UserMapper;
import com.example.plus.entity.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
 
import java.util.List;
import java.util.Map;
 
@RestController
@RequestMapping("/test")
@Slf4j//控制台打印日志(lombok)
public class TestController {
 
    @Autowired(required = false)
    private UserMapper userMapper;
 
    @GetMapping("/list")
    public List listUser(){
        //参数是一个Wrapper,条件结构器,这里先不用 填null
        List userList = userMapper.selectList(null);
        return userList;
    }
 
    /**
     * 查询name不为null的用户,并且邮箱不为null的永不,年龄大于等于20的用户
     *
     * @return
     */
    @GetMapping("/t1")
    public List t1(){
        QueryWrapper wrapper =new QueryWrapper<>();
        wrapper.isNotNull("name");
        wrapper.isNotNull("email");
        wrapper.ge("age",20);//ge 是大于  le是小于
        return userMapper.selectList(wrapper);
    }
 
    /**
     * 查询name为shuishui的用户
     *
     * @return
     */
    @GetMapping("/t2")
    public List t2(){
        QueryWrapper wrapper =new QueryWrapper<>();
        wrapper.eq("name","shuishui");
        return userMapper.selectList(wrapper);
    }
 
    /**
     * 查询年龄在20~30岁之间的用户
     *
     * @return
     */
    @GetMapping("/t3")
    public Integer t3(){
        QueryWrapper wrapper =new QueryWrapper<>();
        wrapper.between("age",20,30);
        return userMapper.selectCount(wrapper);//查询结果数
    }
 
    /**
     * 模糊查询
     *
     * @return
     */
    @GetMapping("/t4")
    public List> t4(){
        QueryWrapper wrapper =new QueryWrapper<>();
 
        wrapper.notLike("name","s");//相当于NOT LIKE '%s%'
        wrapper.likeRight("email","s");//相当于LIKE 's%'
        List>maps = userMapper.selectMaps(wrapper);//查询结果数
        return maps;
    }
 
    /**
     * 子查询
     *
     * @return
     */
    @GetMapping("/t5")
    public List t5(){
        QueryWrapper wrapper =new QueryWrapper<>();
        wrapper.inSql("id","select id from user where id<3");
        List objects = userMapper.selectObjs(wrapper);
        return objects;
    }
 
    /**
     * 通过id进行排序
     *
     * @return
     */
    @GetMapping("/t6")
    public List t6(){
        QueryWrapper wrapper =new QueryWrapper<>();
        //通过id进行排序
        wrapper.orderByAsc("id");
        return userMapper.selectList(wrapper);
    }
 
    /**
     * 姓王年龄大于等于25,按年龄降序,年龄相同按id升序排列
     *
     * @return
     */
    @GetMapping("/t7")
    public List t7(){
        QueryWrapper wrapper =new QueryWrapper<>();
        wrapper.likeRight("name","王").or().ge("age",25).orderByDesc("age").orderByAsc("id");
        return userMapper.selectList(wrapper);
    }
 
    /**
     * 创建日期为2019年2月14日并且直属上级为姓王
     *
     * @return
     */
    @GetMapping("/t8")
    public List t8(){
        QueryWrapper wrapper =new QueryWrapper<>();
        wrapper
                .apply("date_fromat(create_time,'%Y-%m-%d')='2019-02-14'")
                .inSql("manager_id","select id from user where name like '王%'");
        return userMapper.selectList(wrapper);
    }
 
    /**
     * 姓王并且(年龄小于40或者邮箱不为空)
     *
     * @return
     */
    @GetMapping("/t9")
    public List t9(){
        QueryWrapper wrapper =new QueryWrapper<>();
        //lt小于,gt大于
        wrapper
                .likeRight("name","王")
                .and(wq->wrapper.lt("age",40)
                        .or().isNotNull("email"));
        return userMapper.selectList(wrapper);
    }
 
    /**
     * 不列出所有字段
     *
     * @return
     */
    @GetMapping("/t10")
    public List t10(){
        QueryWrapper wrapper =new QueryWrapper<>();
 
        wrapper.select("id","name").like("name","雨").lt("age",40);
        //不显示时间和id
        //wrapper.select(User.class,info->!info.getColumn().equals("create_time")&&!info.getColumn().equals("manager_id")).like("name","雨").lt("age",40);
        return userMapper.selectList(wrapper);
    }
} 
  

QueryWrapper的方法:

SpringBoot+MyBatisPlus_第1张图片

 注意:本博客测试时未写service业务层

service 接口:

SpringBoot+MyBatisPlus_第2张图片

service 实现类:

SpringBoot+MyBatisPlus_第3张图片

  •  IService 实现类( 泛型:M 是 mapper 对象,T 是实体 )
  • M代表的是:继承了BaseMapper的对象
  • T代表的是:实体类对象

 

你可能感兴趣的:(java,spring,boot,java,spring,mybatis)