1.了解
2.快速入門
2.1.創(chuàng)建數(shù)據(jù)庫(kù)以及表
docker run -id \
-p 3307:3306 \
--name=c_mysql \
-v $PWD/conf:/etc/mysql/conf.d \
-v $PWD/logs:/logs \
-v $PWD/data:/var/lib/mysql \
-e MYSQL_ROOT_PASSWORD=cjchnws1991 \
mysql:5.6
-- 創(chuàng)建測(cè)試表
CREATE TABLE `tb_user` (
`id` bigint(20) NOT NULL AUTO_INCREMENT COMMENT '主鍵ID',
`user_name` varchar(20) NOT NULL COMMENT '用戶名',
`password` varchar(20) NOT NULL COMMENT '密碼',
`name` varchar(30) DEFAULT NULL COMMENT '姓名',
`age` int(11) DEFAULT NULL COMMENT '年齡',
`email` varchar(50) DEFAULT NULL COMMENT '郵箱',
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8;
-- 插入測(cè)試數(shù)據(jù)
INSERT INTO `tb_user` (`id`, `user_name`, `password`, `name`, `age`, `email`) VALUES ('1', 'zhangsan', '123456', '張三', '18', 'test1@itcast.cn');
INSERT INTO `tb_user` (`id`, `user_name`, `password`, `name`, `age`, `email`) VALUES ('2', 'lisi', '123456', '李四', '20', 'test2@itcast.cn');
INSERT INTO `tb_user` (`id`, `user_name`, `password`, `name`, `age`, `email`) VALUES ('3', 'wangwu', '123456', '王五', '28', 'test3@itcast.cn');
INSERT INTO `tb_user` (`id`, `user_name`, `password`, `name`, `age`, `email`) VALUES ('4', 'zhaoliu', '123456', '趙六', '21', 'test4@itcast.cn');
INSERT INTO `tb_user` (`id`, `user_name`, `password`, `name`, `age`, `email`) VALUES ('5', 'sunqi', '123456', '孫七', '24', 'test5@itcast.cn');
2.2.mybatis查詢
不用骨架創(chuàng)建maven工程
<?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>com.lines.mp</groupId>
<artifactId>bj-mybatis-plus</artifactId>
<version>1.0-SNAPSHOT</version>
<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>
<!--簡(jiǎn)化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>
創(chuàng)建子模塊
<?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>bj-mybatis-plus</artifactId>
<groupId>com.lines.mp</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>mybatis-plus-simple</artifactId>
</project>
2.3.mybatis+MP查詢
2.4.Spring+mybatis+MP查詢
2.5.SpringBoot+mybatis+MP查詢
3.通用crud詳解
3.1.插入
3.2.TableField
- 對(duì)象中的屬性名和字段名不一致的問(wèn)題
- 對(duì)象中的屬性字段在表中不存在的問(wèn)題
- 查詢的時(shí)候不想查某些字段
-
上述三條實(shí)際生產(chǎn)不用
3.3.更新
3.4.刪除
實(shí)際使用基本都是根據(jù)id刪除
3.5.查詢
3.6.SQL注入原理
- ISqlInjector接口負(fù)責(zé)SQL注入
- ISqlInjector的實(shí)現(xiàn)類AbstractSqlInjector的inspectInject方法負(fù)責(zé)檢查注入
- inspectInject方法中的關(guān)鍵是methodList.forEach(m -> m.inject(builderAssistant, mapperClass, modelClass, tableInfo));
- AbstractMethod.inject注入自定義方法玩荠,核心方法是 injectMappedStatement(mapperClass, modelClass, tableInfo);
- injectMappedStatement是抽象方法渤愁,看看哪些類實(shí)現(xiàn)了這個(gè)抽象方法
- 發(fā)現(xiàn)BaseMapper所提供的方法都是對(duì)應(yīng)一個(gè)一個(gè)的類,且這些類都實(shí)現(xiàn)了injectMappedStatement方法
- 以SelectById為例薯蝎,生成SQL語(yǔ)句德绿,最終return this.addSelectMappedStatementForTable(mapperClass, getMethod(sqlMethod), sqlSource, tableInfo);
- 最終實(shí)現(xiàn)BaseMapper中的一系列的方法注冊(cè)到meppedStatements中
4.配置
4.1.configLocation
4.2.mapperLocations
4.3.typeAliasesPackage
4.4.mapUnderscoreToCamelCase&cacheEnabled
4.5.DB策略配置
最好的使用場(chǎng)景:表名統(tǒng)一tb_+實(shí)體類名
5.條件構(gòu)造器
5.1.AllEq
5.2.Eq
5.3.模糊查詢
5.4.排序
5.5.邏輯查詢
5.6.select
在MP查詢中昵仅,默認(rèn)查詢所有的字段缓熟,如果有需要也可以通過(guò)select方法進(jìn)行指定字段。6.源代碼
https://gitee.com/baojun521/bj-mybatis-plus.git
https://gitee.com/baojun521/lines-mp-springboot.git