Java三层结构,附源码

1,三层架构分为:数据访问层(dao,持久层),业务逻辑层(service,业务层),表示层(view,表示层)

2,数据访问层:主要负责数据库的访问(增删改查)。

3,业务逻辑层:主要负责业务处理和数据传递,将数据访问层传来的数据加工处理,并将数据传给表示层。

如图4所示,表示层:将业务逻辑层处理好的数据展示给用户。

5,解析

6,建包

7,数据库

8,实体类

//学生实体类和数据库student中stu表对应
public class Student {
	private int stuid;	//学生编号
	private String stuname; //学生姓名
	private int age;//年龄
	private String address;//地址
	//get、set
	public int getStuid() {
		return stuid;
	}
	public void setStuid(int stuid) {
		this.stuid = stuid;
	}
	public String getStuname() {
		return stuname;
	}
	public void setStuname(String stuname) {
		this.stuname = stuname;
	}
	public int getAge() {
		return age;
	}
	public void setAge(int age) {
		this.age = age;
	}
	public String getAddress() {
		return address;
	}
	public void setAddress(String address) {
		this.address = address;
	}
	@Override
	public String toString() {
		return "Student [stuid=" + stuid + ", stuname=" + stuname + ", age=" + age + ", address=" + address + "]";
	}
	public Student(int stuid, String stuname, int age, String address) {
		super();
		this.stuid = stuid;
		this.stuname = stuname;
		this.age = age;
		this.address = address;
	}
	public Student() {
		super();
	}
	public Student(String stuname, int age, String address) {
		super();
		this.stuname = stuname;
		this.age = age;
		this.address = address;
	}
	
}

9,数访问层

/**
 * 数据访问层:只要和数据库打交道,对数据库进行crud操作
 */
public class StudentDao {
	//查询所有学生信息
	public List<Student> getStudentAll() {
		//创建连接对象
		Connection conn = null;
		//执行对象
		PreparedStatement pst = null;
		//结果集
		ResultSet rs = null;
		//集合
		List<Student> list = new ArrayList<Student>();
		//建立连接
		conn=DBUtil.getConnection();
		//sql语句
		String sql="select * from stu";
		try {
			//创建执行对象
			pst=conn.prepareStatement(sql);
			//执行并返回结果
			rs=pst.executeQuery();
			//把结果集每一条添加到实体类,再将实体类添加到list
			while (rs.next()) {
				Student student = new Student(rs.getInt(1),rs.getString(2),rs.getInt(3),rs.getString(4));
				list.add(student);
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}finally {
			//关闭连接
			DBUtil.connClose(rs, pst, conn);
		}
		//返回结果
		return list;
	}
}

10,业务逻辑层

/**
 * 业务逻辑层:进行业务处理,数据加工,依赖于数据访问层
 *
 */
public class StudentService {
	StudentDao studentDao = new StudentDao();
	
	//查询所有学生信息
	public List<Student> getStudentAll(){
		return studentDao.getStudentAll();
	}
}

11,表示层

public class StudentView {
	//创建service对象
	static StudentService service = new StudentService();
	//创建输入对象
	static Scanner sc = new Scanner(System.in);
	
	public static void main(String[] args) {
		display();
	}
	//显示界面
	public static void display() {
		while (true) {
			System.out.println("----------学生管理系统----------");
			System.out.println("1.查询  2.添加  3.修改  4.删除  5.退出");
			System.out.println("请选择:");
			int result = sc.nextInt();
			switch (result) {
			case 1:
				//查询
				select();
				break;
			case 2:
				//添加
				break;
			case 3:
				//修改
				break; 
			case 4:
				//删除
				break;
			case 5:
				//退出
				System.out.println("退出成功");
				System.exit(0);
				break;
			default:
				System.out.println("输入有误");
				break;
			}
		}
	}
	//查询所有用户
	public static void select() {
		//调用service层  查询所有用户  方法
		List<Student> list = service.getStudentAll();
		//遍历输出
		for (Student student : list) {
			System.out.println(student);
		}
	}
	
}

附:源码

猜你喜欢

转载自blog.csdn.net/Love_codes/article/details/83620700
今日推荐