springboot集成mybatis-plus

springboot集成mybatis-plus,第1张

springboot集成mybatis-plus

用于记录,mybatis-plus只是mybatis的一个增强版。

创建user表

导入依赖



    4.0.0
    
        org.springframework.boot
        spring-boot-starter-parent
        2.1.5.RELEASE
    


    com.lzp
    mybatis
    0.0.1-SNAPSHOT
    mybatis
    Demo project for Spring Boot
    
        1.8
    
    
        
            mysql
            mysql-connector-java
        
        
        
            com.baomidou
            mybatis-plus-boot-starter
            3.3.0
        
        
            org.projectlombok
            lombok
            
            provided
        

        
            org.springframework.boot
            spring-boot-starter
        

        
            org.springframework.boot
            spring-boot-starter-test
            test
        
        
            org.junit.jupiter
            junit-jupiter
            RELEASE
            test
        
        
            org.springframework.boot
            spring-boot-starter-web
        
    

    
        
            
                org.springframework.boot
                spring-boot-maven-plugin
            
        
    


配置yml

spring:
  application:
    name: test
  datasource:
    username: root
    password: a
    url: jdbc:mysql://localhost:3306/tb_test?useUnicode=true&characterEncoding=utf-8&useSSL=true&serverTimezone=UTC
    driver-class-name: com.mysql.cj.jdbc.Driver
#mybatis-plus
mybatis-plus:
  mapper-locations: classpath:/mapper/*Mapper.xml

 编写实体类与方法

 

 User

@Data
@EqualsAndHashCode(callSuper = false)
@Accessors(chain = true)
@ToString
@NoArgsConstructor
@AllArgsConstructor
@TableName("user")
public class User implements Serializable {

    private static final long serialVersionUID = 1L;

    @TableId(value = "id", type = IdType.ASSIGN_ID)
    private Integer id;
    private String uname;
    private String upwd;

}

UserMapper.xml





    
        SELECT
            *
        FROM
            user
        WHERe
            1=1
    

UserMapper

package com.lzp.mybatis.mapper;

import com.baomidou.mybatisplus.core.mapper.baseMapper;
import com.lzp.mybatis.entity.User;
import org.apache.ibatis.annotations.Mapper;

import java.util.List;

@Mapper
public interface UserMapper extends baseMapper {

    List findAll();
}

 IUserService

package com.lzp.mybatis.service;

import com.baomidou.mybatisplus.extension.service.IService;
import com.lzp.mybatis.entity.User;

import java.util.List;

public interface IUserService extends IService {
    List findAll();

}

UserServiceImpl

package com.lzp.mybatis.service.Impl;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lzp.mybatis.entity.User;
import com.lzp.mybatis.mapper.UserMapper;
import com.lzp.mybatis.service.IUserService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class UserServiceImpl extends ServiceImpl implements IUserService {

    @Resource
    private UserMapper zgqUsersMapper;

    @Override
    public List findAll() {
        //获取所有user
        List list=this.list();
        //根据条件查询
        List list2=this.list(Wrappers.lambdaQuery().eq(User::getId,1).eq(User::getUname,"a"));
        //获取list中 User的id属性 转为集合
        List list3=list.stream().map(User::getId).collect(Collectors.toList());
        List list5 = list.stream().filter(i -> i.getUname().contains("a")).collect(Collectors.toList());
        List list4=baseMapper.selectList(Wrappers.lambdaQuery().in(User::getId,list3));
        User user=new User();
        user.setUname("d");
        user.setUpwd("2");
        boolean i=this.update(user,Wrappers.lambdaUpdate().eq(User::getId,4));
        List list1=zgqUsersMapper.findAll();
        return list;
    }
}

UserControl

package com.lzp.mybatis.controller;

import com.lzp.mybatis.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class UserController {
    @Autowired
    IUserService iUserService;

    @GetMapping("/test")
    public String test(){
        iUserService.findAll();
        return "1";
    }
}

 详细说明:

mybatis-plus超详细讲解_zdsg45的博客-CSDN博客_mybatis-plus

 

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/zaji/5697108.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-12-17
下一篇 2022-12-17

发表评论

登录后才能评论

评论列表(0条)

保存