cgb2007-jingtao day03

1. MybatisPlus

1.1 MP introduction

MyBatis-Plus (MP for short) is an enhancement tool for MyBatis. On the basis of MyBatis, it only enhances and does not change. It is born to simplify development and improve efficiency.

1.2 Features

No intrusion: only make enhancements without making changes. The introduction of it will not affect the existing project and is as smooth as silk.
Low loss: basic CURD will be automatically injected at startup, with basically no loss of performance, direct object-oriented operation and
powerful CRUD operation :Built-in general Mapper and General Service, only a small amount of configuration can realize most of the CRUD operations of a single table, and a more powerful condition constructor, which can meet various usage needs.
Support Lambda form invocation: It is convenient to write various types through Lambda expressions No need to worry about writing wrong fields anymore.
Support primary key automatic generation: support up to 4 primary key strategies (including distributed unique ID generator-Sequence), freely configurable, and perfectly solve primary key problems.
Support ActiveRecord mode: support ActiveRecord form calls , Entity classes only need to inherit the Model class to perform powerful CRUD operations.
Support custom global general operations: support global general method injection (Write once, use anywhere)
Built-in code generator: use code or Maven plug-in to quickly generate Mapper and Model , Service, Controller layer code, support template engine, and more custom configurations are waiting for you to use
Built-in paging plug-in: Based on MyBatis physical paging, developers don’t need to care about specific operations. After configuring the plug-in, writing paging is equivalent to ordinary List query
Paging plug-in supports a variety of databases: supports MySQL, MariaDB, Oracle, DB2, H2, HSQL, SQLite, Postgre, SQLServer and other database
built-in performance analysis plug-ins: can output Sql statements and their execution time, it is recommended to enable this function during development and testing , Can quickly uncover slow queries
Built-in global interception plug-in: Provides full table delete and update operation intelligent analysis and blocking, and can also customize interception rules to prevent misoperation

1.3 ORM thought

1.3.1 Business scenario

A programmer who has been developing for 30 years, he is quite proficient in business, but in order to complete the business, he also needs to write particularly simple SQL statements.
Requirements: Can the development efficiency be effectively improved?

1.3.2 Introduction to ORM

Object Relational Mapping (English: Object Relational Mapping, ORM for short, or O/RM, or O/R mapping) is a programming technique used to implement data conversion between different types of systems in object-oriented programming languages . In effect, it actually creates a "virtual object database" that can be used in programming languages. There are many free and paid ORM products, and some programmers prefer to create their own ORM tools.

Knowledge foreshadowing:
Sql statement is a process-oriented language.sql: select * from user ResultSet result set object~~~~ User objects need to manually encapsulate the results, and the efficiency of development is low.

ORM方式:  以对象的方法操作数据库, **可以实现结果集与对象的自动的映射**  不需要自己手写.

1.4 Description of the working principle of MybatisPlus

1. 对象与哪张表要完成映射     		可以自定义注解进行标识.
2. 对象的属性与表中的字段如何一一对应.		起名时应该写成一样的. 利用特定的注解指定.
3. 工具方法如何简化    MP动态的生成CURD操作的接口,只要其他的Mapper继承接口即可
4. 对象如何转化为SQL语句		    利用对象中的属性及属性的值动态**拼接sql**,之后交给Mybatis(jdbc)去执行
eg.userMapper.insert(user对象)
eg:deptMapper.insert(dept对象)
sql: insert into 表名(字段信息....) values(属性值....)

1.5 MP specific implementation

1.5.1 Import jar package

Note: The Mybatisplus package already contains Mybatis information, so the original package needs to be deleted.

	<!--spring整合mybatis-plus -->
		<dependency>
			<groupId>com.baomidou</groupId>
			<artifactId>mybatis-plus-boot-starter</artifactId>
			<version>3.2.0</version>
		</dependency>

1.5.2 Edit POJO Object

Insert picture description here

1.5.3 Implement interface inheritance

Insert picture description here

1.5.4 Edit YML configuration file

Insert picture description here

1.5.5 Getting Started Case

Use MP mechanism for query
Insert picture description here

1.5.6 Sql statement printing

server:
  port: 8090
  servlet:
    context-path: /
spring:
  datasource:
    #驱动版本问题 高版本需要添加cj关键字  一般可以省略
    #driver-class-name: com.mysql.cj.jdbc.Driver
    url: jdbc:mysql://127.0.0.1:3306/jtdb?serverTimezone=GMT%2B8&useUnicode=true&characterEncoding=utf8&autoReconnect=true&allowMultiQueries=true
    username: root
    password: root

mybatis-plus:
  #别名包定义 Mapper的resultType中只需要写类名 之后自动拼接即可
  type-aliases-package: com.jt.pojo
  #加载指定的xml映射文件
  mapper-locations: classpath:/mybatis/mappers/*.xml
  #开启驼峰映射
  configuration:
    map-underscore-to-camel-case: true


# 实现sql打印
logging:
  level:
    com.jt.mapper: debug

Guess you like

Origin blog.csdn.net/qq_16804847/article/details/109378783