本文共 7725 字,大约阅读时间需要 25 分钟。
(简称 MP)是一个 的增强工具,在 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','tom@hedon.com','1',22),(2,'Hedon','Hedon@hedon,com','1',21),(3,'Jerry','jerry@hedon.com','2',18),(4,'White','white@hedon.com','2',30);UNLOCK TABLES;
重点:
org.projectlombok lombok true com.baomidou mybatis-plus-boot-starter 3.3.2
spring.application.name=itcast-mp-springbootspring.datasource.driver-class-name=com.mysql.cj.jdbc.Driverspring.datasource.url=jdbc:mysql://localhost:3306/mpspring.datasource.username=rootspring.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{ }
@RunWith(SpringRunner.class)@SpringBootTestclass ItcastMpSpringbootApplicationTests { @Autowired EmployeeMapper employeeMapper; @Test public void testSearchList(){ Listemployees = 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")@SpringBootApplicationpublic 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{ }
spring.application.name=itcast-mp-springbootspring.datasource.driver-class-name=com.mysql.cj.jdbc.Driverspring.datasource.url=jdbc:mysql://localhost:3306/mpspring.datasource.username=rootspring.datasource.password=root
log4j.rootLogger=DEBUG,A1log4j.appender.A1=org.apache.log4j.ConsoleAppenderlog4j.appender.A1.layout = org.apache.log4j.PatternLayoutlog4j.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)@SpringBootTestclass ItcastMpSpringbootApplicationTests { @Autowired EmployeeMapper employeeMapper; @Test void contextLoads() { } @Test public void testSearchList(){ Listemployees = employeeMapper.selectList(null); for (Employee employee:employees){ System.out.println(employee); } }}
4.0.0 org.springframework.boot spring-boot-starter-parent 2.3.1.RELEASE it.cast.mp itcast-mp-springboot 0.0.1-SNAPSHOT itcast-mp-springboot Demo project for Spring Boot 1.8 org.springframework.boot spring-boot-starter org.springframework.boot spring-boot-starter-test test org.projectlombok lombok true com.baomidou mybatis-plus-boot-starter 3.3.2 org.springframework.boot spring-boot-maven-plugin
转载地址:http://cqded.baihongyu.com/