如何在继承时正确构建dao类

时间:2017-10-16 00:06:30

标签: java database jdbc dao pojo

我有一个问题。 我创建了一个数据库,我有一个User表,它由两个表(Employee& Customer)扩展,稍后它将被用作Web应用程序中的一个角色。 DB model 如何正确编写dao和pojo类? 在我写的那一刻: 用户,客户,员工和项目是Pojo类,我在编写时使用了继承:

public class User {
private Integer id;
private String firstName;
private String lastName;
private String mail;
private String password;
private String telephone;
private String photoPath;
private Role role;

public User(int id, String firstName, String lastName, String mail, String password, String telephone, String photoPath, Role role) {
    this.id = id;
    this.firstName = firstName;
    this.lastName = lastName;
    this.mail = mail;
    this.password = password;
    this.telephone = telephone;
    this.photoPath = photoPath;
    this.role = role;
}

public User() {}

public enum Role {
    MANAGER, EMPLOYEER, CUSTOMER
}

// +所有获取者和设定者 }

public class Employee extends User {
private Integer salary;
private Rank rank;
private Condition condition;
private Direction direction;


public Employee(int id, String firstName, String lastName, String mail, String password, String telephone, String photoPath, User.Role role, Integer salary, Rank rank, Condition condition, Direction direction) {
    super(id, firstName, lastName, mail, password, telephone, photoPath, role);
    this.salary = salary;
    this.rank = rank;
    this.condition = condition;
    this.direction = direction;
}

public Employee() {
}

public Employee(int id, String firstName, String lastName, String mail, String password, String telephone, String photoPath, Role role) {
    super(id, firstName, lastName, mail, password, telephone, photoPath, role);
}

public enum Rank {
    JUNIOR, MIDDLE, SENIOR
}

public enum Direction {
    FRONT_END, BACK_END, TESTER;
}

public enum Condition {
    UNCONFIRMED, WORKING, FREE
}

}

public class Customer extends User {
private String companyName;

public Customer() {
}

public Customer(int id, String firstName, String lastName, String mail, String password, String telephone, String photoPath, Role role, String companyName) {
    super(id, firstName, lastName, mail, password, telephone, photoPath, role);
    this.companyName = companyName;
}

写下这些道: UserDao,CustomerDao,EmployeeDao,ProjectDao。这是接口(还没有实现):

 public interface UserDao {
    User getUserById(int id) throws SQLException;
    void deleteUserById(int id) throws SQLException;
    void updateUser(User user) throws SQLException;
    void saveUser(User user) throws SQLException;

    List<User> getAllUsers() throws SQLException;
}



 public interface CustomerDao {
    Customer getCustomerById(int id) throws SQLException;
    void deleteCustomerById(int id) throws SQLException;
    void updateCustomer(Customer customer) throws SQLException;
    void saveCustomer(Customer customer) throws SQLException;

    List<Customer> getAllCustomers() throws SQLException;
}

    public interface EmployeeDao {
    Employee getEmployeeById(int id) throws SQLException;
    void deleteEmployeeById(int id) throws SQLException;
    void updateEmployee(Employee employee) throws SQLException;
    void saveEmployee(Employee employee) throws SQLException;

    List<Employee> getAllEmployeesByRank(Employee.Rank rank) throws SQLException;
    List<Employee> getAllEmployees() throws SQLException;
}


public interface ProjectDao {
    Project getProjectById(int id) throws SQLException;
    void deleteProjectById(int id) throws SQLException;
    void updateProject(Project project) throws SQLException;
    void saveProject(Project project) throws SQLException;
    List<Project> getAllProjects() throws SQLException;
    List<Project> getProjectsByCondition(Project.Condition condition) throws SQLException;
}

如何正确执行以及是否为User和Customer创建单独的pojo。 或者最好将数据库更改为另一个(可以摆脱表客户)。 我会很感激任何建议/帮助,这是我第一次这样做。 要连接到数据库,将使用jdbc。 我不能使用Spring |冬眠。

0 个答案:

没有答案