Java体系知识之监听器&数据库连接池
(1)监听器使用
(2)数据库连接池使用
1 监听器
1.1 技术简介
(1)JavaWeb三大组件:
Servlet | Filter | Listener
Servlet:动态网页编程技术
Filter和Listener:高级特性,辅助Servlet工作,让web程序增色
Filter:过滤请求资源 req resp
Listener:监听域对象相关的内容
(2)Servlet三个域对象:
request:HttpServletRequest 请求对象 一次请求一次响应 转发
session:HttpSession 会话对象 多次请求
context:ServletContext 服务器对象|全局对象
(3)Listener:
监听域对象的创建和销毁
request:ServletRequestListener
session:HttpSessionListener
context:ServletContextListener
1.2 使用步骤
(1)Servlet:
创建一个类,继承HttpServlet类;
重写方法;
配置:
web.xml;
注解;@WebServlet("/xx")
(2)Filter:
创建一个类,实现Filter接口;
重写方法;
配置:
web.xml;
注解;@WebFilter("/xx")
(3)Listener:
创建一个类,实现对应接口;
重写方法;
配置:
web.xml;
注解;@WebListener
1.3 案例实现
1.3.1 HttpSessionListenerDemo
package com.javasm.listener;
import javax.servlet.http.HttpSession;
import javax.servlet.http.HttpSessionEvent;
import javax.servlet.http.HttpSessionListener;
import java.util.HashMap;
import java.util.Map;
public class HttpSessionListenerDemo implements HttpSessionListener {
Map<String, HttpSession> online = new HashMap<>();
@Override
public void sessionCreated(HttpSessionEvent se) {
HttpSession session = se.getSession();
String sessionId = session.getId();
online.put(sessionId, session);
System.out.println("有用户上线了:" + sessionId);
System.out.println("目前用户在线情况:" + online);
session.setMaxInactiveInterval(5);
}
@Override
public void sessionDestroyed(HttpSessionEvent se) {
HttpSession session = se.getSession();
String sessionId = session.getId();
online.remove(sessionId);
System.out.println("有用户下线了:" + sessionId);
System.out.println("目前用户在线情况:" + online);
}
}
1.3.2 ServletContextListenerDemo
package com.javasm.listener;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import javax.servlet.annotation.WebListener;
@WebListener
public class ServletContextListenerDemo implements ServletContextListener {
@Override
public void contextInitialized(ServletContextEvent sce) {
System.out.println("服务器启动时,触发该方法执行");
System.out.println("加载第三方资源");
}
@Override
public void contextDestroyed(ServletContextEvent sce) {
System.out.println("服务器关闭时,触发该方法执行");
System.out.println("销毁资源");
}
}
2 数据库连接池
2.1 技术简介
(1)池化技术:
复用资源
(2)JDBC使用步骤:
加载驱动;
创建连接;
有语句对象;
处理结果集;
关闭连接;
(3)数据库连接:
资源宝贵;
创建数据库连接:耗时;
高效管理的数据库连接,提高程序的操作性能;
数据库连接池
(4)数据库连接池:
负责分配、管理、释放数据库连接;
重复使用数据库连接资源;
(4)常用数据库连接:
C3P0;
DBCP;
Druid;
(5)案例演示:
Druid
(6)项目改造:
数据库连接资源的初始化和销毁,伴随服务器的开启和关闭执行
2.2 案例实现
2.2.1 DBUtils2
package com.javasm.finance.util;
import com.alibaba.druid.pool.DruidDataSource;
import java.sql.*;
import java.util.Properties;
public class DBUtils2 {
static String user;
static String pwd;
static String url;
static String driver;
static DruidDataSource dataSource = new DruidDataSource();
static {
Properties properties = new Properties();
try {
properties.load(DBUtils2.class.getClassLoader().getResourceAsStream("jdbc.properties"));
driver = properties.getProperty("jdbc.driver");
url = properties.getProperty("jdbc.url");
user = properties.getProperty("jdbc.user");
pwd = properties.getProperty("jdbc.pwd");
dataSource.setDriverClassName(driver);
dataSource.setUrl(url);
dataSource.setUsername(user);
dataSource.setPassword(pwd);
} catch (Exception e) {
e.printStackTrace();
}
}
public static DruidDataSource getDataSource() {
return dataSource;
}
public static Connection getConn() {
Connection connection = null;
try {
connection = dataSource.getConnection();
} catch (SQLException e) {
e.printStackTrace();
}
return connection;
}
public static void getClose(Connection connection, Statement statement, PreparedStatement preparedStatement, ResultSet resultSet) {
try {
if (connection != null) connection.close();
if (statement != null) statement.close();
if (preparedStatement != null) preparedStatement.close();
if (resultSet != null) resultSet.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
2.2.2 DBListener
package com.javasm.finance.listener;
import com.alibaba.druid.pool.DruidDataSource;
import javax.servlet.ServletContext;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import javax.servlet.annotation.WebListener;
import java.sql.SQLException;
@WebListener
public class DBListener implements ServletContextListener {
static DruidDataSource dataSource = new DruidDataSource();
@Override
public void contextInitialized(ServletContextEvent servletContextEvent) {
ServletContext servletContext = servletContextEvent.getServletContext();
String driver = servletContext.getInitParameter("jdbc.driver");
String url = servletContext.getInitParameter("jdbc.url");
String user = servletContext.getInitParameter("jdbc.user");
String pwd = servletContext.getInitParameter("jdbc.pwd");
dataSource.setDriverClassName(driver);
dataSource.setUrl(url);
dataSource.setUsername(user);
dataSource.setPassword(pwd);
try {
dataSource.init();
} catch (SQLException e) {
throw new RuntimeException(e);
}
}
public static DruidDataSource getDataSource() {
return dataSource;
}
@Override
public void contextDestroyed(ServletContextEvent servletContextEvent) {
dataSource.close();
}
}
2.3 持久层实现类
2.3.1 内容概述
从数据库连接池中获取数据库连接资源
2.3.2 案例实现
package com.javasm.finance.dao.impl;
import com.javasm.finance.dao.UserDao;
import com.javasm.finance.entity.User;
import com.javasm.finance.listener.DBListener;
import com.javasm.finance.util.DBUtils2;
import org.apache.commons.dbutils.BasicRowProcessor;
import org.apache.commons.dbutils.GenerousBeanProcessor;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;
import org.apache.commons.dbutils.handlers.ScalarHandler;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
public class UserDaoImpl implements UserDao {
QueryRunner runner = new QueryRunner(DBListener.getDataSource());
@Override
public User findLoginUser(String userName, String userPwd) throws SQLException {
String sql = "select user_id,user_name,user_pwd from fin_admin_user where user_name=? and user_pwd=?";
BeanHandler<User> beanHandler = new BeanHandler<>(User.class, new BasicRowProcessor(new GenerousBeanProcessor()));
User loginUser = runner.query(sql, beanHandler, userName, userPwd);
return loginUser;
}
@Override
public List<User> queryUser(Integer page, Integer pageSize, User user) throws SQLException {
String sql = "select user_id,user_name,role_id,reg_time,login_time,is_valid,head_img from fin_admin_user ";
boolean isWhere = true;
List paramList = new ArrayList();
if (user.getUserId() != null && !"".equals(user.getUserId())) {
if (isWhere) {
sql += "where ";
isWhere = false;
} else {
sql += "and ";
}
sql += "user_id=? ";
paramList.add(user.getUserId());
}
if (user.getUserName() != null && !"".equals(user.getUserName())) {
if (isWhere) {
sql += "where ";
isWhere = false;
} else {
sql += "and ";
}
sql += "user_name like ? ";
paramList.add("%" + user.getUserName() + "%");
}
sql += "limit " + (page - 1) * pageSize + "," + pageSize;
BeanListHandler<User> listHandler = new BeanListHandler<>(User.class, new BasicRowProcessor(new GenerousBeanProcessor()));
List<User> userList = runner.query(sql, listHandler, paramList.toArray());
return userList;
}
@Override
public long queryUserNum(User user) throws SQLException {
String sql = "select count(1) totalNum from fin_admin_user ";
boolean isWhere = true;
List paramList = new ArrayList();
if (user.getUserId() != null && !"".equals(user.getUserId())) {
if (isWhere) {
sql += "where ";
isWhere = false;
} else {
sql += "and ";
}
sql += "user_id=? ";
paramList.add(user.getUserId());
}
if (user.getUserName() != null && !"".equals(user.getUserName())) {
if (isWhere) {
sql += "where ";
isWhere = false;
} else {
sql += "and ";
}
sql += "user_name like ? ";
paramList.add("%" + user.getUserName() + "%");
}
ScalarHandler<Long> scalarHandler = new ScalarHandler<>();
long totalNum = runner.query(sql, scalarHandler, paramList.toArray());
return totalNum;
}
@Override
public int addUser(User user) throws SQLException {
String sql = "insert into fin_admin_user(user_name,user_pwd,role_id,reg_time,is_valid,head_img,version_id) values(?,1234,?,?,?,?,100)";
int rows = runner.update(sql, user.getUserName(), user.getRoleId(), user.getRegTime(), user.getIsValid(), user.getHeadImg());
return rows;
}
@Override
public User queryByUserId(Integer userId) throws SQLException {
String sql = "select user_id,user_name,user_pwd,role_id,reg_time,login_time,is_valid,head_img,version_id from fin_admin_user where user_id=?";
BeanHandler<User> beanHandler = new BeanHandler<>(User.class, new BasicRowProcessor(new GenerousBeanProcessor()));
User user = runner.query(sql, beanHandler, userId);
return user;
}
@Override
public User queryByUidAndVid(Integer userId, Integer versionId) throws SQLException {
String sql = "select user_id,user_name,user_pwd,role_id,reg_time,login_time,is_valid,head_img,version_id from fin_admin_user where user_id=? and version_id=?";
BeanHandler<User> beanHandler = new BeanHandler<>(User.class, new BasicRowProcessor(new GenerousBeanProcessor()));
User user = runner.query(sql, beanHandler, userId, versionId);
return user;
}
@Override
public int editUser(User user) throws SQLException {
String sql = "update fin_admin_user set user_name=?,user_pwd=?,role_id=?,reg_time=?,is_valid=?,head_img=?,version_id=?+1 where user_id=?";
int rows = runner.update(sql, user.getUserName(), user.getUserPwd(), user.getRoleId(), user.getRegTime(), user.getIsValid(), user.getHeadImg(), user.getVersionId(), user.getUserId());
return rows;
}
@Override
public int deleteUser(Integer userId) throws SQLException {
String sql = "delete from fin_admin_user where user_id=?";
int rows = runner.update(sql, userId);
return rows;
}
}