1.mybatis-plus整合springboot实现增删改查
第一步、创建项目结构
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/8eda562f9fb3a34ae60967cc006344e2.png)
第二步、引入依赖包
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.6.6</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.qingcheng</groupId>
<artifactId>demo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>demo</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
</properties>
<packaging>jar</packaging>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- 热部署工具 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
<optional>true</optional>
</dependency>
<!-- 自动生成get ,set 的依赖包 -->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<!-- mysql 连驱动-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
<!-- mybatis-plus 启动器-->
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.0.5</version>
</dependency>
<!--mybatisplus 代码生成器-->
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-generator</artifactId>
<version>3.2.0</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
第三步、编写配置application.properties
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost:3306/tx_cloud_pay?useUnicode=true&characterEncoding=utf8
spring.datasource.username=root
spring.datasource.password=123456
第四步、编写启动类
package com.qingcheng.demo;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@MapperScan("com.qingcheng.demo.mapper")
@SpringBootApplication
public class DemoApplication {
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
}
第五步、创建实体类
package com.qingcheng.demo.pojo;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
import lombok.*;
@NoArgsConstructor
@AllArgsConstructor
@Getter
@Setter
@Data
@TableName("t_account")
public class AliAcount {
@TableId(value = "id", type = IdType.ID_WORKER)
private int id;
private String name;
private double money;
}
第六步、 创建mapper
package com.qingcheng.demo.mapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.qingcheng.demo.pojo.AliAcount;
import org.springframework.stereotype.Repository;
@Repository
public interface AliAccountMapper extends BaseMapper<AliAcount> {
}
第七步、 测试增删改查
package com.qingcheng.demo;
import com.qingcheng.demo.mapper.AliAccountMapper;
import com.qingcheng.demo.pojo.AliAcount;
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 DemoApplicationTests {
@Autowired
private AliAccountMapper aliAccountMapper;
@Test
void contextLoads() {
List<AliAcount> aliAcountList = aliAccountMapper.selectList(null);
System.out.println(aliAcountList);
}
@Test
public void add(){
AliAcount aliAcount = new AliAcount();
aliAcount.setMoney(100);
aliAcount.setName("王五");
aliAccountMapper.insert(aliAcount);
}
@Test
public void update(){
AliAcount aliAcount = new AliAcount();
aliAcount.setId(3);
aliAcount.setMoney(1010);
aliAcount.setName("王五");
aliAccountMapper.updateById(aliAcount);
}
@Test
public void detele(){
aliAccountMapper.deleteById(1);
}
}