1、快速生成mapper和service
(自动生成简单的单表sql)
2、springboot配置swagger(路径不用加/api)
(1)主pom导包(子pom要引用,可选依赖)
XML
<!-- swagger3用-->
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-boot-starter</artifactId>
<version>3.0.0</version>
</dependency>
<dependency>
<groupId>com.github.xiaoymin</groupId>
<artifactId>knife4j-spring-boot-starter</artifactId>
<version>3.0.3</version>
</dependency>
(2)配置swagger和mvc
Matlab
server:
port: 8081
spring:
# swagger使用
mvc:
path-match:
matching-strategy: ant_path_matcher
datasource:
# 指定连接池的类型
type: com.alibaba.druid.pool.DruidDataSource
druid:
url: "jdbc:mysql://localhost:3306/mybatis?characterEncoding=UTF-8&useSSL=false&useUnicode=true&serverTimezone=Asia/Shanghai"
username: "root"
password: "lht660036"
max-active: 20
# ???
# wall ??druid ???
# stat ??druid?????
filters: "wall,slf4j,stat"
web-stat-filter:
enabled: true
url-pattern: /*
# ??????????
exclusions: "/druid,*.png"
# ???????
stat-view-servlet:
enabled: true
url-pattern: "/druid/*"
reset-enable: true
login-username: admin
login-password: admin
mybatis:
# ??mapper.xml
mapper-locations: "classpath*:mapper/**/*.xml"
# ???
type-aliases-package: "com.lht.project.data.mybatis.entity"
swagger:
base-package: "com.lht.project.data.mybatis.controller"
title: "京东商城在线Api文档"
group-name: "订单商城"
description: "出现bug,请熟读该文档"
name: "刘惠棠帅哥"
url: "https://www.baidu.com"
email: "11111@163.com"
version: "1.0.0"
(3)定义swaggerProperties
java
package com.lht.project.data.mybatis.config;
import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;
@Component
@ConfigurationProperties("swagger")
@Data
public class SwaggerProperties {
private String title;
private String basePackage;
private String groupName;
private String description;
private String name;
private String url;
private String email;
private String version;
}
(4)定义swaggerConfig
java
package com.lht.project.data.mybatis.config;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.oas.annotations.EnableOpenApi;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Contact;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;
import javax.annotation.Resource;
@Configuration
@EnableOpenApi
public class SwaggerConfig {
@Resource
private SwaggerProperties swaggerProperties;
@Bean
public Docket docket(ApiInfo apiInfo){
return new Docket(DocumentationType.OAS_30).apiInfo(apiInfo)
.groupName(swaggerProperties.getGroupName())
.select()
.apis(RequestHandlerSelectors.basePackage(swaggerProperties.getBasePackage()))
.paths(PathSelectors.any())
.build();
}
@Bean
public ApiInfo apiInfo(){
return new ApiInfoBuilder()
.title(swaggerProperties.getTitle())
.description(swaggerProperties.getDescription())
.version(swaggerProperties.getVersion())
.contact(new Contact(swaggerProperties.getName(), swaggerProperties.getUrl(), swaggerProperties.getEmail()))
.build();
}
}
(5)controller调用
3、配置junit5、日志(springboot自带)
4、日志的级别
info(正常信息)、debug、error(异常信息)
5、mybatisplus用了之后mybatis取别名就用不了了
6、目前导包都是写在父工程dependencyManagement里面,然后子类去调用的
7、springboot整合mybatisplus(自动有很多sql单表)
(1)父pom导入依赖(子也要引用)
XML
<!-- mybatisplus-->
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.5.2</version>
</dependency>
(2)mapper里面继承basemapper
java
public interface BrandMapper extends BaseMapper<Brand> {
}
(3)在实体类上映射好数据库表名与类名、属性名和字段名
java
package com.lht.project.data.mybatis.entity;
import java.util.Date;
import com.baomidou.mybatisplus.annotation.*;
import lombok.Data;
@Data
@TableName("sys_brand")
public class Brand {
/**
* 主键
*/
@TableId(value = "brand_id",type = IdType.ASSIGN_ID)
private Integer brandId;
/**
* 中文名
*/
@TableField("chinese_name")
private String chineseName;
/**
* 英文名
*/
@TableField("english_name")
private String englishName;
/**
* 产地id
*/
@TableField("producer_id")
private Integer producerId;
/**
* 备注
*/
@TableField("notes")
private String notes;
/**
* 图案(string)代替
*/
@TableField("logo")
@TableLogic(value = "1",delval = "0")
private String logo;
/**
* 创建时间
*/
@TableField("create_date")
private Date createDate;
//动态select去除列用
public static final String COL_CHINESE_NAME="chinese_name";
public static final String COL_STATUS="status";
}
8、分页查询
9、 QueryWrapper条件构造器
XML
QueryWrapper<Brand> queryWrapper = new QueryWrapper<>();
queryWrapper.select().eq().gt()
queryWrapper相当于where
|------------------|--------------|
| select() | 过滤列 |
| eq() | = |
| ne() | != |
| allEq() | 检查所有参数是否相等 |
| gt() | > |
| ge() | >= |
| lt | < |
| le | <= |
| between() | 范围 |
| notBetween() | 非范围 |
| like | 模糊查询 |
| notLike | 不进行模糊匹配查询的条件 |
| likeLeft | %k |
| likeRight | k% |
| in | 多条件 |
(1)用法
1)实体类写固定值
2)写构造器
java
package com.lht.project.data.mybatis.mapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.lht.project.data.mybatis.entity.Brand;
import lombok.extern.slf4j.Slf4j;
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;
import javax.annotation.Resource;
import static org.junit.jupiter.api.Assertions.*;
@SpringBootTest
@Slf4j
public class BrandMapperTest {
@Resource
private BrandMapper brandMapper;
@Test
void selectOne() {
//条件构造器
QueryWrapper<Object> qw = new QueryWrapper<>();
qw.eq(Brand.COL_CHINESE_NAME,"张三").or().eq(Brand.COL_STATUS,"1");
}
}
12、选中之后control+shirt+U强转为大写
13、like索引必失效
14、创建全文索引(索引就不会失效了)
sql
CREATE FULLTEXT INDEX idx_product_name 0N pms_product (product_name);
15、数据库表,主表增删改,用InnoDB(有事务,查询慢所以不用),从表查询,用MyISAM(不支持事务,查询效率高)
16、排序,分组,过滤列
17、条件函数一般与groupBy配合使用
18、测试list里面存放map
java
@Test
void selectMaps() {
QueryWrapper<Brand> qw = new QueryWrapper<>();
qw.select("COUNT(*) product_count","AVG(product_price) product_avg")
.groupBy("producer_id");
List<Map<String, Object>> maps = brandMapper.selectMaps(qw);
log.info("测试分组函数");
}
19、封装分页
(1)添加配置类
java
package com.lht.project.data.mybatis.config;
import com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor;
import com.baomidou.mybatisplus.extension.plugins.inner.PaginationInnerInterceptor;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
//注册所有的mapper接口
@MapperScan("com.lht.**.mapper")
public class MybatisPlusConfig {
/**
* 乐观锁
* @param paginationInnerInterceptor
* @return
*/
@Bean
public MybatisPlusInterceptor mybatisPlusInterceptor(PaginationInnerInterceptor paginationInnerInterceptor){
MybatisPlusInterceptor mybatisPlusInterceptor = new MybatisPlusInterceptor();
mybatisPlusInterceptor.addInnerInterceptor(paginationInnerInterceptor);
return mybatisPlusInterceptor;
}
/**
* 分页插件注册
* @return
*/
@Bean
public PaginationInnerInterceptor paginationInnerInterceptor(){
PaginationInnerInterceptor paginationInnerInterceptor = new PaginationInnerInterceptor();
paginationInnerInterceptor.setOverflow(true);
paginationInnerInterceptor.setMaxLimit(500L);
return paginationInnerInterceptor;
}
}
(2)调用执行
java
void selectPage(){
Page<Brand> brandPage = brandMapper.selectPage(new Page<>(1, 10), null);
log.info("总页数===>{}: 测试占位符{}",brandPage.getPages(),"1111");
log.info("核心数据{}",brandPage.getRecords());
log.info("总条数{}",brandPage.getTotal());
}
20、" "那里如果反方向的话就会判断多次
21、typeId的类型
22、配置全局雪花id+逻辑删除
(1)全局(不推荐)
(2)局部
23、配置逻辑删除(都要加注解)
(1)全局
XML
mybatis-plus:
global-config:
db-config:
logic-delete-field: flag # 全局逻辑删除的实体字段名(since 3.3.0,配置后可以忽略不配置步骤2)
logic-delete-value: 1 # 逻辑已删除值(默认为 1)
logic-not-delete-value: 0 # 逻辑未删除值(默认为0)
(2)局部 (就是写value)
(3)作用:自动加状态码(都要查的时候不适用)
24、乐观锁与悲观锁
1.悲观锁是当线程拿到资源时,就对资源上锁,并在提交后,才释放锁资源,其他线程才能使用资源。
2.乐观锁是当线程拿到资源时,上乐观锁,在提交之前,其他的锁也可以操作这个资源,当有冲突的时候,并发机制会保留前一个提交,打回后一个提交,让后一个线程重新获取资源后,再操作,然后提交。和git上传代码一样,两个线程都不是直接获取资源本身,而是先获取资源的两个copy版本,然后在这两个copy版本上修改。
3.悲观锁和乐观锁在并发量低的时候,性能差不多,但是在并发量高的时候,乐观锁的性能远远优于悲观锁
全文们常用的synchronized是悲观锁,lock是乐观锁
25、配置乐观锁
(1)配置类
java
package com.lht.project.data.mybatis.config;
import com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor;
import com.baomidou.mybatisplus.extension.plugins.inner.PaginationInnerInterceptor;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
//注册所有的mapper接口
@MapperScan("com.lht.**.mapper")
public class MybatisPlusConfig {
/**
* 乐观锁
* @param paginationInnerInterceptor
* @return
*/
@Bean
public MybatisPlusInterceptor mybatisPlusInterceptor(PaginationInnerInterceptor paginationInnerInterceptor){
MybatisPlusInterceptor mybatisPlusInterceptor = new MybatisPlusInterceptor();
mybatisPlusInterceptor.addInnerInterceptor(paginationInnerInterceptor);
return mybatisPlusInterceptor;
}
/**
* 分页插件注册
* @return
*/
@Bean
public PaginationInnerInterceptor paginationInnerInterceptor(){
PaginationInnerInterceptor paginationInnerInterceptor = new PaginationInnerInterceptor();
paginationInnerInterceptor.setOverflow(true);
paginationInnerInterceptor.setMaxLimit(500L);
return paginationInnerInterceptor;
}
}