数据表与简单Java类映射-依靠代码链

package cn.Test.lzh;

/**
 * dept(部门表): deptno(部门编号)、 dname(部门名称)、 loc(位置) emp(雇员表):
 * empno(雇员编号)、ename(雇员名称)、job(职位)、sal(工资)、 comm(佣金)、 depno(部门编号)、 mgr(领导)
 * 
 * 关系: 1):一个部门有多个雇员
 *      2):一个雇员有可以一个或零个领导
 *
 * 步骤:1):实现基本字段的转换,写出Dept和Emp的属性和方法 
 *     2):解决外键关系:
 *       一个雇员属于一个部分,在雇员里面保存部分信息,在Emp类中增加Dept dept对象及相应的set/get方法
 *       一个部分有多个雇员,使用数组来完成功能,在Dept类中增加Emp emps[]对象数组及相应的set/get方法 一个雇员除KING外都有一个领导
 *     3):设置并取得数据  
 *        根据结构设置数据: 
 *        根据结构取出数据
 */

/**
 * 部门表设计
 */
class Dept {
	private int deptno;
	private String dname;
	private String loc;
	private Emp[] emps;

	public Emp[] getEmps() {
		return emps;
	}

	public void setEmps(Emp[] emps) {
		this.emps = emps;
	}

	public int getDeptno() {
		return deptno;
	}

	public void setDeptno(int deptno) {
		this.deptno = deptno;
	}

	public String getDname() {
		return dname;
	}

	public void setDname(String dname) {
		this.dname = dname;
	}

	public String getLoc() {
		return loc;
	}

	public void setLoc(String loc) {
		this.loc = loc;
	}

	public Dept() {
		super();
	}

	public Dept(int deptno, String dname, String loc) {
		this.deptno = deptno;
		this.dname = dname;
		this.loc = loc;
	}

	public String getInfo() {
		return "部门编号:" + this.deptno + ",部门名称:" + this.dname + ",位置:" + this.loc;
	}
}

/**
 * 雇员表设计
 */
class Emp {
	private int empno;
	private String ename;
	private String job;
	private double sal;
	private double comm;
	private Dept dept;
	private Emp mgr;

	public int getEmpno() {
		return empno;
	}

	public void setEmpno(int 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 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 Dept getDept() {
		return dept;
	}

	public void setDept(Dept dept) {
		this.dept = dept;
	}

	public Emp getMgr() {
		return mgr;
	}

	public void setMgr(Emp mgr) {
		this.mgr = mgr;
	}

	public Emp() {
		super();
	}

	public Emp(int empno, String ename, String job, double sal, double comm) {
		this.empno = empno;
		this.ename = ename;
		this.job = job;
		this.sal = sal;
		this.comm = comm;

	}

	public String getInfo() {
		return "雇员编号:" + this.empno + ",雇员名称:" + this.ename + ",职位:" + this.job + ",工资:" + this.sal + ",佣金:"
				+ this.comm;
	}
}

/**
 * 测试:数据表与简单Java类映射
 * 依靠代码链进行读取操作
 * @author lzh
 *
 */

public class DataLinkDemo {

	public static void main(String[] args) {
		// 设置部门和雇员的测试数据
		Dept dept = new Dept(10, "ACCOUNTING", "New York"); // 部门信息
		Emp ea = new Emp(7369, "SMITH", "CLERK", 800.0, 0.00); // 雇员ea信息
		Emp eb = new Emp(7902, "FORD", "MANAGER", 2450.0, 0.00); // 雇员eb信息
		Emp ec = new Emp(7369, "KING", "PRESIDENT", 5000.0, 0.00); // 雇员ec信息

		// 设置雇员和领导关系
		ea.setMgr(eb);
		eb.setMgr(ec);

		// 设置雇员与对应的部门信息
		ea.setDept(dept);
		eb.setDept(dept);
		ec.setDept(dept);

		// 设置部门与雇员对应的信息
		dept.setEmps(new Emp[] { ea, eb, ec });

		// 测试取出数据
		// 输出雇员信息
		System.out.println("ea雇员信息:" + ea.getInfo());
		// 根据雇员查询所对应的领导信息
		System.out.println("ea雇员领导的信息:" + ea.getMgr().getInfo());

		// 根据雇员查询所对应的部门信息
		System.out.println("ea雇员部门的信息:" + ea.getDept().getInfo());

		// 取出部门信息
		System.out.println("dept部门信息:" + dept.getInfo());
		// 根据部门取出所有雇员及每个雇员的领导信息
		for (int x = 0; x < dept.getEmps().length; x++) {
			System.out.println("\t|-部门雇员:" + dept.getEmps()[x].getInfo());
			if (dept.getEmps()[x].getMgr() != null) {
				System.out.println("\t\t|-对应领导:" + dept.getEmps()[x].getMgr().getInfo());
			}

		}

	}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
private static void printTableMetaInfo(Session session) { Connection connection = session.connection(); try { DatabaseMetaData metaData = connection.getMetaData(); ResultSet result = metaData.getColumns(null, null, NameOfTable, null); String strInJava = ""; String typeInJava; while (result.next()) { String columnName = result.getString(4); if ("stampTime".equalsIgnoreCase(columnName)) { continue; } int columnType = result.getInt(5); String nameFirstLetterLower = columnName.substring(0, 1).toLowerCase() + columnName.substring(1); switch (columnType) { case Types.VARCHAR: case Types.LONGVARCHAR: case Types.LONGNVARCHAR: case Types.NVARCHAR: case Types.CHAR: typeInJava = "String"; break; case Types.TINYINT: case Types.SMALLINT: case Types.INTEGER: typeInJava = useInteger ? "Integer" : "int"; break; case Types.TIMESTAMP: case Types.BINARY: typeInJava = "Calendar"; break; case Types.DECIMAL: typeInJava = "BigDecimal"; break; case Types.BIGINT: typeInJava = "BigInteger"; break; case Types.LONGVARBINARY: typeInJava = "byte[]"; break; case Types.DATE: typeInJava = "Calendar"; break; default: throw new Exception("Unknown type " + columnType + " and column is " + columnName); } strInJava += " private " + typeInJava + " " + nameFirstLetterLower + ";\n"; // strInHibernate += "\n"; } String str = "import javax.persistence.Entity;\n" + "import javax.persistence.Id;\n" + "import javax.persistence.Table;\n" + "import java.util.Calendar;\n\n"; str += "@Entity\n"; str += "@Table(name=\"$\")\n".replace("$", NameOfTable); str += "public class $ {\n".replace("$", NameOfTable.substring(2)); str += "\n @Id\n"; str += strInJava; str += "}"; System.out.println(str); StringSelection stringSelection = new StringSelection(str); Clipboard clpbrd = Toolkit.getDefaultToolkit().getSystemClipboard(); clpbrd.setContents(stringSelection, null); } catch (Exception e) { e.printStackTrace(); }

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值