前提:有mybatis使用经验的最佳
环境:SpringBoot
数据库:MySql
MyBatis-Plus 是基于MyBatis的增强版,保留了MyBatis的所有功能,并基于MyBatis做了很多增强,有利于开发者进行快速开发
<properties>
<java.version>1.8</java.version>
<mysql.version>8.0.19</mysql.version>
<druid.version>1.2.4</druid.version>
<mybatis-plus.version>3.4.3</mybatis-plus.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>${mysql.version}</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid-spring-boot-starter</artifactId>
<version>${druid.version}</version>
</dependency>
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>${mybatis-plus.version}</version>
</dependency>
<!--不是必要的,为了方便演示-->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>cn.hutool</groupId>
<artifactId>hutool-all</artifactId>
<version>5.7.21</version>
</dependency>
</dependencies>
spring:
datasource:
druid:
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://192.168.64.2:3306/mybatis-plus
username: root
password: root
mybatis-plus:
configuration:
log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
CREATE TABLE `mall_order` (
`id` bigint NOT NULL AUTO_INCREMENT,
`name` varchar(255) DEFAULT NULL COMMENT '姓名',
`mobile` varchar(100) DEFAULT NULL COMMENT '手机号',
`address` varchar(255) DEFAULT NULL COMMENT '地址',
`order_status` int DEFAULT NULL COMMENT '订单状态',
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci;
利用在线网站根据sql生成entity/service/mapper,拒绝手写,容易出错https://java.bejson.com/generator/
package com.lglbc.mybatispluslearning;
import cn.hutool.json.JSONUtil;
import com.lglbc.mybatispluslearning.entity.MallOrder;
import com.lglbc.mybatispluslearning.mapper.MallOrderMapper;
import com.lglbc.mybatispluslearning.service.MallOrderService;
import lombok.extern.slf4j.Slf4j;
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
@Slf4j
class MybatisPlusLearningApplicationTests {
@Autowired
private MallOrderMapper mallOrderMapper;
@Test
void contextLoads() {
}
@Test
public void testQuery(){
List<MallOrder> list = mallOrderMapper.selectList(null);
log.info("查询数据:"+ JSONUtil.toJsonStr(list));
}
@Test
public void testInsert(){
MallOrder mallOrder = MallOrder.builder()
.address("中国安徽")
.mobile("110")
.name("皖")
.orderStatus(1)
.build();
mallOrderMapper.insert(mallOrder);
}
@Test
public void testDelete(){
mallOrderMapper.deleteById(1);
}
@Test
public void testUpdate(){
MallOrder mallOrder = MallOrder.builder()
.address("中国安徽 新")
.mobile("110 新")
.name("皖 新")
.orderStatus(2)
.id(2L)
.build();
mallOrderMapper.updateById(mallOrder);
}
}
用途:映射表名
@TableName("mall_order")
public class MallOrder
用途:标识主键
@TableId(type = IdType.AUTO)
/**
* id
*/
private Long id;
用途:标识字段名
/**
* 姓名
*/
@TableField("name")
private String name;
用途:乐观锁
用途:逻辑删除字段处理 value: 逻辑未删除的值 delval:逻辑删除的值
/**
* 删除
*/
@TableLogic(value = "0",delval = "1")
private int delFlag
成长心路 | 优质书单 | 面试资料
牛人故事 | 前沿技术 | 视频教程