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>
对应的Java实现接口解析
我们可以基于org.mybatis.spring.sqlSessionTemplate提供的模板类来进行数据库操作的具体实现,例如以下是经过对模板的再封装进行实现的,封装类的过程做了日志记录操作等,在此具体不在细说:
@Override public int countGroupMasterByUid(String uid) { return getsqlSessionTemplate().selectOne("countGroupMasterByUid", uid); } @Override public int countGroupMaster(String uid, String groupId) { Map<String,String> map = new HashMap<String,String>(); map.put("uid", uid); map.put("groupId", groupId); return getsqlSessionTemplate().selectOne("countGroupMaster", map); } @Override public GetGroupMember queryMasterByGroupId(String groupId) { return getsqlSessionTemplate().selectOne("queryMasterByGroupId", groupId); } @Override public String queryMasterIdByUid(String uid) { return getsqlSessionTemplate().selectOne("queryMasterIdByUid", uid); } @Override public List<GroupMaster> queryMasterList(int index, int pageSize) { Map<String,Object> map = new HashMap<String,Object>(); map.put("index", (index-1)*pageSize); map.put("pageSize", pageSize); return getsqlSessionTemplate().selectList("queryMasterList",map); }
亦可以参考:http://lydia-fly.iteye.com/blog/2153102
动态sql参考:http://www.cnblogs.com/ywqbj/p/5707652.html
selectKey的使用
<!-- 这里需要说明一下不同的数据库主键的生成,对各自的数据库有不同的方式: --> 注意:插入的时候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>
组合查询设置不存在的列
<select id="queryUserList" parameterType="hashmap" resultType="com.xxx.smart.group.model.User"> SELECT tu.id casualId, tu.name name, tu.expire_time validdate, tu.pb_upload, tu.pb_down, tu.pb_share, tu.pb_rename, tu.pb_view, tu.pb_delete, tu.account, IF(ul.id>0,1,0) status FROM t_mc_store_casual_user tu JOIN t_mc_store_manage_department md ON tu.dept_id = md.dept_id LEFT JOIN t_mc_store_user_lock ul ON tu.account = ul.account WHERE md.u_id = #{uid} AND tu.is_delete = 0 LIMIT #{index},#{pageSize} </select>
原生sql语句
<select id="queryExpireUser" parameterType="hashmap" resultType="com.xxx.smart.group.model.ExpireUser"> <![CDATA[ SELECT account, password FROM t_mc_store_casual_user WHERE expire_time <= #{endTime} AND expire_time > #{beginTime} AND is_delete = 0 ]]> </select>