满汉楼项目主要是利用JDBC相关的知识,实现用JAVA程序操作mysql数据库,所以一定要有相关JDBC基础才能看本篇【本篇项目 相关教程请参考韩顺平老师,本文主要是对细节加以说明】
目录
1、引入必要的包并创建包
创建一个项目:我命名为mhl
再引入相关的jar文件【与JDBC相关联】,要引入mysql实现JDBC的jar文件以便于java程序使用,还要用到druid德鲁伊连接池来实现java程序和mysql的连接,用Apache-dbutils来解决resultSet的相关问题【注意引入这3个文件后要右键->add as librarys才行】
BasicDAO的相关实现:
package com.jpl.mhl.DAO;
import com.jpl.mhl.utils.JDBCUtilsByDruid;
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.Connection;
import java.sql.SQLException;
import java.util.List;
/**
* creates 2024-05-13:41
*
* @author 姜暮
* @version 1.0
* 开发 BasicDAO , 是其他 DAO 的父类, 使用到 apache-dbutils
*/
public class BasicDAO<T> {
private QueryRunner qur = new QueryRunner();
//开发通用的dml方法,针对任意的表
public int update(String sql, Object...parameters) throws SQLException {
Connection connection = null;
try {
connection = JDBCUtilsByDruid.getConnection();
int update = qur.update(connection, sql, parameters);
return update;
} catch (SQLException e) {
throw new RuntimeException(e);
} finally {
JDBCUtilsByDruid.close(null, null, connection);
}
}
//返回多个对象(查询结果是多行),针对任意的表
public List<T> queryMulti(String sql, Class<T> clazz, Object...parameters) throws SQLException {
Connection connection = null;
try {
connection = JDBCUtilsByDruid.getConnection();
return qur.query(connection, sql, new BeanListHandler<>(clazz), parameters);
} catch (SQLException e) {
throw new RuntimeException(e);
} finally {
JDBCUtilsByDruid.close(null, null, connection);
}
}
//查询单行结果 的通用方法
public T querySingle(String sql, Class<T> clazz, Object... parameters) throws SQLException {
Connection connection = null;
try {
connection = JDBCUtilsByDruid.getConnection();
return qur.query(connection, sql, new BeanHandler<T>(clazz), parameters);
} catch (SQLException e) {
throw new RuntimeException(e); //将编译异常->运行异常 ,抛出
} finally {
JDBCUtilsByDruid.close(null, null, connection);
}
}
//查询单行单列的方法,即返回单值的方法
public Object queryScalar(String sql, Object... parameters) throws SQLException {
Connection connection = null;
try {
connection = JDBCUtilsByDruid.getConnection();
return qur.query(connection, sql, new ScalarHandler(), parameters);
} catch (SQLException e) {
throw new RuntimeException(e); //将编译异常->运行异常 ,抛出
} finally {
JDBCUtilsByDruid.close(null, null, connection);
}
}
}
2、Employee
既然是满汉楼系统,那么就要有员工登录这个系统,首先一定是先在数据库创建表,再在java中创建对应的domain,具体对应表的数据就可以在数据库中进行插入数据,然后再利用这个员工登录即可
Employee:
package com.jpl.mhl.domain;
import java.lang.ref.PhantomReference;
/**
* creates 2024-05-11:32
*
* @author 姜暮
* @version 1.0
* 这是一个javabean,和employee相对应
create table employee(
id int primary key auto_increment,
empId varchar(50) unique not null default '', #员工号
pwd char(32) not null default '',
name varchar(50) not null default '',
job varchar(50) not null default '' #岗位
)charset = utf8;
*/
public class Employee {
private Integer id;
private String empId;
private String name;
private String pwd;
private String job;
public Employee() {//Apache-DBUtils反射需要无参构造器
}
public Employee(Integer id, String empId, String name, String pwd, String job) {
this.id = id;
this.empId = empId;
this.name = name;
this.pwd = pwd;
this.job = job;
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getEmpId() {
return empId;
}
public void setEmpId(String empId) {
this.empId = empId;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPwd() {
return pwd;
}
public void setPwd(String pwd) {
this.pwd = pwd;
}
public String getJob() {
return job;
}
public void setJob(String job) {
this.job = job;
}
}
对应插入的员工数据:
EmployeeDAO:【下文就不给DAO了,因为都是类似的,都是继承BasicDAO】
EmployeeService:
package com.jpl.mhl.service;
import com.jpl.mhl.DAO.EmployeeDAO;
import com.jpl.mhl.domain.Employee;
import java.sql.SQLException;
/**
* creates 2024-05-11:39
*
* @author 姜暮
* @version 1.0
* 该类完成对employee表的各种操作(通过调用EmployeeDAO对象完成)
*/
public class EmployeeService {//【通过建立用户来实现登录】
//定义一个EmployeeDAO属性
private EmployeeDAO employeeDAO = new EmployeeDAO();
//根据 empId 和 pwd 返回一个Employee对象,因为主菜单需要拿到员工
//登录系统时只有查到对应员工存在才会让登录系统
//如果查询不到,就返回null
public Employee getEmployeeByIdAndPwd(String empId, String pwd){
//传进的密码要经过md5函数的处理转成加密后的值后再去匹配的,没有md5是查不出来的
try {
return employeeDAO.querySingle("select * from employee where empId = ? and pwd = md5(?)",
Employee.class,empId,pwd);
//如果找不到会返回null
} catch (SQLException e) {
throw new RuntimeException(e);
}
}
}