动物管理系统
学习目的:
- 将封装、接口、多态、反射、泛型多个知识点串通进行实现一个动物管理系统的小项目
通过Console控制台
进行键盘录入来操作内存中的数据,了解实体数据在业务中处理流程
功能:
程序架构:
三层架构 :Controller>Service>Dao
代码结构:
EntryApplication
// 方法的入口,此主页会将键盘录入的选择标记传入Controller层
public class AnimalEntry {
public static void main(String[] args) throws NoSuchFieldException, IllegalAccessException {
Scanner scanner = new Scanner(System.in);
A:
while (true) {
System.out.println ("------------------------欢迎来到动物管理系统------------------------");
System.out.println("请输入您的选择: 1.狗狗🐕管理 2.猪猪🐖管理 3.退出");
switch (scanner.nextInt()) {
case 1:
new AnimalController().animalSystem(1);
break;
case 2:
new AnimalController().animalSystem(2);
break;
case 3:
System.out.println("--------管理系统关闭,下次再见--------");
System.exit(0);
default:
System.out.println("你输入的选择有误,请重新录入");
break A;
}
}
}
}
Controller
// 将控制层共性展示内容进行抽取,通过用户的录入数字类判断其类型
public class WelcomeString {
static Scanner scanner = new Scanner(System.in);
static public void welcomeStr(Integer selectType) {
String typeStr = selectType == 1 ? "🐕狗狗" : "🐖猪猪";
System.out.println("系统提示:正在加载......");
System.out.println("----------------------欢迎来到 《" + typeStr + "》 管理系统----------------------");
System.out.println("请输入您的选择: 1.添加" + typeStr + " 2.删除" + typeStr + " 3.修改" + typeStr + " 4.查看" + typeStr + " 5.退出");
}
// 任何CRUD操作都将先询问Dao层获取是否能访问的标记
public class AnimalController {
private Boolean isNoExists(Integer id) throws NoSuchFieldException {
Boolean result = animalServiceTemplate.selectById(id) == null;
if (!result) {
System.out.println("输入的Id已经存在,请重新输入");
}
return result;
}
private Boolean isExists(Integer id) throws NoSuchFieldException {
Boolean result = animalServiceTemplate.selectById(id) == null;
if (result) {
System.out.println("输入的Id不存在存在,请重新输入");
}
return result;
}
}
Service
// 使用泛型将实现将可以处理同属性实体类
public interface AnimalServiceTemplate<T> {
T selectById(Integer id) throws NoSuchFieldException;
T[] selectAll();
Boolean insert(T t);
Boolean update(Integer id,T t) throws NoSuchFieldException, IllegalAccessException;
Boolean delete(Integer id);
Integer getCount();
}
注意:使用泛型若进行强制转换需要
instanceof
保留字健壮性效验
DaoFactroy
// 使用工厂模式进行Dao与Service层的解耦,工厂将通过配置文件获取真实对象
public class DaoFactory {
public static AnimalsDaoTemplate getReflectionDaoImpl(){
InputStream resourceAsStream = ClassLoader.getSystemClassLoader().getResourceAsStream("application.properties");
Properties properties = new Properties();
try {
properties.load(resourceAsStream);
} catch (IOException e) {
e.printStackTrace();
}
String className = properties.getProperty("class");
Object o = null;
try {
o = Class.forName(className).newInstance();
} catch (Exception e) {
e.printStackTrace();
}
if (o instanceof AnimalsDaoImpl){
return (AnimalsDaoImpl) o;
}
if (o instanceof OtherAnimalsDaoImpl){
return (OtherAnimalsDaoImpl)o;
}
return null;
}
}
注意:配置资源需要在src目录下
class=com.xy2.dao.impl.AnimalsDaoImpl
# 或使用class=com.xy2.dao.impl.OtherAnimalsDaoImpl
利用类加载器的I/O流读功能取src下配置文件,并通过Class类将String类型内容全包名的对象进行实例化,得到真实Dao
Dao
// 数据访问层通用接口模板
public interface AnimalsDaoTemplate<T> {
Object[] select();
T selectById(int id) throws NoSuchFieldException;
Integer selectCount();
Boolean insert(T t);
Boolean update(Integer id,T t) throws NoSuchFieldException, IllegalAccessException;
Boolean delete(int id);
}
注意:使用泛型若进行强制转换需要
instanceof
保留字健壮性效验