一、Mybatis的特性
- MyBatis支持定制化SQL、存盤程序以及高級映射
- Mybatis避免了幾乎所有的JDBC代碼和手動設定引數以及結果集決議操作
- Mybatis可以使用簡單的XML或注解實作配置和原始映射,將介面和Java 的POJO(普通的Java物件)映射成資料庫中的記錄
- Mybatis是一個半自動的ORM(物件關系映射)框架
二、Mybatis的優勢
*輕量級,性能出色
*SQL和Java編碼分開,功能邊界清晰,Java代碼專注業務邏輯,SQL陳述句專注資料
*能夠與Spring很好的集成
三、Mybatis的基本用法
1.添加Mybatis所需依賴
1.Mybatis核心包
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.5.7</version>
</dependency>
2.MySQL驅動
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.3</version>
</dependency>
3.junit單元測驗
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
4.log4j日志
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.17</version>
</dependency>
2.配置Mybatis的全域組態檔mybatis-config.xml
1.settings標簽內
- mapUnderscoreToCamelCase:駝峰式命名,為true表示
開啟自動映射駝峰式命名規則
<setting name="mapUnderscoreToCamelCase" value="https://www.cnblogs.com/luochenggui/archive/2021/06/14/true"/> - aggressiveLazyLoading:積極的懶加載,使用在Mybatis的association關聯物件(對一)和collection關聯集合物件(對多)的延遲加載,Mybatis版本大于3.4.1默認關閉
- lazyLoadingEnabled:開始時,設定為True
<setting name="aggressiveLazyLoading" value="https://www.cnblogs.com/luochenggui/archive/2021/06/14/false"/>
<setting name="lazyLoadingEnabled" value="https://www.cnblogs.com/luochenggui/archive/2021/06/14/true"/>
2.properties標簽:關聯外部屬性檔案
<properties resource="jdbc.properties"/>
3.mappers標簽:注冊Mapper組態檔
- mapper標簽:具體注冊一個Mapper組態檔
<mapper resource="mappers/EmployeeMapper.xml"/>
- package標簽:指定Mapper組態檔所在包,全部注冊
注意:如果工程是Maven工程,那么Mapper組態檔還是要放在resources目錄下,且目錄結構要和Mapper介面的目錄結構保持一致
<package name="com.atguigu.mybatis.mapper"/>
- environments標簽:配置Mybatis使用的環境,通常會轉移到Spring中配置
<environments default="development">
<!-- environment表示配置Mybatis的一個具體的環境 -->
<environment id="development">
<!-- Mybatis的內置的事務管理器 -->
<transactionManager type="JDBC"/>
<!-- 配置資料源 -->
<dataSource type="POOLED">
<!-- 建立資料庫連接的具體資訊 -->
<property name="driver" value="https://www.cnblogs.com/luochenggui/archive/2021/06/14/${wechat.dev.driver}"/>
<property name="url" value="https://www.cnblogs.com/luochenggui/archive/2021/06/14/${wechat.dev.url}"/>
<property name="username" value="https://www.cnblogs.com/luochenggui/archive/2021/06/14/${wechat.dev.username}"/>
<property name="password" value="https://www.cnblogs.com/luochenggui/archive/2021/06/14/${wechat.dev.password}"/>
</dataSource>
</environment>
</environments>
3.配置Mybatis的映射檔案Mapper.xml
一、基本增刪改查操作
1.select標簽
-
resultType屬性:指定封裝查詢結果的物體型別別
1.簡單資料型別
<select id="selectCount" resultType="int"> SELECT count(*) from t_emp </select>2.Map型別
<select id="selectEmpNameAndMaxSalary" resultType="map"> select emp_name,max(emp_salary) from t_emp </select>3.物體型別別
- 回傳單個物體類
<select id="selectEmpById" resultType="com.atguigu.mybatis.entity.Emp"> select emp_id ,emp_name,emp_salary from t_emp where emp_id=#{empId} </select>- 回傳物體類的集合:mybatis再將查詢結果封裝到物體類物件之后,會自動把多個物體類物件放在List集合回傳
<select id="selectAll" resultType="com.atguigu.mybatis.entity.Emp"> select * from t_emp; </select> -
resultMap屬性
1.映射物體類自身屬性<!--專門宣告一個resultMap設定column到property之間的對應關系--> <resultMap id="empMapResultMap" type="com.atguigu.mybatis.entity.Emp"> <id column="emp_id" property="empId"/> <result column="emp_name" property="empName"/> <result column="emp_salary" property="empSalary"/> </resultMap> <!--Emp selectWithResultMap(long empId)--> <select id="selectWithResultMap" resultMap="empMapResultMap"> select emp_id ,emp_name,emp_salary from t_emp where emp_id = #{empId} </select>2.映射關聯屬性
- 對一:association標簽和javaType屬性
<resultMap id="selectOrderWithCustomerResultMap" type="com.atguigu.mybatis.entity.Order"> <id column="order_id" property="orderId"/> <result column="order_name" property="orderName"/> <result column="customer_id" property="customerId"/> <!-- 使用association標簽配置“對一”關聯關系 --> <association property="customer" javaType="com.atguigu.mybatis.entity.Customer"> <result column="customer_id" property="customerId"/> <result column="customer_name" property="customerName"/> </association> </resultMap> <!--Order selectOrderWithCustomer(long orderId);--> <!--關鍵詞:association javaType--> <select id="selectOrderWithCustomer" resultMap="selectOrderWithCustomerResultMap"> select order_id,order_name,c.customer_id,customer_name from t_order o left join t_customer c on o.customer_id = c.customer_id where order_id = #{orderId} </select>- 對多:collection標簽和ofType屬性
<resultMap id="selectCustomerWithOrderListResultMap" type="com.atguigu.mybatis.entity.Customer"> <id column="customer_id" property="customerId"/> <result column="customer_name" property="customerName"/> <collection property="orderList" ofType="com.atguigu.mybatis.entity.Order"> <result column="order_id" property="orderId"/> <result column="order_name" property="orderName"/> <result column="customer_id" property="customerId"/> </collection> </resultMap> <!--Customer selectCustomerWithOrderList(long customerId);--> <!--關鍵詞:collection ofType--> <select id="selectCustomerWithOrderList" resultMap="selectCustomerWithOrderListResultMap"> select c.customer_id,customer_name,order_id,order_name from t_customer c left join t_order o ON c.customer_id = o.customer_id where c.customer_id = #{customerId} </select>
2.insert標簽
- useGeneratedKey屬性:設定為true時表示使用資料庫自增主鍵
- keyProperty屬性:指定物體類中接收自增主鍵的屬性
<insert id="getGeneratedKeys" useGeneratedKeys="true" keyProperty="empId">
insert into t_emp(emp_name, emp_salary) values (#{empName},#{empSalary})
</insert>
3.update標簽
<update id="updateEmp">
update t_emp set emp_name=#{empName},emp_salary=#{empSalary} where emp_id=#{empId}
</update>
4.delete標簽
<delete id="deleteEmp">
delete from t_emp where emp_id=#{empId}
</delete>
二、給SQL傳參
#{}:將來變成?占位符${}:將來拼接字串
三、動態SQL
動態SQL存在的意義是為了解決拼接SQL陳述句字串時的痛點問題,
- where標簽:會自動去掉標簽體內前面、后面多余的and/or
- if標簽
<select id="selectEmpByCondition" resultType="com.atguigu.mybatis.entity.Emp">
select * from t_emp
<where>
<if test="empName != null">
or emp_name = #{empName}
</if>
<if test="empSalary > 800.00">
or emp_salary > #{empSalary}
</if>
</where>
</select>
- set標簽
- trim標簽
- prefix屬性:指定要動態添加的前綴
- suffix屬性:指定要動態添加的后綴
- prefixOverrides屬性:指定要動態去掉的前綴,使用"|"分隔有可能的多個值
- suffixOverrrides屬性:指定要動態驅動的后綴,使用"|"分隔有可能的多個值
<select id="selectEmpByConditionByTrim" resultType="com.atguigu.mybatis.entity.Emp">
SELECT emp_id,emp_name,emp_salary from t_emp
<trim prefix="where" suffixOverrides="and|or">
<if test="empName != null">
emp_name = #{empName} or
</if>
<if test="empSalary < 1000">
emp_salary <#{empSalary}
</if>
</trim>
</select>
- sql標簽
- foreach標簽
- collection屬性:指定要遍歷的集合
- 使用@Param注解,按照注解指定的集合名來參考
- 沒使用@Param注解,則使用默認名collection或list
- item屬性:指定遍歷出來每個元素的名字
- open屬性:指定整個回圈把字串拼好后,字串整體的前面要添加的字串
- close屬性
- separator屬性:回圈體之間的間隔符號
- index屬性:遍歷程序中的索引
- collection屬性:指定要遍歷的集合
- 注意:在拼接多條SQL陳述句的時候,需要讓連接資料庫的地址后面附加一個引數:allowMultiQueries=true
<--批量插入多條資料-->
<insert id="insertEmpByBatch">
insert into t_emp(emp_name,emp_salary)
<foreach collection="empList" index="index" item="emp" open="values" separator=",">
(#{emp.empName},#{emp.empSalary})
</foreach>
</insert>
<--批量更新多條資料-->
<update id="updateEmpByBatch">
<foreach collection="empList" separator=";" item="emp" >
update t_emp set emp_name = #{emp.empName},emp_salary = #{emp.empSalary} where emp_id = #{emp.empId}
</foreach>
</update>
</mapper>
- choose/when/otherwise標簽
- include標簽:參考已抽取的SQL片段,于sql標簽搭配使用
四、開啟二級快取功能
- 使用cache標簽:type屬性:指定具體的快取產品,例如EHCache
<cache type="org.mybatis.caches.ehcache.EhcacheCache"/>
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/287293.html
標籤:其他
上一篇:C++ RAII
