搭建MyBatis Plus框架并进行数据库增删改查

搭建MyBatis Plus框架并进行数据库增删改查

搭建MyBatis-Plus框架并进行数据库添加
MyBatis-Plus(简称 MP)是一个 MyBatis 的增强工具,在 MyBatis 的基础上只做增强不做改变,为简化开发、提高效率而生。
1.搭建环境
Jdk:jdk1.8.0_261
Idea:IntelliJ IDEA
Maven:apache-maven-3.3.6
MySQL:mysql-5.5.61
2.项目搭建
搭建MyBatis Plus框架并进行数据库增删改查_第1张图片
搭建MyBatis Plus框架并进行数据库增删改查_第2张图片
搭建MyBatis Plus框架并进行数据库增删改查_第3张图片
3.配置maven
搭建MyBatis Plus框架并进行数据库增删改查_第4张图片
搭建MyBatis Plus框架并进行数据库增删改查_第5张图片
4.项目节够
创建数据库

CREATE DATABASE sql03;

USE sql03;

CREATE TABLE user(
   id INT(11) PRIMARY KEY AUTO_INCREMENT,
   name VARCHAR(50),
   pass VARCHAR(50),
 
);

INSERT INTO user(name,passs) VALUES('baba','1890');


SELECT * FROM user;

新建完成以后,打开pom.xml后添加以下依赖

 <dependency>
            <groupId>com.baomidou</groupId>
            <artifactId>mybatis-plus-boot-starter</artifactId>
            <version>3.4.0</version>
        </dependency>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>5.1.49</version>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <version>1.18.12</version>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-test</artifactId>
        </dependency>

5.配置 MapperScan 注解
扫描mapper层

package com.sql.demo;

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

@MapperScan("com.sql.demo.mapper")
@SpringBootApplication
public class DemoApplication {

    public static void main(String[] args) {

        SpringApplication.run(DemoApplication.class, args);
    }

}

6.创建实体

package com.sql.demo.pojo;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableId;




public class user {
    @TableId(value = "id")
    public int id;
    @TableField(value = "name")
    public String name;
    @TableField(value = "pass")
    public String pass;
    @Override
    public String toString() {
        return "user{" +
                "id=" + id +
                ", name='" + name + '\'' +
                ", pass='" + pass + '\'' +
                '}';
    }
    public user(){}
    public int getId() {
        return id;
    }

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

    public String getName() {
        return name;
    }

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

    public String getPass() {
        return pass;
    }

    public void setPass(String pass) {
        this.pass = pass;
    }


}

7.创建接口

package com.sql.demo.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.sql.demo.pojo.user;

import org.springframework.stereotype.Repository;

@Repository
public interface Userdao extends BaseMapper<user> {
}

8.测试
在test文件里自动生成DemoApplicationTests类里往数据库中添加数据进行测试。

package com.sql.demo;


import com.sql.demo.mapper.Userdao;
import com.sql.demo.pojo.user;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;

@SpringBootTest
class DemoApplicationTests {

    @Autowired
    private Userdao userdao;

    @Test
        void insert(){
           user user1=new user();
         user1.setName("巴巴");
          user1.setPass("1890");


    int result=userdao.insert(user1);
            System.out.println("result:"+result);
            System.out.println(user1);//id会自动回填到user对象中
        }

    }

8.1 updateById
需求信息:将id为1的员工的姓名更改为"sql"

 @Test
    void testUpdateById() {
        user user2=userdao.selectById(1);
        user2.setName("sql");
        int result = userdao.updateById(user2);
        System.out.println(result);
    }
}

8.2 selectById
查询id为1的员工信息

 @Test
    void testDeleteById() {
        int result = userdao.deleteById(1);
        System.out.println(result);
    }

8.3 deleteById
删除id为1的员工信息

 @Test
    void testDeleteById() {
        int result = userdao.deleteById(1);
        System.out.println(result);
    }

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