分层:数据访问层、业务逻辑层、视图层

分层:数据访问层、业务逻辑层、视图层,第1张

分层:开发模式
    数据访问层     业务逻辑层:调用数据访问层     视图层:调用业务逻辑层
                                               数据库表

1、创建项目
2、创建包:
    com.zking.util    
    com.zking.entity
    com.zking.dao(放dao接口)
    com.zking.dao.imp    (放dao接口实现类)
    com.zking.biz
    com.zking.biz.imp

 这里是链接oracle数据库的,下面会我给大家演示用户是如何分层的,如果想学习的你可以直接复制到你的项目中 理解我们的分层:数据访问层、业务逻辑层、视图层 DBHelper.java类
package com.zking.util;

import java.sql.Connection;
import java.sql.Driver;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;

public class DBHelper {
	private static String user = "scott";
	private static String upwd = "tiger";
	private static String cname = "oracle.jdbc.driver.OracleDriver";
	private static String url = "jdbc:oracle:thin:@localhost:1521:orcl";
	
	//注册驱动类
	static {
		try {
			Class.forName(cname);
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		}
	}
	//连接数据库
	/**
	 * 连接数据库
	 * @return
	 */
	public static Connection getCon() {
		Connection con = null;
		try {
			con = DriverManager.getConnection(url, user, upwd);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return con;
	}
	
	/**
	 * 关闭连接
	 * @param con
	 * @param ps
	 * @param rs
	 */
	public static void closeDb(Connection con,PreparedStatement ps,ResultSet rs) {
		try {
			if(con!=null) {
				con.close();
			}
			if(ps!=null) {
				ps.close();
			}
			if(rs!=null) {
				rs.close();
			}
			
			
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	/**
	 * 下一个获取下一个编号的方法
	 * @return 下一个编号
	 */
	public static int getNextId(String tableName,String col) {
		int id = 1;
		Connection con = null;
		PreparedStatement ps = null;
		ResultSet rs = null;
		try {
			con = DBHelper.getCon();
			ps = con.prepareStatement("select max("+col+") from "+tableName);
			rs = ps.executeQuery();
			if(rs.next()) {
				id = rs.getInt(1)+1;
			}
			
		} catch (Exception e) {
			e.printStackTrace();
		}finally {
			DBHelper.closeDb(con, ps, rs);
		}
		return id;
	}
	
	
	//关闭连接

}
User.java  实体类
package com.zking.entity;

public class User {
	private int uuid;
	private String uname;
	private String uwpd;
	private String uinfo;
	public int getUuid() {
		return uuid;
	}
	public void setUuid(int uuid) {
		this.uuid = uuid;
	}
	public String getUname() {
		return uname;
	}
	public void setUname(String uname) {
		this.uname = uname;
	}
	public String getUwpd() {
		return uwpd;
	}
	public void setUwpd(String uwpd) {
		this.uwpd = uwpd;
	}
	public String getUinfo() {
		return uinfo;
	}
	public void setUinfo(String uinfo) {
		this.uinfo = uinfo;
	}
	public User() {
		super();
	}
	public User(String uname, String uwpd, String uinfo) {
		super();
		this.uname = uname;
		this.uwpd = uwpd;
		this.uinfo = uinfo;
	}
	public User(int uuid, String uname, String uwpd, String uinfo) {
		super();
		this.uuid = uuid;
		this.uname = uname;
		this.uwpd = uwpd;
		this.uinfo = uinfo;
	}
	@Override
	public String toString() {
		return "User [uuid=" + uuid + ", uname=" + uname + ", uwpd=" + uwpd + ", uinfo=" + uinfo + "]";
	}


}
IUserDao.java  用户的数据访问层接口
package com.zking.dao;

import com.zking.entity.User;

/**
 * 用户的数据访问层接口
 * @author d
 *
 */
public interface IUserDao {
	/**
	 * 用户登录方法
	 * @param uname 用户名
	 * @param upwd 用户密码
	 * @return 登录成功返回用户对象,否则返回null
	 */
	public User login(String uname,String upwd);
	
	/**
	 * 用户注册方法
	 * @param user 要注册的用户
	 * @return 成功返回1,失败返回0
	 */
	public int register(User user);

}
Imp_UserDao 用户的数据访问层实现类
package com.zking.dao.imp;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;

import com.zking.dao.IUserDao;
import com.zking.entity.User;
import com.zking.util.DBHelper;

/**
 * 	用户的数据访问层实现类
 * @author d
 *
 */
public class Imp_UserDao implements IUserDao {

	
	public User login(String uname, String upwd) {
		Connection con = null;
		PreparedStatement ps = null;
		ResultSet rs = null;
		User user = null;
		try {
			con = DBHelper.getCon();
			ps = con.prepareStatement("select * from T277 where uname=? and upwd=?");
			ps.setString(1, uname);
			ps.setString(2, upwd);
			rs = ps.executeQuery();
			if(rs.next()) {
				user = new User(rs.getInt(1), uname, upwd, rs.getString(4));
			}
		} catch (Exception e) {
			e.printStackTrace();
		}finally {
			DBHelper.closeDb(con, ps, rs);
		}
		return user;
	}

	public int register(User user) {
		return 0;
	}

}
IUserBiz  用户的业务逻辑层接口
package com.zking.biz;

import com.zking.entity.User;

/**
 * 用户的业务逻辑层接口
 * @author d
 *
 */
public interface IUserBiz {
	/**
	 * 用户登录方法
	 * @param uname 用户名
	 * @param upwd 用户密码
	 * @return 登录成功返回用户对象,否则返回null
	 */
	public User login(String uname,String upwd);
	
	/**
	 * 用户注册方法
	 * @param user 要注册的用户
	 * @return 成功返回true,失败返回false
	 */
	public boolean register(User user);


}
 Imp_UserBiz   用户的业务逻辑层实现类
package com.zking.biz.imp;

import com.zking.biz.IUserBiz;
import com.zking.dao.IUserDao;
import com.zking.dao.imp.Imp_UserDao;
import com.zking.entity.User;

/**
 * 用户的业务逻辑层实现类
 * @author  T277
 *
 */
public class Imp_UserBiz implements IUserBiz {

	//多态
	IUserDao iud = new  Imp_UserDao();
	
	public User login(String uname, String upwd) {
		return iud.login(uname, upwd);
	}

	public boolean register(User user) {
		return iud.register(user)==1?true:false;
	}
	
	
	

}
我们的登录页面就是直接调用了 dologin.jsp
<%
	//接收数据
	//调用登录方法-实例化业务逻辑层对象
	IUserBiz iub = new Imp_UserBiz();
	User us = iub.login("", "");
	
%>
我们的login.jsp页面是不会用到我们的java代码的了 在接下来 我也会跟大家一起学习,初来驾到有很多不懂得东西希望大伙多多指教(๑′ᴗ‵๑)I Lᵒᵛᵉᵧₒᵤ❤ 后面我会给大家出个使用分层实现购物车2.0版

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/langs/735548.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-04-27
下一篇 2022-04-27

发表评论

登录后才能评论

评论列表(0条)

保存