Mybatis-Plus
了解Mybatis-Plus
Mybatis-Plus介绍
· Mybatis-Plus简称(MP)是一个Mybatis的增强工具,在Mybatis的基础上只做增强不做改变,为简化开发提高效率而生
· 官网:https://mybatis.plus/ 或 https://mp.baomidou.com/
代码以及文档
文档地址:https://mybatis.plus/guide/
源码地址:https://github.com/baomidou/mybatis-plus
特性
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-jqGs7DXb-1646572487356)(C:\Users\maybe3032\AppData\Roaming\Typora\typora-user-images\image-20220305165151166.png)]
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-XQ6Gh2Cp-1646572487357)(C:\Users\maybe3032\AppData\Roaming\Typora\typora-user-images\image-20220305165216116.png)]
架构
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-RXVmiJXR-1646572487358)(C:\Users\maybe3032\AppData\Roaming\Typora\typora-user-images\image-20220305165413766.png)]
整合Mybatis-Plus
· 对于Mybatis整合MP有三种常用用法,分别是Mybatis+MP、Spring+Mybatis+MP、SpringBoot+Mybatis+MP
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-MnAdDnr6-1646572487359)(C:\Users\maybe3032\AppData\Roaming\Typora\typora-user-images\image-20220305170000264.png)]
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-MihzkB9T-1646572487360)(C:\Users\maybe3032\AppData\Roaming\Typora\typora-user-images\image-20220305170011376.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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>cn.itcast.mp</groupId>
<artifactId>itcast-mybatis-plus</artifactId>
<version>1.0-SNAPSHOT</version>
<modules>
<module>itcast-mybatis-plus-simple</module>
</modules>
<packaging>pom</packaging>
<dependencies>
<!-- mybatis-plus插件依赖 -->
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus</artifactId>
<version>3.1.1</version>
</dependency>
<!-- MySql -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.47</version>
</dependency>
<!-- 连接池 -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.0.11</version>
</dependency>
<!--简化bean代码的工具包--> <dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
<version>1.18.4</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<version>1.6.4</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
</plugins>
</build>
</project>
Mybatis + MP
· 纯Mybatis与Mybatis-Plus整合
创建Modile
<?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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>itcast-mybatis-plus</artifactId>
<groupId>cn.itcast.mp</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<packaging>jar</packaging>
<artifactId>itcast-mybatis-plus-simple</artifactId>
</project>
· log4j.properties:
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
Mybatis实现查询User
· 编写mybatis-config.xml文件
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
<environments default="development">
<environment id="development">
<transactionManager type="JDBC"/>
<dataSource type="POOLED">
<property name="driver" value="com.mysql.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://127.0.0.1:3306/mp? useUnicode=true&characterEncoding=utf8&autoReconnect=true&allowMultiQuerie s=true&useSSL=false"/>
<property name="username" value="root"/>
<property name="password" value="root"/>
</dataSource>
</environment>
</environments>
<mappers>
<mapper resource="UserMapper.xml"/>
</mappers>
</configuration>
· 编写User实体对象:(这里使用Lombok进行优化bean操作)
package cn.itcast.mp.simple.pojo;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
@Data @NoArgsConstructor
@AllArgsConstructor
public class User {
private Long id;
private String userName;
private String password;
private String name;
private Integer age;
private String email;
}
· 编写UserMapper接口:
package cn.itcast.mp.simple.mapper;
import cn.itcast.mp.simple.pojo.User;
import java.util.List;
public interface UserMapper {
List<User> findAll();
}
· 编写UserMapper.xml文件
<?xml version="1.0" encoding="UTF-8" ?>