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