java Dao层与service层分析案例

之前不知道java分dao层与service层有什么用,直到进行业务逻辑编写发现分层简直太好了!Dao层是直接连接数据库的最底层,可以直接操作数据库,进行增删改查,service操作数据时直接调用Dao层的接口,无需知道具体实现内容。下面给出例子包括Dao层接口,Dao层接口实现,ServiceManager,serviceManager的实现,Dao层与Service的测试不再给出。

Dao

import java.util.List;

import lmh.edu.cn.computer.entity.Computer;

public interface ComputerDao {
	void add(Computer pcomputer); 

	void delete(Computer pcomputer);

	Computer findByName(String name);

	List<Computer> queryall();

	void upDate(Computer pcomputer);
}

Dao实现

import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import com.mysql.jdbc.PreparedStatement;

import lmh.edu.cn.computer.dao.ComputerDao;
import lmh.edu.cn.computer.entity.Computer;

public class ComputerDaoImpl extends Variable implements ComputerDao {

	List<Computer> computers = null;

	public void connection() throws ClassNotFoundException, SQLException { 
		Class.forName("com.mysql.jdbc.Driver");
		this.con = DriverManager.getConnection(url, usr, password);
	}

	public void refuse() throws SQLException { 
		if (this.rs != null)
			rs.close();
		if (this.ps != null)
			ps.close();
		if (this.st != null)
			st.close();
		if (this.con != null)
			con.close();
	}

	@Override
	public void add(Computer pcomputer) {
		try {
			connection();
			this.sql = "insert into p_computer(Name,Price,Size) values(?,?,?)";
			this.ps = con.prepareStatement(sql);
			ps.setString(1, pcomputer.getName());
			ps.setString(2, pcomputer.getPrice());
			ps.setString(3, pcomputer.getSize());
			ps.executeUpdate();
			refuse();
		} catch (ClassNotFoundException e) {
			logger.info("连接失败!");
			e.printStackTrace();
		} catch (SQLException e) {
			logger.info("出现异常!");
			e.printStackTrace();
		}

	}

	@Override
	public void delete(Computer pcomputer) {
		try {
			Class.forName("com.mysql.jdbc.Driver");
			this.con = DriverManager.getConnection(url, usr, password);
			this.sql = "delete from p_computer where name=?";
			this.ps = con.prepareStatement(sql);
			ps.setString(1, pcomputer.getName());
			ps.executeUpdate();
			refuse();
		} catch (ClassNotFoundException e) {
			logger.info("连接失败!");
			e.printStackTrace();
		} catch (SQLException e) {
			logger.info("出现异常!");
			e.printStackTrace();
		}
	}

	@Override
	public Computer findByName(String name) {
		try {
			Class.forName("com.mysql.jdbc.Driver");
			this.con = DriverManager.getConnection(url, usr, password);
			this.sql = "SELECT NAME,PRICE,SIZE FROM p_computer WHERE Name=?";
			this.ps = con.prepareStatement(sql);
			ps.setString(1, name);
			this.rs = ps.executeQuery();
			if (rs.next()) {
				String pName = rs.getString(1);
				String pPrice = rs.getString(2);
				String pSize = rs.getString(3);
				Computer pComputer = new Computer(pName, pPrice, pSize);
				return pComputer;
			}
			refuse();
			return null;
		} catch (ClassNotFoundException e) {
			logger.info("连接失败!");
			e.printStackTrace();
			return null;
		} catch (SQLException e) {
			logger.info("出现异常!");
			e.printStackTrace();
			return null;
		}
	}

	@Override
	public List<Computer> queryall() {
		try {
			Class.forName("com.mysql.jdbc.Driver");
			this.con = DriverManager.getConnection(url, usr, password);
			this.st = con.createStatement();
			this.sql = "SELECT NAME,PRICE,SIZE FROM p_computer";
			this.rs = st.executeQuery(sql);
			computers = new ArrayList<Computer>();
			while (rs.next()) {
				Computer pc = new Computer();
				pc.setName(rs.getString(1));
				pc.setPrice(rs.getString(2));
				pc.setSize(rs.getString(3));
				computers.add(pc);
			}
			refuse();
		} catch (ClassNotFoundException e) {
			logger.info("连接失败!");
			e.printStackTrace();
		} catch (SQLException e) {
			logger.info("出现异常!");
			e.printStackTrace();
		}
		return computers;
	}

	@Override
	public void upDate(Computer pcomputer) {
		try {
			Class.forName("com.mysql.jdbc.Driver");
			this.con = DriverManager.getConnection(url, usr, password);
			this.sql = "UPDATE p_computer SET Price=?,Size=? where Name=?";
			this.ps = con.prepareStatement(sql);
			logger.info(pcomputer.price);
			ps.setString(1, pcomputer.getPrice());
			ps.setString(2, pcomputer.getSize());
			ps.setString(3, pcomputer.getName());
			ps.execute();
			refuse();
		} catch (ClassNotFoundException e) {
			logger.info("连接失败!");
			e.printStackTrace();
		} catch (SQLException e) {
			logger.info("出现异常!");
			e.printStackTrace();
		}
	}
}
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

import com.mysql.jdbc.PreparedStatement;

//配置设置
public class Variable {
	static Logger logger = LogManager.getLogger(Variable.class);
	public String url = "jdbc:mysql://127.0.0.1/computer?"; //本地电脑的数据库
	public String usr = "root";
	public String password = "7654321a";
	public java.sql.Connection con = null;
	public java.sql.Statement st = null;
	public java.sql.PreparedStatement ps = null;
	public ResultSet rs = null;
	public String sql = null;
}

ServiceManager

import java.util.List;

import lmh.edu.cn.computer.entity.Computer;

public interface ComputerManager {
    
	Computer findByNameComp(String name);
    
	List<Computer> queryAllComp();
	
	void upDateComp(Computer pComputer);
	
	void deleteComp(Computer pComputer);
}

ServiceManager实现

import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.CellType;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

import lmh.edu.cn.computer.dao.ComputerDao;
import lmh.edu.cn.computer.dao.impl.ComputerDaoImpl;
import lmh.edu.cn.computer.entity.Computer;
import lmh.edu.cn.computer.service.ComputerManager;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

public class ComputerManagerImpl implements ComputerManager {
	protected Logger logger = LogManager.getLogger(this.getClass().getName());
	List<Computer> computerList;
	ComputerDao computerDao = new ComputerDaoImpl();

	@Override
	public Computer findByNameComp(String name) {
		return computerDao.findByName(name);
	}

	@Override
	public List<Computer> queryAllComp() {
		return computerDao.queryall();
	}

	@Override
	public void upDateComp(Computer pComputer) {
		computerDao.upDate(pComputer);

	}

	@Override
	public void deleteComp(Computer pComputer) {
		computerDao.delete(pComputer);
	}
}

 

  • 9
    点赞
  • 32
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值