MyBatis-Plus(简称 MP)是一个 MyBatis 的增强工具,在 MyBatis 的基础上只做增强不做改变,为简化开发、提高效率而生。
CREATE DATABASE mp;
USE mp;
DROP TABLE IF EXISTS `tbl_employee`;
CREATE TABLE `tbl_employee` (
`id` int NOT NULL AUTO_INCREMENT,
`last_name` varchar(45) DEFAULT NULL,
`email` varchar(45) DEFAULT NULL,
`gender` char(1) DEFAULT NULL,
`age` int DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=5 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci;
LOCK TABLES `tbl_employee` WRITE;
INSERT INTO `tbl_employee` VALUES (1,'Tom','[email protected]','1',22),(2,'Hedon','Hedon@hedon,com','1',21),(3,'Jerry','[email protected]','2',18),(4,'White','[email protected]','2',30);
UNLOCK TABLES;
重点:
<dependency>
<groupId>org.projectlombokgroupId>
<artifactId>lombokartifactId>
<optional>trueoptional>
dependency>
<dependency>
<groupId>com.baomidougroupId>
<artifactId>mybatis-plus-boot-starterartifactId>
<version>3.3.2version>
dependency>
spring.application.name=itcast-mp-springboot
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost:3306/mp
spring.datasource.username=root
spring.datasource.password=root
@MapperScan
注解,扫描 Mapper 文件夹package it.cast.mp.itcastmpspringboot.pojo;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.ToString;
/**
* @author Hedon Wang
* @create 2020-06-24 22:53
*/
@Data
@AllArgsConstructor
@NoArgsConstructor
@ToString
@TableName(value = "tbl_employee")
public class Employee {
@TableId(value = "id",type = IdType.AUTO)
private Integer id ; // int
private String lastName;
private String email ;
private Integer gender;
private Integer age ;
private Double salary ;
}
package it.cast.mp.itcastmpspringboot.mapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import it.cast.mp.itcastmpspringboot.pojo.Employee;
/**
* @author Hedon Wang
* @create 2020-06-25 00:25
*/
public interface EmployeeMapper extends BaseMapper<Employee> {
}
@RunWith(SpringRunner.class)
@SpringBootTest
class ItcastMpSpringbootApplicationTests {
@Autowired
EmployeeMapper employeeMapper;
@Test
public void testSearchList(){
List<Employee> employees = employeeMapper.selectList(null);
for (Employee employee:employees){
System.out.println(employee);
}
}
}
package it.cast.mp.itcastmpspringboot;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@MapperScan("it.cast.mp.itcastmpspringboot.mapper")
@SpringBootApplication
public class ItcastMpSpringbootApplication {
public static void main(String[] args) {
SpringApplication.run(ItcastMpSpringbootApplication.class, args);
}
}
package it.cast.mp.itcastmpspringboot.pojo;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.ToString;
/**
* @author Hedon Wang
* @create 2020-06-24 22:53
*/
@Data
@AllArgsConstructor
@NoArgsConstructor
@ToString
@TableName(value = "tbl_employee")
public class Employee {
@TableId(value = "id",type = IdType.AUTO)
private Integer id ; // int
private String lastName;
private String email ;
private Integer gender;
private Integer age ;
private Double salary ;
}
package it.cast.mp.itcastmpspringboot.mapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import it.cast.mp.itcastmpspringboot.pojo.Employee;
/**
* @author Hedon Wang
* @create 2020-06-25 00:25
*/
public interface EmployeeMapper extends BaseMapper<Employee> {
}
spring.application.name=itcast-mp-springboot
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost:3306/mp
spring.datasource.username=root
spring.datasource.password=root
log4j.rootLogger=DEBUG,A1
log4j.appender.A1=org.apache.log4j.ConsoleAppender
log4j.appender.A1.layout = org.apache.log4j.PatternLayout
log4j.appender.A1.layout.ConversionPattern=[%t] [%c]-[%p] %m%n
package it.cast.mp.itcastmpspringboot;
import it.cast.mp.itcastmpspringboot.mapper.EmployeeMapper;
import it.cast.mp.itcastmpspringboot.pojo.Employee;
import org.junit.jupiter.api.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.context.junit4.SpringRunner;
import java.util.List;
@RunWith(SpringRunner.class)
@SpringBootTest
class ItcastMpSpringbootApplicationTests {
@Autowired
EmployeeMapper employeeMapper;
@Test
void contextLoads() {
}
@Test
public void testSearchList(){
List<Employee> employees = employeeMapper.selectList(null);
for (Employee employee:employees){
System.out.println(employee);
}
}
}
<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.0modelVersion>
<parent>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-parentartifactId>
<version>2.3.1.RELEASEversion>
<relativePath/>
parent>
<groupId>it.cast.mpgroupId>
<artifactId>itcast-mp-springbootartifactId>
<version>0.0.1-SNAPSHOTversion>
<name>itcast-mp-springbootname>
<description>Demo project for Spring Bootdescription>
<properties>
<java.version>1.8java.version>
properties>
<dependencies>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starterartifactId>
dependency>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-testartifactId>
<scope>testscope>
dependency>
<dependency>
<groupId>org.projectlombokgroupId>
<artifactId>lombokartifactId>
<optional>trueoptional>
dependency>
<dependency>
<groupId>com.baomidougroupId>
<artifactId>mybatis-plus-boot-starterartifactId>
<version>3.3.2version>
dependency>
dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-maven-pluginartifactId>
plugin>
plugins>
build>
project>