首先导入druid包,然后配置configPlugin
@Override
public void configPlugin(Plugins me) {
/**配置druid数据连接池插件**/
DruidPlugin dp=new DruidPlugin(getProperty("jdbcUrl"),getProperty("user"),getProperty("password").trim());
/**配置druid监控**/
dp.addFilter(new StatFilter());
WallFilter wall=new WallFilter();
wall.setDbType("mysql");
dp.addFilter(wall);
me.add(dp); ActiveRecordPlugin arp = new ActiveRecordPlugin(dp);
me.add(arp);
arp.setShowSql(true);
arp.addMapping("user", User.class);
}
其次,配置handle
@Override
public void configHandler(Handlers me) {
DruidStatViewHandler dvh=new DruidStatViewHandler("/druid",new IDruidStatViewAuth(){
public boolean isPermitted(HttpServletRequest request) {
// 这里只是简单的判断访问者是否登录,还可以做更加细致的权限控制
User user=(User) request.getSession().getAttribute("user");
if(user==null){
return false;
}
return "admin".equals(user.getStr("uname"));
}
});
me.add(dvh);
}
这样,只要输入 localhost/druid就可以了。这里设置只有admin登陆之后可以查看。