三、DAO及相关实现类
DAO
DAO(Data Access Object)访问数据信息的类和接口,包括了对数据的CRUD(Create、Retrival、Update、 Delete),而不包含任何业务相关的信息。
DAO作用: 为了实现功能的模块化,更有利于代码的维护和升级。
层次结构:
代码如下:
pojo.User.java package com.sun.pojo; /** * @author JumperSun * @date 2022-09-09-12:51 */ public class User { int id; String name; String password; public User(int id, String name, String password) { super(); this.id = id; this.name = name; this.password = password; } public int getId() { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } @Override public String toString() { return "User{" + "id=" + id + ", name='" + name + '\'' + ", password='" + password + '\'' + '}'; } }
BaseDao.java
package com.sun.dao; import com.sun.util.JDBCUtils; import com.sun.util.PsUtils; import java.lang.reflect.ParameterizedType; import java.lang.reflect.Type; import java.sql.Connection; import java.sql.SQLException; import java.util.List; /** * @author JumperSun * @date 2022-09-09-12:53 * 定义一个用来被继承的对数据库进行基本操作的Dao */ public abstract class BaseDao<T> { private PsUtils PsUtils = new PsUtils(); // 定义一个变量来接收泛型的类型 private Class<T> type; // 获取T的Class对象,获取泛型的类型,泛型是在被子类继承时才确定 public BaseDao() { // 获取子类的类型 Class clazz = this.getClass(); // 获取父类的类型 // getGenericSuperclass()用来获取当前类的父类的类型 // ParameterizedType表示的是带泛型的类型 ParameterizedType parameterizedType = (ParameterizedType) clazz.getGenericSuperclass(); // 获取具体的泛型类型 getActualTypeArguments获取具体的泛型的类型 // 这个方法会返回一个Type的数组 Type[] types = parameterizedType.getActualTypeArguments(); // 获取具体的泛型的类型· this.type = (Class<T>) types[0]; } /** * 通用的增删改操作 * @param sql * @param params * @return */ public boolean update(String sql, Object... params) { boolean flag = false; try { flag = PsUtils.update(sql, params); } catch (Exception e) { e.printStackTrace(); } return flag; } /** * 获取一个对象 * * @param sql * @param params * @return */ public T getBean(String sql, Object... params) { T t = null; try { t = PsUtils.getBean(type,sql, params); } catch (Exception e) { e.printStackTrace(); } return t; } /** * 获取所有对象 * @param sql * @param params * @return */ public List<T> getBeanList(String sql, Object... params) { List<T> list = null; try { list = PsUtils.getBeanList(type, sql, params); } catch (Exception e) { e.printStackTrace(); } return list; } }
UserDao.java
package com.sun.dao; import com.sun.pojo.User; import java.util.List; /** * @author JumperSun * @date 2022-09-09-13:52 */ public interface UserDao { /** * 查询所有用户 * @return */ List<User> getUsers(); /** * 根据用户名查询用户 * @param name * @return */ User getUserByName(String name); /** * 添加用户 * @param user * @return */ boolean addUser(User user); /** * 删除用户 * @param user * @return */ boolean deleteUser(User user); /** * 修改用户 * @param user * @return */ boolean updateUser(User user); }
UserDaoImpl.java
package com.sun.dao.impl; import com.sun.dao.BaseDao; import com.sun.dao.UserDao; import com.sun.pojo.User; import java.util.List; /** * @author JumperSun * @date 2022-09-09-13:53 */ public class UserDaoImpl extends BaseDao implements UserDao { @Override public List<User> getUsers() { String sql = "select * from user"; List UserList = getBeanList(sql); return UserList; } @Override public User getUserByName(String name) { String sql = "select * from user where name = ?"; User user = (User) getBean(sql, name); return user; } @Override public boolean addUser(User user) { String sql = "insert into user values(?,?,?)"; boolean flag = update(sql, user.getId(), user.getName(), user.getPassword()); return flag; } @Override public boolean deleteUser(User user) { String sql = "delete from user where name = ? and password = ?"; boolean flag = update(sql, user.getName(), user.getPassword()); return flag; } @Override public boolean updateUser(User user) { String sql = "update user set name = ? ,password = ? where id = ?"; boolean flag = update(sql, user.getName(), user.getPassword(),user.getId()); return flag; } }