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

长沙做营销型网站公司网站引流推广怎么做

长沙做营销型网站公司,网站引流推广怎么做,登陆wordpress,互联网 网站定制前言 多表关联查询是软件开发中最常见的应用场景,多表查询需要将数据实体之间的一对多、多对多、一对一的关系的转换为复杂的数据对象。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.khdw.cn/news/27931.html

相关文章:

  • 嵌入式网站开发培训网络推广有哪几种方法
  • 企业为什么做平台网站提交网址给百度
  • 商场网站开发东莞关键词seo优化
  • 深圳罗湖做网站的公司大连seo外包平台
  • 美妆网站开发论文合肥网络公司seo
  • wordpress 视频预览杭州网站关键词排名优化
  • 怎样做淘宝推广网站seo实战密码电子版
  • 淘客请人做网站今日新闻
  • 一个公司可以做两个网站吗关键词排名优化怎么做
  • 永诚网络谷歌seo搜索优化
  • 外网代理ip学好seo
  • 宁德北京网站建设网络营销的现状和发展趋势
  • 罗湖网站建设深圳信科培训方案怎么做
  • 怎么在网站上做旅游推广sem和seo是什么职业
  • wordpress博客平台昆明seo博客
  • 哪个网站专题做的好平台推广
  • 水头网站建设网络推广的具体方式
  • 彩票自己开盘做网站什么是seo文章
  • 免费域名领取如何做关键词优化
  • 定制手机微网站最快新闻资讯在哪看
  • 如何做跨境电商免费网站seo排名优化
  • 在上海做网站手机seo排名
  • 网站开发公司徐州ping站长工具
  • 石家庄 做网站帮收款的接单平台
  • 网站策划书范文模板搜索引擎营销案例分析题
  • 哈密网站建设公司哪家专业seo经验
  • 做移民类网站个人备案网站建设百度推广
  • 做个电商网站需要怎么做软文网站大全
  • 中国武汉建设网优化大师免费版
  • 域名查ip地址查询windows10优化软件