Mybatis plus learning and CRUD cases

Posted by Brunda on Sun, 27 Oct 2019 11:18:41 +0100

Understanding mybatis plus

describe

MyBatis plus (MP for short) is an enhancement tool of MyBatis. Based on MyBatis, only enhancements are made and no changes are made. It is generated to simplify development and improve efficiency.

Vision achieved

Our vision is to become the best partner of MyBatis, just like 1P and 2P in the soul duel, matching with friends and double the efficiency.

Features

1. No invasion: only strengthen and do not change, the introduction of it will not affect the existing project, as smooth as silk
2. Powerful crud operation: built in general Mapper and general Service, most CRUD operations of a single table can be realized only through a small amount of configuration, and more powerful condition builder can meet various use requirements.
3. Support Lambda form Calling: through Lambda expression, it is convenient to write all kinds of query conditions without worrying about wrong field writing.

Fast start

Create database

First you need to create a database and data table in MySQL (the database I use)
Create database sql as follows:

CREATE DATABASE database name

Create the data table sql as follows:

CREATE TABLE `product` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `productName` varchar(50) NOT NULL,
  `price` double NOT NULL,
  `weight` int(11) NOT NULL,
  PRIMARY KEY (`id`)
)
INSERT INTO product (id, product_name, price,weight) VALUES (1, 'HUAWEI p20',2300,24);
INSERT INTO product (id, product_name, price,weight) VALUES (2, 'HUAWEI p30', 3600,32);
INSERT INTO product (id, product_name, price,weight) VALUES (3, 'HUAWEI mate20',4600,50);
INSERT INTO product (id, product_name, price,weight) VALUES (4, 'HUAWEI mate30',5600,60);
INSERT INTO product (id, product_name, price,weight) VALUES (5, 'HUAWEI p20 pro',3000,36);
INSERT INTO product (id, product_name, price,weight) VALUES (6, 'HUAWEI p30 pro',4000,40);
Create Maven project

If you don't know how to use maven, you can go to my home page to view the tutorial about maven, which will not be explained here. Here we use the SpringBpoot method. We use STS to create a maven project. We mainly modify the pom.xml file:

<!-- Spring Boot Start parent dependency -->
	<parent>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-parent</artifactId>
		<version>2.0.4.RELEASE</version>
	</parent>
	<!-- Project global properties -->
	<properties>
		<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
		<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
		<java.version>1.8</java.version>
		<mybatis-spring-boot>1.2.0</mybatis-spring-boot>
		<mysql-connector>5.1.39</mysql-connector>
	</properties>
	<dependencies>

		<!-- Spring Boot Web rely on -->
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-web</artifactId>
		</dependency>
		<!--To configure mybatis-plus rely on-->
        <dependency>
			<groupId>com.baomidou</groupId>
			<artifactId>mybatis-plus-boot-starter</artifactId>
			<version>3.1.1</version>
		</dependency>
		<!-- Spring Boot Test rely on -->
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-test</artifactId>
			<scope>test</scope>
		</dependency>
		<!-- To configure lombox rely on -->
		<dependency>
			<groupId>org.projectlombok</groupId>
			<artifactId>lombok</artifactId>
			<optional>true</optional>
			<version>1.16.12</version>
		</dependency>

		<!-- Spring Boot devtools Hot deployment dependency -->
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-devtools</artifactId>
		</dependency>
		
		<!-- MySQL Connection driven dependency -->
		<dependency>
			<groupId>mysql</groupId>
			<artifactId>mysql-connector-java</artifactId>
			<version>${mysql-connector}</version>
		</dependency>
		

		<!-- Junit Unit test class -->
		<dependency>
			<groupId>junit</groupId>
			<artifactId>junit</artifactId>
			<version>4.12</version>
		</dependency>
	</dependencies>
Configure data sources

Add the application.properties file in the src/main/resources folder. The file content is as follows:

spring.datasource.url=jdbc:mysql://localhost:3306/Database name?useUnicode=true&characterEncoding=utf8
spring.datasource.username=User name
spring.datasource.password=Password
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
Write main program entry

Note: mapperscan (write your own path ")

@SpringBootApplication
@MapperScan("com.zhaosong.dao")
public class Application {
	public static void main(String[] args) {
		SpringApplication.run(Application.class, args);
	}
}
Write entity class
@Data//get,set,toString and other methods will be automatically produced during compilation.
@AllArgsConstructor//Production reference method
@NoArgsConstructor//Production without parameters
public class Product {
	//Primary key
	@TableId(type =IdType.AUTO)
	private Integer id;
	//Product name
	private String productName;
	//Product price
	private Double price;
	//Product weight
	private Integer weight;
}
Write dao interface
public interface ProductDao extends BaseMapper<Product> {
}

Write unit test class
@RunWith(SpringRunner.class)
@SpringBootTest
public class ProductTest {
	@Resource
	private ProductDao productDao;

	@Test
	public void getProduct() {
		List<Product> pr = productDao.selectList(null);
		pr.forEach(w -> {
			System.out.println(w);
		});
	}
}

A simple query is finished here. The following implements a unit test class based on Fuzzy name query

@Test
	public void getProductName() {
		QueryWrapper<Product> qw=new QueryWrapper<Product>();
		qw.like("product_name", "%pro%");
		List<Product> pr = productDao.selectList(qw);
		pr.forEach(System.out::println);
	}
Writing unit test classes for add operations
@RunWith(SpringRunner.class)
@SpringBootTest
public class ProductTest {
	@Resource
	private ProductDao productDao;
	@Test
	public void addProduct() {
		Product pr=new Product(7,"HUAWEI X",13000.0,100);
		int count = productDao.insert(pr);
		System.out.println("Number of entries added"+count);
	}
}
Write unit test class for modification
//Implement single modification according to id
	@Test
	public void updateProduct() {
		Product pr=new Product(7,"HUAWEI Xs",13000.0,100);
		int count = productDao.updateById(pr);
		System.out.println("Number of entries added"+count);
	}

Write unit test class for delete operation
@Test
	public void delProductId() {
		int count = productDao.deleteById(7);
		System.out.println("Number of entries added"+count);
	}

Topics: Spring Mybatis MySQL Database