MyBatis如何处理表关联

1.左连接查询collection集合标签property表示实体类中的属性ofType表示实体类中属性对应的集合当中泛型中的对象类型2.子查询column将查询到的字段值作为参数传递给子查询select属性子查询的sql中id属性的值3.关联查询中对象的操作association标签property代表实体类中的属性javaType代表实体类中的属性的类型4.mybatis开启驼峰映射规则,在核心配置文件中配置设置settings配置驼峰映射规则后单表查询时,可以使用resultType多表关联时,使用resultMap,如果封装的对象需要开启自动映射(autoMapping=true),则需要添加autoMapping属性,最好标识主键。5.mybatis缓存机制:缓存位于服务器和数据库之间大量相同的请求查询数据库,则数据库要执行多次查询相同的查询sql,造成开发压力高,查询效率低,引入缓存机制,可以提升用户的查询效率mybatis有2级缓存1级缓存,利用同一个sqlSession,执行多次数据库的操作,则缓存有效,sql只执行一次,一级缓存默认是开启状态2级缓存,sqlSessionFactory级别,利用同一个sqlSessionFactory创建不同的sqlSession,可以实现数据的共享(缓存机制),二级缓存默认也是开启状态注意:sqlSession使用完毕必须关闭,否则二级缓存不生效使用二级缓存时,需要在各自的xml配置文件中添加<cache/>标签,否则二级缓存不生效6.如果使用mybatis的缓存机制时,pojo实体类对象必须实现序列化接口,否则对象不可以被缓存序列化接口:将对象转化为字节并按照特定的格式排列反序列化:按照特定排列格式将的字节信息转化为对象7.springboot整合mybatis8.application.yml配置文件9..@Mapperscan注解:ibatis提供的注解,添加在主启动类上,需要指定包路径,会自动扫描该路径下的mapper接口,并交给spring容器管理

resultMap属性使用场景:

表中字段名称与对象当中属性名不一致

关联查询时,实现数据的封装

1.左连接查询

collection集合标签 property表示实体类中的属性  ofType表示实体类中属性对应的集合当中泛型中的对象类型

<select id="findAll" resultMap="deptRM">
    select
    e.emp_id,
    e.emp_name,
    d.dept_id,
    d.dept_name
    from
    emp e left join dept d
    on
    e.dept_id=d.dept_id
</select>
<resultMap id="deptRM" type="Dept">
    <id column="dept_id" property="deptId"/>
    <result column="dept_name" property="deptName"/>
    <collection property="emps" ofType="Emp">
        <id property="empId" column="emp_id"/>
        <result column="emp_name" property="empName"/>
    </collection>
</resultMap>

2.子查询  column 将查询到的字段值作为参数传递给子查询

select 属性  子查询的sql中id属性的值

<select id="selectChildren" resultMap="deptRMs">
    select * from dept
</select>
<resultMap id="deptRMs" type="Dept">
    <id column="dept_id" property="deptId"/>
    <result column="dept_name" property="deptName"/>
    <collection property="emps" ofType="Emp" select="findEmp" column="dept_id"/>
</resultMap>
<select id="findEmp" resultMap="empRM">
    select * from emp where dept_id = #{dept_id}
</select>
<resultMap id="empRM" type="Emp">
    <id column="emp_id" property="empId"/>
    <result column="emp_name" property="empName"/>
</resultMap>
3.关联查询中对象的操作

association标签 property代表实体类中的属性  javaType代表实体类中的属性的类型

<select id="findAll" resultMap="EmpRM">
    select
    e.emp_id,
    e.emp_name,
    d.dept_id,
    d.dept_name
    from emp e left join dept d
    on
    e.dept_id = d.dept_id
</select>

<resultMap id="EmpRM" type="Emp">
    <id column="emp_id" property="empId"/>
    <result column="emp_name" property="empName"/>
    <association property="dept" javaType="Dept">
        <id property="deptId" column="dept_id"/>
        <result property="deptName" column="dept_name"/>
    </association>
</resultMap>

4.mybatis开启驼峰映射规则,在核心配置文件中配置设置settings

<settings>
    <setting name="mapUnderscoreToCamelCase" value="true"/>
</settings>
配置驼峰映射规则后

单表查询时,可以使用resultType

多表关联时,使用resultMap,如果封装的对象需要开启自动映射(autoMapping=true),则需要添加autoMapping属性,最好标识主键。例:

<select id="findAll" resultMap="EmpRM">
    select
    e.emp_id,
    e.emp_name,
    d.dept_id,
    d.dept_name
    from emp e left join dept d
    on
    e.dept_id = d.dept_id
</select>
<resultMap id="EmpRM" type="Emp" autoMapping="true">
    <id column="emp_id" property="empId"/>
    <association property="dept" javaType="Dept" autoMapping="true">
        <id property="deptId" column="dept_id"/>
    </association>
</resultMap>

5.mybatis缓存机制:缓存位于服务器和数据库之间

大量相同的请求查询数据库,则数据库要执行多次查询相同的查询sql,造成开发压力高,查询效率低,引入缓存机制,可以提升用户的查询效率

mybatis有2级缓存

1级缓存,利用同一个sqlSession,执行多次数据库的操作,则缓存有效,sql只执行一次,一级缓存默认是开启状态

2级缓存,sqlSessionFactory级别,利用同一个sqlSessionFactory创建不同的sqlSession,可以实现数据的共享(缓存机制),二级缓存默认也是开启状态

注意:sqlSession使用完毕必须关闭,否则二级缓存不生效

使用二级缓存时,需要在各自的xml配置文件中添加<cache/>标签,否则二级缓存不生效

6.如果使用mybatis的缓存机制时,pojo实体类对象必须实现序列化接口,否则对象不可以被缓存

序列化接口:将对象转化为字节并按照特定的格式排列

反序列化:按照特定排列格式将的字节信息转化为对象

7.springboot整合mybatis

pom.xml包:

<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
        <scope>test</scope>
        <exclusions>
            <exclusion>
                <groupId>org.junit.vintage</groupId>
                <artifactId>junit-vintage-engine</artifactId>
            </exclusion>
        </exclusions>
    </dependency>

    <dependency>
        <groupId>org.projectlombok</groupId>
        <artifactId>lombok</artifactId>
    </dependency>

    <dependency>
        <groupId>org.mybatis.spring.boot</groupId>
        <artifactId>mybatis-spring-boot-starter</artifactId>
        <version>2.2.0</version>
    </dependency>

    <dependency>
        <groupId>mysql</groupId>
        <artifactId>mysql-connector-java</artifactId>
    </dependency>
</dependencies>

8.application.yml配置文件:

#指定端口号
server:
  port: 8090

spring:
  datasource:
    #配置数据库连接驱动
    driver-class-name: com.mysql.cj.jdbc.Driver
    #访问数据库路径
    url: jdbc:mysql://127.0.0.1:3306/jt?serverTimezone=GMT%2B8&amp;useUnicode=true&amp;characterEncoding=utf8&amp;autoReconnect=true&amp;allowMultiQueries=true
    #数据库账号
    username: root
    #数据库密码
    password: root
#springboot整合mybatis
mybatis:
  #配置别名包
  type-aliases-package: com.jt.pojo
  #指定配置文件,*通配符,自动选择xml配置文件
  mapper-locations: classpath:mappers/*.xml
  configuration:
    #开启驼峰映射
    map-underscore-to-camel-case: true

9.@Mapperscan注解:ibatis提供的注解,添加在主启动类上,需要指定包路径,会自动扫描该路径下的mapper接口,并交给spring容器管理
 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值