8.HRMS系统实现
本章内容
一、HRMS系统实现(项目贯穿)
1、项目结构如下图:
2、pom.xml导依赖
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.29</version>
</dependency>
3、创建配置文件 jdbc.properties
注意该文件不能直接放到src目录下,要放到resources文件下
db.driver=com.mysql.cj.jdbc.Driver
db.url=jdbc:mysql://localhost:3306/hrms?useUnicode=true&characterEncoding=UTF-8&serverTimezone=Asia/Shanghai
db.username=root
db.password=root
4、创建工具类JdbcUtils
package com.woniuxy.util;
import java.io.IOException;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.Properties;
/**
* @author :fengsir
* @date :Created in 2023/2/15 12:32
* @description:连接数据工具类
* @modified By:
* @version: 1.0
*/
public class JdbcUtils {
static Properties properties = new Properties();
static {
InputStream inputStream = DBUtil.class.getClassLoader().getResourceAsStream("jdbc.properties");
try {
properties.load(inputStream);
Class.forName(properties.getProperty("jdbc.driver"));
} catch (IOException e) {
throw new RuntimeException(e);
} catch (ClassNotFoundException e) {
throw new RuntimeException(e);
}
}
/**
* @Description 获取连接(封装到方法中)
* @Return java.sql.Connection
* @Author fengSir
* @Date Create by 2023/2/15 12:44
*/
public static Connection getconnection() throws Exception {
String url = properties.getProperty("db.url");
String user = properties.getProperty("db.username");
String password = properties.getProperty("db.password");
Connection conn = DriverManager.getConnection(url, user, password);
return conn;
}
/**
* @param rs
* @param pstm
* @param conn
* @Description 释放资源
* @Return void
* @Author fengSir
* @Date Create by 2023/2/15 12:45
*/
public static void release(ResultSet rs, PreparedStatement pstm, Connection conn) throws Exception {
if (rs != null) {
rs.close();
}
if (pstm != null) {
pstm.close();
}
if (conn != null) {
conn.close();
}
}
}
5、创建泛型接口IBaseDao
package com.woniuxy.dao;
import java.util.List;
public interface IBaseDao<T, K> {
public int add(T t) throws Exception;
public int update(T t) throws Exception;
public List<T> queryAll() throws Exception;
public T queryById(K k) throws Exception;
public int delete(K k) throws Exception;
}
6、创建员工dao层接口
public interface IEmployeeDao extends IBaseDao<Employee,Integer>{
}
7、创建dao层实现类
package com.woniuxy.dao;
import com.woniuxy.entity.Employee;
import com.woniuxy.util.JdbcUtils;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;
/**
* @author :fengsir
* @date :Created in 2023/2/15 14:00
* @description:实现类
* @modified By:
* @version: 1.0
*/
public class IEmployeeDaoImpl implements IEmployeeDao {
private Connection conn = null;
private PreparedStatement ps = null;
private ResultSet rs = null;
private String sql = "";
@Override
public int add(Employee employee) throws Exception {
int i = 0;
sql = "insert into employee(id,`name`,sex,age) VALUES(0,?,?,?)";
try {
conn = JdbcUtils.getconnection();
ps = conn.prepareStatement(sql);
ps.setString(1,employee.getEmpName());
ps.setString(2,employee.getSex());
ps.setInt(3,employee.getAge());
i = ps.executeUpdate();
} catch (Exception e) {
throw new RuntimeException(e);
} finally {
JdbcUtils.release(rs,ps,conn);
}
return i;
}
@Override
public int update(Employee employee) throws Exception {
int i = 0;
sql = "update employee set name = ? where id =?";
try {
conn = JdbcUtils.getconnection();
ps = conn.prepareStatement(sql);
ps.setString(1,employee.getEmpName());
ps.setInt(2,employee.getEmpId());
i = ps.executeUpdate();
} catch (Exception e) {
throw new RuntimeException(e);
} finally {
JdbcUtils.release(rs,ps,conn);
}
return i;
}
@Override
public List<Employee> queryAll() throws Exception {
List<Employee> list = new ArrayList<>();
sql = "select * from employee";
try {
conn = JdbcUtils.getconnection();
ps = conn.prepareStatement(sql);
rs = ps.executeQuery();
while(rs.next()){
Employee employee = new Employee();
employee.setEmpId(rs.getInt(1));
employee.setEmpName(rs.getString(2));
employee.setSex(rs.getString(3));
employee.setAge(rs.getInt(4));
list.add(employee);
}
} catch (Exception e) {
throw new RuntimeException(e);
} finally {
JdbcUtils.release(rs,ps,conn);
}
return list;
}
@Override
public Employee queryById(Integer empId) throws Exception {
sql = "select * from employee where id=?";
Employee employee = new Employee();
try {
conn = JdbcUtils.getconnection();
ps = conn.prepareStatement(sql);
ps.setInt(1,empId);
rs = ps.executeQuery();
if(rs.next()){
employee.setEmpId(rs.getInt(1));
employee.setEmpName(rs.getString(2));
employee.setSex(rs.getString(3));
employee.setAge(rs.getInt(4));
}
} catch (Exception e) {
throw new RuntimeException(e);
} finally {
JdbcUtils.release(rs,ps,conn);
}
return employee;
}
@Override
public int delete(Integer empId) throws Exception {
int i = 0;
sql = "delete from employee where id =?";
try {
conn = JdbcUtils.getconnection();
ps = conn.prepareStatement(sql);
ps.setInt(1,empId);
i = ps.executeUpdate();
} catch (Exception e) {
throw new RuntimeException(e);
} finally {
JdbcUtils.release(rs,ps,conn);
}
return i;
}
}
8、测试,实体类省略
9、添加注册方法引出service层概念
用户在注册时要先判断是否添加过,如果添加过则提示账号已经存在
思维导图
本文来自博客园,作者:icui4cu,转载请注明原文链接:https://www.cnblogs.com/icui4cu/p/18818221