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

s001网站建设智卡会员管理系统

s001网站建设,智卡会员管理系统,平台型综合电子商务的平台有哪些,下载别人的wordpress模板目录 1.分页插件 1.1 添加配置类 1.2 测试 2.xml自定义分页 2.1 UserMapper中定义接口方法 2.2 UserMapper.xml中编写SQL 2.3 测试 3. 乐观锁 3.1 场景 3.2 乐观锁与悲观锁 3.3 模拟修改冲突 1. 数据库增加商品表 2. 添加数据 3. 添加实体 4. 添加mapper 5. 测试…

目录

1.分页插件

1.1 添加配置类

1.2 测试

2.xml自定义分页

2.1 UserMapper中定义接口方法

2.2 UserMapper.xml中编写SQL

2.3 测试

3. 乐观锁

 3.1 场景

3.2 乐观锁与悲观锁

3.3 模拟修改冲突

1. 数据库增加商品表

2. 添加数据

 3. 添加实体

4. 添加mapper

5. 测试

 3.4 乐观锁实现流程

3.5 乐观锁实现流程

1. 修改实体类

 2. 添加乐观锁插件配置

 3. 测试修改冲突

 4. 优化流程


1.分页插件

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

1.1 添加配置类

@Configuration
@MapperScan("com.qcby.mybatisplus.mapper")//可以将主类中的注解移到此处
public class MyBatisPlusConfig {@Beanpublic MybatisPlusInterceptor mybatisPlusInterceptor() {MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();interceptor.addInnerInterceptor(newPaginationInnerInterceptor(DbType.MYSQL));return interceptor;}
}

1.2 测试

@Testpublic void testPage1(){
//设置分页参数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());}


2.xml自定义分页

2.1 UserMapper中定义接口方法

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

注意!!

        分页的插件不要删掉,他是分页功能的前提。

2.2 UserMapper.xml中编写SQL

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapperPUBLIC "-//mybatis.org//DTD Mapper 3.0//EN""http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.qcby.mybatisplus.mapper.UserMapper"><!--SQL片段,记录基础字段--><sql id="BaseColumns">uid,username,age,email</sql><!--IPage<User> selectPageVo(Page<User> page, Integer age);--><select id="selectPageVo" resultType="com.qcby.mybatisplus.entity.User">SELECT <include refid="BaseColumns"></include> FROM t_user WHERE age>#{age}</select>
</mapper>

配置映射:

mybatis-plus:configuration:# 配置MyBatis日志log-impl: org.apache.ibatis.logging.stdout.StdOutImplglobal-config:db-config:# 配置MyBatis-Plus操作表的默认前缀table-prefix: t_# 配置MyBatis-Plus的主键策略id-type: automapper-locations: classpath:mapper/*.xml

2.3 测试

@Testpublic 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());}


3. 乐观锁

 3.1 场景

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

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

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

3.2 乐观锁与悲观锁

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

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

3.3 模拟修改冲突

1. 数据库增加商品表

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)
);

2. 添加数据

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

 

 3. 添加实体

@Data
public class Product {private Long id;private String name;private Integer price;private Integer version;
}

4. 添加mapper


@Repository
public interface ProductMapper extends BaseMapper<Product> {
}

5. 测试

@Testpublic 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());}

 

 

 

显然,最后的结果是70. 

 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

3.5 乐观锁实现流程

1. 修改实体类

@Data
public class Product {private Long id;private String name;private Integer price;@Versionprivate Integer version;
}

 2. 添加乐观锁插件配置

@Configuration
@MapperScan("com.qcby.mybatisplus.mapper")//可以将主类中的注解移到此处
public class MyBatisPlusConfig {@Beanpublic MybatisPlusInterceptor mybatisPlusInterceptor() {MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();//添加分页插件interceptor.addInnerInterceptor(newPaginationInnerInterceptor(DbType.MYSQL));//添加乐观锁插件interceptor.addInnerInterceptor(new OptimisticLockerInnerInterceptor()); return interceptor;}
}

 3. 测试修改冲突

 小李查询商品信息:

SELECT id,name,price,version FROM t_product WHERE id=?

 小王查询商品信息:

SELECT id,name,price,version FROM t_product WHERE id=?

 

 小李修改商品价格,自动将version+1

UPDATE t_product SET name=?, price=?, version=? WHERE id=? AND version=?
Parameters: 外星人笔记本(String), 150(Integer), 1(Integer), 1(Long), 0(Integer)

 

 小王修改商品价格,此时version已更新,条件不成立,修改失败

UPDATE t_product SET name=?, price=?, version=? WHERE id=? AND version=?
Parameters: 外星人笔记本(String), 70(Integer), 1(Integer), 1(Long), 0(Integer)

 

 最终,小王修改失败,查询价格:  150

 

SELECT id,name,price,version FROM t_product WHERE id=?

 

 4. 优化流程

@Testpublic 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());}


文章转载自:

http://mjpFHze6.thntp.cn
http://JhBpdIvG.thntp.cn
http://usEjQIIy.thntp.cn
http://ZkB1vRSe.thntp.cn
http://q3KTchUA.thntp.cn
http://3Za16Crd.thntp.cn
http://owgLjSe1.thntp.cn
http://g8sDOmrn.thntp.cn
http://1Kp5C0Gk.thntp.cn
http://xfLfySLC.thntp.cn
http://flrgR0Mq.thntp.cn
http://5cEQbACi.thntp.cn
http://d7a8cQJ7.thntp.cn
http://0766hyMe.thntp.cn
http://5yMDVEkp.thntp.cn
http://liT3RQwp.thntp.cn
http://p9vWS87w.thntp.cn
http://uECat6Wp.thntp.cn
http://IrQXjBKn.thntp.cn
http://xLjncihs.thntp.cn
http://U0QIAuos.thntp.cn
http://sxmcrTUk.thntp.cn
http://ZaqvFqeC.thntp.cn
http://CoD24pYB.thntp.cn
http://cuv9pSkC.thntp.cn
http://QdaDVDJu.thntp.cn
http://yOk9jSlu.thntp.cn
http://yJLhv7JY.thntp.cn
http://hjCUp2O8.thntp.cn
http://N7ki0fk1.thntp.cn
http://www.dtcms.com/wzjs/625156.html

相关文章:

  • 外贸网站建设服务商网站开发项目责任分配矩阵
  • 仙桃哪里做网站长治哪家公司做网站好
  • 北京做网站的公司排行谷歌seo顾问
  • 电脑做网站用什么软件微信小程序怎么添加到桌面
  • 网站建设 英汇网络做追星网站效果图
  • wordpress 网站很慢手工活接单正规平台
  • 网站建设郑州公司青浦手机网站制作
  • 个人如何做一个网站网页设计编辑器
  • 做网站应该用什么配置的电脑wap网站开发工具
  • 网站改版需要多久开一个做网站的公司
  • 网站建设的行业资讯_为什么平面设计最后都转行了
  • 企业网站开发平台网站底部关键词指向
  • 杭州建设招聘信息网站伪静态一个虚拟空间做两个网站
  • 招聘网站费用怎么做分录wordpress邮件key
  • 天津定制网站建设公司网站开发使用的开发工具
  • 那个网站做3d高权重网站出售
  • 重庆做商城网站设计郑州做网站托管
  • 深圳高端网站设计建设网站主页面设计模板
  • 做外贸怎样上外国网站wordpress视频教程百度云
  • 国外网站怎么建设如何用dw做网站首页
  • 南昌建站系统外包自适应网站设计稿
  • 国内网站绕过备案方法网站建设套定额
  • 简单的网站建设合同书北京网站定制流程
  • 上海网站托管永久免费crm都有什么
  • 台州市网站制作网络广告营销的概念
  • 旅游网站系统建设方案做同城购物网站
  • 广东做网站策划做竞价推广大概多少钱
  • 在线logo制作网站wordpress 换行符
  • a站播放量最高的视频架设网站服务器
  • 深圳做网站联雅新网站开发