大数据WEB阶段Mybatis(二)

Mybatis(二)

零、目录

  • Mybatis接口形式
  • Mybatis整合Spring
  • Mybatis的缓存机制
  • 手动封装结果集
  • 一对一表操作
  • 一对多表操作
  • 多对多表操作
  • SpringMVC 、 Spring 、 Mybatis三大框架整合

一、 Mybatis接口形式

  1. 为表User创建映射接口
    1. 此时要注意
      1. 接口的全名称要与映射文件中的namespace一致。
      2. 接口中的方法名与映射文件中sql的id一致
  2. 示例:

    映射文件
    <mapper namespace="com.tj.mapper.UserMapper">
        <select id="findAll" resultType="com.tj.pojo.User" > 
            select * from user ; 
        </select>
    
    </mapper>
    
    映射接口
    package com.tj.mapper;
    
    import java.util.List;
    
    import com.tj.pojo.User;
    public interface UserMapper {
        /**
         * 查询所有用户
         * */
        public List<User> findAll();
    }
    

二、mybatis整合Spring

  1. 导入在Mybatis所有jar包和Spring的所有jar包的基础上 , 加上mybatis-spring-1.2.0.jar
    1. 这里写图片描述
  2. 配置文件
    1. 在mybatis配置文件中删除所有的配置 , 仅剩根节点即可
    2. Spring在原有的基础上添加两个bean
      1. 会话工厂
        1. 数据源
        2. 核心配置文件
        3. 映射文件配置
      2. 映射接口扫描器
      3. <?xml version="1.0" encoding="UTF-8"?>
        <beans
        xmlns="http://www.springframework.org/schema/beans"
        xmlns:tx="http://www.springframework.org/schema/tx"
        xmlns:aop="http://www.springframework.org/schema/aop"
        xmlns:context="http://www.springframework.org/schema/context"
        xmlns:util="http://www.springframework.org/schema/util"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xmlns:p="http://www.springframework.org/schema/p"
        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd
        http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.2.xsd
        http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.2.xsd
        http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd
        ">
        <!-- 配置mybatis -->
        <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <!-- 配置数据源 -->
        <property name="dataSource" ref = "datasSource"></property>
        <!-- 注入mybatis核心配置文件 -->
        <property name="configLocation" value="classpath:/sqlMapConfig.xml"></property>
        <!-- 注入映射文件 -->
        <property name="mapperLocations" value="classpath:/com/tj/pojo/*.xml"></property>
        </bean>
        <!-- 配置映射接口扫描器 -->
        <bean class = "org.mybatis.spring.mapper.MapperScannerConfigurer">
        <property name="basePackage" value="com.tj.mapper"></property>
        </bean>
        <!-- 读取外部配置文件 -->
        <context:property-placeholder location="classpath:/jdbc.properties" />
        <!-- 配置数据源 -->
        <bean id = "datasSource" class = "com.mchange.v2.c3p0.ComboPooledDataSource" >
        <property name="user" value="${user}"></property>
        <property name="password" value="${password}"></property>
        <property name="jdbcUrl" value="${jdbcUrl}"></property>
        <property name="driverClass" value="${driverClass}"></property>
        </bean>
        <!-- 开启事务注解模式 -->
        <tx:annotation-driven transaction-manager="transactionManager"/>
        <!-- 开启注解模式 -->
        <context:annotation-config />
        <!-- 开启包扫描 -->
        <context:component-scan base-package="com.tj"></context:component-scan><!-- 扫描com.tj子包下的所有类 -->
        <!-- 配置aop -->
        <aop:aspectj-autoproxy/>
        </beans>

三、mybatis缓存机制

  1. mybatis中缓存机制分为一级缓存和二级缓存
  2. 一级缓存 , 默认共享同一个session中的数据
  3. 二级缓存共享同一个sessionFactory中的数据
  4. 一级缓存默认是开启的 , 但是二级缓存默认是关闭的
  5. 开启二级缓存

    1. 在mybatis核心配置文件中配置二级缓存的总开关

      <settings>
          <setting name="cacheEnabled" value="true"/>
      </settings>
      
    2. 在需要的mapper中添加二级缓存

      <cache/>
      
    3. mapper对应的实体类需要实现序列化接口

      public class User implements Serializable{
      
      }
      
  6. 注意:
    1. 无论时一级缓存还是二级缓存 , 当数据做出修改后 , 缓存的数据就会被清除, 下次查询回去数据库中取值
    2. 二级缓存的数据只有当session会话被关闭时才会把数据缓存起来 。

四、手动封装结果集

  1. 如果实体类中的属性名和数据库中的字段名不一致时 , 结果集自动封装就会失败 , 需要手动封装
  2. 注意:结果集自动化封装实际上是以setxxx方法的xxx为准的
  3. 示例:

    <select id="findAll" resultMap="userRM" > 
        select * from user ; 
    </select>
    
    <resultMap type="com.tj.pojo.User" id="userRM" autoMapping="true"><!-- 如果字段名与属性名一致则自动注入 --> 
        <!-- 主键必须写 -->
        <id column="id" property="id"/>
    
        <!-- 自定义类型需要手动封装结果集 -->
        <association property="userinfo" javaType="com.tj.pojo.UserInfo">
            <!-- 在上面配置了字段名与属性名一致时自动注入 , 但是在自定义类型中不生效 -->
            <id column="uid" property="uid"/>
            <result column="phone" property="phone"/>
            <result column="parentid" property="parentid"/>
        </association>
    </resultMap>
    

五、 一对一表操作

  1. 这里写图片描述
  2. 一对一查询中 , 主表可以将非主键自动封装(但是要求表字段与实体类的属性相同) , 主键必须手动注入,且从表不能手动注入

六、 一对多表操作

  1. 实体类中属性为List类型
  2. 这里写图片描述
  3. 2.

七、 多对多表操作

  1. 查询老师教的所有学生
  2. 查询一个学生的所有老师
  3. 如果添加一个新的Teacher实体 针对这个实体进行数据查询的时候需要做的事儿:

    1. 创建TeacherMapper接口
    2. 创建TeacherMapper.xml映射文件 在映射文件里 把nameSpace改成 cn.tedu.mapper.TeacherMapper
    3. 在TeacherMapper.xml映射文件中写sql语句 在接口类中创建和sql语句id一致的方法名
    4. 在核心配置文件中 添加teacherMapper.xml的引入

      <resultMap type="cn.tedu.pojo.Student" id="studentRM">
          <id column="sid" property="id"/>
          <result column="sname" property="name"/>
          <collection property="teachers" ofType="cn.tedu.pojo.Teacher">
              <id column="tid" property="id"/>
          <result column="tname" property="name"/>
          </collection>
       </resultMap>
        <select id="findAllStudents" resultMap="studentRM">
        SELECT s.id sid,s.name sname,t.`id` tid,t.`name` tname FROM
      (SELECT * FROM
      student s
          LEFT JOIN
      t_s ts
          ON
          s.`id`=ts.`sid`) s
          LEFT JOIN
      teacher t
          ON
          s.tid=t.`id`
        </select>
      

八、 三大框架整合

  1. 这里写图片描述
  2. 一共四个配置文件: web.xml 、applicationContext.xml 、applicationContext-mvc.xml 、 sqlMapConfig.xml
  3. 配置文件改动

    1. web.xml中不仅引入SpringMVC的核心配置文件 , 还要引入Spring的核心配置文件
    2. mybatis核心配置文件留下根节点 , 其余删除 , 在需要时可以在其中配置缓存 、 别名等
    3. Spring 核心配置文件中添加两个bean

      <!-- 配置sql会话工厂 -->
          <bean class="org.mybatis.spring.SqlSessionFactoryBean">
          <!-- 配置数据源 -->
              <property name="dataSource" ref="dataSource"></property>
              <!-- 引入Mybatis的核心配置文件 -->
              <property name="configLocation" 
              value="classpath:/sqlMapConfig.xml"></property>
      <!-- 引入所有的Mapper配置文件 -->
              <property name="mapperLocations" 
              value="classpath:/cn/tedu/pojo/*.xml"></property>
          </bean>
      
          <!-- 配置mapper接口扫描器 -->
          <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
              <property name="basePackage" value="cn.tedu.mapper"></property>
          </bean>
      
  4. 注意:在web工程中如果访问src路径下面的内容 , 需要在文件名前加上classpath:/如:classpath:/jdbc.properties
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值