当前位置: 首页 > 科技观察

xml的Spring三层项目架构,你学会了吗?

时间:2023-03-13 19:41:47 科技观察

业务背景要求:使用三层架构开发,将用户信息导入数据库目标:熟悉三层架构开发的核心操作:开发两套项目,比较Spring下三层项目建设的区别接管与传统的三层工程构建注意:本例中的数据访问层并没有先连接数据库,只是进行简单的数据模拟。非Spring接管下的三层工程构造实体类+各接入层实体类:com.example.pojoUser实体类User实体类默认包含:无参构造方法+全属性(有参构造方法+getter、setter方法+toString方法)数据访问层:com.example.daoUserMapper.java(接口)UserMapperImpl.java(实现类)业务逻辑层:com.example.serviceUserService.java(接口)UserServiceImpl.java(实现类)接口层:com.example.controllerUserController.java工程结构实体类包com.example.pojo;公共类用户{私有字符串名称;私人年龄;privateStringaddress;}数据访问层接口包com.example.dao;importcom.example.pojo.User;/***数据访问层接口*/publicinterfaceUserMapper{//导入用户信息intinsertUser(Useruser);}实现类包com.example.dao;importcom.example.pojo.User;/***数据访问层实现类*/publicclassUserMapperImplimplementsUserMapper{//模拟用户信息import@OverridepublicintinsertUser(Useruser){System.out.println("用户:"+user.getName()+",导入成功!");返回1;}}业务逻辑层接口packagecom.example.Service;importcom.example.pojo.User;/***业务逻辑层接口*/publicinterfaceUserService{//导入用户数据函数intinsertUser(Useruser);}实现类包com.example.Service.impl;importcom.example.Service.UserService;importcom.example.dao.UserMapper;importcom.example.dao.UserMapperImpl;importcom.example.pojo.User;/***业务逻辑层实现类*/publicclassUserServiceImplimplementsUserService{//数据访问层接口指向数据访问层实现类UserMapperuserMapper=newUserMapperImpl();@OverridepublicintinsertUser(Useruser){returnuserMapper.insertUser(user);}}接口层包com.example.controller;importcom.example.Service.UserService;importcom.example.Service.impl.UserServiceImpl;importcom.example.pojo.User;/***接口层*/publicclassUserController{//业务逻辑层接口指向业务逻辑层实现类UserServiceuserService=newUserServiceImpl();publicintinsertUser(用户用户){返回userService.insertUser(用户);}}测试包com.example.test;importcom.example.controller.UserController;importcom.example.pojo.User;importorg.junit.Test;publicclassTestInsert{//测试非Spring框架的简单三层架构@TestpublicvoidtestInsertUser(){UserControlleruserController=newUserController();intnum=userController.insertUser(newUser("荷包蛋",20,"黑河"));if(num==1){System.out.println("简单的非Spring框架三层架构,运行成功!");}else{System.out.println("非Spring框架的简单三层架构,运行失败!");}}}输出结果User:荷包蛋,导入成功!Spring接手简单的三层架构,运行成功!Processfinishedwithexitcode0TestAnalysisTestExecutionProcessSchematic测试分析级别变化:接口层-->业务逻辑层-->数据访问层-->业务逻辑层-->接口层对象访问变化:接口层对象-->业务逻辑层接口指向业务逻辑层实现类-->数据访问层接口指向数据访问层实现类-->数据访问层实现类完成对数据的操作方法调用Change:insertUser(User接口层对象的u)-->业务逻辑层实现类的insertUser(Useru)-->数据访问层实现类的insertUser(Useru)access”在上面的测试分析中,需要用到的实现类有:UserController、UserServiceImpl、UserMapperImpl。在Spring的接管下,上述实体类的对象都需要注册到bean工厂中,并将原本需要程序员手动创建和管理的对象交给Spring框架来接管管理业务逻辑层实现类,改为://此时的业务逻辑层实现类:不再手动创建数据访问层对象,交给SpringContainer管理,添加:setter方法和无参构造函数包com.example.Service.impl;importcom.example.Service.UserService;importcom.example.dao.UserMapper;importcom.example.pojo.用户;/***业务逻辑层实现类*/publicclassUserServiceImplimplementsUserService{//数据访问层接口指向数据访问层实现类publicUserMapperuserMapper;publicvoidsetUserMapper(UserMapperuserMapper){this.userMapper=userMapper;}publicUserServiceImpl(){}@OverridepublicintinsertUser(Useruser){returnuserMapper.insertUser(user);}}接口层的修改与业务逻辑层的实现类的修改类似:packagecom.example.controller;importcom.example.Service.UserService;importcom.example.pojo.User;/***接口层*/publicclassUserController{//业务逻辑层接口指向业务逻辑层实现类UserServiceuserService;publicvoidsetUserService(UserServiceuserService){这个。用户服务=用户服务;}publicUserController(){}publicintinsertUser(Useruser){returnuserService.insertUser(user);}}applicationContext.xml测试包gecom.example.test;导入com.example.controller.UserController;导入com.example.pojo.User;导入org.junit.Test;导入org.springframework.context.ApplicationContext;导入org.springframework.context.support。类路径XmlApplicationContext;publicclassTestInsert{//测试Spring接管下的简单三层架构@TestpublicvoidtestInsertUser(){//创建Spring容器ApplicationContextapplicationContext=newClassPathXmlApplicationContext("applicationContext.xml");//取出接口层对象UserControlleruController=(UserController)applicationContext.getBean("uController");//调用接口层对象方法intnum=uController.insertUser(newUser("荷包蛋",20,"黑河"));if(num==1){System.out.println("Spring接管下的简单三层架构,运行成功!");}else{System.out.println("Spring接管下的简单三层架构,运行失败!");}}}输出结果User:荷包蛋,导入成功!Spring接管下的简单三层架构,运行成功!进程结束,退出代码为0