• MyBatis-Plus 框架 2022-8-2


    持久层

    操作数据存储的层

    与什么数据库无关

    与什么技术无关

    ORM

    O(Object)R(Relationship)M(Mapping)对象关系映射

    MyBatis 框架是一款持久层的ORM框架

    MyBatis 与 JDBC 的关系

    MyBatis 底层是JDBC,基于反射技术在运行时调用JDBC,实现数据库编程

    苞米豆 baomidou (公司名)

    MyBatis-Plus (baomidou.com)

    MyBatis-Plus 与 SpringBoot 的关系

    MyBatis-Plus 框架能够与 SpringBoot 框架无缝整合

    学习一门框架

    1.搭建环境

            (1)安装哪些依赖(三方库)

            (2)需要哪些配置

            (3)框架提供了哪些API,怎么用

    2.如何应用

    3.研究它的底层

    创建新的工程

    需要配置web目录(或者叫webapp),放在src/main/ 里面

     把web删掉后需要 点击apply

     

    安装JSP依赖

    1. <dependency>
    2. <groupId>org.apache.tomcat.embedgroupId>
    3. <artifactId>tomcat-embed-jasperartifactId>
    4. <scope>providedscope>
    5. dependency>
    6. <dependency>
    7. <groupId>jstlgroupId>
    8. <artifactId>jstlartifactId>
    9. <version>1.2version>
    10. <scope>providedscope>
    11. dependency>
    12. <dependency>
    13. <groupId>taglibsgroupId>
    14. <artifactId>standardartifactId>
    15. <version>1.1.2version>
    16. <scope>providedscope>
    17. dependency>

    还要一个mysql 连接 java 的依赖

    1. <dependency>
    2. <groupId>mysqlgroupId>
    3. <artifactId>mysql-connector-javaartifactId>
    4. <version>5.1.47version>
    5. dependency>

    配置maven 打包web 目录

    1. <resources>
    2. <resource>
    3. <directory>src/main/webdirectory>
    4. <targetPath>META-INF/resourcestargetPath>
    5. <includes>
    6. <include>**/*.*include>
    7. includes>
    8. resource>
    9. <resource>
    10. <directory>src/main/resourcesdirectory>
    11. <includes>
    12. <include>**/*.xmlinclude>
    13. <include>**/*.propertiesinclude>
    14. includes>
    15. resource>
    16. resources>

    更改一下驱动会爆红 去掉 .cj 

    1. spring.application.name=springboot-mybatisplus-01
    2. #com.mysql.cj.jdbc.Driver ??? 8.x??????????
    3. #com.mysql.jdbc.Driver ???5.x????????
    4. spring.datasource.driver-class-name=com.mysql.jdbc.Driver
    5. spring.datasource.name=defaultDataSource
    6. spring.datasource.url=jdbc:mysql://localhost:3306/shop?useUnicode=true&characterEncoding=utf-8&useSSL=false\
    7. &serverTimezone=UTC
    8. spring.datasource.username=root
    9. spring.datasource.password=root
    10. server.port=8080
    11. spring.mvc.view.prefix=/jsp/
    12. spring.mvc.view.suffix=.jsp

     MyBatis-Plus 替代了原生的JDBC

    代码写在工程的哪个包里面

    dao 包 / mapper 包

    如果写的是用原生JDBC写包名建议叫 dao 包

    用框架写的包名建议叫 mapper 包

    创建持久层的子包

    先把user实体类和baseEntity实体类弄过来

    在mapper子包中定义接口

    在xml文件中编写SQL语句

    xml文件写在  resources/mapper

     xml 文件头 

    1. mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
    2. "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
    3. <mapper>
    4. mapper>

     

     

    将接口的全局路径 粘贴 xml 文件中

    安装插件,方便切换

     File - Settings - Plugins - Marketplace 插件市场 

    点一下小鸟就切换了

    需要绑定路径才出来小鸟 

     把XML 文件中的

    
    
    

    删掉

    Alt + Enter 会自动在XML生成 insert

     

     实现接口与xml 绑定后快速访问

    检查接口中的抽象方法在绑定xml 文件 是否有对应的标签绑定

     

     让框架发现使用在启动类上使用

    @MapperScan("mapper包的全路径")注解,会扫描这个包
    

    如何拿到 Mapper接口的实现类?

            拿不到

            为什么拿不到(因为没有物理文件,因为它存在于JVM内存中)

    如何拿到框架穿件的Mapper接口的实现类的对象?

            框架说:实现类你拿不到,对象我帮你创建好放在内存中,你直接拿对象

    public class UserMapperImpl implements UserMapper {

            @Override

            public int insert (User user)throws Exception{

                    根据接口的绑定关系寻址 xml 文件

                    根据方法的绑定关系寻址绑定的SQL 语句        

                    获取连接

                    预编译SQl        

                    填充参数

                    执行SQL        

            }

    }

    UserMapper userMapper = new UserMapperImpl

    图片代码有修改过

     

     

    拿对象

    测试类

    添加

     

    添加日志文件 

    运行时 方便查看sql 语句 错误

    mybatis-plus.configuration.log-impl=org.apache.ibatis.logging.stdout.StdOutImpl
    

    删除

     

     

    更新

     

     

    MyBatis 写 SQL 语句的几种方式

    1.在绑定 XML 文件中写

           

           

           

           

    上面就是第一种

    2.不需要 XML 文件,使用注解写

            @insert

            @Update

            @Delete

            @Select

    动态SQL 语句还是 xml 文件中写比较方便

    3.不绑定 XML 文件

    !!!!!重点用这种 

    pom.xml

    安装JSP依赖

    1. <dependency>
    2. <groupId>org.apache.tomcat.embedgroupId>
    3. <artifactId>tomcat-embed-jasperartifactId>
    4. <scope>providedscope>
    5. dependency>
    6. <dependency>
    7. <groupId>jstlgroupId>
    8. <artifactId>jstlartifactId>
    9. <version>1.2version>
    10. <scope>providedscope>
    11. dependency>
    12. <dependency>
    13. <groupId>taglibsgroupId>
    14. <artifactId>standardartifactId>
    15. <version>1.1.2version>
    16. <scope>providedscope>
    17. dependency>

    mysql 连接 java 的依赖

    1. <dependency>
    2. <groupId>mysqlgroupId>
    3. <artifactId>mysql-connector-javaartifactId>
    4. <version>5.1.47version>
    5. dependency>

    1. <resources>
    2. <resource>
    3. <directory>src/main/webdirectory>
    4. <targetPath>META-INF/resourcestargetPath>
    5. <includes>
    6. <include>**/*.*include>
    7. includes>
    8. resource>
    9. <resource>
    10. <directory>src/main/resourcesdirectory>
    11. <includes>
    12. <include>**/*.xmlinclude>
    13. <include>**/*.propertiesinclude>
    14. includes>
    15. resource>
    16. resources>

    把mapper全路径放进去

     

     

     

     在application.properties

     

    1. spring.application.name=springboot-mybatisplus-02
    2. spring.datasource.driver-class-name=com.mysql.jdbc.Driver
    3. spring.datasource.name=defaultDataSource
    4. spring.datasource.url=jdbc:mysql://localhost:3306/shop?useUnicode=true&characterEncoding=utf-8&useSSL=false\
    5. &serverTimezone=UTC
    6. spring.datasource.username=root
    7. spring.datasource.password=root
    8. mybatis-plus.configuration.log-impl=org.apache.ibatis.logging.stdout.StdOutImpl
    9. server.port=8080
    10. spring.mvc.view.prefix=/jsp/
    11. spring.mvc.view.suffix=.jsp

    1. package com.iweb.springbootmybatisplus02;
    2. import com.baomidou.mybatisplus.core.toolkit.Assert;
    3. import com.iweb.springbootmybatisplus02.entity.Animal;
    4. import com.iweb.springbootmybatisplus02.mapper.AnimalMapper;
    5. import org.junit.jupiter.api.Test;
    6. import org.springframework.boot.test.context.SpringBootTest;
    7. import javax.annotation.Resource;
    8. import java.util.ArrayList;
    9. import java.util.HashMap;
    10. import java.util.List;
    11. @SpringBootTest
    12. public class AnimalMapperTest {
    13. @Resource
    14. private AnimalMapper animalMapper;
    15. @Test
    16. public void insert(){
    17. Animal animal = new Animal();
    18. animal.setName("皮卡丘");
    19. animal.setSex("男");
    20. animal.setLevel("100");
    21. animal.setPrice("10000");
    22. animal.setStock("1");
    23. int row = animalMapper.insert(animal);
    24. Assert.isTrue(row>0,"测试失败");
    25. }
    26. @Test
    27. public void deleteById(){
    28. animalMapper.deleteById(1);
    29. }
    30. @Test
    31. public void deleteByMap(){
    32. HashMap map = new HashMap<>();
    33. map.put("name","animal_user_0");
    34. animalMapper.deleteByMap(map);
    35. }
    36. @Test
    37. public void deleteBathlds(){
    38. List idList = new ArrayList<>();
    39. idList.add(2);
    40. idList.add(3);
    41. idList.add(4);
    42. animalMapper.deleteBatchIds(idList);
    43. }
    44. @Test
    45. public void updateById(){
    46. Animal animal = new Animal();
    47. animal.setId(6);
    48. animal.setName("杰尼龟");
    49. animalMapper.updateById(animal);
    50. }
    51. @Test
    52. public void selectById(){
    53. System.out.println(animalMapper.selectById(5));
    54. }
    55. @Test
    56. public void selectBatchIds(){
    57. List idList = new ArrayList<>();
    58. idList.add(5);
    59. idList.add(6);
    60. idList.add(7);
    61. System.out.println(animalMapper.selectBatchIds(idList));
    62. }
    63. @Test
    64. public void selectByMap(){
    65. HashMap map = new HashMap<>();
    66. // map.put("name","杰尼龟");
    67. map.put("sex","男");
    68. List list = animalMapper.selectByMap(map);
    69. for (int i = 0; i < list.size(); i++) {
    70. System.out.println(list.get(i));
    71. }
    72. }
    73. }

  • 相关阅读:
    如何使用家庭网络运行Aleo Prover
    GMS地下水数值模拟及溶质(包含反应性溶质)运移模拟技术
    AIGC技术周报|图灵测试不是AGI的智力标准;SegGPT:在上下文中分割一切;ChatGPT能玩好文字游戏吗?
    Autosar CAN硬件配置-Tc27x基于Davinci Cfg
    【计算机视觉】OpenCV3编程入门-笔记(一)
    Linux下的的GDB调试技巧一 —— 基础知识和介绍
    【初试404分】杭电843学长经验分享
    cpolar内网穿透
    数据集笔记:芝加哥共享单车OD数据
    java计算机毕业设计航帆学院网站MyBatis+系统+LW文档+源码+调试部署
  • 原文地址:https://blog.csdn.net/ZSDLZ37/article/details/126114523