1.Mybatis关联查询时,查到对象但对象属性为null
- 原因:没有映射相关列,或者在数据查询时没有关联相关表。Mybatis封装对象时,需要先从数据库查到相关列再封装为对象。
//RoleMapper.xml
<resultMap id="BaseResultMap" type="entity.Role">
//映射主键
<id column="ROLE_ID" property="roleId" />
<result column="NAME" property="name" />
//映射实体中的List集合
<-- select="mapper.ModuleMapper.findModuleByRoleId":此处为映射相关Mapper中的方法,全限定名 -->
<collection property="modules" select="mapper.ModuleMapper.findModuleByRoleId" column="ROLE_ID">
</collection>
</resultMap>
//ModuleMapper.xml
<mapper namespace="mapper.ModuleMapper">
<resultMap id="BaseResultMap" type="entity.Module">
<id column="MODULE_ID" jdbcType="DECIMAL" property="moduleId" />
<result column="NAME" jdbcType="VARCHAR" property="name" />
</resultMap>
2.Mybatis的批量删除的sql语句的写法
<!-- 删除并不是真的删除,只是把状态改为2 -->
<delete id="deleteByRecommenderId" parameterType="hashmap">
update account set status = #{status},
pause_date=null,
close_date=SYSDATE
where account_id in
<!-- map:Collection也是使用集合名 -->
<foreach collection="ids" item="id" open="("
close=")" separator=",">
#{id}
</foreach>
</delete>
3.Junit测试方法中正常,但到Controller层出错
- 原因是没加@Autowired注解或者配置文件中没有注入相应的Bean
4.SpringMVC中参数为Map
- 进入Controller方法中时,要封装的方法参数为Map时要加@RequestParam注解。
5.Restful方式进行状态修改
//JSP页面
//开通
function start_account(id) {
var r = window.confirm("确定要开通此账务账号吗?");
if(r) {
location.href = "${pageContext.request.contextPath}/account/startAccount/"+id;
}
}
//暂停
function pause_account(id) {
var r = window.confirm("确定要暂停此账务账号吗?");
if(r) {
location.href = "${pageContext.request.contextPath}/account/pauseAccount/"+id;
}
}
/**
* 启动帐号
* @PathVariable("recommenderId"):此注解作用获取路径上的请求参数
* @param recommenderId
* @return
*/
@RequestMapping(value="/startAccount/{recommenderId}",method=RequestMethod.GET)
public String startAccount(@PathVariable("recommenderId")Integer recommenderId){
as.updateStatus(recommenderId, "0");
return "redirect:/account/findAccount";
}
/**
* 暂停帐号
* @param recommenderId
* @return
*/
@RequestMapping(value="/pauseAccount/{recommenderId}",method=RequestMethod.GET)
public String pauseAccount(@PathVariable("recommenderId")Integer recommenderId){
as.updateStatus(recommenderId, "1");
//暂停account帐号时,同步暂停其下属的service帐号
sm.pauseByAccount(recommenderId);
return "redirect:/account/findAccount";
}
6.Mybatis的关联查询
<!-- 一个管理员可以拥有多个角色 -->
<resultMap id="adminResultMap" type="admin">
<id column="admin_id" property="admin_id" />
<collection ofType="role"
//property:实体类中映射的集合名
//javaType:集合roles的类型
//column:通过哪一列关联,指数据库中的列名
//select:查询的方法
property="roles" javaType="java.util.ArrayList" column="admin_id"
select="selectRolesByAdmin">
</collection>
</resultMap>
/**
* sql查询语句
*/
<select id="findByPage" parameterType="adminQueryBean" resultMap="adminResultMap">
select * from (
select a.*,ROWNUM r from (
select * from admin_info
where admin_id in (
select a.admin_id
from admin_info a
join admin_role ar on a.admin_id=ar.admin_id
join role_info ri on ri.role_id=ar.role_id
join role_module rm on rm.role_id=ri.role_id
<where>
<if test="roleName!=null && roleName.length()>0">
and instr(ri.name,#{roleName})>0
</if>
<if test="moduleId!=null">
and rm.module_id=#{moduleId}
</if>
</where>
)
order by admin_id
) a where ROWNUM<=#{end}
) where r>=#{begin}
</select>
<!-- 关联查询管理员拥有的所有角色,就是adminResultMap中Collections中映射的方法 -->
<select id="selectRolesByAdmin" parameterType="int" resultType="role">
select * from role_info where role_id in (
select role_id from admin_role
where admin_id=#{id}
)
</select>
<!-- 关联查询Role下面的所有Module -->
<resultMap id="roleMap" type="role">
<id column="role_id" property="role_id" />
<collection ofType="module"
property="modules" column="role_id" select="selectModulesByRoleId">
</collection>
</resultMap>
<!-- 通过role_id查询该role_id拥有的所有mudule -->
<select id="selectModulesByRoleId"
parameterType="int"
resultType="module">
select * from module_info where module_id in (
select module_id from role_module
where role_id=#{role_id}
)
</select>
先查询到Admin,然后在ResultMap通过admin_id(数据库中的列名)关联查询所拥有的Role
再通过RoleMapper.xml中的ResultMap关联查询Module
7.SpringMVC的登陆拦截和权限验证
- 1.在登录的Controller层将登录的用户添加到session中
- 2.将admin所拥有的权限放到session中
@RequestMapping("/login.do")
@ResponseBody
public Map<String, Object> login(String adminCode,String password,String code,HttpSession session) {
Map<String, Object> result = new HashMap<String, Object>();
//imageCode:生成的验证码
String imageCode = (String) session.getAttribute("imageCode");
//code:输入的验证码,判断与生成的是否一致
if(code == null
|| !code.equalsIgnoreCase(imageCode)) {
result.put("flag", IMAGE_CODE_ERROR);
return result;
}
//校验帐号:adminCode:帐号
Admin admin = am.findByCode(adminCode);
if(admin == null) {
result.put("flag", ADMIN_CODE_ERROR);
return result;
//校验密码:
} else if (!admin.getPassword().equals(password)) {
result.put("flag", PASSWORD_ERROR);
return result;
} else {
//登录成功把admin对象放到session中
session.setAttribute("admin", admin);
//查看当前登录用户拥有的所有权限(即模块):
//存储到session当中
//权限校验需要:
List<Module> modules = am.findModulesByAdmin(admin.getAdmin_id());
session.setAttribute("allModules", modules);
result.put("flag", SUCCESS);
return result;
}
}
//进入目标controller之前
//登录拦截,判断session中是否含用户,有的话放行,没有跳转到登录页面。
//因为如果已经登录后的话Controller中已经将用户添加到session中了
@Override
public boolean preHandle(HttpServletRequest request,
HttpServletResponse response, Object obj) throws Exception {
System.err.println("loginIntercptor");
Admin admin = (Admin) request.getSession().getAttribute("admin");
if(admin == null) {
response.sendRedirect(
//request.getContextPath():获取绝对路径
request.getContextPath() + "/login/toLogin.do");
//阻止请求
return false;
} else {
//http://localhost:8080/NetCtoss/role/findRoles.do
//return true:放行请求
return true;
}
}
//权限拦截,此拦截器作用是获取要访问的模块权限,并将模块权限对应的数字添加到session,与后面拦截器中用户拥有的权限进行比对
@Override
public boolean preHandle(HttpServletRequest request,HttpServletResponse response, Object obj) throws Exception {
// 判断当前用户访问的模块
//获取请求地址
String url = request.getRequestURL().toString();
System.err.println("url=" + url);
int currentModule = 0; // 默认0是NETCTOSS首页,登录
//通过url是否包含要判断访问的模块,并将访问模块对应的数字添加到session中,在下个拦截器中进行比对
if (url.contains("role")) {
currentModule = 1;
} else if (url.contains("admin")) {
currentModule = 2;
} else if (url.contains("cost")) {
currentModule = 3;
} else if (url.contains("account")) {
currentModule = 4;
} else if (url.contains("service")) {
currentModule = 5;
}
request.getSession().setAttribute("currentModule", currentModule);
//把请求放行到下一个拦截器处理
return true;
}
//进行权限验证,在上个拦截器中已经添加了要访问的模块到session中
//此拦截器作用是将要访问的模块权限与此用户所用的权限模块进行比对,如果包含允许访问,否则不准。
@SuppressWarnings("unchecked")
@Override
public boolean preHandle(HttpServletRequest request,
HttpServletResponse response, Object obj) throws Exception {
//获取登录用户有权限的所有模块
List<Module> modules = (List<Module>)
request.getSession().getAttribute("allModules");
//获取用户当前要访问的模块
int currentModule = (Integer)
request.getSession().getAttribute("currentModule");
System.err.println("currentModule : " + currentModule);
//判断用户有权限的模块是否包含当前模块
for (Module module : modules) {
if (module.getModule_id() == currentModule) {
//有当前访问模块的权限
//放行到Controller中
return true;
}
}
//没有当前访问模块的权限
response.sendRedirect(
request.getContextPath() + "/login/nopower.do");
return false;
}