零基础学JavaWeb开发(十七)之 mybatis(2)

29 篇文章 2 订阅

5、MyBatis - 映射文件标签

5.1、映射文件的顶级元素

select:映射查询语句

insert:映射插入语句

update:映射更新语句

delete:映射删除语句

sql:可以重用的 sql 代码块

resultMap:最复杂,最有力量的元素,用来描述如何从数据库结果集中加载你的对象

cache:配置给定命名空间的缓存

cache-ref:从其他命名空间引用缓存配置

5.2、select 标签的属性信息

<select
  <!-- 
    1. id(必须配置)
    id是命名空间中的唯一标识符,可被用来代表这条语句
    一个命名空间(namespace)对应一个dao接口
    这个id也应该对应dao里面的某个方法(sql相当于方法的实现),因此id应该与方法名一致
   -->
  id="selectUser"

  <!-- 
    2. parapeterType(可选配置,默认由mybatis自动选择处理)
    将要传入语句的参数的完全限定名或别名,如果不配置,mybatis会通过ParamterHandler根据参数类型默认选择合适的typeHandler进行处理
    paramterType 主要指定参数类型,可以是int, short, long, string等类型,也可以是复杂类型(如对象)
   -->
  parapeterType="int"

  <!-- 
    3. resultType(resultType 与 resultMap 二选一配置)
    用来指定返回类型,指定的类型可以是基本类型,也可以是java容器,也可以是javabean
   -->
  resultType="hashmap"
  
  <!-- 
    4. resultMap(resultType 与 resultMap 二选一配置)
    用于引用我们通过 resultMap 标签定义的映射类型,这也是mybatis组件高级复杂映射的关键
   -->
  resultMap="USER_RESULT_MAP"
  
  <!-- 
    5. flushCache(可选配置)
    将其设置为true,任何时候语句被调用,都会导致本地缓存和二级缓存被清空,默认值:false
   -->
  flushCache="false"

  <!-- 
    6. useCache(可选配置)
    将其设置为true,会导致本条语句的结果被二级缓存,默认值:对select元素为true
   -->
  useCache="true"

  <!-- 
    7. timeout(可选配置)
    这个设置是在抛出异常之前,驱动程序等待数据库返回请求结果的秒数,默认值为:unset(依赖驱动)
   -->
  timeout="10000"

  <!-- 
    8. fetchSize(可选配置)
    这是尝试影响驱动程序每次批量返回的结果行数和这个设置值相等。默认值为:unset(依赖驱动)
   -->
  fetchSize="256"

  <!-- 
    9. statementType(可选配置)
    STATEMENT, PREPARED或CALLABLE的一种,这会让MyBatis使用选择Statement, PrearedStatement或CallableStatement,默认值:PREPARED
   -->
  statementType="PREPARED"

  <!-- 
    10. resultSetType(可选配置)
    FORWARD_ONLY,SCROLL_SENSITIVE 或 SCROLL_INSENSITIVE 中的一个,默认值为:unset(依赖驱动)
   -->
  resultSetType="FORWORD_ONLY"
></select>

5.3、resultMap 标签的属性信息

<!-- 
  1. type 对应的返回类型,可以是javabean, 也可以是其它
  2. id 必须唯一, 用于标示这个resultMap的唯一性,在使用resultMap的时候,就是通过id引用
  3. extends 继承其他resultMap标签
 -->
<resultMap type="" id="" extends="">  
  <!-- 
    1. id 唯一性,注意啦,这个id用于标示这个javabean对象的唯一性, 不一定会是数据库的主键(不要把它理解为数据库对应表的主键)
    2. property 属性对应javabean的属性名
    3. column 对应数据库表的列名
       (这样,当javabean的属性与数据库对应表的列名不一致的时候,就能通过指定这个保持正常映射了)
   -->
  <id property="" column=""/>
        
  <!-- 
    result 与id相比,对应普通属性
   -->    
  <result property="" column=""/>
        
  <!-- 
    constructor 对应javabean中的构造方法
   -->
  <constructor>
    <!-- idArg 对应构造方法中的id参数 -->
       <idArg column=""/>
       <!-- arg 对应构造方法中的普通参数 -->
       <arg column=""/>
   </constructor>
   
   <!-- 
    collection 为关联关系,是实现一对多的关键 
    1. property 为javabean中容器对应字段名
    2. ofType 指定集合中元素的对象类型
    3. select 使用另一个查询封装的结果
    4. column 为数据库中的列名,与select配合使用
    -->
  <collection property="" column="" ofType="" select="">
    <!-- 
      当使用select属性时,无需下面的配置
     -->
    <id property="" column=""/>
    <result property="" column=""/>
  </collection>
        
  <!-- 
    association 为关联关系,是实现一对一的关键
    1. property 为javabean中容器对应字段名
    2. javaType 指定关联的类型,当使用select属性时,无需指定关联的类型
    3. select 使用另一个select查询封装的结果
    4. column 为数据库中的列名,与select配合使用
   -->
  <association property="" column="" javaType="" select="">
    <!-- 
      使用select属性时,无需下面的配置
     -->
    <id property="" column=""/>
    <result property="" column=""/>
  </association>
</resultMap>

5.4、insert 标签的属性信息

<insert
  <!--
    同 select 标签
   -->
  id="insertProject"

  <!-- 
    同 select 标签
   -->
  paramterType="projectInfo"
  
  <!-- 
    1. useGeneratedKeys(可选配置,与 keyProperty 相配合)
    设置为true,并将 keyProperty 属性设为数据库主键对应的实体对象的属性名称
   --> 
  useGeneratedKeys="true"

  <!-- 
    2. keyProperty(可选配置,与 useGeneratedKeys 相配合)
    用于获取数据库自动生成的主键
   -->
  keyProperty="projectId"
>

5.5、重用 sql 标签

<sql id="userColumns">id,username,password</sql>

5.6、完全限定名使用别名替代

每个 sql 映射文件的要元素中,都需要指定一个名称空间,用以确保每个映射语句的 id 属性不会重复。如<mapper namespace="com.mayikt.mapper.UserMapper">

在 Java 代码中引用某个 sql 映射时,使用的亦是含有名称空间的全路径。如

session.update("com.mayikt.mapper.UserMapper.udpateUser", user);

6、mybatis多条件查询

6.1、mybatis多条件查询

第一种方法 传递map 型;

第二种方法:多个参数如果不封装成Map 参数值需要通过,多个参数的时候要使用 @Param 给指定参数,否则会出现找不到参数的错误

第三种方法:传递pojo ; 非常多参数 sql语句中获取参数值名称 与对象成员属性名称需要保持一致

List<FlightEntity> getByIdFlightParameterMap(Map<String, String> parameterMap);

List<FlightEntity> getByIdFlightParameter(@Param("company") String company,
                                          @Param("departureAirport") String departureAirport,
                                          @Param("arriveAirport") String arriveAirport);

    List<FlightEntity> getByIdFlightPoJo(FlightEntity flightEntity);
    <select id="getByIdFlightParameterMap" resultMap="flightEntityMap">
         SELECT * from mayikt_flight where company=#{company}
and departure_airport=#{departureAirport} and arrive_airport=#{arriveAirport};
  </select>

    <select id="getByIdFlightParameter" resultMap="flightEntityMap">
         SELECT * from mayikt_flight where company=#{company}
and departure_airport=#{departureAirport} and arrive_airport=#{arriveAirport};
  </select>

    <select id="getByIdFlightPoJo" parameterType="com.mayikt.entity.FlightEntity" resultMap="flightEntityMap">
         SELECT * from mayikt_flight where company=#{company}
and departure_airport=#{departureAirport} and arrive_airport=#{arriveAirport};
  </select>

6.2、mybatis动态条件查询

动态 SQL 是 MyBatis 的强大特性之一。如果你使用过 JDBC 或其它类似的框架,你应该能理解根据不同条件拼接 SQL 语句有多痛苦,例如拼接时要确保不能忘记添加必要的空格,还要注意去掉列表最后一个列名的逗号。利用动态 SQL,可以彻底摆脱这种痛苦。

使用动态 SQL 并非一件易事,但借助可用于任何 SQL 映射语句中的强大的动态 SQL 语言,MyBatis 显著地提升了这一特性的易用性。

如果你之前用过 JSTL 或任何基于类 XML 语言的文本处理器,你对动态 SQL 元素可能会感觉似曾相识。在 MyBatis 之前的版本中,需要花时间了解大量的元素。借助功能强大的基于 OGNL 的表达式,MyBatis 3 替换了之前的大部分元素,大大精简了元素种类,现在要学习的元素种类比原来的一半还要少。

  • if
  • choose (when, otherwise)
  • trim (where, set)
  • foreach

mysql 加上输出日志

    <settings>
        <!-- 打印sql日志 -->
        <setting name="logImpl" value="STDOUT_LOGGING" />
    </settings>
    
        <?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration
        PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-config.dtd">
<!-- 数据库相关的配置-->
<configuration>

    <settings>
        <!-- 打印sql日志 -->
        <setting name="logImpl" value="STDOUT_LOGGING" />
    </settings>
    <environments default="development">
        <environment id="development">
            <transactionManager type="JDBC"/>
            <dataSource type="POOLED">
                <property name="driver" value="com.mysql.cj.jdbc.Driver"/>
                <property name="url" value="jdbc:mysql://127.0.0.1:3306/flight?serverTimezone=GMT%2B8"/>
                <property name="username" value="root"/>
                <property name="password" value="root"/>
            </dataSource>
        </environment>
    </environments>
    <mappers>
        <mapper resource="mybatis/flightMapper.xml"/>
    </mappers>

</configuration>

<if test="逻辑判断"></if>

    <select id="getByIdFlightDynamicParameter" parameterType="com.mayikt.entity.FlightEntity"
            resultMap="flightEntityMap">
        SELECT * from mayikt_flight where
        <if test="company!=null and  company!=''">
            company=#{company}
        </if>
        <if test="departureAirport!=null and  departureAirport!=''">
            and departure_airport=#{departureAirport}
        </if>
        <if test="arriveAirport!=null and  arriveAirport!=''">
            and arrive_airport=#{arriveAirport};
        </if>

    </select>

缺陷 如果没有传递company 导致sql报错

  <select id="getByIdFlightDynamicParameter" parameterType="com.mayikt.entity.FlightEntity"
            resultMap="flightEntityMap">
        SELECT * from mayikt_flight where
        1=1
        <if test="company!=null and  company!=''">
           and  company=#{company}
        </if>
        <if test="departureAirport!=null and  departureAirport!=''">
            and departure_airport=#{departureAirport}
        </if>
        <if test="arriveAirport!=null and  arriveAirport!=''">
            and arrive_airport=#{arriveAirport};
        </if>

    </select>

可以使用<where></where>


    <select id="getByIdFlightDynamicParameter" parameterType="com.mayikt.entity.FlightEntity"
            resultMap="flightEntityMap">
        SELECT * from mayikt_flight

        <where>
            <if test="company!=null and  company!=''">
                and company=#{company}
            </if>
            <if test="departureAirport!=null and  departureAirport!=''">
                and departure_airport=#{departureAirport}
            </if>
            <if test="arriveAirport!=null and  arriveAirport!=''">
                and arrive_airport=#{arriveAirport};
            </if>
        </where>


    </select>

7、mybatis注解开发

8、问题

8.1、常见问题1

alt+enter 选择第一个

8.2、常见问题2

Mapped Statements collection does not contain value for getByUsersAll

没有找到该getByUsersAll 对应sql语句 没有将Mapped 交给mybatis扫描到

8.3、常见问题3

Could not find resource mybatis/userMapper2.xml

clean 清除 编译class

常见问题4

Cause: org.apache.ibatis.builder.BuilderException: Error parsing Mapper XML. Cause: java.lang.IllegalArgumentException: Mapped Statements collection already contains value for userMapper.getByUsers

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

出世&入世

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值