Mybatis double、int类型执行update语句的问题。

前提:一般执行update语法和insert语法时,都会用到if test的标签,动态条件来执行。

然后刚开始的时候发现,double类型字段if test标签也是写成了 <if test="xxx != null and xxx !=' ' ">  这个时候发现,当更新语法时,这个字段没有传入值时,被默认的改成0。

然后去搜索资料,说写成这种形式即可,<if test="xxx != null and xxx !=' ' or xxx != 0  ">  这样确实可以保证,当不传入xxx这个值时,不会去改动,不会变成0。

但是后来问题来了,当需要这个字段更新成0时,用这种就执行不了,查了好多资料,基本都不行。后来发现,在实体类中将这double 和 int 类型字段改成包装类即可。

包装类即将这些字段改成Object类型,因为8大数据类型不具备面向对象的功能,同String一样判断  把double改成DOUBLE,把int改成Integer即可,然后

<if test="xxx != null   "> 只需判断不等于null就可以完美解决。最后附上8大基本数据类型对应的包装类

基本数据类型及对应的包装类
基本数据类型对应的包装类
byteByte
shortShort
intInteger
longLong
charCharacter
floatFloat
doubleDouble
booleanBoolean
### 使用 MyBatisIntelliJ IDEA 中实现药店管理系统的教程 #### 创建 Maven 项目并配置依赖项 为了在 IntelliJ IDEA 中创建一个基于 MyBatis 的药店管理系统,首先需要设置一个新的 Maven 项目。确保 pom.xml 文件中包含了必要的依赖项: ```xml <dependencies> <!-- Spring Boot Starter --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <!-- MyBatis-SpringBoot-Starter --> <dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> <version>2.2.0</version> </dependency> <!-- MySQL Connector Java --> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <scope>runtime</scope> </dependency> <!-- Other dependencies as needed... --> </dependencies> ``` #### 配置数据源和 MyBatis 设置 编辑 application.properties 或者 application.yml 来定义数据库连接和其他 MyBatis 特定属性。 对于 `application.properties`: ```properties spring.datasource.url=jdbc:mysql://localhost:3306/pharmacy_management?useSSL=false&serverTimezone=UTC spring.datasource.username=root spring.datasource.password=password spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver # MyBatis configuration mybatis.mapper-locations=classpath:mapper/*.xml mybatis.type-aliases-package=com.example.pharmacymanagement.model ``` #### 设计实体 (Entity Classes) 根据数据库中的表结构来构建对应的实体文件。例如,如果有一个名为 "medicines" 的表格,则可以创建如下所示的一个 Medicine : ```java package com.example.pharmacymanagement.model; public class Medicine { private Long id; private String name; private Double price; private Integer stockQuantity; // Getters and Setters... } ``` #### 编写 Mapper 接口及其 XML 映射文件 为每一个业务对象编写接口以及关联的 SQL 查询语句映射文件。比如针对上面提到的 Medicine 对象,我们可以有这样一个 mapper 接口: ```java package com.example.pharmacymanagement.mapper; import org.apache.ibatis.annotations.Mapper; import java.util.List; @Mapper public interface MedicineMapper { List<Medicine> findAll(); void insert(Medicine medicine); int updateById(Long id, Medicine updatedFields); boolean deleteById(Long id); } ``` 接着,在 resources/mapper/ 下面放置相应的 XML 映射文件 (`MedicineMapper.xml`) ,其中包含具体的 CRUD 操作 SQL 文本: ```xml <?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd"> <mapper namespace="com.example.pharmacymanagement.mapper.MedicineMapper"> <select id="findAll" resultType="com.example.pharmacymanagement.model.Medicine"> SELECT * FROM medicines ORDER BY id ASC </select> <insert id="insert" parameterType="com.example.pharmacymanagement.model.Medicine"> INSERT INTO medicines(name,price,stock_quantity) VALUES(#{name}, #{price}, #{stockQuantity}) </insert> <update id="updateById" parameterType="map"> UPDATE medicines SET name=#{updatedFields.name}, price=#{updatedFields.price}, stock_quantity=#{updatedFields.stockQuantity} WHERE id = #{id}; </update> <delete id="deleteById" parameterType="long"> DELETE FROM medicines WHERE id = #{id} </delete> </mapper> ``` #### 测试与调试 完成上述步骤之后就可以运行应用程序来进行基本的功能测试了。可以通过单元测试或者 Postman 这样的 API 客户端工具去调用 RESTful Web Service 并验证其行为是否符合预期[^4]。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值