当前位置: 首页 > news >正文

网站登录 退出怎么做如何提高网站在搜索引擎中的排名

网站登录 退出怎么做,如何提高网站在搜索引擎中的排名,做网站连接数据库怎么显示图片,网站搜索引擎优化案例目录 六、插件 6.1、分页插件 6.1.1、添加配置类 6.1.2、测试 6.2、xml自定义分页 6.2.1、UserMapper中定义接口方法 6.2.2、UserMapper.xml中编写SQL 6.2.3、测试 6.3、乐观锁 6.3.1、场景 6.3.2、乐观锁与悲观锁 6.3.3、模拟修改冲突 数据库中增加商品表 添加数…

目录

六、插件

6.1、分页插件

6.1.1、添加配置类

6.1.2、测试

6.2、xml自定义分页

6.2.1、UserMapper中定义接口方法

6.2.2、UserMapper.xml中编写SQL

6.2.3、测试

6.3、乐观锁

6.3.1、场景

6.3.2、乐观锁与悲观锁

6.3.3、模拟修改冲突

数据库中增加商品表

添加数据

添加实体

添加mapper

测试

6.3.4、乐观锁实现流程

6.3.5、Mybatis-Plus实现乐观锁

修改实体类

添加乐观锁插件配置

测试修改冲突

优化流程

七、通用枚举

7.1、数据库表添加字段sex

7.2、创建通用枚举类型

7.3、配置扫描通用枚举

7.4、测试

八、代码生成器

8.1、引入依赖

8.2、快速生成

九、多数据源

9.1、创建数据库及表

9.2、引入依赖

9.3、配置多数据源

9.4、创建用户service

9.5、创建商品service

9.6、测试

十、MyBatisX插件


六、插件

6.1、分页插件

MyBatis Plus自带分页插件,只要简单的配置即可实现分页功能

6.1.1、添加配置类

@Configuration
@MapperScan("com.oracle.mapper")
public class MybatisPlusConfig {@Beanpublic MybatisPlusInterceptor mybatisPlusInterceptor() {MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();interceptor.addInnerInterceptor(new PaginationInnerInterceptor(DbType.MYSQL));return interceptor;}}

6.1.2、测试

@Test
public void testPage(){// 设置分页参数Page<User> page = new Page<>(1, 5);userMapper.selectPage(page, null);// 获取分页数据List<User> list = page.getRecords();list.forEach(System.out::println);System.out.println("当前页:"+page.getCurrent());System.out.println("每页显示的条数:"+page.getSize());System.out.println("总记录数:"+page.getTotal());System.out.println("总页数:"+page.getPages());System.out.println("是否有上一页:"+page.hasPrevious());System.out.println("是否有下一页:"+page.hasNext());
}

6.2、xml自定义分页

6.2.1、UserMapper中定义接口方法

/*** 根据年龄查询用户列表,分页显示* @param page 分页对象,xml中可以从里面进行取值,传递参数 Page 即自动分页,必须放在第一位* @param age 年龄* @return*/
IPage<User> selectPageVo(@Param("page") Page<User> page, @Param("age") Integer age);

6.2.2、UserMapper.xml中编写SQL

<!--SQL片段,记录基础字段-->
<sql id="BaseColumns">id,username,age,email</sql><select id="selectPageVo" resultType="com.oracle.pojo.User">SELECT <include refid="BaseColumns"></include> FROM t_user WHERE age > #{age}
</select>

6.2.3、测试

@Test
public void testSelectPageVo(){// 设置分页参数Page<User> page = new Page<>(1, 5);userMapper.selectPageVo(page, 20);// 获取分页数据List<User> list = page.getRecords();list.forEach(System.out::println);System.out.println("当前页:"+page.getCurrent());System.out.println("每页显示的条数:"+page.getSize());System.out.println("总记录数:"+page.getTotal());System.out.println("总页数:"+page.getPages());System.out.println("是否有上一页:"+page.hasPrevious());System.out.println("是否有下一页:"+page.hasNext());
}

6.3、乐观锁

6.3.1、场景

一件商品,成本价是80元,售价是100元。老板先是通知小李,说你去把商品价格增加50元。小李正在玩游戏,耽搁了一个小时。正好一个小时后,老板觉得商品价格增加到150元,价格太高,可能会影响销量。又通知小王,你把商品价格降低30元。


此时,小李和小王同时操作商品后台系统。小李操作的时候,系统先取出商品价格100元;小王也在操作,取出的商品价格也是100元。小李将价格加了50元,并将100+50=150元存入了数据库;小王将商品减了30元,并将100-30=70元存入了数据库。是的,如果没有锁,小李的操作就完全被小王的覆盖了。


现在商品价格是70元,比成本价低10元。几分钟后,这个商品很快出售了1千多件商品,老板亏1万多。

6.3.2、乐观锁与悲观锁

上面的故事,如果是乐观锁,小王保存价格前,会检查下价格是否被人修改过了。如果被修改过了,则重新取出的被修改后的价格,150元,这样他会将120元存入数据库。


如果是悲观锁,小李取出数据后,小王只能等小李操作完之后,才能对价格进行操作,也会保证最终的价格是120元。

6.3.3、模拟修改冲突

数据库中增加商品表
CREATE TABLE t_product
(id BIGINT(20) NOT NULL COMMENT '主键ID',NAME VARCHAR(30) NULL DEFAULT NULL COMMENT '商品名称',price INT(11) DEFAULT 0 COMMENT '价格',VERSION INT(11) DEFAULT 0 COMMENT '乐观锁版本号',PRIMARY KEY (id)
);

添加数据
INSERT INTO t_product (id, NAME, price) VALUES (1, '外星人笔记本', 100);

添加实体
package com.oracle.pojo;import lombok.Data;@Data
public class Product {private Long id;private String name;private Integer price;private Integer version;
}

添加mapper
public interface ProductMapper extends BaseMapper<Product> {}

测试
@Test
public void testConcurrentUpdate() {// 1、小李Product p1 = productMapper.selectById(1L);System.out.println("小李取出的价格:" + p1.getPrice());// 2、小王Product p2 = productMapper.selectById(1L);System.out.println("小王取出的价格:" + p2.getPrice());// 3、小李将价格加了50元,存入了数据库p1.setPrice(p1.getPrice() + 50);int result1 = productMapper.updateById(p1);System.out.println("小李修改结果:" + result1);// 4、小王将商品减了30元,存入了数据库p2.setPrice(p2.getPrice() - 30);int result2 = productMapper.updateById(p2);System.out.println("小王修改结果:" + result2);// 最后的结果Product p3 = productMapper.selectById(1L);// 价格覆盖,最后的结果:70System.out.println("最后的结果:" + p3.getPrice());
}

6.3.4、乐观锁实现流程

数据库中添加version字段

取出记录时,获取当前version

SELECT id,`name`,price,`version` FROM product WHERE id=1

更新时,version + 1,如果where语句中的version版本不对,则更新失败

UPDATE product SET price=price+50, `version`=`version` + 1 WHERE id = 1 AND `version` = 1;

6.3.5、Mybatis-Plus实现乐观锁

修改实体类
@Data
public class Product {private Long id;private String name;private Integer price;@Version // 标识乐观锁版本号private Integer version;
}

添加乐观锁插件配置
package com.oracle.config;import com.baomidou.mybatisplus.annotation.DbType;
import com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor;
import com.baomidou.mybatisplus.extension.plugins.inner.OptimisticLockerInnerInterceptor;
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
@MapperScan("com.oracle.mapper")
public class MybatisPlusConfig {@Beanpublic MybatisPlusInterceptor mybatisPlusInterceptor() {MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();interceptor.addInnerInterceptor(new PaginationInnerInterceptor(DbType.MYSQL));// 添加乐观锁插件interceptor.addInnerInterceptor(new OptimisticLockerInnerInterceptor());return interceptor;}}

测试修改冲突

优化流程
@Test
public void testConcurrentVersionUpdate() {// 小李取数据Product p1 = productMapper.selectById(1L);// 小王取数据Product p2 = productMapper.selectById(1L);// 小李修改 + 50p1.setPrice(p1.getPrice() + 50);int result1 = productMapper.updateById(p1);System.out.println("小李修改的结果:" + result1);// 小王修改 - 30p2.setPrice(p2.getPrice() - 30);int result2 = productMapper.updateById(p2);System.out.println("小王修改的结果:" + result2);if(result2 == 0){// 失败重试,重新获取version并更新p2 = productMapper.selectById(1L);p2.setPrice(p2.getPrice() - 30);result2 = productMapper.updateById(p2);}System.out.println("小王修改重试的结果:" + result2);// 老板看价格Product p3 = productMapper.selectById(1L);System.out.println("老板看价格:" + p3.getPrice());
}

七、通用枚举

表中的有些字段值是固定的,例如性别(男或女),此时我们可以使用MyBatis-Plus的通用枚举来实现

7.1、数据库表添加字段sex

7.2、创建通用枚举类型

package com.oracle.enums;import com.baomidou.mybatisplus.annotation.EnumValue;
import lombok.Getter;@Getter
public enum SexEnum {MALE(1, "男"),FEMALE(2, "女");@EnumValueprivate Integer sex;private String sexName;SexEnum(Integer sex, String sexName) {this.sex = sex;this.sexName = sexName;}
}

7.3、配置扫描通用枚举

# 配置MyBatis日志
mybatis-plus:configuration:log-impl: org.apache.ibatis.logging.stdout.StdOutImplglobal-config:db-config:# 配置MybatisPlus操作表的默认前缀table-prefix: t_# 配置MyBatis-Plus的主键策略id-type: auto# 配置扫描通用枚举type-enums-package: com.oracle.enums

7.4、测试

@Test
public void testSexEnum(){User user = new User();user.setName("Enum");user.setAge(20);// 设置性别信息为枚举项,会将@EnumValue注解所标识的属性值存储到数据库user.setSex(SexEnum.MALE);// INSERT INTO t_user ( username, age, sex ) VALUES ( ?, ?, ? )// Parameters: Enum(String), 20(Integer), 1(Integer)userMapper.insert(user);
}

八、代码生成器

8.1、引入依赖

<dependency><groupId>com.baomidou</groupId><artifactId>mybatis-plus-generator</artifactId><version>3.5.1</version>
</dependency>
<dependency><groupId>org.freemarker</groupId><artifactId>freemarker</artifactId><version>2.3.31</version>
</dependency>

8.2、快速生成

package com.oracle;import com.baomidou.mybatisplus.generator.FastAutoGenerator;
import com.baomidou.mybatisplus.generator.config.OutputFile;
import com.baomidou.mybatisplus.generator.engine.FreemarkerTemplateEngine;import java.util.Collections;public class FastAutoGeneratorTest {public static void main(String[] args) {FastAutoGenerator.create("jdbc:mysql://127.0.0.1:3306/mybatis_plus?characterEncoding=utf-8&userSSL=false", "root", "root").globalConfig(builder -> {builder.author("atguigu") // 设置作者//.enableSwagger() // 开启 swagger 模式.fileOverride() // 覆盖已生成文件.outputDir("D://mybatis_plus"); // 指定输出目录}).packageConfig(builder -> {builder.parent("com.atguigu") // 设置父包名.moduleName("mybatisplus") // 设置父包模块名.pathInfo(Collections.singletonMap(OutputFile.mapperXml, "D://mybatis_plus"));// 设置mapperXml生成路径}).strategyConfig(builder -> {builder.addInclude("t_user") // 设置需要生成的表名.addTablePrefix("t_", "c_"); // 设置过滤表前缀}).templateEngine(new FreemarkerTemplateEngine()) // 使用Freemarker引擎模板,默认的是Velocity引擎模板.execute();}
}

九、多数据源

适用于多种场景:纯粹多库、 读写分离、 一主多从、 混合模式等
目前我们就来模拟一个纯粹多库的一个场景,其他场景类似
场景说明:
我们创建两个库,分别为:mybatis_plus(以前的库不动)与mybatis_plus_1(新建),将
mybatis_plus库的product表移动到mybatis_plus_1库,这样每个库一张表,通过一个测试用例分别获取用户数据与商品数据,如果获取到说明多库模拟成功

9.1、创建数据库及表

创建数据库mybatis_plus_1和表product

CREATE DATABASE `mybatis_plus_1` /*!40100 DEFAULT CHARACTER SET utf8mb4 */;
use `mybatis_plus_1`;
CREATE TABLE product
(id BIGINT(20) NOT NULL COMMENT '主键ID',name VARCHAR(30) NULL DEFAULT NULL COMMENT '商品名称',price INT(11) DEFAULT 0 COMMENT '价格',version INT(11) DEFAULT 0 COMMENT '乐观锁版本号',PRIMARY KEY (id)
);
添加测试数据
INSERT INTO product (id, NAME, price) VALUES (1, '外星人笔记本', 100);
删除 mybatis_plus product
use mybatis_plus;
DROP TABLE IF EXISTS product;

9.2、引入依赖

<dependency><groupId>com.baomidou</groupId><artifactId>dynamic-datasource-spring-boot-starter</artifactId><version>3.5.0</version>
</dependency>

9.3、配置多数据源

说明:注释掉之前的数据库连接,添加新配置
spring:datasource:dynamic:primary: masterstrict: falsedatasource:master:url: jdbc:mysql://localhost:3306/mybatis_plus?characterEncoding=utf-8&useSSL=falsedriver-class-name: com.mysql.cj.jdbc.Driverusername: rootpassword: rootslave_1:url: jdbc:mysql://localhost:3306/mybatis_plus_1?characterEncoding=utf-8&useSSL=falsedriver-class-name: com.mysql.cj.jdbc.Driverusername: rootpassword: root

9.4、创建用户service

public interface UserService extends IService<User> {}
@DS("master") //指定所操作的数据源
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService {}

9.5、创建商品service

public interface ProductService extends IService<Product> {}
@DS("slave_1")
@Service
public class ProductServiceImpl extends ServiceImpl<ProductMapper, Product> implements ProductService {}

9.6、测试

@Autowired
private UserService userService;@Autowired
private ProductService productService;@Test
public void testDynamicDataSource(){System.out.println(userService.getById(1L));System.out.println(productService.getById(1L));
}

结果:
1、都能顺利获取对象,则测试成功
2、如果我们实现读写分离,将写操作方法加上主库数据源,读操作方法加上从库数据源,自动切换,是不是就能实现读写分离?

十、MyBatisX插件

MyBatis-Plus为我们提供了强大的mapper和service模板,能够大大的提高开发效率。
但是在真正开发过程中,MyBatis-Plus并不能为我们解决所有问题,例如一些复杂的SQL,多表联查,我们就需要自己去编写代码和SQL语句,我们该如何快速的解决这个问题呢,这个时候可以使用MyBatisX插件。
MyBatisX一款基于 IDEA 的快速开发插件,为效率而生。

MyBatisX插件用法:https://baomidou.com/pages/ba5b24/

http://www.hengruixuexiao.com/news/34585.html

相关文章:

  • 网站内部链接建设企业网站首页
  • wordpress discuz区别搜索引擎seo是什么
  • 网站上传视频怎么做百度权重是什么意思
  • 免费建站长平台网站二级域名注册
  • 怎么做网站树洞网站建设方案书范文
  • 做网站和做产品网站建设外包
  • 如何建设赌博网站竞价网络推广托管
  • 做网站推广有前景吗排名函数rank怎么用
  • 哪些大学网站做的比较好游戏推广引流软件
  • flash asp设计公司网站源码免费有效的推广网站
  • 深圳维特网站建设2021百度新算法优化
  • 网站建设方案下载百度识图搜索图片来源
  • 上海网站建设 永灿友情链接有哪些展现形式
  • 怎么做网站的三级目录热词分析工具
  • 长沙建站智找有为太极关键字优化
  • 看今天的新闻关键词优化推广排名
  • 无锡做网站需要多少钱东莞网站公司
  • 电子商务公司网站建立可以免费投放广告的平台
  • 中国兰州网pc主站凡客建站
  • 网站建设流程 报读文库国外网页模板
  • 网站搭建h5是什么在线教育
  • 西安做酒店用品的网站seo网站优化助理
  • html做网站怎样推广app别人才愿意下载
  • h5网站开发公司免费个人网站源码
  • 最近几年做电影网站怎么样免费推广的途径与原因
  • 外国网站在中国做推广如何做一个自己的网站呢
  • 全民建网站搜索大全浏览器
  • 临沂网站制作策划谷歌浏览器 免费下载
  • 东莞政务网站建设方案网络链接推广
  • 独立网店系统上海网络优化服务