Xml的Spring三层项目架构,你学会了吗?
业务背景
需求:使用三层架构开发,层项将用户信息导入到数据库中目标:初步熟悉三层架构开发核心操作:开发两套项目,目架对比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项目结构
实体类package com.example.pojo;
public class User {
private String name;
private int age;
private String address;
}数据访问层接口package com.example.dao;
import com.example.pojo.User;
/
*** 数据访问层接口
*/
public interface UserMapper {
//导入用户信息
int insertUser(User user);
}实现类package com.example.dao;
import com.example.pojo.User;
/
*** 数据访问层的实现类
*/
public class UserMapperImpl implements UserMapper{
//模拟用户信息导入
@Override
public int insertUser(User user) {
System.out.println("用户: " + user.getName() + ", 导入成功!");
return 1;
}
}业务逻辑层接口package com.example.Service;
import com.example.pojo.User;
/
*** 业务逻辑层接口
*/
public interface UserService {
//导入用户数据的功能
int insertUser(User user);
}实现类package com.example.Service.impl;
import com.example.Service.UserService;
import com.example.dao.UserMapper;
import com.example.dao.UserMapperImpl;
import com.example.pojo.User;
/
*** 业务逻辑层实现类
*/
public class UserServiceImpl implements UserService {
//数据访问层接口指向数据访问层实现类
UserMapper userMapper = new UserMapperImpl();
@Override
public int insertUser(User user) {
return userMapper.insertUser(user);
}
}界面层package com.example.controller;
import com.example.Service.UserService;
import com.example.Service.impl.UserServiceImpl;
import com.example.pojo.User;
/
*** 界面层
*/
public class UserController {
//业务逻辑层接口指向业务逻辑层实现类
UserService userService = new UserServiceImpl();
public int insertUser(User user){
return userService.insertUser(user);
}
}测试package com.example.test;
import com.example.controller.UserController;
import com.example.pojo.User;
import org.junit.Test;
public class TestInsert {
//测试非Spring框架的简单三层架构
@Test
public void testInsertUser(){
UserController userController = new UserController();
int num = userController.insertUser(new User("荷包蛋", 20, "黑河"));
if(num == 1){
System.out.println("非Spring框架的简单三层架构,运行成功!");
}else{
System.out.println("非Spring框架的服务器托管简单三层架构,运行失败!");
}
}
}输出结果用户: 荷包蛋, 导入成功!
Spring接管下的简单三层架构,运行成功!
Process finished with exit code 0测试分析测试执行流程示意图
测试分析层级变化:界面层 --> 业务逻辑层 --> 数据访问层 --> 业务逻辑层 --> 界面层对象访问的变化:界面层对象 --> 业务逻辑层接口指向业务逻辑层实现类 --> 数据访问层接口指向数据访问层实现类 --> 数据访问层实现类完成对数据的操作方法调用变化:界面层对象的insertUser(User u) --> 业务逻辑层实现类的insertUser(User u) --> 数据访问层实现类的insertUser(User u)
Spring接管下的三层项目构建对传统三层项目构建的修改:由上述测试分析中"对象访问的变化可知",需要用到的构学实现类有:UserController,UserServiceImpl,层项UserMapperImpl在Spring接管下,免费源码下载目架需要在bean工厂中,构学注册上述实体类的层项对象,将原先需要程序员手动创建管理的目架对象交给Spring框架去接手管理
业务逻辑层实现类修改为:
//此时的业务逻辑层实现类:不再手动创建数据访问层的对象,交给Spring容器来管理,构学新增:setter方法和无参构造函数
package com.example.Service.impl;
import com.example.Service.UserService;
import com.example.dao.UserMapper;
import com.example.pojo.User;
/
*** 业务逻辑层实现类
*/
public class UserServiceImpl implements UserService {
//数据访问层接口指向数据访问层实现类
public UserMapper userMapper;
public void setUserMapper(UserMapper userMapper) {
this.userMapper = userMapper;
}
public UserServiceImpl() {
}
@Override
public int insertUser(User user) {
return userMapper.insertUser(user);
}
}界面层所做修改与对业务逻辑层的层项实现类的修改类似:package com.example.controller;
import com.example.Service.UserService;
import com.example.pojo.User;
/
*** 界面层
*/
public class UserController {
//业务逻辑层接口指向业务逻辑层实现类
UserService userService;
public void setUserService(UserService userService) {
this.userService = userService;
}
public UserController() {
}
public int insertUser(User user){
return userService.insertUser(user);
}
}applicationContext.xml
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
测试package com.example.test;
import com.example.controller.UserController;
import com.example.pojo.User;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class TestInsert {
//测试Spring接管下的简单三层架构
@Test
public void testInsertUser(){
//创建Spring容器
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
//取出界面层对象
UserController uController = (UserController) applicationContext.getBean("uController");
//调用界面层对象方法
int num = uController.insertUser(new User("荷包蛋", 20, "黑河"));
if(num == 1){
System.out.println("Spring接管下的简单三层架构,运行成功!");
}else{
System.out.println("Spring接管下的简单三层架构,运行失败!");
}
}
}输出结果用户: 荷包蛋, 导入成功!
Spring接管下的WordPress模板简单三层架构,运行成功!
Process finished with exit code 0本文地址:http://www.bzuk.cn/html/031c31399655.html
版权声明
本文仅代表作者观点,不代表本站立场。
本文系作者授权发表,未经许可,不得转载。