SpringBoot + MyBatis(注解版),常用的SQL方法

一、新建项目及配置

1.1 新建一个SpringBoot项目,并在pom.xml下加入以下代码

  <dependency>
    <groupId>org.mybatis.spring.boot</groupId><artifactId>mybatis-spring-boot-starter</artifactId><version>2.0.1</version></dependency>

  application.properties文件下配置(使用的是MySql数据库)

# 注:我的SpringBoot 是2.0以上版本,数据库驱动如下
spring.datasource.driverClassName=com.mysql.cj.jdbc.Driver spring.datasource.url=jdbc:mysql://127.0.0.1:3306/database?characterEncoding=utf8&serverTimezone=UTC spring.datasource.username=your_username spring.datasource.password=your_password

# 可将 com.dao包下的dao接口的SQL语句打印到控制台,学习MyBatis时可以开启
logging.level.com.dao=debug

  SpringBoot启动类Application.java 加入@SpringBootApplication 注解即可(一般使用该注解即可,它是一个组合注解)

@SpringBootApplication
public class Application {public static void main(String[] args) {SpringApplication.run(Application.class, args);}
}

  之后dao层的接口文件放在Application.java 能扫描到的位置即可,dao层文件使用@Mapper注解

@Mapper
public interface UserDao {/*** 测试连接*/@Select("select 1 from dual")int testSqlConnent();
}

测试接口能返回数据即表明连接成功

二、简单的增删查改sql语句

  2.1 传入参数

  (1) 可以传入一个JavaBean

  (2) 可以传入一个Map

  (3) 可以传入多个参数,需使用@Param("ParamName") 修饰参数

  2.2 Insert,Update,Delete 返回值

  接口方法返回值可以使用 void 或 int,int返回值代表影响行数

  2.3 Select 中使用@Results 处理映射

  查询语句中,如何名称不一致,如何处理数据库字段映射到Java中的Bean呢?

  (1) 可以使用sql 中的as 对查询字段改名,以下可以映射到User 的 name字段

  @Select("select "1" as name from dual")User testSqlConnent();

  (2) 使用 @Results,有 @Result(property="Java Bean name", column="DB column name"), 例如:

   @Select("select t_id, t_age, t_name  "+ "from sys_user             "+ "where t_id = #{id}        ")@Results(id="userResults", value={@Result(property="id",   column="t_id"),@Result(property="age",  column="t_age"),@Result(property="name", column="t_name"),})
   User selectUserById(@Param("id") String id);

  对于resultMap 可以给与一个id,其他方法可以根据该id 来重复使用这个resultMap。例如:

   @Select("select t_id, t_age, t_name  "+ "from sys_user             "+ "where t_name = #{name}        ")@ResultMap("userResults")User selectUserByName(@Param("name") String name);

  2.4 注意一点,关于JavaBean 的构造器问题

  我在测试的时候,为了方便,给JavaBean 添加了一个带参数的构造器。后面在测试resultMap 的映射时,发现把映射关系@Results 注释掉,返回的bean 还是有数据的;更改查询字段顺序时,出现 java.lang.NumberFormatException: For input string: "hello"的异常。经过测试,发现是bean 的构造器问题。并有以下整理:

  (1) bean 只有一个有参的构造方法,MyBatis 调用该构造器(参数按顺序),此时@results 注解无效。并有查询结果个数跟构造器不一致时,报异常。

  (2) bean 有多个构造方法,且没有 无参构造器,MyBatis 调用跟查询字段数量相同的构造器;若没有数量相同的构造器,则报异常。

  (3) bean 有多个构造方法,且有 无参构造器, MyBatis 调用无参数造器。

  (4) 综上,一般情况下,bean 不要定义有参的构造器;若需要,请再定义一个无参的构造器。

  2.5 简单查询例子

   /*** 测试连接*/@Select("select 1 from dual")int testSqlConnent();/*** 新增,参数是一个bean*/@Insert("insert into sys_user       "+ "(t_id, t_name, t_age)    "+ "values                   "+ "(#{id}, #{name}, ${age}) ")int insertUser(User bean);/*** 新增,参数是一个Map*/@Insert("insert into sys_user       "+ "(t_id, t_name, t_age)    "+ "values                   "+ "(#{id}, #{name}, ${age}) ")int insertUserByMap(Map<String, Object> map);/*** 新增,参数是多个值,需要使用@Param来修饰* MyBatis 的参数使用的@Param的字符串,一般@Param的字符串与参数相同*/@Insert("insert into sys_user       "+ "(t_id, t_name, t_age)    "+ "values                   "+ "(#{id}, #{name}, ${age}) ")int insertUserByParam(@Param("id") String id, @Param("name") String name,@Param("age") int age);/*** 修改*/@Update("update sys_user set  "+ "t_name = #{name},  "+ "t_age  = #{age}    "+ "where t_id = #{id} ")int updateUser(User bean);/*** 删除*/@Delete("delete from sys_user  "+ "where t_id = #{id}  ")int deleteUserById(@Param("id") String id);/*** 删除*/@Delete("delete from sys_user ")int deleteUserAll();/*** truncate 返回值为0*/@Delete("truncate table sys_user ")void truncateUser();/*** 查询bean* 映射关系@Results* @Result(property="java Bean name", column="DB column name"),*/@Select("select t_id, t_age, t_name  "+ "from sys_user             "+ "where t_id = #{id}        ")@Results(id="userResults", value={@Result(property="id",   column="t_id"),@Result(property="age",  column="t_age"),@Result(property="name", column="t_name", javaType = String.class),})User selectUserById(@Param("id") String id);/*** 查询List*/@ResultMap("userResults")@Select("select t_id, t_name, t_age "+ "from sys_user            ")List<User> selectUser();@Select("select count(*) from sys_user ")int selectCountUser();

三、MyBatis动态SQL

  注解版下,使用动态SQL需要将sql语句包含在script标签里

<script></script>

3.1 if

  通过判断动态拼接sql语句,一般用于判断查询条件

<if test=''>...</if>

3.2 choose

  根据条件选择

<choose><when test=''> ...</when><when test=''> ...</when><otherwise> ...</otherwise> 
</choose>

3.3 where,set

  一般跟if 或choose 联合使用,这些标签或去掉多余的 关键字 或 符号。如

<where><if test="id != null "> and t_id = #{id}</if>
</where>

  若id为null,则没有条件语句;若id不为 null,则条件语句为 where t_id = ? 

<where> ... </where>
<set> ... </set>

3.4 bind

  绑定一个值,可应用到查询语句中

<bind name="" value="" />

3.5 foreach

  循环,可对传入和集合进行遍历。一般用于批量更新和查询语句的 in

<foreach item="item" index="index" collection="list" open="(" separator="," close=")">#{item}
</foreach>

  (1) item:集合的元素,访问元素的Filed 使用 #{item.Filed}

  (2) index: 下标,从0开始计数

  (3) collection:传入的集合参数

  (4) open:以什么开始

  (5) separator:以什么作为分隔符

  (6) close:以什么结束

  例如 传入的list 是一个 List<String>: ["a","b","c"],则上面的 foreach 结果是: ("a", "b", "c")

3.6 动态SQL例子

    /*** if 对内容进行判断* 在注解方法中,若要使用MyBatis的动态SQL,需要编写在<script></script>标签内* 在 <script></script>内使用特殊符号,则使用java的转义字符,如  双引号 "" 使用&quot;&quot; 代替* concat函数:mysql拼接字符串的函数*/@Select("<script>"+ "select t_id, t_name, t_age                          "+ "from sys_user                                       "+ "<where>                                             "+ "  <if test='id != null and id != &quot;&quot;'>     "+ "    and t_id = #{id}                                "+ "  </if>                                             "+ "  <if test='name != null and name != &quot;&quot;'> "+ "    and t_name like CONCAT('%', #{name}, '%')       "+ "  </if>                                             "+ "</where>                                            "+ "</script>                                           ")@Results(id="userResults", value={@Result(property="id",   column="t_id"),@Result(property="name", column="t_name"),@Result(property="age",  column="t_age"),})List<User> selectUserWithIf(User user);/*** choose when otherwise 类似Java的Switch,选择某一项* when...when...otherwise... == if... if...else... */@Select("<script>"+ "select t_id, t_name, t_age                                     "+ "from sys_user                                                  "+ "<where>                                                        "+ "  <choose>                                                     "+ "      <when test='id != null and id != &quot;&quot;'>          "+ "            and t_id = #{id}                                   "+ "      </when>                                                  "+ "      <otherwise test='name != null and name != &quot;&quot;'> "+ "            and t_name like CONCAT('%', #{name}, '%')          "+ "      </otherwise>                                             "+ "  </choose>                                                    "+ "</where>                                                       "+ "</script>                                                      ")@ResultMap("userResults")List<User> selectUserWithChoose(User user);/*** set 动态更新语句,类似<where>*/@Update("<script>                                           "+ "update sys_user                                  "+ "<set>                                            "+ "  <if test='name != null'> t_name=#{name}, </if> "+ "  <if test='age != null'> t_age=#{age},    </if> "+ "</set>                                           "+ "where t_id = #{id}                               "+ "</script>                                        ")int updateUserWithSet(User user);/*** foreach 遍历一个集合,常用于批量更新和条件语句中的 IN* foreach 批量更新*/@Insert("<script>                                  "+ "insert into sys_user                    "+ "(t_id, t_name, t_age)                   "+ "values                                  "+ "<foreach collection='list' item='item'  "+ " index='index' separator=','>           "+ "(#{item.id}, #{item.name}, #{item.age}) "+ "</foreach>                              "+ "</script>                               ")int insertUserListWithForeach(List<User> list);/*** foreach 条件语句中的 IN*/@Select("<script>"+ "select t_id, t_name, t_age                             "+ "from sys_user                                          "+ "where t_name in                                        "+ "  <foreach collection='list' item='item' index='index' "+ "    open='(' separator=',' close=')' >                 "+ "    #{item}                                            "+ "  </foreach>                                           "+ "</script>                                              ")@ResultMap("userResults")List<User> selectUserByINName(List<String> list);/*** bind 创建一个变量,绑定到上下文中*/@Select("<script>                                              "+ "<bind name=\"lname\" value=\"'%' + name + '%'\"  /> "+ "select t_id, t_name, t_age                          "+ "from sys_user                                       "+ "where t_name like #{lname}                          "+ "</script>                                           ")@ResultMap("userResults")List<User> selectUserWithBind(@Param("name") String name);

 

四、MyBatis 对一,对多查询

  首先看@Result注解源码

public @interface Result {boolean id() default false;String column() default "";String property() default "";Class<?> javaType() default void.class;JdbcType jdbcType() default JdbcType.UNDEFINED;Class<? extends TypeHandler> typeHandler() default UnknownTypeHandler.class;One one() default @One;Many many() default @Many;
}

  可以看到有两个注解 @One 和 @Many,MyBatis就是使用这两个注解进行对一查询和对多查询

  @One 和 @Many写在@Results 下的 @Result 注解中,并需要指定查询方法名称。具体实现看以下代码

// User Bean的属性
private String id;
private String name;
private int age;
private Login login; // 每个用户对应一套登录账户密码
private List<Identity> identityList; // 每个用户有多个证件类型和证件号码// Login Bean的属性
private String username;
private String password;// Identity Bean的属性
private String idType;
private String idNo;
   /*** 对@Result的解释* property:       java bean 的成员变量* column:         对应查询的字段,也是传递到对应子查询的参数,传递多参数使用Map column = "{param1=SQL_COLUMN1,param2=SQL_COLUMN2}"* one=@One:       对一查询* many=@Many:     对多查询* select:         需要查询的方法,全称或当前接口的一个方法名
   * fetchType.EAGER: 急加载
*/@Select("select t_id, t_name, t_age "+ "from sys_user "+ "where t_id = #{id} ")@Results({@Result(property="id", column="t_id"),@Result(property="name", column="t_name"),@Result(property="age", column="t_age"),@Result(property="login", column="t_id",one=@One(select="com.github.mybatisTest.dao.OneManySqlDao.selectLoginById", fetchType=FetchType.EAGER)),@Result(property="identityList", column="t_id",many=@Many(select="selectIdentityById", fetchType=FetchType.EAGER)),})User2 selectUser2(@Param("id") String id);/*** 对一 子查询*/@Select("select t_username, t_password "+ "from sys_login "+ "where t_id = #{id} ")@Results({@Result(property="username", column="t_username"),@Result(property="password", column="t_password"),})Login selectLoginById(@Param("id") String id);/*** 对多 子查询*/@Select("select t_id_type, t_id_no "+ "from sys_identity "+ "where t_id = #{id} ")@Results({@Result(property="idType", column="t_id_type"),@Result(property="idNo", column="t_id_no"),})List<Identity> selectIdentityById(@Param("id") String id);

  测试结果,可以看到只调用一个方法查询,相关对一查询和对多查询也会一并查询出来

// 测试类代码
    @Testpublic void testSqlIf() {User2 user = dao.selectUser2("00000005");System.out.println(user);System.out.println(user.getLogin());System.out.println(user.getIdentityList());}    // 查询结果User2 [id=00000005, name=name_00000005, age=5]Login [username=the_name, password=the_password][Identity [idType=01, idNo=12345678], Identity [idType=02, idNo=987654321]]

五、MyBatis开启事务

5.1 使用 @Transactional 注解开启事务

  @Transactional标记在方法上,捕获异常就rollback,否则就commit。自动提交事务。

  /*** @Transactional 的参数* value                   |String                        | 可选的限定描述符,指定使用的事务管理器* propagation             |Enum: Propagation             | 可选的事务传播行为设置* isolation               |Enum: Isolation               | 可选的事务隔离级别设置* readOnly                |boolean                       | 读写或只读事务,默认读写* timeout                 |int (seconds)                 | 事务超时时间设置* rollbackFor             |Class<? extends Throwable>[]  | 导致事务回滚的异常类数组* rollbackForClassName    |String[]                      | 导致事务回滚的异常类名字数组* noRollbackFor           |Class<? extends Throwable>[]  | 不会导致事务回滚的异常类数组* noRollbackForClassName  |String[]                      | 不会导致事务回滚的异常类名字数组*/@Transactional(timeout=4)public void testTransactional() {// dosomething..
}

5.2 使用Spring的事务管理

  如果想使用手动提交事务,可以使用该方法。需要注入两个Bean,最后记得提交事务。

  @Autowiredprivate DataSourceTransactionManager dataSourceTransactionManager;@Autowiredprivate TransactionDefinition transactionDefinition;public void testHandleCommitTS(boolean exceptionFlag) {
//        DefaultTransactionDefinition transactionDefinition = new DefaultTransactionDefinition();
//        transactionDefinition.setPropagationBehavior(TransactionDefinition.PROPAGATION_REQUIRES_NEW);// 开启事务TransactionStatus transactionStatus = dataSourceTransactionManager.getTransaction(transactionDefinition);try {// dosomething
// 提交事务 dataSourceTransactionManager.commit(transactionStatus);} catch (Exception e) {e.printStackTrace();// 回滚事务 dataSourceTransactionManager.rollback(transactionStatus);}}

六、使用SQL语句构建器

  MyBatis提供了一个SQL语句构建器,可以让编写sql语句更方便。缺点是比原生sql语句,一些功能可能无法实现。

  有两种写法,SQL语句构建器看个人喜好,我个人比较熟悉原生sql语句,所有挺少使用SQL语句构建器的。

  (1) 创建一个对象循环调用方法

new SQL().DELETE_FROM("user_table").WHERE("t_id = #{id}").toString()

  (2) 内部类实现 

new SQL() {{DELETE_FROM("user_table");
   WHERE("t_id = #{id}"); }}.toString();

  需要实现ProviderMethodResolver接口,ProviderMethodResolver接口属于比较新的api,如果找不到这个接口,更新你的mybatis的版本,或者Mapper的@SelectProvider注解需要加一个 method注解, @SelectProvider(type = UserBuilder.class, method = "selectUserById")

  继承ProviderMethodResolver接口,Mapper中可以直接指定该类即可,但对应的Mapper的方法名要更Builder的方法名相同 。

Mapper:
@SelectProvider(type = UserBuilder.class)
public User selectUserById(String id);UserBuilder:
public static String selectUserById(final String id)...

  dao层代码:(建议在dao层中的方法加上@see的注解,并指示对应的方法,方便后期的维护)

  /*** @see UserBuilder#selectUserById()*/@Results(id ="userResults", value={@Result(property="id",   column="t_id"),@Result(property="name", column="t_name"),@Result(property="age",  column="t_age"),})@SelectProvider(type = UserBuilder.class)User selectUserById(String id);/*** @see UserBuilder#selectUser(String)*/@ResultMap("userResults")@SelectProvider(type = UserBuilder.class)List<User> selectUser(String name);/*** @see UserBuilder#insertUser()*/@InsertProvider(type = UserBuilder.class)int insertUser(User user);/*** @see UserBuilder#insertUserList(List)*/@InsertProvider(type = UserBuilder.class)int insertUserList(List<User> list);/*** @see UserBuilder#updateUser()*/@UpdateProvider(type = UserBuilder.class)int updateUser(User user);/*** @see UserBuilder#deleteUser()*/@DeleteProvider(type = UserBuilder.class)int deleteUser(String id);

  Builder代码:

public class UserBuilder implements ProviderMethodResolver {private final static String TABLE_NAME = "sys_user";public static String selectUserById() {return new SQL().SELECT("t_id, t_name, t_age").FROM(TABLE_NAME).WHERE("t_id = #{id}").toString();}public static String selectUser(String name) {SQL sql = new SQL().SELECT("t_id, t_name, t_age").FROM(TABLE_NAME);if (name != null && name != "") {sql.WHERE("t_name like CONCAT('%', #{name}, '%')");}return sql.toString();}public static String insertUser() {return new SQL().INSERT_INTO(TABLE_NAME).INTO_COLUMNS("t_id, t_name, t_age").INTO_VALUES("#{id}, #{name}, #{age}").toString();}/*** 使用SQL Builder进行批量插入* 关键是sql语句中的values格式书写和访问参数* values格式:values (?, ?, ?) , (?, ?, ?) , (?, ?, ?) ...* 访问参数:MyBatis只能读取到list参数,所以使用list[i].Filed访问变量,如 #{list[0].id}*/public static String insertUserList(List<User> list) {SQL sql = new SQL().INSERT_INTO(TABLE_NAME).INTO_COLUMNS("t_id, t_name, t_age");StringBuilder sb = new StringBuilder();for (int i = 0; i < list.size(); i++) {if (i > 0) {sb.append(") , (");}sb.append("#{list[");sb.append(i);sb.append("].id}, ");sb.append("#{list[");sb.append(i);sb.append("].name}, ");sb.append("#{list[");sb.append(i);sb.append("].age}");}sql.INTO_VALUES(sb.toString());return sql.toString();}public static String updateUser() {return new SQL().UPDATE(TABLE_NAME).SET("t_name = #{name}", "t_age = #{age}").WHERE("t_id = #{id}").toString();}public static String deleteUser() {return new SQL().DELETE_FROM(TABLE_NAME).WHERE("t_id = #{id}").toString();

  或者  Builder代码:

public class UserBuilder2 implements ProviderMethodResolver {private final static String TABLE_NAME = "sys_user";public static String selectUserById() {return new SQL() {{SELECT("t_id, t_name, t_age");FROM(TABLE_NAME);WHERE("t_id = #{id}");}}.toString();}public static String selectUser(String name) {return new SQL() {{SELECT("t_id, t_name, t_age");FROM(TABLE_NAME);if (name != null && name != "") {WHERE("t_name like CONCAT('%', #{name}, '%')");                }}}.toString();}public static String insertUser() {return new SQL() {{INSERT_INTO(TABLE_NAME);INTO_COLUMNS("t_id, t_name, t_age");INTO_VALUES("#{id}, #{name}, #{age}");}}.toString();}public static String updateUser(User user) {return new SQL() {{UPDATE(TABLE_NAME);SET("t_name = #{name}", "t_age = #{age}");WHERE("t_id = #{id}");}}.toString();}public static String deleteUser(final String id) {return new SQL() {{DELETE_FROM(TABLE_NAME);WHERE("t_id = #{id}");}}.toString();}
}

 七、附属链接

7.1 MyBatis官方源码 

  https://github.com/mybatis/mybatis-3 ,源码的测试包跟全面,更多的使用方法可以参考官方源码的测试包

7.2 MyBatis官方中文文档

  http://www.mybatis.org/mybatis-3/zh/index.html ,官方中文文档,建议多阅读官方的文档

7.3 我的测试项目地址,可做参考

  https://github.com/caizhaokai/spring-boot-demo ,SpringBoot+MyBatis+Redis的demo,有兴趣的可以看一看

转载于:https://www.cnblogs.com/caizhaokai/p/10982727.html

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/387626.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

项目进行JVM调优 Jconsole

最近对公司的项目进行JVM调优&#xff0c;使用了JDK自带的jconsole查看Tomcat运行情况&#xff0c;记录下配置以便以后参考&#xff1a; 首先&#xff0c;修改Tomcat的bin目录下的catalina.bat文件&#xff0c;在JAVA_OPTS变量中添加下面四行&#xff0c;即可 set JAVA_OPTS %J…

ECharts 点击非图表区域的点击事件不触发问题

1. 通过 myChart.getZr().on(click, fn) 监听整个图表的点击事件&#xff0c;注册回调 myChart.getZr().on(click, () > {//拿到index即可取出被点击数据的所有信息console.log(clickIndex) }) 2. 在 tooltip 的 formatter 函数中&#xff0c;每次调用都记录下需要的参数&am…

强大的django-debug-toolbar,django项目性能分析工具

强大的django-debug-toolbar,django项目性能分析工具 给大家介绍一个用于django中debug模式下查看网站性能等其他信息的插件django-debug-toolbar 首先安装 pip install django-debug-toolbar 接下来在自己django项目中的settings中添加配置 INSTALLED_APPS [debug_toolbar,]M…

个人作业——软件工程实践总结

一、请回望暑假时的第一次作业&#xff0c;你对于软件工程课程的想象 1&#xff09;对比开篇博客你对课程目标和期待&#xff0c;“希望通过实践锻炼&#xff0c;增强计算机专业的能力和就业竞争力”&#xff0c;对比目前的所学所练所得&#xff0c;在哪些方面达到了你的期待和…

利用jdk自带的运行监控工具JConsole观察分析Java程序的运行 Jtop

利用jdk自带的运行监控工具JConsole观察分析Java程序的运行 原文链接 一、JConsole是什么 从Java 5开始 引入了 JConsole。JConsole 是一个内置 Java 性能分析器&#xff0c;可以从命令行或在 GUI shell 中运行。您可以轻松地使用 JConsole&#xff08;或者&#xff0c;它更高端…

java版电子商务spring cloud分布式微服务b2b2c社交电商:服务容错保护(Hystrix断路器)...

断路器断路器模式源于Martin Fowler的Circuit Breaker一文。“断路器”本身是一种开关装置&#xff0c;用于在电路上保护线路过载&#xff0c;当线路中有电器发生短路时&#xff0c;“断路器”能够及时的切断故障电路&#xff0c;防止发生过载、发热、甚至起火等严重后果。在分…

微信小程序页面跳转、逻辑层模块化

一、页面的跳转 微信小程序的页面跳转函数方法有两个&#xff0c;另外还有两种模块跳转方式。 函数跳转&#xff1a; 1.wx.navigateTo(OBJECT)&#xff1a; wx.navigateTo({url: test?id1})//保留当前页面&#xff0c;跳转到应用内的某个页面&#xff0c;使用wx.navigateBack可…

java内存溢出分析工具:jmap使用实战

java内存溢出分析工具&#xff1a;jmap使用实战 在一次解决系统tomcat老是内存撑到头&#xff0c;然后崩溃的问题时&#xff0c;使用到了jmap。 1 使用命令 在环境是linuxjdk1.5以上&#xff0c;这个工具是自带的&#xff0c;路径在JDK_HOME/bin/下 jmap -histo pid>a.log…

Oracle加密解密

Oracle内部有专门的加密包&#xff0c;可以很方便的对内部数据进行加密&#xff08;encrypt&#xff09;和解密&#xff08;decrypt&#xff09;. 介绍加密包之前&#xff0c;先简单说一下Oracle基本数据类型——RAW类型。 RAW&#xff0c;用于保存位串的数据类型&#xff0c;类…

条件变量 sync.Cond

sync.Cond 条件变量是基于互斥锁的&#xff0c;它必须有互斥锁的支撑才能发挥作用。 sync.Cond 条件变量用来协调想要访问共享资源的那些线程&#xff0c;当共享资源的状态发生变化的时候&#xff0c;它可以用来通知被互斥锁阻塞的线程条件变量的初始化离不开互斥锁&#xff0c…

JDK内置工具使用

JDK内置工具使用 一、javah命令(C Header and Stub File Generator) 二、jps命令(Java Virtual Machine Process Status Tool) 三、jstack命令(Java Stack Trace) 四、jstat命令(Java Virtual Machine Statistics Monitoring Tool) 五、jmap命令(Java Memory Map) 六、jinfo命令…

mall整合RabbitMQ实现延迟消息

摘要 本文主要讲解mall整合RabbitMQ实现延迟消息的过程&#xff0c;以发送延迟消息取消超时订单为例。RabbitMQ是一个被广泛使用的开源消息队列。它是轻量级且易于部署的&#xff0c;它能支持多种消息协议。RabbitMQ可以部署在分布式和联合配置中&#xff0c;以满足高规模、高可…

竞价打板的关键点

竞价打板&#xff0c;主要是速度&#xff0c;其他不重要的&#xff0c;如果为了当天盈利大&#xff0c;失去竞价打板的本质含义&#xff0c;因为竞价可以买到&#xff0c;盘中买不到&#xff0c;才是竞价打板的目的&#xff0c;也就是从竞价打板的角度看&#xff0c;主要是看习…

Java常见的几种内存溢出及解决方法

Java常见的几种内存溢出及解决方法【情况一】&#xff1a;java.lang.OutOfMemoryError:Javaheapspace&#xff1a;这种是java堆内存不够&#xff0c;一个原因是真不够&#xff08;如递归的层数太多等&#xff09;&#xff0c;另一个原因是程序中有死循环&#xff1b;如果是java…

docker操作之mysql容器

1、创建宿主机器的挂载目录 /opt/docker/mysql/conf /opt/docker/mysql/data /opt/docker/mysql/logs 2、创建【xxx.cnf】配置文件&#xff0c;内容如下所示&#xff1a; [mysqld]#服务唯一Idserver-id 1port 3306log-error /var/log/mysql/error.log #只能用IP地址skip_nam…

Windows10系统下wsappx占用CPU资源过高?wsappx是什么?如何关闭wsappx进程?

在Windows10系统开机的时候&#xff0c;wsappx进程占用的CPU资源非常高&#xff0c;导致电脑运行速度缓慢&#xff0c;那么我们如何关闭wsappx进程&#xff0c;让电脑加快运行速度呢&#xff1f;下面就一起来看一下操作的方法吧。 【现象】 1、先来看一下电脑刚开机的时候&…

如何通过Windows Server 2008 R2建立NFS存储

如何通过Windows Server 2008 R2建立NFS存储在我们日常工作的某些实验中&#xff0c;会需要使用存储服务器。而硬件存储成本高&#xff0c;如StarWind之类的iSCSI软存储解决方案需要单独下载服务器端程序&#xff0c;且配置比较繁琐&#xff0c;令很多新手们很是头疼。事实上&a…

python-windows安装相关问题

1.python的环境配置&#xff0c;有些时候是没有配置的&#xff0c;需要在【系统环境】-【path】里添加。 2.安装pip&#xff1a;从官网下载pip包&#xff0c;然后到包目录》python setup.py install 安装 3.安装scrapyd&#xff1a;正常使用pip3 install scrapyd安装不起&…

hdu 1542/1255 Atlantis/覆盖的面积

1542 1255 两道扫描线线段树的入门题。 基本没有什么区别&#xff0c;前者是模板&#xff0c;后者因为是求覆盖次数至少在两次以上的&#xff0c;这个同样是具有并集性质的&#xff0c;所以把cover的判断条件更改一下就可以了qwq。 hdu1542 代码如下&#xff1a; #include<i…

使用了JDK自带的jconsole查看Tomcat运行情况

最近对公司的项目进行JVM调优&#xff0c;使用了JDK自带的jconsole查看Tomcat运行情况&#xff0c;记录下配置以便以后参考&#xff1a;首先&#xff0c;修改Tomcat的bin目录下的catalina.bat文件&#xff0c;在JAVA_OPTS变量中添加下面四行&#xff0c;即可set JAVA_OPTS %JAV…