Oracle数据库存储过程
/*获取主菜单存储过程定义*/
create or replace procedure getMainMenu(
userAccount t_user.user_account%TYPE,
userPassword t_user.user_password%TYPE,
right_cursor out sys_refcursor
)
is
begin
open right_cursor for select * from t_right where right_id in (select right_id from t_role_right where
role_id in (select role_id from t_user_role where user_id in
(select user_Id from t_user where user_account=userAccount and user_password=userPassword))) and parent_id = 'root';
end;
/*获取子菜单存储过程定义*/
create or replace procedure getChildMenuByParentId(
userAccount t_user.user_account%TYPE,
userPassword t_user.user_password%TYPE,
parentId t_right.parent_id%TYPE,
right_cursor out sys_refcursor
)
is
begin
open right_cursor for select * from t_right where right_id in (select right_id from t_role_right where
role_id in (select role_id from t_user_role where user_id in
(select user_Id from t_user where user_account=userAccount and user_password=userPassword))) and parent_id = parentId;
end;
Mapper.xml
<!-- 获取主菜单 方法 parameterType 传入参数类型 statementType="CALLABLE" 固定写法注明调用oracle 存储过程
Call getMainMenu 返回游标名
#{user.userAccount},
#{user.userPassword}为传入实际参数
#{map.right_cursor}为接收oracle游标返回的数据
jdbcType=CURSOR,mode=OUT,javaType=java.sql.ResultSet, 固定写法
resultMap=com.ssyt.lms.mapper.RightInfoMapper.BaseResultMap 返回值类型必须全路径
BaseResultMap 为mapper.xml中的 -->
<resultMap id="BaseResultMap" type="com.ssyt.lms.po.RightInfo" >
<id column="RIGHT_ID" property="rightId" jdbcType="VARCHAR" />
<result column="PARENT_ID" property="parentId" jdbcType="VARCHAR" />
<result column="RIGHT_NAME" property="rightName" jdbcType="VARCHAR" />
<result column="RIGHT_URL" property="rightUrl" jdbcType="VARCHAR" />
</resultMap>
<select id="getMainMenus" parameterType="map" statementType="CALLABLE">
{call getMainMenu(
#{user.userAccount},
#{user.userPassword},
#{map.right_cursor,jdbcType=CURSOR,mode=OUT,javaType=java.sql.ResultSet, resultMap=com.ssyt.lms.mapper.RightInfoMapper.BaseResultMap})}
</select>
<select id="getChildMenusByParentId" parameterType="map" statementType="CALLABLE">
{call getChildMenuByParentId(
#{user.userAccount},
#{user.userPassword},
#{parentId},
#{map.right_cursor,jdbcType=CURSOR,mode=OUT,javaType=java.sql.ResultSet, resultMap=com.ssyt.lms.mapper.RightInfoMapper.BaseResultMap})}
</select>
Mapper.java
<!-- @Param("user") 将后面的userInfo 对象转化为 map< ‘user’,userInfo>
为了方便xml里面取到具体的值
-->
List<RightInfo> getMainMenus(@Param("user") UserInfo userInfo,@Param("map") Map<String,Object> map);
List<RightInfo> getChildMenusByParentId(@Param("user") UserInfo userInfo,@Param("map") Map<String,Object> map,@Param("parentId") String parentId);
Service.java
<接口方法>
/// 定义获取用户的主菜单
List<RightInfo> getMainMenus(UserInfo user);
/// 定义菜单返回的json字符串的业务方法
String getMenuJson(UserInfo user);
<实现类方法>
<主菜单>
@Override
public List<RightInfo> getMainMenus(UserInfo user) {
Map<String, Object> map = new HashMap<String, Object>();
map.put(CommonConstant.GET_MAIN_MENU_CURSOR.getKey(),new ArrayList<RightInfo>());
rightMapper.getMainMenus(user, map );
return (List<RightInfo>) map.get(CommonConstant.GET_MAIN_MENU_CURSOR.getKey());
}
<子菜单 需要模型 例如(mainmenus(id: ; menu list<childmenu> ))
Childmenu( text, items : list< item>)
Item( id: ; text: ; href)>
/**
* 查询用户子菜单(处理菜单模型)
*/
public String listChildMenu(UserInfo user, String baseUrl) {
// 定义存储主菜单集合对象
List<MainMenu> mainList = new ArrayList<MainMenu>();
// 获取获取当前用户的主菜单
List<RightInfo> rightList = listMenu(user);
if (rightList != null && rightList.size() > 0) {
// 循环遍历取出当前用户所持有的菜单项
for (RightInfo right : rightList) {
// 获取某一个菜单项的id和名字
String rightId = right.getRightId();
String rightName = right.getRightName();
// 创建一个主菜单MainMenu对象
MainMenu mainMenu = new MainMenu();
mainMenu.setId(rightId);
// 定义存储子菜单的集合
List<ChildMenu> childList = new ArrayList<ChildMenu>();
mainMenu.setMenu(childList);
// 新建map集合接收oracle存储过程返回值
Map<String, Object> map = new HashMap<String, Object>();
map.put(CommonConstant.GET_MAIN_MENU_CURSOR.getKey(),
new ArrayList<RightInfo>());
rightInfoMapper.selectChildMenu(user, map, rightId);
// 获取到存储过程返回值(菜单项所属的子菜单)
@SuppressWarnings("unchecked")
List<RightInfo> childRightList = (List<RightInfo>) map
.get(CommonConstant.GET_MAIN_MENU_CURSOR.getKey());
if (childRightList != null && childRightList.size() > 0) {
// 创建ChildMenu对象
ChildMenu childMenu = new ChildMenu();
// 子菜单所属菜单项也就是菜单项的名字
childMenu.setText(rightName);
// 创建存储子菜单项集合
List<Item> itemList = new ArrayList<Item>();
childMenu.setItems(itemList);
// 存储到childList对象中
childList.add(childMenu);
for (RightInfo child : childRightList) {
String childId = child.getRightId();
String childName = child.getRightName();
String childHref = child.getRightUrl();
// 创建一个Item对象(单个子菜单“权限”)
Item item = new Item();
item.setHref(baseUrl + "/" + childHref);
item.setId(childId);
item.setText(childName);
// 添加子菜单集合中
itemList.add(item);
}
}
// 将单个主菜单项放入菜单项集合中去
mainList.add(mainMenu);
}
}
// 将该集合转换为json对象的字符串
return JSON.toJSONString(mainList);
}
/**
* 查询用户子菜单(处理菜单模型)
*/
public String listChildMenu(UserInfo user, String baseUrl) {
// 定义存储主菜单集合对象
List<MainMenu> mainList = new ArrayList<MainMenu>();
// 获取获取当前用户的主菜单
List<RightInfo> rightList = listMenu(user);
if (rightList != null && rightList.size() > 0) {
// 循环遍历取出当前用户所持有的菜单项
for (RightInfo right : rightList) {
// 获取某一个菜单项的id和名字
String rightId = right.getRightId();
String rightName = right.getRightName();
// 创建一个主菜单MainMenu对象
MainMenu mainMenu = new MainMenu();
mainMenu.setId(rightId);
// 定义存储子菜单的集合
List<ChildMenu> childList = new ArrayList<ChildMenu>();
mainMenu.setMenu(childList);
// 新建map集合接收oracle存储过程返回值
Map<String, Object> map = new HashMap<String, Object>();
map.put(CommonConstant.GET_MAIN_MENU_CURSOR.getKey(),
new ArrayList<RightInfo>());
rightInfoMapper.selectChildMenu(user, map, rightId);
// 获取到存储过程返回值(菜单项所属的子菜单)
@SuppressWarnings("unchecked")
List<RightInfo> childRightList = (List<RightInfo>) map
.get(CommonConstant.GET_MAIN_MENU_CURSOR.getKey());
if (childRightList != null && childRightList.size() > 0) {
// 创建ChildMenu对象
ChildMenu childMenu = new ChildMenu();
// 子菜单所属菜单项也就是菜单项的名字
childMenu.setText(rightName);
// 创建存储子菜单项集合
List<Item> itemList = new ArrayList<Item>();
childMenu.setItems(itemList);
// 存储到childList对象中
childList.add(childMenu);
for (RightInfo child : childRightList) {
String childId = child.getRightId();
String childName = child.getRightName();
String childHref = child.getRightUrl();
// 创建一个Item对象(单个子菜单“权限”)
Item item = new Item();
item.setHref(baseUrl + "/" + childHref);
item.setId(childId);
item.setText(childName);
// 添加子菜单集合中
itemList.add(item);
}
}
// 将单个主菜单项放入菜单项集合中去
mainList.add(mainMenu);
}
}
// 将该集合转换为json对象的字符串
return JSON.toJSONString(mainList);
}