当前位置: 首页 > news >正文

电商沙盘seo裤子关键词长沙建站优化

电商沙盘seo裤子关键词,长沙建站优化,电子商务网站建设 ppt,摄影网站制作流程前言 多表关联查询是软件开发中最常见的应用场景,多表查询需要将数据实体之间的一对多、多对多、一对一的关系的转换为复杂的数据对象。mybaits提供的association和collection元素,通过映射文件构造复杂实体对象,在构造实体过程中&#xff0…

前言

多表关联查询是软件开发中最常见的应用场景,多表查询需要将数据实体之间的一对多、多对多、一对一的关系的转换为复杂的数据对象。mybaits提供的association和collection元素,通过映射文件构造复杂实体对象,在构造实体过程中,mybaits提供的了嵌套查询和嵌套结果查询两种查询方式,前者通过执行多次SQL语句,并支持延迟加载;后者执行一次SQL语句,通过SQL语句的执行结果构造实体。

实验目的

掌握嵌套查询的使用
掌握嵌套结果查询的使用
掌握单个实体(association)和实体集合(collection)的使用

实验内容

以教师和课程呈现的一对一关系为例,验证association元素的使用
以教师和课程呈现的一对一关系为例,验证嵌套查询使用
以教师和课程呈现的一对一关系为例,验证嵌套结果查询使用
以学生和课程呈现的一对多关系为例,验证collection元素的使用
以学生和课程呈现的一对多关系为例,验证嵌套查询使用
以学生和课程呈现的一对多关系为例,验证嵌套结果查询使用

实验步骤

1. 实验准备

  • 创建表和数据维护,表结构如下
    学生表(tb_student)
字段名称字段代码数据类型备注
学号snointeger主键自增
学生姓名snamevarchar(50)
年龄sageinteger
教师表(tb_tearcher)
字段名称字段代码数据类型备注
工号tnointeger主键自增
教师姓名tnamevarchar(50)
年龄tageinteger
课程表(tb_course)
字段名称字段代码数据类型备注
课程号cnointeger主键自增
课程名cnamevarchar(50)
学分ccreditinteger

学生选课表(tb_sc)

字段名称字段代码数据类型备注
课程号cnointeger主键自增
课程名cnamevarchar(50)
学分ccreditinteger
每个表增加不少于5条记录
  • 使用maven创建控制台工程,搭建myBatis的运行环境
  • 编写实体类文件
    教师实体类代码如下
@Data  
public class TeacherEntity {  private Integer tno;  private String tname;  private Integer tage;  
}  

课程实体文件如下所示

@Data
public class CourseEntity  {private int cno;private String cname;private int ccredit;private TeacherEntity teacher;
}

学生实体类代码如下

@Data
public class StudentEntity  {private Integer sno;private String sname;private Integer sage;private List<CourseEntity> courses;
}
  • 编写接口文件
    教师接口文件
public interface TeacherDao {TeacherEntity getTeacherByCourse(Integer cno);
}

课程接口文件

public interface CourseDao {List<CourseEntity> getAllCourse();List<CourseEntity> getAllCourseByResult();List<CourseEntity> getCourseByStudentId(Integer sno);
}

学生接口文件

public interface StudentDao {List<StudentEntity> getAllStudent();List<StudentEntity> getAllStudent2();
}

查询课程信息,并显示该课程的任课教师(一门课只安排一个教师)

展示一对一信息,需要在映射文件中使用association元素

  • 使用嵌套查询
    课程映射文件(CourseMapper.xml)代码如下
<mapper namespace="com.bjwl.dao8.CourseDao" ><resultMap id="CoursePojo" type="com.bjwl.pojo8.CourseEntity"><id property="cno" column="cno"></id><result property="cname" column="cname"></result><result property="ccredit" column="ccredit"></result><association property="teacher" column="cno" fetchType="eager"javaType="com.bjwl.pojo8.TeacherEntity" select="com.bjwl.dao8.TeacherDao.getTeacherByCourse"></association></resultMap><select id="getAllCourse" resultMap="CoursePojo" >select * from tb_course</select>
</mapper>

代码中首先定义了一个resultMap ,建立复杂对象的映射关系,association代表在课程实体(CourseEntity)中有一个教师(teacher)的属性,对应数据类型是教师的实体,二者之间通过列cno关联,数据的获取是通过com.bjwl.dao8.TeacherDao.getTeacherByCourse实现的。其次定义获取全部课程数据使用的方法(getAllCourse)。

定义教师的映射文件(TeacherMapper.xml),在映射文件中定义getTeacherByCourse对应的SQL,代码如下

<mapper namespace="com.bjwl.dao8.TeacherDao" ><select id="getTeacherByCourse" resultType="com.bjwl.pojo8.TeacherEntity">select * from tb_teacher where cno = #{cno}</select>
</mapper>

测试代码如下

    public void test11Nest() throws IOException {SqlSession sqlSession = BatisUtils.getSqlSessionFactory().openSession();CourseDao dao = sqlSession.getMapper(CourseDao.class);List<CourseEntity> courses = dao.getAllCourse();for (CourseEntity course : courses ){System.out.println(course.toString());}

运行结果如下
在这里插入图片描述
上图中总共执行4次SQL语句。

  • 使用嵌套结果查询
    嵌套结果查询只执行一次SQL语句,有查询结果中组织复杂实体对象,映射文件代码如下;
<mapper namespace="com.bjwl.dao8.CourseDao" >  <resultMap id="CoursePojo2" type="com.bjwl.pojo8.CourseEntity">  <id property="cno" column="cno"></id>  <result property="cname" column="cname"></result>  <result property="ccredit" column="ccredit"></result>  <association property="teacher" javaType="com.bjwl.pojo8.TeacherEntity">  <id property="tno" column="tno"></id>  <result property="tname" column="tname"></result>  <result property="tage" column="tage"></result>  </association>  </resultMap>  <select id="getAllCourseByResult"  resultMap="CoursePojo2">  select * from tb_course a left join tb_teacher b on a.cno =b.cno  </select>  
</mapper>  

代码中association ,定义返回结果集中列和实体属性的对应关系。测试代码如下图所示

public void test11Result() throws IOException {  SqlSession sqlSession = BatisUtils.getSqlSessionFactory().openSession();  CourseDao dao = sqlSession.getMapper(CourseDao.class);  List<CourseEntity> courses = dao.getAllCourseByResult();  for (CourseEntity course : courses ){  System.out.println(course.toString());  }  
} 

运行结果如下图所示
在这里插入图片描述

查询学生信息,并显示该学生所修课程(一个学生有多门课程)

展示一对多信息,需要在映射文件中使用实体集合(collection)元素,

  • 使用嵌套查询
    学生映射文件(StudentMapping)代码如下
<mapper namespace="com.bjwl.dao8.StudentDao" ><resultMap id="studentInfo" type="com.bjwl.pojo8.StudentEntity"><id property="sno" column="sno"></id><result property="sname" column="sname"></result><result property="sage" column="sage"></result><collection property="courses" column="sno" ofType="com.bjwl.pojo8.CourseEntity"select="com.bjwl.dao8.CourseDao.getCourseByStudentId"></collection></resultMap><select id="getAllStudent" resultMap="studentInfo">select * from tb_student</select>
</mapper>

代码中首先定义了一个resultMap ,建立复杂对象的映射关系,collection 代表在学生实体中(StudentEntity)中有一个课程(courses)的属性,二者之间通过列sno关联,数据的获取是通过com.bjwl.dao8.CourseDao.getCourseByStudentId实现的。其次定义获取全部课程数据使用的方法(getCourseByStudentId)。
定义课程的映射文件(TeacherMapper.xml),在映射文件中定义getCourseByStudentId对应的SQL,代码如下

<mapper namespace="com.bjwl.dao8.CourseDao" ><select id="getCourseByStudentId" resultType="com.bjwl.pojo8.CourseEntity">select * from tb_course a,tb_sc bwhere a.cno = b.cno andb.sno = #{sno}</select>
</mapper>

测试代码如下:

    public void test1nNest() throws IOException {SqlSession sqlSession = BatisUtils.getSqlSessionFactory().openSession(ExecutorType.BATCH);StudentDao dao = sqlSession.getMapper(StudentDao.class);List<StudentEntity> students = dao.getAllStudent();for (StudentEntity student : students ){System.out.println(student.toString());}}

执行结果如下图所示
在这里插入图片描述

  • 使用嵌套结果
    对应的映射文件为
<mapper namespace="com.bjwl.dao8.StudentDao" ><resultMap id="studentInfo2" type="com.bjwl.pojo8.StudentEntity"><id property="sno" column="sno"></id><result property="sname" column="sname"></result><result property="sage" column="sage"></result><collection property="courses" column="sno" ofType="com.bjwl.pojo8.CourseEntity"><id property="cno" column="cno"></id><result property="cname" column="cname"></result><result property="ccredit" column="ccredit"></result></collection></resultMap><select id="getAllStudent2" resultMap="studentInfo2">SELECT aa.*,bb.* FROM(SELECT a.`sno`,a.`sname`,a.`sage`,b.`cno`FROM tb_student a LEFT JOIN  tb_sc b ON a.sno = b.`sno`) aa  LEFT JOIN tb_course bbON aa.cno = bb.`cno`;</select>

测试代码如下:

    public void test1nResult() throws IOException {SqlSession sqlSession = BatisUtils.getSqlSessionFactory().openSession(ExecutorType.BATCH);StudentDao dao = sqlSession.getMapper(StudentDao.class);List<StudentEntity> students = dao.getAllStudent2();for (StudentEntity student : students ){System.out.println(student.toString());}}

运行结果如下图所示
在这里插入图片描述

多对多的关系,可以当作两个一对多的关系完成

http://www.yidumall.com/news/20804.html

相关文章:

  • 怎样做境外网站上赚钱百度seo快速排名优化软件
  • 硅谷电视剧他们做的是网站还是软件外包公司和劳务派遣
  • 现在帮人做网站赚钱吗semi final
  • 诸城网站建设电脑优化
  • 上传网站怎么安装公司官网制作多少钱
  • 大连网站建设找简维科技全网推广平台有哪些
  • 搜网站内容最成功的网络营销案例
  • 网站建设搜索百度入口网址
  • 签约做网站模板百度一下你就知道百度官网
  • 做网站 科目恩城seo的网站
  • 建筑师网站怎样优化关键词到首页
  • 网站网页建设实训心得体会看广告赚钱
  • 域名跟网站的区别吗无锡百度信息流
  • 最权威的做网站设计哪家好互联网广告
  • 页面排版东莞seo优化排名推广
  • 优秀的设计网站域名收录查询工具
  • 做网站的前景国外seo
  • 专业网站设计制作服务网盘搜索引擎入口
  • 标准件做啥网站北京百度推广seo
  • 做宠物网站导航应该写什么字销售管理软件
  • 网站后台怎么给图片做水印推广手段
  • 网站建设名片seo关键词排名优化价格
  • java做的网站如何部署免费域名申请网站大全
  • 政府网站建设 问题佛山企业用seo策略
  • 杭州企业名录大全广东网站seo策划
  • 做网站找 汇搜网络百度开放平台登录
  • 百度快照提交入口seo快速排名百度首页
  • 国内免费自建网站搜索大全引擎地址
  • 上海网络营销网站建设关键词竞价广告
  • wordpress跨站脚本攻击漏洞深圳seo优化排名优化