欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

MVC分层开发思想

程序员文章站 2022-07-15 11:41:55
...
package com.bjsxt.test;

import java.util.ArrayList;

import com.bjsxt.pojo.Emp;
import com.bjsxt.service.EmpService;
import com.bjsxt.service.impl.EmpServiceImpl;

public class TestEmp {
	public static void main(String[] args) {
		//创建业务层对象
		EmpService es=new EmpServiceImpl();
		//调用业务层方法
		ArrayList<Emp> list=es.getAllEmpInfoService();
		System.out.println(list);
	}
}
package com.bjsxt.pojo;

import java.sql.Date;

public class Emp {
	private Integer empno;
	private String ename;
	private String job;
	private Integer mgr;
	private Date hiredate;
	private Double sal;
	private Double comm;
	private Integer deptno;
	public Integer getEmpno() {
		return empno;
	}
	public void setEmpno(Integer empno) {
		this.empno = empno;
	}
	public String getEname() {
		return ename;
	}
	public void setEname(String ename) {
		this.ename = ename;
	}
	public String getJob() {
		return job;
	}
	public void setJob(String job) {
		this.job = job;
	}
	public Integer getMgr() {
		return mgr;
	}
	public void setMgr(Integer mgr) {
		this.mgr = mgr;
	}
	public Date getHiredate() {
		return hiredate;
	}
	public void setHiredate(Date hiredate) {
		this.hiredate = hiredate;
	}
	public Double getSal() {
		return sal;
	}
	public void setSal(Double sal) {
		this.sal = sal;
	}
	public Double getComm() {
		return comm;
	}
	public void setComm(Double comm) {
		this.comm = comm;
	}
	public Integer getDeptno() {
		return deptno;
	}
	public void setDeptno(Integer deptno) {
		this.deptno = deptno;
	}
	@Override
	public String toString() {
		return "Emp [empno=" + empno + ", ename=" + ename + ", job=" + job + ", mgr=" + mgr + ", hiredate=" + hiredate
				+ ", sal=" + sal + ", comm=" + comm + ", deptno=" + deptno + "]";
	}
	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + ((comm == null) ? 0 : comm.hashCode());
		result = prime * result + ((deptno == null) ? 0 : deptno.hashCode());
		result = prime * result + ((empno == null) ? 0 : empno.hashCode());
		result = prime * result + ((ename == null) ? 0 : ename.hashCode());
		result = prime * result + ((hiredate == null) ? 0 : hiredate.hashCode());
		result = prime * result + ((job == null) ? 0 : job.hashCode());
		result = prime * result + ((mgr == null) ? 0 : mgr.hashCode());
		result = prime * result + ((sal == null) ? 0 : sal.hashCode());
		return result;
	}
	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		Emp other = (Emp) obj;
		if (comm == null) {
			if (other.comm != null)
				return false;
		} else if (!comm.equals(other.comm))
			return false;
		if (deptno == null) {
			if (other.deptno != null)
				return false;
		} else if (!deptno.equals(other.deptno))
			return false;
		if (empno == null) {
			if (other.empno != null)
				return false;
		} else if (!empno.equals(other.empno))
			return false;
		if (ename == null) {
			if (other.ename != null)
				return false;
		} else if (!ename.equals(other.ename))
			return false;
		if (hiredate == null) {
			if (other.hiredate != null)
				return false;
		} else if (!hiredate.equals(other.hiredate))
			return false;
		if (job == null) {
			if (other.job != null)
				return false;
		} else if (!job.equals(other.job))
			return false;
		if (mgr == null) {
			if (other.mgr != null)
				return false;
		} else if (!mgr.equals(other.mgr))
			return false;
		if (sal == null) {
			if (other.sal != null)
				return false;
		} else if (!sal.equals(other.sal))
			return false;
		return true;
	}
	public Emp() {
		super();
		// TODO Auto-generated constructor stub
	}
	public Emp(Integer empno, String ename, String job, Integer mgr, Date hiredate, Double sal, Double comm,
			Integer deptno) {
		super();
		this.empno = empno;
		this.ename = ename;
		this.job = job;
		this.mgr = mgr;
		this.hiredate = hiredate;
		this.sal = sal;
		this.comm = comm;
		this.deptno = deptno;
	}	
}
package com.bjsxt.service;

import java.util.ArrayList;

import com.bjsxt.pojo.Emp;

public interface EmpService {
	/**
	 * 获取所有的员工信息
	 * @return
	 */
	ArrayList<Emp> getAllEmpInfoService();
	
}
package com.bjsxt.service.impl;

import java.util.ArrayList;

import com.bjsxt.dao.EmpDao;
import com.bjsxt.dao.impl.EmpDaoImpl;
import com.bjsxt.pojo.Emp;
import com.bjsxt.service.EmpService;

public class EmpServiceImpl implements EmpService {
	//创建Dao层对象
	EmpDao ed=new EmpDaoImpl();
	@Override
	public ArrayList<Emp> getAllEmpInfoService() {
		//调用dao层对象获取员工信息
		return ed.getAllEmpInfoDao();
	}
	
}
package com.bjsxt.dao;

import java.util.ArrayList;

import com.bjsxt.pojo.Emp;

public interface EmpDao<T> {
	/**
	 * 操作数据库获取所有的员工信息
	 * @return
	 */
	ArrayList<T> getAllEmpInfoDao();

}
package com.bjsxt.dao.impl;

import java.lang.reflect.Method;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.util.ArrayList;

import com.bjsxt.dao.EmpDao;
import com.bjsxt.pojo.Emp;
import com.bjsxt.util.DBUtil;

public class EmpDaoImpl implements EmpDao {
	//获取所有的员工信息
		//封装的思想:相同的保留,不同的传参。
	
	@Override
	public ArrayList<Emp> getAllEmpInfoDao() {
		
		return DBUtil.<Emp>executeQuery("select * from emp",new Emp(), null);
	}
	
}		
package com.bjsxt.util;

import java.io.IOException;
import java.io.InputStream;
import java.lang.reflect.Method;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.Properties;

public class DBUtil {
	//声明全局变量记录jdbc参数
	private static String driver;
	private static String url;
	private static String username;
	private static String password;
	//使用静态代码块,在类加载时即完成对属性文件的读取
	static{
		//动态获取属性配置文件的流对象
		InputStream in=DBUtil.class.getResourceAsStream("/db.properties");
		//创建Properties对象
		Properties p=new Properties();
		//加载
		try {
			p.load(in);//会将属性配置文件的所有数据存储到Properties对象中
			//将读取的jdbc参数赋值给全局变量
			driver=p.getProperty("driver");
			url=p.getProperty("url");
			username=p.getProperty("username");
			password=p.getProperty("password");
			//加载驱动
			Class.forName(driver);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	//创建连接对象并返回
	public static Connection getConnection(){
		Connection conn=null;
		try {
			conn = DriverManager.getConnection(url, username, password);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		return conn;
	}
	//关闭资源
	public static void closeAll(ResultSet rs,Statement stmt,Connection conn){
		try {
			if(rs!=null){
				rs.close();
			}
		} catch (SQLException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
		try {
			stmt.close();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		try {
			conn.close();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		
	}
	//封装增加删除修改的通用工具方法
	/**
	 * @param sql SQL语句
	 * @param objs	SQL语句占位符实参,如果没有参数则传入null
	 * @return 返回增删改的结果,类型为int
	 */
	public static int executeDML(String sql,Object...objs){
		// 声明jdbc变量
				Connection conn = null;
				PreparedStatement ps = null;
				int i = -1;
				try {
					// 获取连接对象
					conn = DBUtil.getConnection();
					// 开启事务管理
					conn.setAutoCommit(false);
					// 创建SQL命令对象
					ps = conn.prepareStatement(sql);
					// 给占位符赋值
					if(objs!=null){
						for(int j=0;j<objs.length;j++){
							ps.setObject(j+1,objs[j]);
						}
					}
					// 执行SQL
					i = ps.executeUpdate();
					conn.commit();
				} catch (Exception e) {
					try {
						conn.rollback();
					} catch (SQLException e1) {
						// TODO Auto-generated catch block
						e1.printStackTrace();
					}
					e.printStackTrace();
				} finally {
					DBUtil.closeAll(null, ps, conn);
				}
				return i;
	}
	/**
	 * 基于MySQL数据库的。
	 * @param sql 查询方法
	 * @param t	 实体类的实例化对象
	 * @param objs	SQL的参数
	 * @return 返回List
	 * 	注意1:实体类的属性类型必须为 基本类型必须声明为包装类类型,Date类型必须为java.sql.Date
	 * 	注意2:set方法必须按照set字段名的规则声明。
	 */
	public static<T> ArrayList<T> executeQuery(String sql,T t,Object...objs){
		//声明jdbc对象
		Connection conn=null;
		PreparedStatement ps=null;
		ResultSet rs=null;
		ArrayList<T> list=null;
		try {
			//获取连接
			conn=DBUtil.getConnection();
			//创建SQL命令对象
			ps=conn.prepareStatement(sql);
			//给占位符赋值
			if(objs!=null){
				for(int i=0;i<objs.length;i++){
					ps.setObject(i+1,objs[i]);
				}
			}
			//执行SQL
			list=new ArrayList<>();
			rs=ps.executeQuery();
			//获取结果集的表结构和列数
			ResultSetMetaData rm = rs.getMetaData();
				//获取结果集的列数
				int count=rm.getColumnCount();
			//遍历结果
			while(rs.next()){
				//获取实体类对象(反射)
					Class cls=t.getClass();
					T obj=(T)cls.newInstance();
				//获取一条记录,并存储到实体类对象中
					for(int i=0;i<count;i++){
						//获取字段名
						String columnName=rm.getColumnName(i+1);
						//获取实体类的set方法的方法名
						String methodName="set"+columnName.substring(0,1).toUpperCase()+columnName.substring(1).toLowerCase();
						//获取字段类型
						String columnClassName = rm.getColumnClassName(i+1);
						//调用方法将字段值存储到实体类中
							//获取方法对象
							Method m=cls.getDeclaredMethod(methodName, Class.forName(columnClassName));
							//执行方法
							m.invoke(obj, rs.getObject(columnName));
					}
				//将数据存储到list中
					list.add(obj);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}finally{//关闭资源
			DBUtil.closeAll(rs, ps, conn);
		}
		//返回结果
		return list;
	}
	
	
}
driver=com.mysql.jdbc.Driver
url=jdbc:mysql://localhost:3306/407
username=root
password=1234
package com.bjsxt.test;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.util.ArrayList;

import com.bjsxt.util.DBUtil;

public class TestM {
	public static void main(String[] args) {
		//声明jdbc对象
		Connection conn=null;
		PreparedStatement ps=null;
		ResultSet rs=null;
		try {
			//获取连接
			conn=DBUtil.getConnection();
			//创建SQL命令对象
			ps=conn.prepareStatement("select * from emp");
			//执行SQL

			rs=ps.executeQuery();
			//获取结果集的表结构和列数
			ResultSetMetaData rm = rs.getMetaData();
				//获取结果集的列数
				int count=rm.getColumnCount();
			//遍历结果
			while(rs.next()){
				//获取实体类对象(反射)
				//获取一条记录,并存储到实体类对象中
				for(int i=0;i<count;i++){
					//获取字段名
					String columnName=rm.getColumnName(i+1);
					//获取实体类的set方法的方法名
					String methodName="set"+columnName.substring(0,1).toUpperCase()+columnName.substring(1).toLowerCase();
					//获取字段类型
					String  columnTypeName=rm.getColumnClassName(i+1);
					System.out.println(columnName+":"+methodName+":"+columnTypeName);
					
					//调用方法将字段值存储到实体类中
						//获取方法对象
				}				
//				//创建实体类对象存储数据
//				Emp e=new Emp();
//				e.setEmpno(rs.getInt("empno"));
//				e.setEname(rs.getString("ename"));
//				e.setJob(rs.getString("job"));
//				e.setMgr(rs.getInt("mgr"));
//				e.setHiredate(rs.getDate("hiredate"));
//				e.setSal(rs.getDouble("sal"));
//				e.setComm(rs.getDouble("comm"));
//				e.setDeptno(rs.getInt("deptno"));
//				list.add(e);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}finally{//关闭资源
			DBUtil.closeAll(rs, ps, conn);
		}
	}
}