随便写了一个DAO
Posted Hello.World!
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了随便写了一个DAO相关的知识,希望对你有一定的参考价值。
1 package com.java; 2 3 public class ExamStudent { 4 /** 5 * 流水号 6 */ 7 private int flowId; 8 9 /** 10 * 四级、六级 11 */ 12 private int type; 13 14 /** 15 * 身份证号码 16 */ 17 private String idCard; 18 19 /** 20 * 准考证号码 21 */ 22 private String examCard; 23 24 /** 25 * 学生姓名 26 */ 27 private String studentName; 28 29 /** 30 * 区域 31 */ 32 private String location; 33 34 /** 35 * 成绩 36 */ 37 private int grade; 38 39 public int getFlowId() { 40 return flowId; 41 } 42 43 public void setFlowId(int flowId) { 44 this.flowId = flowId; 45 } 46 47 public int getType() { 48 return type; 49 } 50 51 public void setType(int type) { 52 this.type = type; 53 } 54 55 public String getIdCard() { 56 return idCard; 57 } 58 59 public void setIdCard(String idCard) { 60 this.idCard = idCard; 61 } 62 63 public String getExamCard() { 64 return examCard; 65 } 66 67 public void setExamCard(String examCard) { 68 this.examCard = examCard; 69 } 70 71 public String getStudentName() { 72 return studentName; 73 } 74 75 public void setStudentName(String studentName) { 76 this.studentName = studentName; 77 } 78 79 public String getLocation() { 80 return location; 81 } 82 83 public void setLocation(String location) { 84 this.location = location; 85 } 86 87 public int getGrade() { 88 return grade; 89 } 90 91 public void setGrade(int grade) { 92 this.grade = grade; 93 } 94 95 @Override 96 public String toString() { 97 return "ExamStudent [flowId=" + flowId + ", type=" + type + ", idCard=" + idCard + ", examCard=" + examCard 98 + ", studentName=" + studentName + ", location=" + location + ", grade=" + grade + "]"; 99 } 100 101 }
1 package com.java; 2 3 import org.junit.Test; 4 5 public class DaoTest { 6 Dao dao = new Dao(); 7 @Test 8 public void testUpdate() { 9 String sql = "INSERT INTO customers (name,email,birth)" 10 + "VALUES (?,?,?)"; 11 dao.update(sql, "小明","[email protected]", 12 new java.sql.Date(new java.util.Date().getTime())); 13 } 14 15 @Test 16 public void testGetOne() { 17 String sql = "SELECT flow_id flowId,type type,id_card idCard" 18 + ",exam_card examCard,student_name studentName,location location," 19 + "grade grade FROM exam_student"; 20 ExamStudent student = new ExamStudent(); 21 dao.getOne(student.getClass(), sql); 22 23 System.out.println(student); 24 } 25 26 @Test 27 public void testGetForList() { 28 } 29 30 @Test 31 public void testGetForValue() { 32 } 33 34 }
创建表的ddl
/* SQLyog Ultimate v10.42 mysql - 5.5.28 : Database - dic ********************************************************************* */ /*!40101 SET NAMES utf8 */; /*!40101 SET SQL_MODE=‘‘*/; /*!40014 SET @[email protected]@UNIQUE_CHECKS, UNIQUE_CHECKS=0 */; /*!40014 SET @[email protected]@FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS=0 */; /*!40101 SET @[email protected]@SQL_MODE, SQL_MODE=‘NO_AUTO_VALUE_ON_ZERO‘ */; /*!40111 SET @[email protected]@SQL_NOTES, SQL_NOTES=0 */; CREATE DATABASE /*!32312 IF NOT EXISTS*/`dic` /*!40100 DEFAULT CHARACTER SET utf8 */; USE `dic`; /*Table structure for table `customers` */ DROP TABLE IF EXISTS `customers`; CREATE TABLE `customers` ( `ID` int(6) NOT NULL AUTO_INCREMENT, `NAME` varchar(25) DEFAULT NULL, `EMAIL` varchar(25) DEFAULT NULL, `BIRTH` date DEFAULT NULL, PRIMARY KEY (`ID`) ) ENGINE=InnoDB AUTO_INCREMENT=5 DEFAULT CHARSET=utf8; /*Data for the table `customers` */ insert into `customers`(`ID`,`NAME`,`EMAIL`,`BIRTH`) values (2,‘李小龙‘,‘[email protected]‘,‘1940-11-27‘),(3,‘成龙‘,‘[email protected]‘,‘1978-04-25‘),(4,‘小明‘,‘[email protected]‘,‘2016-09-28‘); /*Table structure for table `exam_student` */ DROP TABLE IF EXISTS `exam_student`; CREATE TABLE `exam_student` ( `FLOW_ID` int(20) NOT NULL AUTO_INCREMENT COMMENT ‘流水号‘, `TYPE` int(6) DEFAULT NULL COMMENT ‘四级/六级‘, `ID_CARD` varchar(18) DEFAULT NULL COMMENT ‘身份证号码‘, `EXAM_CARD` varchar(15) DEFAULT NULL COMMENT ‘准考证号码‘, `STUDENT_NAME` varchar(20) DEFAULT NULL COMMENT ‘学生姓名‘, `LOCATION` varchar(20) DEFAULT NULL COMMENT ‘区域‘, `GRADE` int(6) DEFAULT NULL COMMENT ‘成绩‘, KEY `FLOW_ID` (`FLOW_ID`) ) ENGINE=InnoDB AUTO_INCREMENT=12 DEFAULT CHARSET=utf8; /*Data for the table `exam_student` */ insert into `exam_student`(`FLOW_ID`,`TYPE`,`ID_CARD`,`EXAM_CARD`,`STUDENT_NAME`,`LOCATION`,`GRADE`) values (1,4,‘412824195263214584‘,‘200523164754000‘,‘张峰‘,‘郑州‘,85),(2,4,‘222224195263214584‘,‘200523164754001‘,‘孙朋‘,‘大连‘,56),(3,6,‘342824195263214584‘,‘200523164754002‘,‘刘明‘,‘沈阳‘,72),(4,6,‘100824195263214584‘,‘200523164754003‘,‘赵虎‘,‘哈尔滨‘,95),(5,4,‘454524195263214584‘,‘200523164754004‘,‘杨丽‘,‘北京‘,64),(6,4,‘854524195263214584‘,‘200523164754005‘,‘王小红‘,‘太原‘,60),(7,4,‘5‘,‘6‘,‘7‘,‘8‘,9),(8,23,‘24‘,‘25‘,‘26‘,‘27‘,28),(9,77,‘88‘,‘99‘,‘1010‘,‘1111‘,1212),(10,1,‘2‘,‘3‘,‘4‘,‘5‘,6),(11,23,‘45‘,‘56‘,‘67‘,‘78‘,89); /*!40101 SET [email protected]_SQL_MODE */; /*!40014 SET [email protected]_FOREIGN_KEY_CHECKS */; /*!40014 SET [email protected]_UNIQUE_CHECKS */; /*!40111 SET [email protected]_SQL_NOTES */;
以上是关于随便写了一个DAO的主要内容,如果未能解决你的问题,请参考以下文章
idea 单元测试 调用实体类里的id @test代码不会写了!
Mybatis Generator代码自动生成(实体类dao层映射文件)