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

MyBatisPlus是什么?相关的基本操作入门案例

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

MyBatisPlus是什么?相关的基本操作入门案例

总述:MyBatisPlus是 将Mybatis + 通用Mapper + PageHelper升级成 MyBatis Plus

技术升级示意图

(1)pom.xml文件,导入相关依赖jar包

    
    
        org.springframework.boot
        spring-boot-starter-parent
        2.3.5.RELEASE
       







        
        
            org.springframework.boot
            spring-boot-starter-web
        
        
        
            mysql
            mysql-connector-java
        
        
        
            org.projectlombok
            lombok
        
        
        
            org.springframework.boot
            spring-boot-starter-test
        
        
        
            com.baomidou
            mybatis-plus-boot-starter
            3.4.0
        
    

(2)application.yml文件,配置对应的数据库

spring:
  datasource:
    driver-class-name: com.mysql.jdbc.Driver
    url: jdbc:mysql://localhost:3306/cloud_db1?useUnicode=true&characterEncoding=UTF-8&serverTimezone=UTC
    username: root
    password: 1234
mybatis-plus:
  configuration:
    log-impl: org.apache.ibatis.logging.stdout.StdOutImpl  #输出日志

(3)编写启动类

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class TestMyBatisPlusApplication {
    public static void main(String[] args) {
        SpringApplication.run(TestMyBatisPlusApplication.class,args);
    }
}

(4)编写对应的javaBean类以及添加对应的注解

@Data//setget构造
@NoArgsConstructor//无参构造
@AllArgsConstructor//全参构造
@TableName("tmp_customer") //对应数据库的表名
public class Customer {
    @TableId(type = IdType.AUTO)//明确主键 并且主键是自动增长的
    private Integer cid;
    private String cname;
    private String password;
    private String telephone;
    private String money;

    private Integer version;

    @TableField(exist = false)//声明不是数据库中的字段名
    private List ids;
}

(5)编写dao

package com.czxy.mp.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.czxy.mp.domain.Customer;
import org.apache.ibatis.annotations.Mapper;



@Mapper
public interface CustomerMapper extends BaseMapper {
}

(6)编写测试类

import com.czxy.mp.MybatisPlusApplication;
import com.czxy.mp.domain.Customer;
import com.czxy.mp.mapper.CustomerMapper;
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(classes = MybatisPlusApplication.class)
public class TestDemo01 {
    @Resource
    private CustomerMapper customerMapper;

    @Test
    public void testFindAll() {
        //查询所有
        List list = customerMapper.selectList(null);
        list.forEach(System.out::println);
    }
}

 

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

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

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