springboot/mybatis传实体类参数为空,通过设置jdbcType来减少mybatis代码量

在使用SpringBoot和Mybatis进行插入操作时,遇到实体类参数为空导致的错误。错误信息指出JdbcType OTHER类型不允许传空。解决方法包括在Mybatis中使用条件判断,对可为空的参数添加if判断,并且为参数指定合适的jdbcType,避免使用默认的JdbcType OTHER,从而成功执行insert操作。
摘要由CSDN通过智能技术生成

过去参数这块一直没有细究,往往是出问题后百度,解决后遗忘,下次继续百度

1.错误代码

mybatis中实现插入操作

<insert id="insertDetail" parameterType="com.user.po.Vaccine">
        insert into vaccine_question_t(empno,empname,will,status,road,location,reason,tel,create_time)
        values(#{empno},#{empname},#{will},
               #{status},#{road},#{location},
               #{reason},#{tel},sysdate)
    
好的,以下是使用SpringBoot整合mybatis实现简单增删改查操作的代码示例: 1. 首先在pom.xml添加依赖: ```xml <dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> <version>2.1.4</version> </dependency> ``` 2. 在application.properties配置数据库相关信息: ```properties # 数据库配置 spring.datasource.url=jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=UTF-8&serverTimezone=Asia/Shanghai spring.datasource.username=root spring.datasource.password=root spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver # Mybatis配置 mybatis.mapper-locations=classpath*:mapper/*.xml mybatis.type-aliases-package=com.example.demo.entity ``` 3. 创建实体类和Mapper接口: ```java // User实体类 public class User { private Long id; private String name; private Integer age; // getter和setter方法省略 } ``` ```java // UserMapper接口 public interface UserMapper { User selectById(Long id); List<User> selectAll(); void insert(User user); void update(User user); void delete(Long id); } ``` 4. 在resources/mapper目录下创建UserMapper.xml文件,定义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.demo.mapper.UserMapper"> <resultMap id="BaseResultMap" type="com.example.demo.entity.User"> <id column="id" property="id" jdbcType="BIGINT" /> <result column="name" property="name" jdbcType="VARCHAR" /> <result column="age" property="age" jdbcType="INTEGER" /> </resultMap> <select id="selectById" resultMap="BaseResultMap"> SELECT id, name, age FROM user WHERE id = #{id} </select> <select id="selectAll" resultMap="BaseResultMap"> SELECT id, name, age FROM user </select> <insert id="insert"> INSERT INTO user (name, age) VALUES (#{name}, #{age}) </insert> <update id="update"> UPDATE user SET name = #{name}, age = #{age} WHERE id = #{id} </update> <delete id="delete"> DELETE FROM user WHERE id = #{id} </delete> </mapper> ``` 5. 在Service调用Mapper接口: ```java @Service public class UserService { @Autowired private UserMapper userMapper; public User getUserById(Long id) { return userMapper.selectById(id); } public List<User> getAllUser() { return userMapper.selectAll(); } public void addUser(User user) { userMapper.insert(user); } public void updateUser(User user) { userMapper.update(user); } public void deleteUser(Long id) { userMapper.delete(id); } } ``` 最后,启动应用程序即可进行增删改查操作。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值