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 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 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 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();
			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 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 computerList;
	ComputerDao computerDao = new ComputerDaoImpl();

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

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

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

	}

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

 

你可能感兴趣的:(java Dao层与service层分析案例)