xdxxdx
www.xdxxdxxdx.com

有时候我们的主键是自增的,但是我们想要在插入一条数据以后获取这条数据的主键值,而我们知道,mybatis执行完插入操作以后返回的是生效的记录数。那如何才能获取这个主键值呢。

1.在配置文件mapper.xml中加入如下语句。

 1     <insert id="insertSelectiveRePk" parameterType="com.xdx.entity.TMenu"
 2         useGeneratedKeys="true" keyProperty="menuId" keyColumn="menu_id">
 3         insert into t_menu
 4         <trim prefix="(" suffix=")" suffixOverrides=",">
 5             <if test="menuId != null">
 6                 menu_id,
 7             </if>
 8             <if test="menuName != null">
 9                 menu_name,
10             </if>
11             <if test="menuType != null">
12                 menu_type,
13             </if>
14             <if test="menuLevel != null">
15                 menu_level,
16             </if>
17             <if test="menuIcon != null">
18                 menu_icon,
19             </if>
20             <if test="menuSrc != null">
21                 menu_src,
22             </if>
23             <if test="rMenuId != null">
24                 r_menu_id,
25             </if>
26             <if test="pMenuId != null">
27                 p_menu_id,
28             </if>
29             <if test="menuIntro != null">
30                 menu_intro,
31             </if>
32             <if test="priority1 != null">
33                 priority1,
34             </if>
35             <if test="priority2 != null">
36                 priority2,
37             </if>
38             <if test="priority3 != null">
39                 priority3,
40             </if>
41             <if test="createTime != null">
42                 create_time,
43             </if>
44             <if test="updateTime != null">
45                 update_time,
46             </if>
47             <if test="isDel != null">
48                 is_del,
49             </if>
50         </trim>
51         <trim prefix="values (" suffix=")" suffixOverrides=",">
52             <if test="menuId != null">
53                 #{menuId,jdbcType=INTEGER},
54             </if>
55             <if test="menuName != null">
56                 #{menuName,jdbcType=VARCHAR},
57             </if>
58             <if test="menuType != null">
59                 #{menuType,jdbcType=INTEGER},
60             </if>
61             <if test="menuLevel != null">
62                 #{menuLevel,jdbcType=INTEGER},
63             </if>
64             <if test="menuIcon != null">
65                 #{menuIcon,jdbcType=VARCHAR},
66             </if>
67             <if test="menuSrc != null">
68                 #{menuSrc,jdbcType=VARCHAR},
69             </if>
70             <if test="rMenuId != null">
71                 #{rMenuId,jdbcType=INTEGER},
72             </if>
73             <if test="pMenuId != null">
74                 #{pMenuId,jdbcType=INTEGER},
75             </if>
76             <if test="menuIntro != null">
77                 #{menuIntro,jdbcType=VARCHAR},
78             </if>
79             <if test="priority1 != null">
80                 #{priority1,jdbcType=INTEGER},
81             </if>
82             <if test="priority2 != null">
83                 #{priority2,jdbcType=INTEGER},
84             </if>
85             <if test="priority3 != null">
86                 #{priority3,jdbcType=INTEGER},
87             </if>
88             <if test="createTime != null">
89                 #{createTime,jdbcType=TIMESTAMP},
90             </if>
91             <if test="updateTime != null">
92                 #{updateTime,jdbcType=TIMESTAMP},
93             </if>
94             <if test="isDel != null">
95                 #{isDel,jdbcType=INTEGER},
96             </if>
97         </trim>
98     </insert>

注意第二行的几句代码:useGeneratedKeys="true" keyProperty="menuId" keyColumn="menu_id"

useGeneratedKeys="true" :使用自增的主键

keyProperty="menuId"  :主键对应的java实体的成员

keyColumn="menu_id" :主键在数据库中的列名

2.在插入后就可以直接获取主键值了,代码如下

baseDao.addT("TMenuMapper.insertSelectiveRePk", menu);//插入
int key=menu.getMenuId();//直接获取主键

 其中menu就是我们要保存的实体。

posted on 2018-03-29 23:45  xdxxdx  阅读(507)  评论(0编辑  收藏  举报