框架之MyBatis核心配置

configuration 配置
-properties 屬性
-settings 設置
-typeAliases 類型命名
-typeHandlers 類型處理器
-objectFactory 對象工廠
-plugins 插件
-environments 環境
--environment 環境變量
--transactionManager 事務管理器
-dataSource 數據源
-mappers 映射器

<configuration>
    <!-- 配置全局屬性 -->
    <settings>
        <!-- 使用jdbc的getGeneratedKeys獲取數據庫自增主鍵值 -->
        <setting name="useGeneratedKeys" value="true" />

        <!-- 使用列別名替換列名 默認:true -->
        <setting name="useColumnLabel" value="true" />

        <!-- 開啟駝峰命名轉換:Table{create_time} -> Entity{createTime} -->
        <setting name="mapUnderscoreToCamelCase" value="true" />
    </settings>
</configuration>
mapper.xml[insert、update、select、delete]
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper
    PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
    "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.neusoft.flycity.dao.BillDao">
    <insert id="addBill">
        INSERT INTO bill(ticketNum,ticketPrice,totalPrice,bookingID)
        VALUES (#{bookNum},#{price},#{price}*#{bookNum},#{bookingID})
    </insert>

    <delete id="deleteBill" parameterType="int">
        DELETE FROM bill where bookingID = #{bookingID}
    </delete>

    <select id="getById" parameterType="int" resultType="Bill">
        SELECT * FROM bill WHERE bookingID = #{bookingID}
    </select>
</mapper>

核心對象

SqlSessionFactory

String resource = "org/mybatis/example/mybatis-config.xml";
InputStream inputStream = Resources.getResourceAsStream(resource);
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
DataSource dataSource = BlogDataSourceFactory.getBlogDataSource();
TransactionFactory transactionFactory = new JdbcTransactionFactory();
Environment environment = new Environment("development", transactionFactory, dataSource);
Configuration configuration = new Configuration(environment);
configuration.addMapper(BlogMapper.class);
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(configuration);

SqlSession

SqlSession session = sqlSessionFactory.openSession();

SqlSessionFactoryBuilder
這個類可以被實例化、使用和丟棄,一旦創建了 SqlSessionFactory,就不再需要它了。
因此 SqlSessionFactoryBuilder 實例的最佳作用域是方法作用域(也就是局部方法變量)。
SqlSessionFactory
SqlSessionFactory 一旦被創建就應該在應用的運行期間一直存在,沒有任何理由對它進行清除或重建。
因此 SqlSessionFactory 的最佳作用域是應用作用域。有很多方法可以做到,最簡單的就是使用單例模式或者靜態單例模式。
SqlSession
每個線程都應該有它自己的 SqlSession 實例。SqlSession 的實例不是線程安全的,因此是不能被共享的,所以它的最佳的作用域是請求或方法作用域。

動態SQL

  • if
<if test="title != null">
    AND title like #{title}
</if>
  • choose (when, otherwise)
<choose>
    <when test="title != null">
      AND title like #{title}
    </when>
    <when test="author != null and author.name != null">
      AND author_name like #{author.name}
    </when>
    <otherwise>
      AND featured = 1
    </otherwise>
</choose>
  • trim (where, set)
<where> 
    <if test="state != null">
         state = #{state}
    </if> 
    <if test="title != null">
        AND title like #{title}
    </if>
    <if test="author != null and author.name != null">
        AND author_name like #{author.name}
    </if>
</where>

<trim prefix="WHERE" prefixOverrides="AND |OR ">
  ... 
</trim>
<set>
      <if test="username != null">username=#{username},</if>
      <if test="password != null">password=#{password},</if>
      <if test="email != null">email=#{email},</if>
      <if test="bio != null">bio=#{bio}</if>
</set>

<trim prefix="SET" suffixOverrides=",">
  ...
</trim>
  • foreach
<foreach item="item" index="index" collection="list"
      open="(" separator="," close=")">
        #{item}
</foreach>
最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容

  • 1. 簡介 1.1 什么是 MyBatis ? MyBatis 是支持定制化 SQL、存儲過程以及高級映射的優秀的...
    笨鳥慢飛閱讀 5,622評論 0 4
  • 從三月份找實習到現在,面了一些公司,掛了不少,但最終還是拿到小米、百度、阿里、京東、新浪、CVTE、樂視家的研發崗...
    時芥藍閱讀 42,366評論 11 349
  • 官方文檔 簡介 入門 XML配置 XML映射文件 動態SQL Java API SQL語句構建器 日志 一、 JD...
    拾壹北閱讀 3,551評論 0 52
  • 一、安裝 使用MyBatis,首先需要下載其jar包,然后將jar包導入lib中即可,下載地址:github.co...
    涉務閱讀 279評論 0 0
  • 廣告大師約翰·沃納梅克曾經提出過著名的廣告營銷界“哥德巴赫猜想”:“我知道我的廣告費有一半是浪費的,但我不知道浪費...
    9c42a2489e1d閱讀 1,386評論 3 7