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

Scott 数据 映射 MySQL代码实现分享

程序员文章站 2023-01-09 17:07:02
目录1、sql2、实体类2.1、dept.java2.2、emp.java3、数据库模拟代码1、sqldrop table if exists `tb_dept`;create table `tb_d...

1、sql

drop table if exists `tb_dept`;
create table `tb_dept`  (
  `deptno` tinyint(2) unsigned not null  comment '部门编号',
  `dname` varchar(14) character set utf8 collate utf8_general_ci null default null comment '部门名称',
  `loc` varchar(13) character set utf8 collate utf8_general_ci null default null comment '部门地址',
  primary key (`deptno`) using btree
) engine = innodb  character set = utf8 collate = utf8_general_ci row_format = dynamic;

insert into `tb_dept` values (10, 'accounting', 'new york');
insert into `tb_dept` values (20, 'research', 'dallas');
insert into `tb_dept` values (30, 'sales', 'chicago');
insert into `tb_dept` values (40, 'operations', 'boston');

-------------------------------------------------------------------
drop table if exists `tb_emp`;
create table `tb_emp`  (
  `empno` int(4) unsigned not null,
  `ename` varchar(10) character set utf8 collate utf8_general_ci null default null,
  `job` varchar(9) character set utf8 collate utf8_general_ci null default null,
  `mgr` int(4) unsigned  null default null,
  `hiredate` date null default null,
  `sal` decimal(7, 2) null default null,
  `comm` decimal(7, 2) null default null,
  `deptno` tinyint(2) unsigned null default null,
  primary key (`empno`) using btree,
  index `deptno`(`deptno`) using btree,
  constraint `tb_emp_ibfk_1` foreign key (`deptno`) references `tb_dept` (`deptno`) on delete restrict on update restrict
) engine = innodb character set = utf8 collate = utf8_general_ci row_format = dynamic;

insert into `tb_emp` values (7369, 'smith', 'clerk', 7902, '1980-12-17', 800.00, null, 20);
insert into `tb_emp` values (7499, 'allen', 'salesman', 7698, '1981-02-20', 1600.00, 300.00, 30);
insert into `tb_emp` values (7521, 'ward', 'salesman', 7698, '1981-02-22', 1250.00, 500.00, 30);
insert into `tb_emp` values (7566, 'jones', 'manager', 7839, '1981-04-02', 2975.00, null, 20);
insert into `tb_emp` values (7654, 'martin', 'salesman', 7698, '1981-09-28', 1250.00, 1400.00, 30);
insert into `tb_emp` values (7698, 'blake', 'manager', 7839, '1981-05-01', 2850.00, null, 30);
insert into `tb_emp` values (7782, 'clark', 'manager', 7839, '1981-06-09', 2450.00, null, 10);
insert into `tb_emp` values (7788, 'scott', 'analyst', 7566, '1987-04-19', 3000.00, null, 20);
insert into `tb_emp` values (7839, 'king', 'president', null, '1981-11-17', 5000.00, null, 10);
insert into `tb_emp` values (7844, 'turner', 'salesman', 7698, '1981-09-08', 1500.00, 0.00, 30);
insert into `tb_emp` values (7876, 'adams', 'clerk', 7788, '1987-05-23', 1100.00, null, 20);
insert into `tb_emp` values (7900, 'james', 'clerk', 7698, '1981-12-03', 950.00, null, 30);
insert into `tb_emp` values (7902, 'ford', 'analyst', 7566, '1981-12-03', 3000.00, null, 20);
insert into `tb_emp` values (7934, 'miller', 'clerk', 7782, '1982-01-23', 1300.00, null, 10);

2、实体类

2.1、dept.java

/**
 * 部门
 * @author hc
 *
 */
public class dept {
    /**
     * 部门编号
     */
    private integer deptno;
    /**
     * 部门名称
     */
    private string dname;
    /**
     * 部门地址
     */
    private string loc;

    public dept() {
    }

    public dept(integer deptno, string dname, string loc) {
        this.deptno = deptno;
        this.dname = dname;
        this.loc = loc;
    }

    public integer getdeptno() {
        return deptno;
    }

    public void setdeptno(integer 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;
    }

    @override
    public string tostring() {
        return "dept{" +
                "deptno=" + deptno +
                ", dname='" + dname + '\'' +
                ", loc='" + loc + '\'' +
                '}';
    }
}

2.2、emp.java

/**
 * 员工
 * @author hc
 */
public class emp {
    /**
     * 员工编号
     */
    private integer empno;
    /**
     * 员工姓名
     */
    private string ename;
    /**
     * 工作
     */
    private string job;
    /**
     * 上级领导编号
     */
    private integer mgr;
    /**
     * 受雇日期
     */
    private localdate hiredate;
    /**
     * 薪资
     */
    private double sal;
    /**
     * 奖金
     */
    private double comm;
    /**
     * 部门编号
     */
    private integer deptno;

    public emp() {
    }

    public emp(integer empno, string ename, string job, integer mgr, localdate hiredate, double sal, double comm, integer deptno) {
        this.empno = empno;
        this.ename = ename;
        this.job = job;
        this.mgr = mgr;
        this.hiredate = hiredate;
        this.sal = sal;
        this.comm = comm;
        this.deptno = 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 localdate gethiredate() {
        return hiredate;
    }

    public void sethiredate(localdate 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 +
                '}';
    }
}

3、数据库模拟代码

public class db {
    private static list<emp> emps = new arraylist<>();
    private static list<dept> depts = new arraylist<>();

    static {
        depts.add(new dept(10,"accounting","newyork"));
        depts.add(new dept(20,"research","dallas"));
        depts.add(new dept(30,"sales","chicago"));
        depts.add(new dept(40,"operations","boston"));

        emps.add(new emp(7369, "smith", "clerk", 7902,localdate.of(1980, 12, 17), 800d, null, 20));
        emps.add(new emp(7499, "allen", "salesman", 7698, localdate.of(1981, 2, 20), 1600d, 300d, 30));
        emps.add(new emp(7521, "ward", "salesman", 7698, localdate.of(1981, 2, 22), 1250d, 500d, 30));
        emps.add(new emp(7566, "jones", "manager", 7893, localdate.of(1981, 4, 2), 2975d, null, 20));
        emps.add(new emp(7654, "martin", "salesman", 7698, localdate.of(1981, 9, 28), 1250d, 1400d, 30));
        emps.add(new emp(7698, "blake", "manager", 7839, localdate.of(1981, 5, 1), 2850d, null, 30));
        emps.add(new emp(7782, "clark", "manager", 7839, localdate.of(1981, 6, 9), 2450d, 600d, 10));
        emps.add(new emp(7788, "scott", "analyst", 7566, localdate.of(1987, 4, 19), 3000d, null, 20));
        emps.add(new emp(7839, "king", "president", null, localdate.of(1981, 11, 17), 5000d, null, 10));
        emps.add(new emp(7844, "turner", "salesman", 7698, localdate.of(1981, 9, 8), 1500d, null, 30));
        emps.add(new emp(7876, "adams", "clerk", 7788, localdate.of(1987, 5, 23), 1100d, 350d, 20));
        emps.add(new emp(7900, "james", "clerk", 7698, localdate.of(1981, 12, 3), 950d, null, 30));
        emps.add(new emp(7902, "ford", "analyst", 7566, localdate.of(1981, 12, 3), 3000d, null, 20));
        emps.add(new emp(7934, "miller", "clerk", 7782, localdate.of(1982, 1, 23), 1300d, 400d, 10));
    }

    public static list<emp> getemps() {
        return emps;
    }

    public static list<dept> getdepts() {
        return depts;
    }
}

到此这篇关于scott 数据 映射 mysql代码分享的文章就介绍到这了,更多相关scott 数据 映射 mysql内容请搜索以前的文章或继续浏览下面的相关文章希望大家以后多多支持!