Category: Java

MyBatis批量更新问题

今天碰到个奇怪的问题,MyBatis批量更新数据时,在MySQL可以正常执行,但在PostgreSQL数据库上,有时会出现错误。 更新的sql语句如下,用户名称如果不为null才更新,age和sex无论是否为null都会更新: 错误提示大概如下: 这里如果只是简单的更新出错,那么倒没有什么可说。奇怪的地方在于,在PostgreSQL数据库批量更新时,如果列表中任意一条记录age和sex都有值,仍然可以正常更新,但如果列表中所有记录的age和sex都为null,那么就会出现上面的错误。出现错误的原因估计是因为列表所有元素两个字段值都为null时,更新代码不能判断要更新字段值的类型,转换成text类型进行处理了。 解决的方法很简单,就是在更新字段上面指明类型,即修改下面两处即可: when id = #{item.id} then #{item.age, jdbcType=INTEGER} when id = #{item.id} then #{item.sex, jdbcType=SMALLINT} 猜测问题可能是在MyBatis框架?既然已经参数类型parameterType=UserInfo,那么属于该类的字段应该能够默认添加类型才对? 问题出现原因: 其他备忘: 突然想起当时为啥使用sql来进行更新了。 这个问题是在项目中碰到的,之所以使用mapper写sql进行更新还是有原因的。一开始的写法不仅简单,而且还不会出现这个问题,就是在实体中使用注解标识,为null也进行更新,例如: @TableField(value = “age”, updateStrategy = FieldStrategy.IGNORED) 但是有人觉得项目是他接手的,就得按照他的规则来,然后说使用mybatis自动生成的就不能修改,如果有修改,代码自动生成后会毫不犹豫覆盖。当时代码一开始是我生成,后面他有修改过,在做新需求时,发现表都重建了,新增时会自动生成默认值的status字段、创建日期,更新日期字段都变成无默认值了,实体类也被直接覆盖了,根本不看原来的代码是怎样的,实体注解标识id为自动生成、以及设置字段更新策略的注解都没了。 @TableId(value = “id”, type = IdType.AUTO) @TableField(updateStrategy = FieldStrategy.IGNORED) 当时也很傻,觉得跟这种人打交道很烦,就问了下他数据库表字段定义是不是有变更了,也没有怎么说他就默默的在生成器添加了设置id自动生成的注解重新生成,新增对象时也手动设置了状态、创建时间和更新时间,还重新使用sql写了批量更新。

发布java library到Maven Central仓库

Publishing a Java Library to Maven Central 网络上有很多文章有介绍如何发布,很多都说得很详细,这里大概记录一下一些相关信息。 相关网址: 发布申请:sonatype官网 需要注册,并创建issue填写要发布项目的相关信息 官方文档,如何发布: Deploying to OSSRH with Apache Maven – Introduction gpg密钥相关: The GNU Privacy Guard官网 可以下载相关软件用于生成和管理gpg密钥,我使用的版本是 gpg (GnuPG) 2.3.7, libgcrypt 1.10.1,需要注意的是不同版本一些命令可能会有不同 发布时会先发布到repo1仓库,如果发布成功的话,正常30分钟左右就在上面看到,然后大概要4个小时才会同步到Maven Central 项目配置的仓库url 现在是s01开头这个,如果你的issue已经验证了groupId,并且指派者已经处理好的话,可以使用之前在官网注册的用户和密码登录这个仓库。 发布备忘 创建要发布的项目,以及配置好pom pom需要包含的信息: 项目url : <url><https://github.com/william-xu/java-basic></url> 我这里项目仓库在github网站。 开发者信息 <developers> 许可证信息 <licenses> 发布管理 <distributionManagement> 配置要发布到的仓库的信息 scm信息(software configuration management)<scm> 【build】发布到maven central仓库,需要 nexus-staging-maven-plugin […]

关于 @SuppressWarnings

Java compilers are increasingly capable of issuing helpful “lint-like” warnings. To encourage the use of such warnings, there should be some way to disable a warning in a part of the program when the programmer knows that the warning is inappropriate. The annotation type SuppressWarnings supports programmer control over warnings otherwise issued by a Java compiler. It […]

Guidelines for Wildcard Use

转自jdk8文档 One of the more confusing aspects when learning to program with generics is determining when to use an upper bounded wildcard and when to use a lower bounded wildcard. This page provides some guidelines to follow when designing your code. For purposes of this discussion, it is helpful to think of variables as providing […]

volatile 以及同步相关内容

volatile Fields The Java programming language allows threads to access shared variables (§17.1). As a rule, to ensure that shared variables are consistently and reliably updated, a thread should ensure that it has exclusive use of such variables by obtaining a lock that, conventionally, enforces mutual exclusion for those shared variables. The Java programming language provides […]

Java备忘

Java 11 与 Java 8 的区别 日期 Java 11 多了 LocalDate.EPOCH 常量的定义, Java 8没有这个变量 Java中标签的使用 An unlabeled break statement terminates the innermost switch, for, while, or do-while statement, but a labeled break terminates an outer statement.  Java中标签主要是用来中断或者跳转到指定循环(非中断/跳转语句直接所在的循环),以下是随手写的示例代码,仅为展示标签使用: 如果随机生成的l值是10的倍数,则中断do-while循环;如果l值对10取模余数是2,则跳转到第二个for循环。 Maven 3.3.1+ requires JDK 1.7+. Maven 3.3.1+ requires JDK 1.7+. Please set appropriate JDK at Settings | Build, Execution, Deployment | Build Tools | Maven | […]

简单Java Applet示例

下午看到一个涉及到Java Applet的问题,发现自己已经都不知道多少年没接触Applet了,稍微看一下,运行了个简单示例。 JDK8 – Java Applets A Java applet is a special kind of Java program that a browser enabled with Java technology can download from the internet and run. An applet is typically embedded inside a web page and runs in the context of a browser. An applet must be a subclass of the java.applet.Applet class. […]

Java Queues

Queue A collection designed for holding elements prior to processing. Besides basic Collection operations, queues provide additional insertion, extraction, and inspection operations. Each of these methods exists in two forms: one throws an exception if the operation fails, the other returns a special value (either null or false, depending on the operation). The latter form of the insert operation is […]

Java 17 available now

Java 17 LTS is the latest long-term support release for the Java SE platform. JDK 17 binaries are free to use in production and free to redistribute, at no cost, under the Oracle No-Fee Terms and Conditions. JDK 17 will receive updates under these terms, until at least September 2024. JDK 17 Documentation Java Language Updates […]