mybatis学习笔记

来源:互联网 发布:万得 wind 金融数据 编辑:程序博客网 时间:2024/06/15 16:23

一. 架构图

这里写图片描述

  • SqlMapConfig.xml: mybatis的核心配置文件, 创建mybtias的运行环境.

  • SqlSessionFactory对象: 用来创建sqlSession会话对象, 故只需要创建一次此工厂即可(单例).

  • SqlSession对象: 封装了对数据库的CRUD操作.

  • Mapped Statement: 是mybatis底层封装的一个对象,包装了mybatis配置信息以及sql映射信息. Mapper.xml中,一个sql对应一个MappedStatement对象, sql的id即是MappedStatement的id.

二. 运行流程

  1. 通过加载SqlMapConfig.xml, 构造出SqlSessionFactory会话工厂对象.
  2. 通过会话工厂获得SqlSession会话对象.
  3. 通过SqlSession对象操作数据库.

三. 配置

  1. SqlMapConfig.xml核心配置文件
    • properties (引入属性文件,如:db.properties)
    • settings (全局配置参数)
    • typeAliases (别名)
    • typeHandlers (类型处理器)
    • objectFactory (对象工厂)
    • plugins (插件,如:分页插件)
    • environments (环境集合属性对象)
      • environment (环境子属性对象)
        • transactionManager (事务管理器)
        • dataSource (数据源)
    • mappers (映射器, 加载mapper.xml文件)

示例:

<?xml version="1.0" encoding="UTF-8" ?><!DOCTYPE configurationPUBLIC "-//mybatis.org//DTD Config 3.0//EN""http://mybatis.org/dtd/mybatis-3-config.dtd"><configuration>    <properties resource="db.properties"/>    <!-- 和spring整合后 environments配置将废除-->    <environments default="development">        <environment id="development">            <!-- 使用jdbc事务管理-->            <transactionManager type="JDBC" />            <!-- 数据库连接池-->            <dataSource type="POOLED">                <property name="driver" value="${jdbc.driver}" />                <property name="url" value="${jdbc.url}" />                <property name="username" value="${jdbc.username}" />                <property name="password" value="${jdbc.password}" />            </dataSource>        </environment>    </environments></configuration>

四. 开发方式

  1. 原生DAO开发
    需要: dao接口以及实现类.
    思路: 在dao的实现类中创建会话工厂, 获得会话对象, 然后对数据库进行操作.
    缺点: 代码重复性太高, 不停地创建和关闭资源.
  2. Mapper动态代理方式
    需要: dao接口以及对应mapper.xml文件
    要求:
    ① Mapper.xml文件中的namespace与mapper接口的类路径相同.
    ② Mapper接口方法名和Mapper.xml中定义的每个statement的id相同.
    ③ Mapper接口方法的输入参数类型和mapper.xml中定义的每个statement的parameterType的类型相同.
    ④ Mapper接口方法的输出参数类型和mapper.xml中定义的每个statement的resultType的类型相同.

五. 动态sql语句

  1. foreacher
    • 前提: 传入参数是数组或者集合
    • 示例:
<if test="ids!=null and ids.size>0">    <foreach collection="ids" open=" and id in(" close=")" item="id" separator="," >        #{id}    </foreach></if>

六. mybatis的关联查询

需求: 查询订单orders, 以及订单对应的客户信息.

方法1:

思路: 定义一个OrdersVo, 继承Orders, 且多了一个username字段.
代码如下:

<!-- 查询所有订单信息 -->    <select id="findOrdersList" resultType="onlien.bendou.mybatis.po.OrdersVo">    SELECT    orders.*,    user.username,    user.address    FROM    orders, user    WHERE orders.user_id = user.id     </select>

方法2:

思路: Orders实体类中新增一个user对象.
则Mapper.xml中查询方式如下:

<!-- 查询订单关联用户信息使用resultmap --><resultMap type="online.bendou.po.Orders"   id="orderUserResultMap">        <id column="id" property="id"/>        <result column="user_id" property="userId"/>        <result column="number" property="number"/>        <result column="createtime" property="createtime"/>        <result column="note" property="note"/>        <!-- 一对一关联映射 -->        <!--         property:Orders对象的user属性        javaType:user属性对应 的类型         -->Ty        <association property="user" javaType="online.bendou.po.User">            <!-- column:user表的主键对应的列  property:user对象中id属性-->            <id column="user_id" property="id"/>            <result column="username" property="username"/>        </association></resultMap><select id="findOrdersWithUserResultMap" resultMap="orderUserResultMap">        SELECT            o.id,            o.user_id,            o.number,            o.createtime,            o.note,            u.username        FROM            orders o        JOIN `user` u ON u.id = o.user_id</select>

注意:
如果封装的是多个User, 及List< User >, 则需要将< association> 标签换为< collection >

七. spring整合mybatis

  1. 整合思路:
    • SqlSessionFactory对象应放到spring容器中作为单例存在.
    • 传统Dao开发方式, 应从spring容器中获得sqlSession对象.
    • Mapper代理方式中, 应从spring容器中获得mapper的代理对象.
    • 数据源以及事务都交给spring容器完成.

示例:
SqlMapConfig.xml :

<?xml version="1.0" encoding="UTF-8" ?><!DOCTYPE configurationPUBLIC "-//mybatis.org//DTD Config 3.0//EN""http://mybatis.org/dtd/mybatis-3-config.dtd"><configuration>    <typeAliases>        <package name="online.bendou.mybatis.pojo"/>    </typeAliases></configuration>

applicationContext-mybatis :

<?xml version="1.0" encoding="UTF-8"?><beans xmlns="http://www.springframework.org/schema/beans"    xmlns:context="http://www.springframework.org/schema/context" xmlns:p="http://www.springframework.org/schema/p"    xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd    http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd    http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd    http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.0.xsd">    <!-- 加载配置文件 -->    <context:property-placeholder location="classpath:db.properties" />    <!-- 数据库连接池 -->    <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"        destroy-method="close">        <property name="driverClassName" value="${jdbc.driver}" />        <property name="url" value="${jdbc.url}" />        <property name="username" value="${jdbc.username}" />        <property name="password" value="${jdbc.password}" />        <property name="maxActive" value="10" />        <property name="maxIdle" value="5" />    </bean>    <!-- mapper配置 -->    <!-- 让spring管理sqlsessionfactory 使用mybatis和spring整合包中的 -->    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">        <!-- 数据库连接池 -->        <property name="dataSource" ref="dataSource" />        <!-- 加载mybatis的全局配置文件 -->        <property name="configLocation" value="classpath:mybatis/SqlMapConfig.xml" />    </bean>    <!-- 使用扫描包的形式来创建mapper代理对象 -->    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">        <property name="basePackage" value="online.bendou.mybatis.mapper"></property>    </bean></beans>
1 0
原创粉丝点击