标签:java、mybatis
parameterType 和 resultType
parameterType:单个参数用String,多个参数用map
resultType: 可以是 Integer、String、Object
<select id="countGroupMasterByUid" parameterType="String" resultType="Integer"> SELECT COUNT(id) FROM t_mc_store_group_master WHERE u_id = #{uid} </select> <select id="countGroupMaster" parameterType="hashmap" resultType="Integer"> SELECT COUNT(1) FROM t_mc_store_group_master gm, t_mc_store_group g WHERE gm.id = g.master_id AND gm.u_id = #{uid} AND g.id = #{groupId} </select> <select id="queryMasterIdByUid" parameterType="String" resultType="String"> SELECT id FROM t_mc_store_group_master WHERE u_id = #{uid} </select> <select id="queryMasterByGroupId" parameterType="String" resultType="com.xxx.smart.group.model.GetGroupMember"> SELECT gm.u_id mid, gm.u_name mName, 1 mTag, 1 pb_list, 1 pb_view, 1 pb_down, 1 pb_upload, 1 pb_delete, 1 pb_rename, 1 pb_share, 2 isAdmin, 1 isMaster FROM t_mc_store_group g, t_mc_store_group_master gm WHERE gm.id = g.master_id AND g.id = #{groupId} </select>
对应的xx接口解析
selectKey的使用
<!-- 这里需要说明一下不同的数据库主键的生成,对各自的数据库有不同的方式: -->
<!-- mysql:SELECT LAST_INSERT_ID() AS VALUE -->
<!-- mssql:select @@IDENTITY as value -->
<!-- oracle:SELECT STOCKIDSEQUENCE.NEXTVAL AS VALUE FROM DUAL -->
<!-- 还有一点需要注意的是不同的数据库生产商生成主键的方式不一样,有些是预先生成 (pre-generate)主键的,如Oracle和PostgreSQL。
有些是事后生成(post-generate)主键的,如MySQL和SQL Server 所以如果是Oracle数据库,则需要将selectKey写在insert之前 -->
注意:插入的时候id可以省略,但masterId必须对应的上AddMaster这个JavaBean的masterId属性。
表结构如下:
MySQL [mcloud]> desc t_mc_store_group_master;
+-------------+--------------+------+-----+-------------------+-----------------------------+
| Field | Type | Null | Key | Default | Extra |
+-------------+--------------+------+-----+-------------------+-----------------------------+
| id | bigint(16) | NO | PRI | NULL | auto_increment |
| u_id | varchar(128) | NO | MUL | NULL | |
| u_name | varchar(128) | YES | | NULL | |
| dept_name | varchar(128) | YES | | NULL | |
| create_time | timestamp | YES | | CURRENT_TIMESTAMP | |
| update_time | timestamp | YES | | CURRENT_TIMESTAMP | on update CURRENT_TIMESTAMP |
+-------------+--------------+------+-----+-------------------+-----------------------------+
<insert id="addMaster" parameterType="com.xxx.smart.group.model.AddMaster" > INSERT INTO t_mc_store_group_master (id,u_id,u_name,dept_name) VALUES (#{masterId},#{uid},#{uName},#{deptName}) <selectKey resultType="Integer" order="AFTER" keyProperty="masterId"> SELECT LAST_INSERT_ID() AS masterId </selectKey> </insert>
本文出自 “Curiosity” 博客,请务必保留此出处http://flash09.blog.51cto.com/8831225/1909761
标签:java、mybatis
原文地址:http://flash09.blog.51cto.com/8831225/1909761