栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

Mybatis-plus:简单实例,快速使用

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Mybatis-plus:简单实例,快速使用

Mybatis-plus:简单实例

Mybatis-plus就是简化了mybatis的使用,正常情况下,我们要写mapper的xml文件然后编写sql,这样开发者要写的东西就很多,
所以就出了Mybatis-plus,基本的curd可以直接调用,谁让人家自己写好了。复杂的sql就需要学习官方的接口了,所以总的来说,不想写sql就得学Mybatis-plus的接口,相较之下,大部分公司还是选择了Mybatis。毕竟sql还是比较适应开发的

有用的话就点点赞和关注一下哦

这里提供代码
链接:https://pan.baidu.com/s/1W2WkQYi05199Oln5S-3qJA
提取码:xnkq
在下面有数据库的代码
效果图:


这里谷咕咕使用的idea创建的springboot项目
使用步骤:

1.导入依赖
        
        
            mysql
            mysql-connector-java
            5.1.44
            runtime
        
        
        
            com.baomidou
            mybatis-plus-boot-starter
            3.0.5
        
        
2.配置文件application.properties

使用自己的数据库的账户密码
我的数据库版本是mysql5.0所以使用的是com.mysql.jdbc.Driver

spring.datasource.username=root
spring.datasource.password=root
spring.datasource.url=jdbc:mysql://localhost:3306/zhengze?useUnicode=true&characterEncoding=utf-8&autoReconnect=true
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
#配置日志 默认自带日志
mybatis-plus.configuration.log-impl=org.apache.ibatis.logging.stdout.StdOutImpl


3.编写代码

4.创建表,和实体类
create TABLE admin(
id INT(4) PRIMARY KEY,
username VARCHAR(20) not null,
password VARCHAR(20) not null,
perms VARCHAR(20) 
)
alter table admin modify id int auto_increment;

实体类

package com.example.pojo;

public class Admin {
    Integer id;
    String username;
    String password;
    String perms;

    public Admin() {
    }

    public Admin(Integer id, String username, String password, String perms) {
        this.id = id;
        this.username = username;
        this.password = password;
        this.perms = perms;
    }

    public Integer getId() {
        return id;
    }

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

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public String getPerms() {
        return perms;
    }

    public void setPerms(String perms) {
        this.perms = perms;
    }

    @Override
    public String toString() {
        return "Admin{" +
                "id=" + id +
                ", username='" + username + ''' +
                ", password='" + password + ''' +
                ", perms='" + perms + ''' +
                '}';
    }
}

mapper

package com.example.mapper;

import com.baomidou.mybatisplus.core.mapper.baseMapper;
import com.example.pojo.Admin;
import org.apache.ibatis.annotations.Mapper;
import org.springframework.stereotype.Repository;

@Mapper
@Repository
public interface AdminMapper extends baseMapper {
}

5.测试类


测试类

package com.example;

import com.example.mapper.AdminMapper;
import com.example.pojo.Admin;
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 MybatisPlusApplicationTests {
    @Autowired
    AdminMapper adminMapper;

    @Test
    void contextLoads() {
    }
    @Test
    void test1(){
        List userList = adminMapper.selectList(null);
        for (Admin admin : userList) {
            System.out.println(admin);
        }
    }

}

注意在启动类中添加扫描

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/666242.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号