- 在oracle中创建序列语句并执行:
create sequence user_seq start with 1 increment by 1;
在oracle创建触发器语句并执行:
create or replace trigger user_trigger before insert on tb_user for each row begin select user_seq.nextval into :new.id from dual; end ;
2.然后在mybatis的mapper配置文件中插入一条记录时就不用指定id
<?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.cn.mapper.UserMapper"> <insert id="save" parameterType="com.cn.domain.User" useGeneratedKeys="true"> INSERT INTO TB_USER(name,sex,age) VALUES(#{name},#{sex},${age}) </insert> </mapper>
时间: 2024-12-17 16:36:17