mybatis-plus快速开始

mybatis-plus快速开始

数据库准备

创建数据库boot

创建表

CREATE TABLE `user` (
  `id` bigint NOT NULL COMMENT '主键ID',
  `name` varchar(30) DEFAULT NULL COMMENT '姓名',
  `age` int DEFAULT NULL COMMENT '年龄',
  `email` varchar(50) DEFAULT NULL COMMENT '邮箱',
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci;

添加数据

INSERT INTO `user` VALUES
	( 1, '小明', 18, 'xm@66.com' ),
	( 2, '小花', 20, 'xh@66.com' ),
	( 3, '小张', 15, 'xz@66.com' ),
	( 4, '张三', 16, 'zs@66.com' ),
	( 5, '李小四', 24, 'lxs@66.com' );

创建Maven工程

创建工程mybatis-plus-demo
在这里插入图片描述

pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.3.1.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <artifactId>mybatis-plus-demo</artifactId>
    <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>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
        </dependency>
        <dependency>
            <groupId>com.baomidou</groupId>
            <artifactId>mybatis-plus-boot-starter</artifactId>
            <version>3.3.0</version>
        </dependency>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>8.0.20</version>
        </dependency>
    </dependencies>
</project>

application.yml

spring:
  datasource:
    driver-class-name: com.mysql.jdbc.Driver
    url: jdbc:mysql://127.0.0.1:3306/boot?useServerPrepStmts=true&cachePrepStmts=true&serverTimezone=Hongkong&useUnicode=true&characterEncoding=UTF-8&useSSL=false&allowPublicKeyRetrieval=true
    username: root
    password: root

创建实体类User

package com.sunrd.mybatis.domain.po;

import lombok.Data;

/**
 * @author sunrd
 */
@Data
public class User {
    private Long id;
    private String name;
    private Integer age;
    private String email;
}

创建Mapper

package com.sunrd.mybatis.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.sunrd.mybatis.domain.po.User;

/**
 * @author sunrd
 */
public interface UserMapper extends BaseMapper<User> {
}

创建启动类

package com.sunrd.mybatis;

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

/**
 * @author sunrd
 * @description
 * @date 2020/8/12
 */
@SpringBootApplication
@MapperScan("com.sunrd.mybatis.mapper")
public class Application {
    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }
}

编写测试类

package com.sunrd.mybatis;

import com.sunrd.mybatis.domain.po.User;
import com.sunrd.mybatis.mapper.UserMapper;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

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

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

    @Resource
    private UserMapper userMapper;

    @Test
    public void selectTest() {
        List<User> userList = userMapper.selectList(null);
        userList.forEach(System.out::println);
    }

}

输出

User(id=1, name=小明, age=18, email=xm@66.com)
User(id=2, name=小花, age=20, email=xh@66.com)
User(id=3, name=小张, age=15, email=xz@66.com)
User(id=4, name=张三, age=16, email=zs@66.com)
User(id=5, name=李小四, age=24, email=lxs@66.com)
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值