Mybatis及Mybatis-Plus使用

举报
听风与他 发表于 2023/11/24 11:23:37 2023/11/24
【摘要】 Mybatis及Mybatis学习

📑前言

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

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

Mybatis及Mybatis-Plus使用

采用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一样的目录结构作接口映射

image.png

配置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());
        
    }

}
【版权声明】本文为华为云社区用户原创内容,未经允许不得转载,如需转载请自行联系原作者进行授权。如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@huaweicloud.com
  • 点赞
  • 收藏
  • 关注作者

评论(0

0/1000
抱歉,系统识别当前为高风险访问,暂不支持该操作

全部回复

上滑加载中

设置昵称

在此一键设置昵称,即可参与社区互动!

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。