|
@@ -0,0 +1,227 @@
|
|
|
+package com.sf.day29.dao.impl;
|
|
|
+
|
|
|
+import com.alibaba.druid.pool.DruidPooledConnection;
|
|
|
+import com.sf.day16._01_arrayList.dt.Department;
|
|
|
+import com.sf.day28.util.DruidUtil;
|
|
|
+import com.sf.day28.util.JDBCUtil;
|
|
|
+import com.sf.day29.dao.IEmployeeDAO;
|
|
|
+import com.sf.day29.domain.Employee;
|
|
|
+
|
|
|
+import java.sql.PreparedStatement;
|
|
|
+import java.sql.ResultSet;
|
|
|
+import java.sql.SQLException;
|
|
|
+import java.util.ArrayList;
|
|
|
+import java.util.List;
|
|
|
+
|
|
|
+public class EmployeeDAOImpl implements IEmployeeDAO {
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public void insert(Employee employee) {
|
|
|
+ // 获取连接对象
|
|
|
+ DruidPooledConnection conection = DruidUtil.getConection();
|
|
|
+ // 创建sql语句
|
|
|
+ String sql = "insert into employee (name,email,age,password,dept_id) values(?,?,?,?,?)";
|
|
|
+ PreparedStatement preparedStatement = null;
|
|
|
+ try {
|
|
|
+ preparedStatement = conection.prepareStatement(sql);
|
|
|
+ // 设置参数
|
|
|
+ preparedStatement.setString(1, employee.getName());
|
|
|
+ preparedStatement.setString(2, employee.getEmail());
|
|
|
+ preparedStatement.setInt(3, employee.getAge());
|
|
|
+ preparedStatement.setString(4, employee.getPassword());
|
|
|
+ preparedStatement.setLong(5, employee.getDeptId());
|
|
|
+ preparedStatement.executeUpdate();
|
|
|
+ } catch (SQLException e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ } finally {
|
|
|
+ JDBCUtil.close(conection, null, preparedStatement);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public void delete(Long id) {
|
|
|
+// 获取连接对象
|
|
|
+ DruidPooledConnection conection = DruidUtil.getConection();
|
|
|
+ // 创建sql语句
|
|
|
+ String sql = "delete from employee where id = ?";
|
|
|
+ PreparedStatement preparedStatement = null;
|
|
|
+ try {
|
|
|
+ preparedStatement = conection.prepareStatement(sql);
|
|
|
+ // 设置参数
|
|
|
+ preparedStatement.setLong(1, id);
|
|
|
+ preparedStatement.executeUpdate();
|
|
|
+ } catch (SQLException e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ } finally {
|
|
|
+ JDBCUtil.close(conection, null, preparedStatement);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public void update(Employee employee) {
|
|
|
+ DruidPooledConnection conection = DruidUtil.getConection();
|
|
|
+ // 创建sql语句
|
|
|
+ String sql = "update employee set name=? , age=? , email=? , password=? , dept_id = ? where id =?";
|
|
|
+ PreparedStatement preparedStatement = null;
|
|
|
+ try {
|
|
|
+ preparedStatement = conection.prepareStatement(sql);
|
|
|
+ // 设置参数
|
|
|
+ preparedStatement.setString(1, employee.getName());
|
|
|
+ preparedStatement.setInt(2, employee.getAge());
|
|
|
+ preparedStatement.setString(3, employee.getEmail());
|
|
|
+ preparedStatement.setString(4, employee.getPassword());
|
|
|
+ preparedStatement.setLong(5, employee.getDeptId());
|
|
|
+ preparedStatement.setLong(6, employee.getId());
|
|
|
+
|
|
|
+ preparedStatement.executeUpdate();
|
|
|
+ } catch (SQLException e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ } finally {
|
|
|
+ JDBCUtil.close(conection, null, preparedStatement);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public Employee get(Long id) {
|
|
|
+ DruidPooledConnection conection = DruidUtil.getConection();
|
|
|
+ // 创建sql语句
|
|
|
+ String sql = "select * from employee where id =?";
|
|
|
+ PreparedStatement preparedStatement = null;
|
|
|
+ try {
|
|
|
+ preparedStatement = conection.prepareStatement(sql);
|
|
|
+ // 设置参数
|
|
|
+ preparedStatement.setLong(1, id);
|
|
|
+
|
|
|
+ ResultSet resultSet = preparedStatement.executeQuery();
|
|
|
+ if (resultSet.next()) {
|
|
|
+ Employee employee = new Employee();
|
|
|
+ employee.setAge(resultSet.getInt("age"));
|
|
|
+ employee.setEmail(resultSet.getString("email"));
|
|
|
+ employee.setPassword(resultSet.getString("password"));
|
|
|
+ employee.setName(resultSet.getString("name"));
|
|
|
+ employee.setDeptId(resultSet.getLong("dept_id"));
|
|
|
+ employee.setId(resultSet.getLong("id"));
|
|
|
+ return employee;
|
|
|
+ }
|
|
|
+ } catch (SQLException e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ } finally {
|
|
|
+ JDBCUtil.close(conection, null, preparedStatement);
|
|
|
+ }
|
|
|
+ return null;
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public List<Employee> list() {
|
|
|
+ DruidPooledConnection conection = DruidUtil.getConection();
|
|
|
+ // 创建sql语句
|
|
|
+ String sql = "select * from employee";
|
|
|
+ PreparedStatement preparedStatement = null;
|
|
|
+ try {
|
|
|
+ preparedStatement = conection.prepareStatement(sql);
|
|
|
+
|
|
|
+ ResultSet resultSet = preparedStatement.executeQuery();
|
|
|
+ List<Employee> list = new ArrayList<>();
|
|
|
+ while (resultSet.next()) {
|
|
|
+ Employee employee = new Employee();
|
|
|
+ employee.setAge(resultSet.getInt("age"));
|
|
|
+ employee.setEmail(resultSet.getString("email"));
|
|
|
+ employee.setPassword(resultSet.getString("password"));
|
|
|
+ employee.setName(resultSet.getString("name"));
|
|
|
+ employee.setDeptId(resultSet.getLong("dept_id"));
|
|
|
+ employee.setId(resultSet.getLong("id"));
|
|
|
+ list.add(employee);
|
|
|
+ }
|
|
|
+ return list;
|
|
|
+ } catch (SQLException e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ } finally {
|
|
|
+ JDBCUtil.close(conection, null, preparedStatement);
|
|
|
+ }
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public Employee selectEmployeeInfo(Long id) {
|
|
|
+ DruidPooledConnection conection = DruidUtil.getConection();
|
|
|
+ // 创建sql语句
|
|
|
+ String sql = "select * from employee where id =?";
|
|
|
+ PreparedStatement preparedStatement = null;
|
|
|
+ try {
|
|
|
+ preparedStatement = conection.prepareStatement(sql);
|
|
|
+ // 设置参数
|
|
|
+ preparedStatement.setLong(1, id);
|
|
|
+
|
|
|
+ ResultSet resultSet = preparedStatement.executeQuery();
|
|
|
+ if (resultSet.next()) {
|
|
|
+ Employee employee = new Employee();
|
|
|
+ employee.setAge(resultSet.getInt("age"));
|
|
|
+ employee.setEmail(resultSet.getString("email"));
|
|
|
+ employee.setPassword(resultSet.getString("password"));
|
|
|
+ employee.setName(resultSet.getString("name"));
|
|
|
+ employee.setDeptId(resultSet.getLong("dept_id"));
|
|
|
+ employee.setId(resultSet.getLong("id"));
|
|
|
+ // 根据部门id 查询部门信息 , 然后在把部门的信息设置到Employee 当中
|
|
|
+ long deptId = resultSet.getLong("dept_id");
|
|
|
+ // 查询出来结果是部门对象
|
|
|
+ String deptSql = "select * from department where id = ?";
|
|
|
+ PreparedStatement deptPst = conection.prepareStatement(deptSql);
|
|
|
+ deptPst.setLong(1, deptId);
|
|
|
+ ResultSet deptResult = deptPst.executeQuery();
|
|
|
+ // {id:1 , name : xxx ... dept_id: 1,dept:} {id:1 , name: 开发部, sn: sdf}
|
|
|
+ if (deptResult.next()) {
|
|
|
+ Department department = new Department();
|
|
|
+ department.setName(deptResult.getString("name"));
|
|
|
+ department.setSn(deptResult.getString("sn"));
|
|
|
+ department.setId(deptResult.getLong("id"));
|
|
|
+
|
|
|
+ // 最终要把department 设置到Employee 里面
|
|
|
+ employee.setDept(department);
|
|
|
+ }
|
|
|
+
|
|
|
+ return employee;
|
|
|
+ }
|
|
|
+ } catch (SQLException e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ } finally {
|
|
|
+ JDBCUtil.close(conection, null, preparedStatement);
|
|
|
+ }
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public Employee login(String username, String password) {
|
|
|
+ //获取连接对象
|
|
|
+ DruidPooledConnection conection = DruidUtil.getConection();
|
|
|
+ PreparedStatement preparedStatement = null;
|
|
|
+ ResultSet resultSet = null;
|
|
|
+ // 创建sql 语句
|
|
|
+ String sql = "select * from employee where name=? and password=?";
|
|
|
+ try {
|
|
|
+ preparedStatement = conection.prepareStatement(sql);
|
|
|
+ preparedStatement.setString(1, username);
|
|
|
+ preparedStatement.setString(2, password);
|
|
|
+ resultSet = preparedStatement.executeQuery();
|
|
|
+ if (resultSet.next()) {
|
|
|
+ Employee employee = new Employee();
|
|
|
+ employee.setAge(resultSet.getInt("age"));
|
|
|
+ employee.setEmail(resultSet.getString("email"));
|
|
|
+ employee.setPassword(resultSet.getString("password"));
|
|
|
+ employee.setName(resultSet.getString("name"));
|
|
|
+ employee.setDeptId(resultSet.getLong("dept_id"));
|
|
|
+ employee.setId(resultSet.getLong("id"));
|
|
|
+ // 根据部门id 查询部门信息 , 然后在把部门的信息设置到Employee 当中
|
|
|
+ long deptId = resultSet.getLong("dept_id");
|
|
|
+ return employee;
|
|
|
+ }
|
|
|
+ } catch (SQLException e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ } finally {
|
|
|
+ JDBCUtil.close(conection,resultSet,preparedStatement);
|
|
|
+ }
|
|
|
+ // 执行sql语句
|
|
|
+ // 释放资源
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+}
|