您好,欢迎访问三七文档
当前位置:首页 > 临时分类 > mybatis学习重点笔记2
2)创立students.sql和cards.sqldroptablestudents;droptablecards;createtablecards(idint(5)primarykey,numvarchar(20));createtablestudents(idint(5)primarykey,namevarchar(10),cidint(5),第十六章回顾SQL99中连接查询1)内连接2)外连接3)自连接第十七章回顾hibernate多表开发1)一对一2)一对多3)多对多第十八章mybatis一对一映射【学生与身份证】1)参见一对一图示.JPG3)创立Students.java和Card.javapublicclassCard{privateIntegerid;privateStringnum;privateStudentstudent;publicCard(){}publicIntegergetId(){returnid;}publicvoidsetId(Integerid){this.id=id;}publicStringgetNum(){returnnum;}publicvoidsetNum(Stringnum){this.num=num;}publicStudentgetStudent(){returnstudent;}publicvoidsetStudent(Studentstudent){this.student=student;}}publicclassStudent{privateIntegerid;privateStringname;privateCardcard;publicStudent(){}publicIntegergetId(){returnid;}publicvoidsetId(Integerid){this.id=id;constraintcid_fkforeignkey(cid)referencescards(id));insertintocards(id,num)values(1,'111');insertintostudents(id,name,cid)values(1,'哈哈',1);4)创立StudentMapper.xml和CardMapper.xmlCardMapper.xml?xmlversion=1.0encoding=UTF-8?!DOCTYPEmapperPUBLIC-//mybatis.org//DTDMapper3.0//EN=cardNamespaceresultMaptype=cn.itcast.javaee.mybatis.one2one.Cardid=cardMapidproperty=idcolumn=id/resultproperty=numcolumn=num//resultMap/mapperStudentMapper.xml?xmlversion=1.0encoding=UTF-8?!DOCTYPEmapperPUBLIC-//mybatis.org//DTDMapper3.0//EN=studentNamespaceresultMaptype=cn.itcast.javaee.mybatis.one2one.Studentid=studentMapidproperty=idcolumn=id/resultproperty=namecolumn=name/associationproperty=cardresultMap=cardNamespace.cardMap//resultMapselectid=findByIdparameterType=intresultMap=studentMapselects.id,s.name,c.id,c.numfromstudentssinnerjoincardsc}publicStringgetName(){returnname;}publicvoidsetName(Stringname){this.name=name;}publicCardgetCard(){returncard;}publicvoidsetCard(Cardcard){this.card=card;}}droptablestudents;droptablegrades;createtablegrades(gidint(5)primarykey,5)创立StudentCardDao.javapublicclassStudentCardDao{/***查询1号【学生】*/publicStudentfindById(intid)throwsException{SqlSessionsqlSession=null;try{sqlSession=MybatisUtil.getSqlSession();returnsqlSession.selectOne(studentNamespace.findById,id);}catch(Exceptione){e.printStackTrace();throwe;}finally{MybatisUtil.closeSqlSession();}}publicstaticvoidmain(String[]args)throwsException{StudentCardDaodao=newStudentCardDao();Studentstudent=dao.findById(1);System.out.println(student.getId()+:+student.getName());System.out.println(student.getCard().getId()+:+student.getCard().getNum());}}第十九章mybatis一对多映射【班级与学生】1)参见一对多图示.JPG2)创立grades.sql和students.sqlons.cid=c.idands.id=#{id}/select/mapper/***班级(单方)*@authorAdminTC*/publicclassGrade{privateIntegerid;privateStringname;privateListStudentstudentList=newArrayListStudent();publicGrade(){}publicIntegergetId(){returnid;}publicvoidsetId(Integerid){this.id=id;}publicStringgetName(){returnname;}publicvoidsetName(Stringname){this.name=name;}publicListStudentgetStudentList(){returnstudentList;}publicvoidsetStudentList(ListStudentstudentList){this.studentList=studentList;}}/***学生(多方)*@authorAdminTC3)创立Grade.java和Student.sqlgnamevarchar(10));createtablestudents(sidint(5)primarykey,snamevarchar(10),sgidint(5),constraintsgid_fkforeignkey(sgid)referencesgrades(gid));insertintogrades(gid,gname)values(1,'java');insertintostudents(sid,sname,sgid)values(1,'哈哈',1);insertintostudents(sid,sname,sgid)values(2,'呵呵',1);?xmlversion=1.0encoding=UTF-8?!DOCTYPEmapperPUBLIC-//mybatis.org//DTDMapper3.0//EN=gradeNamespaceresultMaptype=cn.itcast.javaee.mybatis.one2many.Gradeid=gradeMapidproperty=idcolumn=gid/resultproperty=namecolumn=gname/collectionproperty=studentListresultMap=studentNamespace.studentMap//resultMapselectid=findGradeByNameparameterType=stringresultMap=gradeMapselectg.gid,g.gname,s.sid,s.snamefromgradesg,studentsswhereg.gid=s.sgid4)创立GradeMapper.xml和StudentMapper.xmlGradeMapper.xml*/publicclassStudent{privateIntegerid;privateStringname;privateGradegrade;publicStudent(){}publicIntegergetId(){returnid;}publicvoidsetId(Integerid){this.id=id;}publicStringgetName(){returnname;}publicvoidsetName(Stringname){this.name=name;}publicGradegetGrade(){returngrade;}publicvoidsetGrade(Gradegrade){this.grade=grade;}}?xmlversion=1.0encoding=UTF-8?!DOCTYPEmapperPUBLIC-//mybatis.org//DTDMapper3.0//EN=studentNamespaceresultMaptype=cn.itcast.javaee.mybatis.one2many.Studentid=studentMapidproperty=idcolumn=sid/resultproperty=namecolumn=sname/associationproperty=graderesultMap=gradeNamespace.gradeMap//resultMapselectid=findAllByNameparameterType=stringresultMap=studentMapselects.sid,s.sname,g.gid,g.gnamefromgradesg,studentsswhereg.gid=s.sgidandg.gname=#{name}/select/mapperpublicclassGradeStudentDao{/***查询java班级有哪些【学生】*/publicListStudentfindAllByName(Stringname)throwsException{SqlSessions
本文标题:mybatis学习重点笔记2
链接地址:https://www.777doc.com/doc-8761738 .html