SSM框架学习总结之路(四)

一、MyBatis实现多表查询

  1. Mybatis实现多表查询方式
    1.1 业务装配 : 对两个表编写单表查询语句,在业务(Service)把查询的两个结果进行关联.
    1.2 使用 Auto Mapping 特性,在实现两表联合查询时通过别名完成映射.
    1.3 使用Mybatis 的 <resultMap>标签进行实现.

  2. 多表查询时,类中包含另一个类的对象的分类
    2.1 单个对象
    2.2 集合对象

二、resultMap 标签

  1. <resultMap>标签写在mapper.xml中,由程序员控制SQL查询结果与实体类的映射关系.
    1.1 默认MyBatis 使用 Auto Mapping 特性.

  2. 使用<resultMap>标签是,<select>标签不写 resultType 属性,而是使用resultMap属性引用<resultMap>标签.

  3. 使用resultMap 实现单表映射关系
    3.1 数据库设计 create table teacher( id int(10) primary key auto_increment, name varchar(20) );
    3.2 实体类设计public class Teacher{ private int id1; private String name1}
    3.3 mapper.xml代码

<resultMap type="teacher" id="mymap">
	<!-- 主键使用 id 标签配置映射关系 --> 
	<id column="id" property="id1" />
	<!-- 其他列使用 result 标签配置映射关系 --> 
	<result column="name" property="name1"/> </resultMap>
<select id="selAll" resultMap="mymap">
	 select * from teacher 
 </select>
  1. 使用resultMap 实现关联单个对象(N+1方式)
    4.1 N+1查询方式,先查询出某个表的全部信息,根据这个表的信息查询另一个表的信息.
    4.2 与业务装配的区别 : (在service里面的代码,由mybatis完成装配)
    4.3 实现步骤
    4.3.1 在Sutdent 实现类中包含了一个Teacher 对象
public class Student { 
 private int id; 
 private String name;
 private int age; 
 private int tid; 
 private Teacher teacher;
 }
  1. 3.2 在 TeacherMapper 中提供一个查询
<select id="selById" resultType="teacher" parameterType="int"> 
	select * from teacher where id=#{0}
 </select>

  1. 3.3 在 StudentMapper 中.
    1. 装配一个对象时使用.
    2. property: 对象在类中的属性名.
    3. select:通过哪个查询查询出这个对象的信息.
    4. column: 把当前表的哪个列的值做为参数传递给另一个查询.
    5. 大前提使用 N+1 方式.时如果列名和属性名相同可以不配置,使用 Auto mapping 特性.但是 mybatis 默认只会给列专配一次.
<resultMap type="student" id="stuMap"> 
	<id property="id" column="id"/> 
	<result property="name" column="name"/> 
	<result property="age" column="age"/> 
	<result property="tid" column="tid"/>
<!-- 如果关联一个对象 --> 
	<association property="teacher" select="com.mapper.TeacherMapper.selById" column="tid">
</association>
 </resultMap> 
 
<select id="selAll" resultMap="stuMap"> 
	 select * from student
</select>

三、使用查询关联集合对象(N+1)

  1. 在Teacher 中添加 List<Student>
public class Teacher {
 private int id; 
 private String name; 
 private List<Student> list;
 }
  1. StudentMapper.xml 中添加通过tid 查询
<select id="selByTid" parameterType="int" resultType="student">
	 select * from student where tid=#{0} 
</select>
  1. 在TeacherMapper.xml 中添加查询全部
    1. 1 <collection/> 当属性是集合类型时使用标签
<resultMap type="teacher" id="mymap"> 
	 <id column="id" property="id"/>
	 <result column="name" property="name"/> 
 	<collection property="list" select="com.mapper.StudentMapper.selByTid" column="id"></collection>
</resultMap> 
<select id="selAll" resultMap="mymap">
   select * from teacher
</select>

四、使用实现加载集合数据(联合查询方式)

  1. 在teacherMapper.xml 中添加
<resultMap type="teacher" id="mymap1">
 	<id column="tid" property="id"/> 
	 <result column="tname" property="name"/>
<collection property="list" ofType="student" > 
<id column="sid" property="id"/> 
<result column="sname" property="name"/> 
<result column="age" property="age"/> 
<result column="tid" property="tid"/> 
</collection> 
</resultMap> 
<select id="selAll1" resultMap="mymap1"> 
	select t.id tid,t.name tname,s.id sid,s.name sname,age,tid from teachert left join students on t.id=s.tid; 
</select>

五、使用 Auto Mapping 结合别名实现多表查询.

  1. 只能使用多表联合查询方式
  2. 要求 : 查询出的列名 不能和属性名 相同
  3. 实现方法 (在SQL是关键字符, 两侧添加反单引号)
<select id="selAll" resultType="student">
	select t.id `teacher.id`,t.name `teacher.name`,s.id id,s.name name,age,tid from student s LEFT JOIN teacher t on t.id=s.tid 
</select>

六、MyBatis 使用注解

  1. MyBatis 的注解简化mapper.xml 文件.

  2. 如果涉及动态SQL 依然使用mapper.xml

  3. mapper.xml和注解可以共存

  4. 使用注解时 mybat.xml 中<mappers>使用–> <package/> --> <mapper class="" />

  5. 实现查询

  6. 实现新增

  7. 实现修改

  8. 实现删除

  9. 使用注解实现<resultMap>功能
    9.1 在StudentMapper接口中添加查询 @Select("select * from student where tid=#{0}") List<Student> selByTid(int tid);
    9.2 在 TeacherMapper 接口添加

    1. @Results() 相当于 <resultMap>
    2. @Result() 相当于<id/> 或者 </result>
    3. @Result(id=true) 相当与<id/>
    4. @Many() 相当于<collection/>
    5. @One() 相当于<association/>
@Results(value={
	@Result(id=true,property="id",column="id"), 
	@Result(property="name",column="name"),
	@Result(property="list",column="id", many=@Many(select="com.mapper.StudentMapper.selByTid"))
 }) 
@Select("select * from teacher") 
List<Teacher> selTeacher();

七、运行原理

  • 1. 运行过程中涉及到的类
    
    • 1.1 ResourcesMyBatis 中 IO 流的工具类
      • 1.1 加载配置文件
    • 1.2 SqlSessionFactoryBuilder() 构建器
      • 1.2.1 作用:创建 SqlSessionFactory 接口的实现类
    • 1.3XMLConfigBuilder MyBatis 全局配置文件内容构建器类
      • 1.3.1 作用负责读取流内容并转换为 JAVA 代码.
    • 1.4Configuration 封装了全局配置文件所有配置信息.
      • 1.4.1 全局配置文件内容存放在 Configuration 中
    • 1.5DefaultSqlSessionFactory 是SqlSessionFactory接口的实现类
    • 1.6Transaction 事务类
      • 16.1 每一个 SqlSession 会带有一个 Transaction 对象.
    • 1.7TransactionFactory 事务工厂
      • 1.7.1 负责生产 Transaction
    • 1.8Executor MyBatis 执行器
      • 1.8.1 作用:负责执行 SQL 命令
      • 1.8.2 相当于 JDBC 中 statement 对象(或 PreparedStatement或 CallableStatement)
      • 1.8.3 默认的执行器 SimpleExcutor
      • 1.8.4 批量操作 BatchExcutor
      • 1.8.5 通过 openSession(参数控制)
    • 1.9DefaultSqlSession 是 SqlSession 接口的实现类
    • 1.10ExceptionFactoryMyBatis 中异常工厂

3.文字解释

  • 在 MyBatis 运行开始时需要先通过 Resources 加载全局配置文件.下面
    需要实例化 SqlSessionFactoryBuilder 构建器.帮助 SqlSessionFactory 接
    口实现类 DefaultSqlSessionFactory.
    在实例化 DefaultSqlSessionFactory 之前需要先创建 XmlConfigBuilder
    解析全局配置文件流,并把解析结果存放在 Configuration 中.之后把
    Configuratin 传递给 DefaultSqlSessionFactory.到此 SqlSessionFactory 工
    厂创建成功.
    由 SqlSessionFactory 工厂创建 SqlSession.
    每次创建 SqlSession 时,都需要由 TransactionFactory 创建 Transaction
    对象,同时还需要创建 SqlSession 的执行器 Excutor,最后实例化
    DefaultSqlSession,传递给 SqlSession 接口.
    根据项目需求使用 SqlSession 接口中的 API 完成具体的事务操作.
    如果事务执行失败,需要进行 rollback 回滚事务.
    如果事务执行成功提交给数据库.关闭 SqlSession
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值