关于mybatis的使用之如何传递参数总结(一)
?<第一个例子>
修改删除这些没有返回值的操作,我们可以这样套用
1,action得到前台传过来的参数,service如何使用参数。
public void modifyUserRole(int userId, int roleId) {Map<String, Object> paramMap = new HashMap<String, Object>();paramMap.put("userId", userId);paramMap.put("roleId", roleId);userRoleMapper.modifyUserRole(paramMap);}
?
2,mapper.java和mapper.xml
public void modifyUserRole(Map<String, Object> map);
?
<update id="modifyUserRole" parameterType="java.util.HashMap"> update TS_USER_ROLE t set t.role_id=#{roleId} where t.user_id=#{userId}</update>
结语:这样就完了,就这么简单。
<第二个例子>
如果有返回值的,我们可以这样套用
1,action直接把前台传过来的参数去调用service
userRoleBean=this.userService.getUserRoleById(user.getId());
?
2,接着传,这是service里面
public UserRoleBean getUserRoleById(int userId) {return this.userRoleMapper.getUserRoleById(userId);}
?
3,mapper.java和mapper.xml
public UserRoleBean getUserRoleById(int userId);
?
<select id="getUserRoleById" resultMap="userRoleResultMap" parameterType="int"> select * from TS_USER_ROLE where user_id=#{userId}</select>
?
结语:接口的方法是用对象声明,mapper.mxl里面的resultMap也是这样返回。
?
<第三个例子>
如果此表里面的字段的外键是关联表的主键,那么,怎么相关联表的查询
1,bean里面这样写
private int id; // idprivate String name;// 姓名private String userName;// 用户名private String password;// 密码private int unitId;// 单位编码private int deptId;// 单位编码private int userType;// 用户类型private int state;// 用户状态private Dept dept;// 部门// deptId是外键,链接Dept表.// 还是把所有字段声明完整,然后再添加一个成员变量
?
2,,mapper.xml配置文件这样
?
<resultMap id="userResultMap" type="com.zjedusoft.gdqs.bean.User"><id property="id" column="id" /><result property="name" column="NAME" /><result property="userName" column="USER_NAME" /><result property="password" column="PASSWORD" /><result property="unitId" column="UNIT_ID" /><result property="userType" column="USER_TYPE" /><result property="state" column="STATE" /><result property="deptId" column="DEPT_ID"/><result property="using" column="using"/><association property="dept" column="DEPT_ID" javaType="com.zjedusoft.gdqs.bean.Dept" resultMap="deptResultMap"/>
?
?
然后使用是这样,这里多了其他的角色和权限,不管了,先贴上来吧。
<select id="getAllUserByPage" resultMap="userResultMap" parameterType="java.util.HashMap"> select zong.* from ( select temp.*,rownum rownum_ from( select u.*,r.role_name as role_name,d.id as did,d.dept_name as dept_name from ts_user_info u left join ts_user_role ur on u.id =ur.user_id left join ts_role r on ur.role_id=r.id left join ts_dept d on u.dept_id=d.id <where> <if test="keyword != '%null%'"> and u.USER_NAME like #{keyword} </if> <if test="keyword != '%null%'"> or u.NAME like #{keyword} </if> <if test="keyword != '%null%'"> or d.dept_name like #{keyword} </if> </where> order by u.id ) temp where rownum <= #{maxResult} ) zong where zong.rownum_ > #{skipResult} </select>
?
?
3,再看action,service,mapper都和其他一样
public List<User> getAllUserByPage(Map<String, Object> map);
?
<第四个例子>
那多对多,一对多怎么办呢。下面看。
1,还是bean里面
//多表查询,要把需要查询的collection当做属性get/setprivate UserRoleBean userRoleBean;private RoleBean roleBean;private RolePopedomBean rolePopedomBean;private PopedomBean popedomBean;private boolean using;// 是否被使用中
?
2.mapper.xml里面的
?
<collection property="userRoleBean" ofType="com.zjedusoft.gdqs.bean.UserRoleBean"><id property="id" column="id" /><result property="userId" column="USER_ID" /><result property="roleId" column="ROLE_ID" /></collection><collection property="roleBean" ofType="com.zjedusoft.gdqs.bean.RoleBean"><id property="id" column="id" /><result property="roleName" column="ROLE_NAME" /><result property="roleComments" column="ROLE_COMMENTS" /><result property="state" column="STATE" /></collection><collection property="rolePopedomBean" ofType="com.zjedusoft.gdqs.bean.RolePopedomBean"><id property="id" column="id" /><result property="roleId" column="ROLE_ID" /><result property="popedomId" column="POPEDOM_ID" /></collection><collection property="popedomBean" ofType="com.zjedusoft.gdqs.bean.PopedomBean"><id property="id" column="id" /><result property="LevleCode" column="LEVEL_CODE" /><result property="ParentId" column="PARENT_ID" /><result property="PopedomName" column="POPEDOM_NAME" /><result property="Url" column="URL" /><result property="PopedomComments" column="POPEDOM_COMMENTS" /><result property="type" column="TYPE" /><result property="state" column="STATE" /></collection></resultMap>
?
然后还是这样,呵呵。
<select id="getAllUserByPage" resultMap="userResultMap" parameterType="java.util.HashMap"> select zong.* from ( select temp.*,rownum rownum_ from( select u.*,r.role_name as role_name,d.id as did,d.dept_name as dept_name from ts_user_info u left join ts_user_role ur on u.id =ur.user_id left join ts_role r on ur.role_id=r.id left join ts_dept d on u.dept_id=d.id <where> <if test="keyword != '%null%'"> and u.USER_NAME like #{keyword} </if> <if test="keyword != '%null%'"> or u.NAME like #{keyword} </if> <if test="keyword != '%null%'"> or d.dept_name like #{keyword} </if> </where> order by u.id ) temp where rownum <= #{maxResult} ) zong where zong.rownum_ > #{skipResult} </select>
?