mybatis-pluse快速使用 springboot

        <dependency>
            <groupId>com.baomidou</groupId>
            <artifactId>mybatis-plus-boot-starter</artifactId>
            <version>3.4.2</version>
        </dependency>

随便写一个model



import com.baomidou.mybatisplus.annotation.TableName;
import lombok.Data;
import org.springframework.stereotype.Component;

@Data
@Component
@TableName
public class User {
    private Long id;
    private String name;
    private Integer age;
    private String email;
}

写一个mapper层

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.example.flyway.model.User;
import org.springframework.stereotype.Service;

@Service
public interface UserMapper extends BaseMapper<User> {
}

启动类加入扫描包:

@SpringBootApplication
@MapperScan("com.example.flyway.mapper")
public class FlywayApplication {

	public static void main(String[] args) {
		SpringApplication.run(FlywayApplication.class, args);
	}

}

写一个controller做测试:



import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.example.flyway.mapper.UserMapper;
import com.example.flyway.model.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.sql.Wrapper;
import java.util.Iterator;
import java.util.List;

@RestController
@RequestMapping("/plus")
public class UserController {
    @Autowired
    private UserMapper userMapper;

    @GetMapping("/list")
    public String  getUser()
    {
        List<User> listUser = userMapper.selectList(null);
        Iterator<User> iterator = listUser.iterator();
        while (iterator.hasNext())
        {
            User user = iterator.next();
            System.out.println(user.toString());
        }
        return "ok";
    }

    @GetMapping("/wapper")
    public String  getUserList()
    {
        QueryWrapper<User> wrapper = Wrappers.query();
        wrapper.like("name" ,"o").orderByDesc("name");
        List<User> listUser = userMapper.selectList(wrapper);
        Iterator<User> iterator = listUser.iterator();
        while (iterator.hasNext())
        {
            User user = iterator.next();
            System.out.println(user.toString());
        }
        return "ok";
    }
}

官网教程:点击这里

这里就做个记录, 官网写的很清楚,很简单。  半个小时就能看会。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值