Scott 数据 映射 MySQL代码实现分享
来源:脚本之家
时间:2023-01-21 16:36:01 373浏览 收藏
知识点掌握了,还需要不断练习才能熟练运用。下面golang学习网给大家带来一个数据库开发实战,手把手教大家学习《Scott 数据 映射 MySQL代码实现分享》,在实现功能的过程中也带大家重新温习相关知识点,温故而知新,回头看看说不定又有不一样的感悟!
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 Listemps = new ArrayList(); private static List 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 getEmps() { return emps; } public static List getDepts() { return depts; } }
今天关于《Scott 数据 映射 MySQL代码实现分享》的内容就介绍到这里了,是不是学起来一目了然!想要了解更多关于mysql的内容请关注golang学习网公众号!
声明:本文转载于:脚本之家 如有侵犯,请联系study_golang@163.com删除
相关阅读
更多>
-
499 收藏
-
244 收藏
-
235 收藏
-
157 收藏
-
101 收藏
最新阅读
更多>
-
368 收藏
-
475 收藏
-
266 收藏
-
273 收藏
-
283 收藏
-
210 收藏
课程推荐
更多>
-
- 前端进阶之JavaScript设计模式
- 设计模式是开发人员在软件开发过程中面临一般问题时的解决方案,代表了最佳的实践。本课程的主打内容包括JS常见设计模式以及具体应用场景,打造一站式知识长龙服务,适合有JS基础的同学学习。
- 立即学习 542次学习
-
- GO语言核心编程课程
- 本课程采用真实案例,全面具体可落地,从理论到实践,一步一步将GO核心编程技术、编程思想、底层实现融会贯通,使学习者贴近时代脉搏,做IT互联网时代的弄潮儿。
- 立即学习 507次学习
-
- 简单聊聊mysql8与网络通信
- 如有问题加微信:Le-studyg;在课程中,我们将首先介绍MySQL8的新特性,包括性能优化、安全增强、新数据类型等,帮助学生快速熟悉MySQL8的最新功能。接着,我们将深入解析MySQL的网络通信机制,包括协议、连接管理、数据传输等,让
- 立即学习 497次学习
-
- JavaScript正则表达式基础与实战
- 在任何一门编程语言中,正则表达式,都是一项重要的知识,它提供了高效的字符串匹配与捕获机制,可以极大的简化程序设计。
- 立即学习 487次学习
-
- 从零制作响应式网站—Grid布局
- 本系列教程将展示从零制作一个假想的网络科技公司官网,分为导航,轮播,关于我们,成功案例,服务流程,团队介绍,数据部分,公司动态,底部信息等内容区块。网站整体采用CSSGrid布局,支持响应式,有流畅过渡和展现动画。
- 立即学习 484次学习