6 插入数据的时候获取自增的id

张创琦 2022.03.12

  1. 在navicat上创建一个sql文件
  1. CREATE TABLE `gamerecord` (
  2. `recordId` varchar(36) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NOT NULL,
  3. `homeTeamId` int DEFAULT NULL COMMENT '主队ID',
  4. `gameDate` datetime DEFAULT CURRENT_TIMESTAMP COMMENT '比赛日期',
  5. `score` int DEFAULT NULL COMMENT '得分',
  6. `visitingTeamId` int DEFAULT NULL COMMENT '客队ID', PRIMARY KEY (`recordId`),
  7. KEY `homeTeamId` (`homeTeamId`),
  8. KEY `visitingTeamId` (`visitingTeamId`),
  9. CONSTRAINT `gamerecord_ibfk_1` FOREIGN KEY (`homeTeamId`) REFERENCES `team` (`teamId`), CONSTRAINT `gamerecord_ibfk_2` FOREIGN KEY (`visitingTeamId`) REFERENCES `team` (`teamId`)
  10. ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci;
  1. 添加实体类
  1. package com.kkb.pojo;
  2. import java.util.Date;
  3. /**
  4. * ClassName: GameRecord
  5. * 球队记录实体类
  6. * @author wanglina
  7. * @version 1.0
  8. */
  9. public class GameRecord {
  10. private String recordId;
  11. private Integer homeTeamId;
  12. private Date gameDate;
  13. private Integer score;
  14. private Integer visitingTeamId;
  15. //省略set get
  16. public String getRecordId() {
  17. return recordId;
  18. }
  19. public void setRecordId(String recordId) {
  20. this.recordId = recordId;
  21. }
  22. public Integer getHomeTeamId() {
  23. return homeTeamId;
  24. }
  25. public void setHomeTeamId(Integer homeTeamId) {
  26. this.homeTeamId = homeTeamId;
  27. }
  28. public Date getGameDate() {
  29. return gameDate;
  30. }
  31. public void setGameDate(Date gameDate) {
  32. this.gameDate = gameDate;
  33. }
  34. public Integer getScore() {
  35. return score;
  36. }
  37. public void setScore(Integer score) {
  38. this.score = score;
  39. }
  40. public Integer getVisitingTeamId() {
  41. return visitingTeamId;
  42. }
  43. public void setVisitingTeamId(Integer visitingTeamId) {
  44. this.visitingTeamId = visitingTeamId;
  45. }
  46. }
  1. 添加mapper接口
  1. package com.kkb.mapper;
  2. import com.kkb.pojo.GameRecord;
  3. /**
  4. * ClassName: GameRecord
  5. * GameRecordMapper接口
  6. * @author wanglina
  7. * @version 1.0
  8. */
  9. public interface GameRecordMapper {
  10. int add(GameRecord record);
  11. }
  1. 添加 TeamRecordMapper.xml 文件
  1. <?xml version="1.0" encoding="UTF-8" ?>
  2. <!DOCTYPE mapper
  3. PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
  4. <!--namespace="名称必须与映射的类的名字一致,是完全限定名"-->
  5. <mapper namespace="com.kkb.mapper.GameRecordMapper">
  6. <!-- 添加一条比赛记录 -->
  7. <insert id="add" parameterType="com.kkb.pojo.GameRecord" >
  8. <!--插入数据之前先获取36位字符串作为id放入属性recordId中order="AFTER/BEFORE" 在insert执行之前还是之后resultType="返回值的类型" -->
  9. <selectKey keyProperty="recordId" order="BEFORE" resultType="java.lang.String"> select uuid()
  10. </selectKey>
  11. INSERT INTO `ssm1`.`gamerecord` (`recordId`, `homeTeamId`, `gameDate`, `score`, `visitingTeamId`) VALUES (#{recordId}, #{homeTeamId},default, #{score}, #{visitingTeamId})
  12. </insert>
  13. </mapper>
  1. mybatis.xml 中注册映射文件
  1. <!-- 注册映射文件 -->
  2. <mappers>
  3. <mapper resource="com/kkb/pojo/Team.xml"/>
  4. <mapper resource="com/kkb/mapper/TeamMapper.xml"></mapper>
  5. <mapper resource="com/kkb/mapper/GameRecordMapper.xml"></mapper>
  6. </mappers>
  1. 编写测试类
    但是会乱码
  1. package com.kkb.test;
  2. import com.kkb.mapper.GameRecordMapper;
  3. import com.kkb.pojo.GameRecord;
  4. import com.kkb.utils.MybatisUtil;
  5. import org.apache.ibatis.session.SqlSession;
  6. import org.junit.Test;
  7. /**
  8. * ClassName: GameRecordMapperTest
  9. * GameRecordMapper测试类
  10. * @author wanglina
  11. * @version 1.0
  12. */
  13. public class GameRecordMapperTest {
  14. private SqlSession sqlSession = MybatisUtil.getSqlSession();
  15. // 调试成功
  16. @Test
  17. public void testAdd(){
  18. GameRecordMapper mapper = sqlSession.getMapper(GameRecordMapper.class);
  19. GameRecord record = new GameRecord();
  20. record.setHomeTeamId(1007);
  21. record.setVisitingTeamId(1002);
  22. record.setScore(118);
  23. int num = mapper.add(record); sqlSession.commit(); // 必须提交才能让增删改生效
  24. System.out.println("add结果:"+num);
  25. System.out.println(record.getRecordId());
  26. }
  27. }

运行结果说明:

先获取 suuid(主键)

在执行插入语句。