MyBatisPlus快速入门

1、创建数据库

1.1、创建表user

CREATE TABLE `user` (
	`id` bigint(20) key auto_increment COMMENT '主键ID',
	`name` varchar(30) DEFAULT NULL COMMENT '姓名',
	`age` int(11) DEFAULT NULL COMMENT '年龄',
	`email` varchar(50) DEFAULT NULL COMMENT '邮箱'
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

1.2、插入数据

INSERT INTO `user` (`name`, `age`, `email`) VALUES ('张三', '23',
'123436.com');
INSERT INTO `user` (`name`, `age`, `email`) VALUES ('张四', '13',
'1436.com');
INSERT INTO `user` (`name`, `age`, `email`) VALUES ('王二', '78',
'789.com');
INSERT INTO `user` (`name`, `age`, `email`) VALUES ('马云', '16',
'123213436.com');
INSERT INTO `user` (`name`, `age`, `email`) VALUES ('马化腾', '15',
'1213436.com');

2、编写java文件

2.1、创建maven工程

2.2、导入maven坐标

	<parent>
	    <groupId>org.springframework.boot</groupId>
	    <artifactId>spring-boot-starter-parent</artifactId>
	    <version>2.1.0.RELEASE</version>
	</parent>
	<dependencies>
       <dependency>
           <groupId>org.springframework.boot</groupId>
           <artifactId>spring-boot-starter</artifactId>
       </dependency>
       <dependency>
           <groupId>org.springframework.boot</groupId>
           <artifactId>spring-boot-starter-test</artifactId>
           <scope>test</scope>
       </dependency>
       <!--简化代码的工具包-->
       <dependency>
           <groupId>org.projectlombok</groupId>
           <artifactId>lombok</artifactId>
           <optional>true</optional>
       </dependency>
       <!--mybatis-plus的springboot支持-->
       <dependency>
           <groupId>com.baomidou</groupId>
           <artifactId>mybatis-plus-boot-starter</artifactId>
           <version>3.0.5</version>
       </dependency>
       <!--mysql驱动-->
       <dependency>
           <groupId>mysql</groupId>
           <artifactId>mysql-connector-java</artifactId>
           <version>5.1.47</version>
       </dependency>
   </dependencies>
   <build>
       <plugins>
           <!--能够将Spring Boot应用打包为可执行的jar或war文件,执行springboot应用-->
           <plugin>
               <groupId>org.springframework.boot</groupId>
               <artifactId>spring-boot-maven-plugin</artifactId>
           </plugin>
       </plugins>
   </build>

2.3、在resource文件夹下创建application.properties

//127.0.0.1为自己的服务器地址

spring.application.name = mybatis-plus
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.url=jdbc:mysql://127.0.0.1:3306/helloworld?useUnicode=true&characterEncoding=utf8&autoReconnect=true&allowMultiQueries=true&useSSL=false
spring.datasource.username=root
spring.datasource.password=root

2.4、创建User对象

package com.mybatisplus.pojo;

public class User {
    private Long id;
    private String name;
    private Integer age;
    private String email;

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Integer getAge() {
        return age;
    }

    public void setAge(Integer age) {
        this.age = age;
    }

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    @Override
    public String toString() {
        return "User{" +
                "id=" + id +
                ", name='" + name + '\'' +
                ", age=" + age +
                ", email='" + email + '\'' +
                '}';
    }
}

2.5、编写UserMapper

package com.mybatisplus.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.mybatisplus.pojo.User;

public interface UserMapper extends BaseMapper<User> {

}

2.6、编写SpringBoot启动类

package com.mybatisplus;


import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;


@MapperScan("com.mybatisplus.mapper")   //设置Mapper扫描包
@SpringBootApplication
public class MyApplication {
    public static void main(String[] args) {
        SpringApplication.run(MyApplication.class,args);
    }
}

2.7、编写单元测试

//在MybatisPlus中,BaseMapper中定义了一些常用的CRUD方法,当我们自定义的Mapper接口继承BaseMapper后即可拥有了这些方法。

package com.mybatisplus.mapper;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.mybatisplus.pojo.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.List;

@RunWith(SpringRunner.class)
@SpringBootTest
public class UserMapperTest {

    @Autowired
    private UserMapper userMapper;

    /**
     * 功能描述:测试查询所有
     * @author lfd
     * @date
     */
    @Test
    public void testSelect(){
        System.out.println("--------------------");
        List<User> userList = userMapper.selectList(null);
        for (User user:userList){
            System.out.println(user);
        }
    }

    //模糊查询
    @Test
    public void testSelectByLike(){
        QueryWrapper<User> userQueryWrapper = new QueryWrapper<>(new User());
        //查询名字中包含马的
        userQueryWrapper.like("name","马");
        List<User> userList = this.userMapper.selectList(userQueryWrapper);
        for (User user:userList){
            System.out.println(user);
        }
    }

    //条件查询
    @Test
    public void testSelectByLe(){
        QueryWrapper<User> userQueryWrapper = new QueryWrapper<>(new User());
        //查询年龄小于30的
        userQueryWrapper.le("age","30");
        List<User> userList = this.userMapper.selectList(userQueryWrapper);
        for (User user:userList){
            System.out.println(user);
        }
    }


    //添加数据
    //必须在User表上设置ID属性上添加@TableId(value = "ID",type = IdType.AUTO)注解
    @Test
    public void testSave(){
        User user = new User();
        user.setName("李彦宏啊");
        user.setAge(30);
        user.setEmail("778778778");
        int insert = this.userMapper.insert(user);
        System.out.println(insert);
    }

    //删除
    @Test
    public void testDelete(){
//        User user = new User();
//        user.setId(8L);
//        QueryWrapper<User> userQueryWrapper = new QueryWrapper<>(user);
//        int delete = this.userMapper.delete(userQueryWrapper);
        int delete = this.userMapper.deleteById(8L);
        System.out.println(delete);
    }

    //修改数据
    @Test
    public void testUpdate(){
        User user = new User();
        user.setName("李彦宏啊");
        user.setId(7L);
        int update = this.userMapper.updateById(user);
        System.out.println(update);

    }


    //分页查询
    @Test
    public void testSelectPage(){
        Page<User> userPage = new Page<>(1,2);
        IPage<User> userIPage = this.userMapper.selectPage(userPage,null);
        System.out.println("总条数----------------->" + userIPage.getTotal());
        System.out.println("当前页数----------------->" + userIPage.getCurrent());
        System.out.println("当前每页显示条数----------------->" + userIPage.getSize());
        List<User> userList = userIPage.getRecords();
        for (User user:userList){
            System.out.println(user);
        }
    }
}

添加数据在User类中添加
在这里插入图片描述
分页查询在启动类中添加下面函数
在这里插入图片描述

2.8自定义配置文件

在Application.properties中

# 指定全局配置文件
mybatis-plus.config-location = classpath:mybatis-config.xml
# 指定mapper.xml文件
mybatis-plus.mapper-locations = classpath*:mybatis/*.xml
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值