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

JDBC Some Templates

JDBCTemplate

是Spring对JDBC的封装,使用JDBCTemplate方便实现对数据的操作。

<!--        orm:Object relationship mapping m对象 关系 映射-->

引入依赖

<!--        基于Maven依赖的传递性,导入spring-content依赖即可导入当前所需的所有jar包--><dependency><groupId>org.springframework</groupId><artifactId>spring-context</artifactId><version>5.2.5.RELEASE</version></dependency><!--        Mybatis核心--><dependency><groupId>org.mybatis</groupId><artifactId>mybatis</artifactId><version>3.5.9</version></dependency><!--        junit--><dependency><groupId>junit</groupId><artifactId>junit</artifactId><version>4.12</version><scope>test</scope></dependency><!--        MySQL--><dependency><groupId>mysql</groupId><artifactId>mysql-connector-java</artifactId><version>8.0.18</version></dependency><!--        LomBok--><dependency><groupId>org.projectlombok</groupId><artifactId>lombok</artifactId><version>1.18.10</version></dependency><!--        log4j日志--><dependency><groupId>log4j</groupId><artifactId>log4j</artifactId><version>1.2.12</version></dependency>
<!--        spring持久层jar包-->
<!--        orm:Object relationship mapping m对象 关系 映射--><dependency><groupId>org.springframework</groupId><artifactId>spring-orm</artifactId><version>5.3.1</version></dependency>
<!--        德鲁伊数据库连接池--><dependency><groupId>com.alibaba</groupId><artifactId>druid</artifactId><version>1.1.21</version></dependency>

创建实体类

User

package com.lobo.entity;import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.ToString;/*** className: User<br/>* author: MacieSerenity <br/>* date: 2022-08-14 14:24**/
@Data
@NoArgsConstructor
@AllArgsConstructor
@ToString
public class User {private Integer id;private String username;private String password;private Integer age;private String email;
}

准备DataSource

jdbc-config.properties

#jdbc.properties
jdbc.driver=com.mysql.cj.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/mybatisstudy?useUnicode=true&characterEncoding=utf8&serverTimezone=GMT
jdbc.username=root
jdbc.password=li1473606768

Spring配置文件

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xmlns:context="http://www.springframework.org/schema/context"xsi:schemaLocation="http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans.xsdhttp://www.springframework.org/schema/contexthttps://www.springframework.org/schema/context/spring-context-4.2.xsd"><!--    引入jdbc-config.properties文件--><context:property-placeholder location="classpath:jdbc-config.properties"/><bean class="com.alibaba.druid.pool.DruidDataSource" id="druidDataSource" ><property name="driverClassName" value="${jdbc.driver}" /><property name="url" value="${jdbc.url}" /><property name="username" value="${jdbc.username}" /><property name="password" value="${jdbc.password}" /></bean><bean class="org.springframework.jdbc.core.JdbcTemplate" ><property name="dataSource" ref="druidDataSource" ></property></bean>
</beans>

注意,Druid数据库连接池配置的property的name=“driverClassName”,不要打错了

测试

package com.lobo;import com.lobo.entity.User;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;import java.util.List;/*** className: test<br/>* author: MacieSerenity <br/>* date: 2022-08-14 10:28**/@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:spring-jdbc.xml")
public class test {//@RunWith(SpringJUnit4ClassRunner.class)//指定当前测试类在spring的测试环境中执行,此时就可以通过依赖注入的方式直接获取IOC容器中的bean//@ContextConfiguration//设置Spring测试环境的配置文件@Autowiredprivate JdbcTemplate jdbcTemplate;@Testpublic void testJdbcTemplateInsert(){String preparedSql="insert into t_user(username,password,age,gender,email) values (?,?,?,?,?)";jdbcTemplate.update(preparedSql,"admin","123",30,"男","hello@123.com");}@Testpublic void testJdbcTemplateQuery(){String sql = "select * from t_user where id = ?";User user = jdbcTemplate.queryForObject(sql, new BeanPropertyRowMapper<>(User.class), 1);System.out.println(user);}@Testpublic void getAllUser(){String sql = "select * from t_user";List<User> query = jdbcTemplate.query(sql, new BeanPropertyRowMapper<>(User.class));query.forEach(System.out::println);}@Testpublic void testGetCount(){String sql = "select count(*) from t_user";Integer integer = jdbcTemplate.queryForObject(sql, Integer.class);System.out.println(integer);}
}

声明式事务

编程式事务

Connection conn=null;
try{//开启事务conn.setAutoCommit(false);//核心操作//提交事务conn.commit()}catch(Exception e){//回滚事务conn.rollBack();e.printStackTrace();}finally{//释放数据库连接conn.close()
}

以上方式存在的缺陷:

操作繁琐、复用性不高

声明式事务

由于事务的代码相对固定,所以框架就可以将固定模式的代码抽取出来,进行相关的封装。

封装之后:提高开发效率,消除冗余代码、可以对健壮性、性能等方面优化。

编程式事务:程序员自己写代码实现功能

声明式事务:通过配置让框架自动实现功能

创建测试表:t_book和t_user

drop table if exists t_book;
CREATE TABLE t_book(
book_id int(11) NOT NULL AUTO_INCREMENT COMMENT '主键',
book_name varchar(20) DEFAULT NULL COMMENT '图书名称',
price int(11) DEFAULT NULL COMMENT '价格',
stock int(10) unsigned DEFAULT NULL COMMENT '库存(无符号)',
PRIMARY KEY (book_id)
) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=utf8;insert into t_book(book_id,book_name,price,stock) values (1,'斗破苍穹' ,80,100),(2,'斗罗大陆',50,100);drop table if exists t_user;
CREATE TABLE t_user(
user_id int(11) NOT NULL AUTO_INCREMENT COMMENT '主键',
username varchar (20) DEFAULT NULL COMMENT '用户名',
balance int(10) unsigned DEFAULT NULL COMMENT ' 余额(无符号) ',
PRIMARY KEY (user_id)
) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=utf8;
insert into t_user(user_id,username,balance) values (1,'admin',50);

在这里插入图片描述

创建Controller、Service、Dao层

package com.lobo.controller;
@Controller
public class BookController {@Autowiredprivate BookService bookService;public void buyBook(Integer userId,Integer bookId){bookService.buyBook(userId,bookId);}
}-----service接口
package com.lobo.service;
public interface BookService {/*** 买书* @param userId 用户ID* @param bookId 图书ID*/void buyBook(Integer userId, Integer bookId);
}
-----service实现类
package com.lobo.service.impl;
@Service
public class BookServiceImpl implements BookService {@Autowiredprivate BookDao bookDao;@Overridepublic void buyBook(Integer userId, Integer bookId) {//1、查询图书价格Integer price =bookDao.getPriceByBookId(bookId);//2、更新图书库存bookDao.updateStock(bookId);//3、更新用户余额bookDao.updateBalance(userId,price);}
}-------Dao接口
package com.lobo.dao;
public interface BookDao {/*** 根据图书ID获取价格* @param bookId 图书ID* @return 图书的价格*/Integer getPriceByBookId(Integer bookId);/*** 更新图书库存* @param bookId 图书ID*/void updateStock(Integer bookId);/*** 更新用户余额* @param userId 用户ID* @param price 价格*/void updateBalance(Integer userId, Integer price);
}-------Dao层实现
package com.lobo.dao.impl;import com.lobo.dao.BookDao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;/*** className: BookDaoImpl<br/>* author: MacieSerenity <br/>* date: 2022-08-14 15:30**/
@Repository
public class BookDaoImpl implements BookDao {@Autowiredprivate JdbcTemplate jdbcTemplate;@Overridepublic Integer getPriceByBookId(Integer bookId) {String sql = "select price from t_book where book_id =?";return jdbcTemplate.queryForObject(sql,Integer.class,bookId);}@Overridepublic void updateStock(Integer bookId) {String sql = "update t_book set stock = stock - 1 where book_id= ?";jdbcTemplate.update(sql,bookId);}@Overridepublic void updateBalance(Integer userId, Integer price) {String sql="update t_user set balance = balance - ? where user_id =?";jdbcTemplate.update(sql,price,userId);}
}

创建配置文件

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xmlns:context="http://www.springframework.org/schema/context"xsi:schemaLocation="http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans.xsdhttp://www.springframework.org/schema/contexthttps://www.springframework.org/schema/context/spring-context-4.2.xsd"><!--    包扫描--><context:component-scan base-package="com.lobo"/><!--    引入jdbc-config.properties文件--><context:property-placeholder location="classpath:jdbc-config.properties"/><bean class="com.alibaba.druid.pool.DruidDataSource" id="druidDataSource" ><property name="driverClassName" value="${jdbc.driver}" /><property name="url" value="${jdbc.url}" /><property name="username" value="${jdbc.username}" /><property name="password" value="${jdbc.password}" /></bean><bean class="org.springframework.jdbc.core.JdbcTemplate" id="jdbcTemplate"><property name="dataSource" ref="druidDataSource"/></bean>
</beans>

创建测试类

package com.lobo;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:tx-annotation.xml")
public class testBuyBook {@Autowiredprivate BookController bookController;@Testpublic void buyBook(){bookController.buyBook(1,1);}
}

错误:

org.springframework.dao.DataIntegrityViolationException: PreparedStatementCallback; SQL [update t_user set balance = balance - ? where user_id =?]; Data truncation: BIGINT UNSIGNED value is out of range in '(`mybatisstudy`.`t_user`.`balance` - 80)'; nested exception is com.mysql.cj.jdbc.exceptions.MysqlDataTruncation: Data truncation: BIGINT UNSIGNED value is out of range in '(`mybatisstudy`.`t_user`.`balance` - 80)'

因为图书价格为80,而用户ID为1的用户只有80,50-80之后就会变为-30,而我们使用的是无符号的数来表示,这样变成负数之后,就会超出范围。

此时,由于没有开启事务,第一个方法update了图书的库存,而第二个方法扣除用户余额时失败。

我们应该将这几个方法封装在一个事务当中,一旦有某一个程序执行错误,我们都需要将整个事务回滚。

开启事务

需要在配置文件中开启事务,以及开启@Transactional注解

在tx-annotation.xml的spring配置类中添加以下内容

<!--    配置事务管理器--><bean class="org.springframework.jdbc.datasource.DataSourceTransactionManager" id="dataSourceTransactionManager"><property name="dataSource" ref="druidDataSource" /></bean><!--    开启事务的注解驱动,注意是使用的tx-->
<!--    将事务管理器中的通知作用到切面中-->
<!--    将@Transaction注解所表示的方法或者类中的所有方法使用事务进行管理-->
<!--    若事务管理器bean的id默认为transactionManager,则该属性可以不用填写--><tx:annotation-driven transaction-manager="dataSourceTransactionManager" />

记住要选择tx:annotation-driven中tx结尾的标签

在这里插入图片描述

然后在需要进行事务管理的方法或者类中,使用@Transactional进行标记(一般事务标记在Service层中)

package com.lobo.service.impl;
...
@Service
public class BookServiceImpl implements BookService {@Autowiredprivate BookDao bookDao;@Override@Transactionalpublic void buyBook(Integer userId, Integer bookId) {//1、查询图书价格Integer price =bookDao.getPriceByBookId(bookId);//2、更新图书库存bookDao.updateStock(bookId);//3、更新用户余额bookDao.updateBalance(userId,price);}
}
* 声明式事务的配置步骤:
* 1、在Spring的配置文件中配置事务管理器
* 2、开启事务的注解驱动* 只需要在需要被事务管理的方法上,添加@Transactional注解,该方法就会被事务管理器管理

声明式事务的属性

只读

告诉数据库,我们当前的操作没有任何修改或者写的操作,这样数据库就可以针对查询操作来进行优化:

@Transactional(readOnly=true)

若在声明了只读的方法或者类中,使用了增删改操作,会抛出以下异常:

SQLException:Connection is read-only,Queries leading to data modification are not allowd;

不光可以防止脏读,开启高隔离后,多个读操作成一个事务还可以防止不可重复读

超时

事务执行的过程中,有可能会因为某些问题导致程序卡主,从而长时间占用数据库资源,而长时间占用资源大概率是因为程序运行出现了问题(程序、数据库、网络等)

此时这个出现问题的程序就应该回滚,释放资源。

@Transactional(timeout = 3)
3表示3秒,以秒为单位
默认是-1,也就是永久等待

可以使用TimeUnit.SECONDS.sleep(5);来测试

若超时,会抛出以下异常:

TransactionTimeOutException:Transaction time out:deadline was ...

抛出异常后,强制回滚

回滚策略

声明式事务默认只对所有的运行时异常进行回滚,编译时异常不进行回滚。

因为什么进行回滚:
rollbackForClassName 
rollbackFor(一般不使用前两个)不因为什么进行回滚:
noRollBackFor
noRollbackForClassName

如果在事务运行中,出现了计算错误的异常(举例),我们不希望对计算错误的异常进行回滚,则可以使用

这里的noRollBackFor的参数是一个数组对象,当数组内的对象只有一个时,可以不使用{}包裹,而数组内有多个时,需要使用{}进行包裹,且用逗号分割

@Transaction(noRollBackFor = ArithmeticException.class)
或者
@Transaction(noRollBackFor = java.lang.ArithmeticException )  全类名

隔离级别

事务的隔离级别

读未提交 read uncommited
只存在理论上,允许事务A读取事务B未提交的修改读已提交 read commited
事务A只能读取事务B已经提交的修改,可能会产生脏读可重复读 repeatable read 
事务A可以多次读取一个字段中相同的值,事务A执行期间,禁止其它事务对这个字段进行更新,也就是当前事务只会读取到同一个值。可能会产生幻读序列化 serializable
确保事务A可以多次从一个表中读取到相同的行,在事务A执行期间,禁止其他事务对这个表进行添加、更新、删除操作。可以避免任何并发问题,但是性能低下。
隔离级别脏读不可重复读幻读
读未提交read uncommited
读已提交read commited
可重复读repeatable read
序列化serializable

各个数据库产品对事务隔离级别的支持

隔离级别OracleMySQL
read uncommitedxv
read commitedvv
repeatable readxv
serializablevv

使用方式:

@Transactional(isolation= Isolation.DEFAULT)//使用数据默认的隔离级别
@Transactional(isolation= Isolation.READ_UNCOMMITED)//读未提交
@Transactional(isolation= Isolation.READ_COMMITTED)//读已提交
@Transactional(isolation= Isolation.REPEATABLE_READ)//可重复读
@Transactional(isolation= Isolation.SERIALIZABLE)//序列化

事务的传播性

停止事务的传播

新建CheckoutService接口

package com.lobo.service;
...
public interface CheckoutService {/*** 为多本书结账* @param userId 用户ID* @param bookIds 图书ID*/void checkOut(Integer userId, Integer[] bookIds);
}

CheckoutServiceImpl

package com.lobo.service.impl;
...
@Service
public class CheckoutServiceImpl implements CheckoutService {@Autowiredprivate BookService bookService;@Override@Transactionalpublic void checkOut(Integer userId, Integer[] bookIds) {for(Integer bookId:bookIds){bookService.buyBook(userId,bookId);}}
}

我们在这个新的类中添加了一个事务,这个方法的功能是调用Service中的其它事务

package com.lobo.service.impl;
...
@Service
public class BookServiceImpl implements BookService {@Autowiredprivate BookDao bookDao;@Override@Transactional(propagation = Propagation.REQUIRES_NEW)public void buyBook(Integer userId, Integer bookId) {//1、查询图书价格Integer price =bookDao.getPriceByBookId(bookId);//2、更新图书库存bookDao.updateStock(bookId);//3、更新用户余额bookDao.updateBalance(userId,price);}
}

可以看出我们的循环是一个事务,而循环内调用的buyBook又是另外一个事务。

测试:

  @Testpublic void buyBook(){bookController.checkOut(1,new Integer[]{1,2});}
@Transactional(propagation = Propagation.REQUIRED) //默认值
@Transactional(propagation = Propagation.REQUIERS_NEW) //开启新事务(使用自己的事务)

当设置为默认时,买两本书的任何一本买不起,都会直接触发事务的回滚,回滚到一开始还没有买书的位置;

而设置为第二种开启新的事务时,其中每一次执行BuyBook都是一次新的事务,无法购买第二本书不会影响第一本书的购买,即便是回滚,也是回滚到买第二本书之前。

mysql> select * from t_user;
+---------+----------+---------+
| user_id | username | balance |
+---------+----------+---------+
|       1 | admin    |      20 |
+---------+----------+---------+
1 row in set (0.00 sec)mysql> select * from t_book;
+---------+-----------+-------+-------+
| book_id | book_name | price | stock |
+---------+-----------+-------+-------+
|       1 | 斗破苍穹  |    80 |    98 |
|       2 | 斗罗大陆  |    50 |   100 |
+---------+-----------+-------+-------+
2 rows in set (0.00 sec)

当设置为默认时,会使用所有的事务,也就是事务的嵌套,将多个事务视作一个事务,被嵌套的任何一个事务失败或者超时,都会使最外面的事务失效回滚。

而设置为 Propagation.REQUIERS_NEW 时,在遇见@Transactional注解时,会自动创建一个新的事务,这个事务独立于其它被嵌套的事务,独立执行。

两个属性中,回滚的地方才是区别(大回滚和小回滚)

基于xml配置文件的声明式事务

想要基于XML配置文件配置声明式事务的话,必须引入aspect的依赖

<!--       spring 切面--><dependency><groupId>org.springframework</groupId><artifactId>spring-aspects</artifactId><version>5.3.1</version></dependency>

然后需要在XML文件中写入:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx"xmlns:aop="http://www.springframework.org/schema/aop"xsi:schemaLocation="http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans.xsdhttp://www.springframework.org/schema/contexthttps://www.springframework.org/schema/context/spring-context-4.2.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd http://www.springframework.org/schema/aop https://www.springframework.org/schema/aop/spring-aop.xsd"><!--    包扫描--><context:component-scan base-package="com.lobo"/><!--    引入jdbc-config.properties文件--><context:property-placeholder location="classpath:jdbc-config.properties"/><bean class="com.alibaba.druid.pool.DruidDataSource" id="druidDataSource" ><property name="driverClassName" value="${jdbc.driver}" /><property name="url" value="${jdbc.url}" /><property name="username" value="${jdbc.username}" /><property name="password" value="${jdbc.password}" /></bean><bean class="org.springframework.jdbc.core.JdbcTemplate" id="jdbcTemplate"><property name="dataSource" ref="druidDataSource"/></bean><!--    配置事务管理器--><bean class="org.springframework.jdbc.datasource.DataSourceTransactionManager" id="dataSourceTransactionManager"><property name="dataSource" ref="druidDataSource" /></bean><!--    开启事务的注解驱动,注意是使用的tx-->
<!--    将事务管理器中的通知作用到切面中-->
<!--    将@Transaction注解所表示的方法或者类中的所有方法使用事务进行管理-->
<!--    若事务管理器bean的id默认为transactionManager,则该属性可以不用填写-->
<!--    <tx:annotation-driven transaction-manager="dataSourceTransactionManager" />-->
<!--    基于xml的配置事务管理需要注释上面这句-->
<!--    配置事务通知,id为唯一标识,transaction-manager指定使用的事务管理器--><tx:advice id="interceptor" transaction-manager="dataSourceTransactionManager"><tx:attributes>
<!--            tx:method指配置具体的事务方法-->
<!--            name属性:指定方法名,可以使用星号代表多个字符-->
<!--            <tx:method name="buyBook" />-->
<!--            将查询方法的read-only指定为true,为读取做优化--><tx:method name="get*" read-only="true" /><tx:method name="query*" read-only="true" /><tx:method name="find*" read-only="true" />
<!--            read-only:设置只读-->
<!--            rollback-for:设置回滚的异常-->
<!--            no-rollback-for:设置不回滚的异常-->
<!--            isolation设置事务的隔离级别-->
<!--            timeout设置事务的超时属性-->
<!--            propagation设置事物的传播级别--><tx:method name="save*" read-only="false" rollback-for="java.lang.Exception" propagation="REQUIRES_NEW" /><tx:method name="update*" read-only="false" rollback-for="java.lang.Exception" propagation="REQUIRES_NEW" /><tx:method name="delete*" read-only="false" rollback-for="java.lang.Exception" propagation="REQUIRES_NEW" /></tx:attributes></tx:advice><aop:config><aop:advisor  advice-ref="interceptor" pointcut="execution(* com.lobo.service.impl.*.*(..))" /></aop:config>
</beans>

请添加图片描述

相关文章:

JDBC Some Templates

JDBCTemplate 是Spring对JDBC的封装&#xff0c;使用JDBCTemplate方便实现对数据的操作。 <!-- orm:Object relationship mapping m对象 关系 映射-->引入依赖 <!-- 基于Maven依赖的传递性&#xff0c;导入spring-content依赖即可导入当前所需的所有…...

dubbo启动指定ip不使用docker虚拟网络ip

java -D 配置系统属性 # 启动时加参数 -DDUBBO_IP_TO_REGISTRY 192.168.1.1 该ip为dubbo所在服务器的公网ip即可。 java -jar myDubboRpc-api.jar -DDUBBO_IP_TO_REGISTRY 192.168.1.1 # xjar启动 nohup ./xjar java -DDUBBO_IP_TO_REGISTRY11.22.33.44 -XX:UseG1GC -jar …...

Bobo String Construction

登录—专业IT笔试面试备考平台_牛客网 题目大意&#xff1a;给出一字符串t&#xff0c;求一个长为n的字符串&#xff0c;使tst中包含且仅包含两个t 1<n<1000;测试样例组数<1000 思路&#xff1a;一开始很容易想到如果t里有1&#xff0c;s就全0&#xff0c;否则s就全…...

基于java在线个人网站源码设计与实现

摘 要 随着社会及个人社交应用平台的飞速发展&#xff0c;人们的沟通成本逐渐降低&#xff0c;互联网信息的普及也进一步提升了人们对于信息的需求度&#xff0c;通过建立个人网站的方式来展示自己的生活信息同时利用平台结交新的朋友&#xff0c;借助个人网站平台的搭建不仅可…...

Ubuntu18.04下编译qgc源码

写在前面 在下载前必须说明&#xff0c;根据你的qgc源码版本进行下载&#xff0c;有的源码必须要求Qt是5.15版本以上。 个人所使用开发软件 版本QT5.12.9qgc源码V4.0Ubuntu18.04 QT下载 &#xff08;1&#xff09;我们可以去官网下载官网下载地址具体的下载方法这里不用多说&a…...

Ros2_windows_install的学习笔记

Ros2_windows_install安装 Iron安装 iex ((New-Object System.Net.WebClient).DownloadString(https://raw.githubusercontent.com/scottcandy34/ros2_windows_install/main/ros2_iron.ps1))启动Iron C:\dev\ros2_iron\local_setup.bat...

5、Kubernetes核心技术 - Controller控制器工作负载

目录 一、Deployments - 控制器应用 二、Deployment升级回滚和弹性收缩 2.1、创建一个 1.14 版本的 pod 2.2、应用升级 2.3、查看升级状态 2.4、查看历史版本 2.5、应用回滚 2.6、弹性伸缩 三、StatefulSet - 有状态应用 四、DaemonSet - 守护进程 五、Job - 单次任…...

【java设计模式】创建型模式介绍(工厂模式、抽象工厂模式、单例模式、建造者模式、原型模式)

文章目录 简介一、工厂模式介绍案例 二、抽象工厂模式介绍案例 三、单例模式介绍案例 四、建造者模式介绍案例 五、原型模式介绍案例 简介 本文介绍Java设计模式中创建型模式的五种 一、工厂模式 工厂模式&#xff08;Factory Pattern&#xff09;是 Java 中最常用的设计模式…...

Redis系列:Redis 的事务机制

1 复习下何为事务机制&#xff1f; Transaction&#xff08;事务&#xff09;是计算机的特有术语&#xff0c;它一般指单个逻辑工作单位&#xff0c;由一系列的操作组合而成&#xff0c;在这些操作执行的时候&#xff0c;要么都执行成功&#xff0c;要么都不执行&#xff0c;防…...

动静态网页、Django创建表关系、Django框架的请求生命周期流程图

一、request对象的几个方法 在视图函数中写方法的时候&#xff0c;都会有一个形参requestdef index(request):passrequest.method # GET POST request.GET.get() # 它获取最后一个元素值 request.GET.getlist() # 获取到所有的request.POST.get() # 它获取最后一个元素值 req…...

神经网络的初始化方法

文章目录 1、随机初始化2、Xavier初始化3、He初始化4、权重预训练初始化5、零初始化 对于神经网络的训练过程中&#xff0c;合适的参数初始化方法有助于更好的处理梯度消失和梯度爆炸问题。通常有以下几种初始化方法&#xff1a; 1、随机初始化 随机初始化&#xff08;Random…...

【SQL Server】DBCC CHECKDB只是一个数据库维护命令吗?

日期&#xff1a;2023年7月27日 作者&#xff1a;Commas 签名&#xff1a;(ง •_•)ง 积跬步以致千里,积小流以成江海…… 注释&#xff1a;如果您觉得有所帮助&#xff0c;帮忙点个赞&#xff0c;也可以关注我&#xff0c;我们一起成长&#xff1b;如果有不对的地方&#xf…...

三、Web安全相关知识

请勿用于非法用途 文章目录 一、Web源码框架二、目录结构1、静态资源2、WEB-INF&#xff08;1&#xff09;classes&#xff08;2&#xff09;lib&#xff08;3&#xff09;web.xml 二、web脚本语言1、脚本种类&#xff08;1&#xff09;ASP&#xff08;2&#xff09;ASP.NET&am…...

Android系统服务之AMS

目录 概述 重点和难点问题 启动方式 main入口&#xff1a; run方法&#xff1a; BootstrapSevices 小结&#xff1a; 与其他线程的通信原理 参考文档&#xff1a; 概述 AMS是Android系统主要负责四大组件的启动&#xff0c;切换&#xff0c;调度以及应用程序进程管理和调度等工…...

Unity UGUI的EventTrigger (事件监听器)组件的介绍及使用

Unity UGUI的EventTrigger (事件监听器)组件的介绍及使用 1. 什么是EventTrigger组件&#xff1f; EventTrigger是Unity UGUI中的一个组件&#xff0c;用于监听和响应UI元素的各种事件&#xff0c;例如点击、拖拽、进入、离开等。通过EventTrigger组件&#xff0c;我们可以方…...

Matlab的SimuLink对FS32K144编程--内部数据存储Flash

​​​​​​​ ​​​​​​​ ​​​​​​​ ​​​​​​​ ​​​​​​​ 前言 Flah擦写是由寿命的&#xff0c;应当减免无效的擦写&#xff0c;如数据值不变不进行擦写 1、新建工程完成后&#xff0c;拖出Flash的存储控制初始化…...

【MySQL】centos 7下MySQL的环境搭建

从本期博客开始我们正式进入到数据库的学习&#xff0c;在学习数据库时所用到的工具是Linux环境下的MySQL 目录 一、检查环境中是否装有MySQL 二、获取MySQL官方yum源 三、配置MySQL官方yum源 四、一键安装MySQL 五、启动mysql服务 六、登录MySQL 七、修改mysql配置文件…...

【SpringCloud Alibaba】(四)使用 Feign 实现服务调用的负载均衡

在上一文中&#xff0c;我们实现了服务的自动注册与发现功能。但是还存在一个很明显的问题&#xff1a;如果用户微服务和商品微服务在服务器上部署多份的话&#xff0c;之前的程序无法实现服务调用的负载均衡功能。 本文就带着大家一起实现服务调用的负载均衡功能 1. 负载均衡…...

ShardingSphere-Proxy水平分片详解与实战

&#x1f680; ShardingSphere &#x1f680; &#x1f332; 算法刷题专栏 | 面试必备算法 | 面试高频算法 &#x1f340; &#x1f332; 越难的东西,越要努力坚持&#xff0c;因为它具有很高的价值&#xff0c;算法就是这样✨ &#x1f332; 作者简介&#xff1a;硕风和炜&…...

PTA 1052 Linked List Sorting

个人学习记录&#xff0c;代码难免不尽人意。 A linked list consists of a series of structures, which are not necessarily adjacent in memory. We assume that each structure contains an integer key and a Next pointer to the next structure. Now given a linked li…...

五,Eureka 第五章

5.3.2 修改pom添加依赖 <dependencies><!--公共部门--><dependency><groupId>cn.bdqn</groupId><artifactId>springcloud-api-commons</artifactId><version>${project.version}</version></dependency><!--e…...

yolov5目标框的融合(两个或多个框)

框的融合 1.多个框的融合 方法一: import os import numpy as np import glob import cv2 from PIL import Image,ImageFont,ImageDraw import randomCOLORS = np.random.uniform(0, 255, size=...

pythonAPI对接示API示例电商数据平台

下面是一个简单的示例&#xff0c;展示了如何对接一个API&#xff0c;并附带了一些Python代码作为参考。 寻找合适的API&#xff1a;首先&#xff0c;你需要找到符合你需求的API。你可以通过搜索引擎或者开发者平台来查找API文档。确保你在使用API时遵循相关的规则和限制。 注…...

如何做好IT类的技术面试

目录 一、IT行业的招聘渠道 二、如何做好技术面试官 三、谈谈IT行业如何做好招聘工作 四、面试IT公司的小技巧 五、面试有哪些常见的问题 六、关于面试的一些建议 面试可能是我们每个人都必须会遇到的事情&#xff0c;而技术面试更具有专业性&#xff0c;以下会从几个方面…...

比memcpy还要快的内存拷贝,了解一下

前言 朋友们有想过居然还有比memcpy更快的内存拷贝吗&#xff1f; 讲道理&#xff0c;在这之前我没想到过&#xff0c;我也一直觉得memcpy就是最快的内存拷贝方法了。 也不知道老板最近是咋了&#xff0c;天天开会都强调&#xff1a;“我们最近的目标就一个字&#xff0c;性能优…...

正则表达式常用字符及案例

引言 正则表达式是一种强大而灵活的工具&#xff0c;它在文本搜索和处理中起到了至关重要的作用。熟练掌握正则表达式的常用字符和使用方法&#xff0c;将能帮助开发者更加高效地进行模式匹配和字符串操作。本文将介绍一些常见的正则表达式字符&#xff0c;并给出一些实际案例…...

周训龙老兵参观广西森林安全紧急救援装备演练

7月21日上午&#xff0c;周训龙老兵参观广西紧急救援促进中心在南宁市青秀山举行森林安全紧急救援装备演练&#xff0c;多功能水罐消防车、无人救援机等先进设备轮番上阵&#xff0c;展示了广西应对突发事件的紧急救援速度和水平。广西壮族自治区应急厅不情愿参此次演练活动。 …...

[开发|java] java 将json转化java对象

使用Jackson库将JSON转换为Java对象&#xff1a; 安装依赖 <!-- Jackson Core --> <dependency><groupId>com.fasterxml.jackson.core</groupId><artifactId>jackson-core</artifactId><version>2.12.5</version> </depen…...

平台化的测试工具推荐|一站式测试平台RunnerGo

互联网行业的发展到今天越来越多的公司更加注重工作效率和团队协作&#xff0c;越来越多的产品也趋于平台化&#xff0c;平台化也更有利于提高团队效率&#xff0c;代码管理、持续构建、持续部署这些工具的发展都是非常超前的&#xff0c;它们对于团队协作的支持和工作效率的提…...

PCB封装设计指导(十五)验证封装的正确性

PCB封装设计指导(十五)验证封装的正确性 封装建立好之后,我们需要验证封装是否能够正常的放入PCB文件中,最好最直接的办法就是直接放入PCB中来验证。 具体操作如下 任意新建一个空白的PCB文件点击File 选择NEW...