Springboot项目整合Mybatis及Mybatis-Plus

Mybatis及Mybatis-Plus使用

📑前言

本文主要是【Mybatis】——Mybatis即Mybatis-Plus使用的文章,如果有什么需要改进的地方还请大佬指出⛺️

🎬作者简介:大家好,我是听风与他🥇
☁️博客首页:CSDN主页听风与他
🌄每日一句:狠狠沉淀,顶峰相见

采用springboot整合Mybatis,运行环境是JDK17

  • 导入mybatis依赖

            <dependency>
                <groupId>org.mybatis.spring.boot</groupId>
                <artifactId>mybatis-spring-boot-starter</artifactId>
                <version>3.0.2</version>
            </dependency>
    

    在resource下配置和java目录下UserMapper一样的目录结构作接口映射

    在这里插入图片描述

配置application.yml文件的数据库url及账号密码,采用本地数据库lianxi

spring:
  datasource:
    driver-class-name: com.mysql.cj.jdbc.Driver
    url: jdbc:mysql://localhost:3306/lianxi
    username: root
    password: 020911

实体类User

package com.testmybatismp.pojo;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

@Data
@NoArgsConstructor
@AllArgsConstructor
public class User {
    private int id;
    private String name;
    private int age;
    private String password;
}

增删改查接口UserMapper

package com.testmybatismp.mapper;

import com.testmybatismp.pojo.User;
import org.apache.ibatis.annotations.Mapper;

import java.util.List;

@Mapper
public interface UserMapper {
    List<User> findAll();
    int addUser(User user);

    int deleteUser(int id);

    int updateUser(User user);

    User findUserById(int id);
}

映射文件UserMapper.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper
        PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.testmybatismp.mapper.UserMapper">
    <insert id="addUser">
        insert into user(name,age,password) values (#{name},#{age},#{password})
    </insert>
    <update id="updateUser">
        update user set name=#{name},age=#{age},password = #{password}
        where id = #{id}
    </update>
    <delete id="deleteUser">
        delete from user where id = #{id}
    </delete>
    <select id="findAll" resultType="com.testmybatismp.pojo.User">
        select * from user;
    </select>
    <select id="findUserById" resultType="com.testmybatismp.pojo.User">
        select * from user where id = #{id}
    </select>
</mapper>

测试时注入为JavaBean对象进行测试,如修改

package com.testmybatismp;

import com.testmybatismp.mapper.UserMapper;
import com.testmybatismp.pojo.User;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;

import java.util.List;

@SpringBootTest
class TestMybatisMpApplicationTests {

    @Autowired
    private UserMapper userMapper;
    @Test
    void contextLoads() {
        User user = new User(0,"王五",18,"123456");
        int i = userMapper.addUser(user);
        System.out.println("i:"+i);
    }

}

采用springboot整合Mybatis-plus,运行环境是JDK17

引入Mybatis-Plus框架

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

实体类User1:

package com.testmybatismp.pojo;

import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

@Data
@NoArgsConstructor
@AllArgsConstructor
@TableName("user1")   //设置数据库的表名
public class User1 {
    @TableId(type = IdType.AUTO)  //设置主键自增
    private int id;
    private String username;
    private String password;
}

在测试类中测试增删改查

package com.testmybatismp;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.testmybatismp.mapper.User1Mapper;
import com.testmybatismp.mapper.UserMapper;
import com.testmybatismp.pojo.User;
import com.testmybatismp.pojo.User1;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;

import java.util.List;

@SpringBootTest
class TestMybatisMpApplicationTests {

    @Autowired
    private User1Mapper user1Mapper; //将user1Mapper对象注入,方便调用
    
    @Test
    void contextLoads() {
        //添加用户
        User1 user1 = new User1(0,"王五","123");
        user1Mapper.insert(user1);
        //删除用户
        user1Mapper.deleteById(2);
        //修改用户
        User1 user2 = new User1(1,"张三","12345");
        user1Mapper.updateById(user2);
        //查询全部用户
        List<User1> list = user1Mapper.selectList(null);
        list.forEach(System.out::println);
        //查询用户
        QueryWrapper<User1> q = new QueryWrapper<>();
        q.eq("username","张三");
        User1 one = user1Mapper.selectOne(q);
        System.out.println(one.toString());
        
    }

}

📑文章末尾

在这里插入图片描述

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

听风与他

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值