JavaORM框架之Mybatis篇(Ibatis)
阿新 • • 發佈:2018-12-12
歡迎檢視,如果您正在為Java後端龐大的體系所困擾,如果您正在為各種繁出不窮的技術和各種框架所迷茫,那麼本系列文章將帶您窺探Java龐大的體系。本系列教程希望您能站在上帝的角度去觀察(瞭解)Java體系。使Java的各種後端技術在你心中模組化;讓你在工作中能將Java各個技術瞭然於心;能夠即插即用。本章我們來一起了解ORM(物件關係對映關係)框架之Mybatis(Ibatis)。
主流ORM框架有Mybatis和Hibernate,本章我們將對Mybatis的核心要點進行了解。什麼是ORM(物件對映關係)框架?
ORM(Object Relational Mapping)物件關係對映,是 一種為了解決面向物件與關係型資料庫不匹配而出現的技術,使開發者能夠用面向物件的方式使用關係型資料庫。
Mybatis和Hibernate有什麼異同?
- Mybatis簡單,Hibernate較複雜,門檻高。
- Mybatis自定製Sql,比Hibernate靈活,可控
- Mybatis與資料庫對映得到的PO與Hibernate對映PO意義不同
/** * @Author:jimisun * @Description: * @Date:Created in 08:37 2018-09-24 * @Modified By: */ public class Main { public static void main(String[] args) throws IOException { String resourcePS:Mybatis支援註解開發,但需要保留空的XML檔案,也就是保留空的名稱空間 ; 如下所示= "mybatis-config.xml"; InputStream inputStream = Resources.getResourceAsStream(resource); SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream); SqlSession sqlSession = sqlSessionFactory.openSession(); TestUserMapper mapper = sqlSession.getMapper(TestUserMapper.class); TestUser testUser = mapper.selectOne(1); System.out.println(testUser.toString()); } }
@Select(" SELECT * from user where id = #{id};")
MyDto selectOne(Integer id);
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" > <mapper namespace="com.jimisun.dao.TestUserMapper"> <!--空--> </mapper>如果你使用Mybatis那麼一定會使用Spring,最常見的框架組合就是SSM(SpringMvc+Spring+Mybatis),那麼Mybatis針對和Spring的整合提供了一個類庫(jar包)
<dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis-spring</artifactId> <version>1.2.2</version> </dependency>以前我們配置在mybatis裡面的配置,現在我們可以將這些配置轉移到了Spring配置中;統一交給Spring進行管理, 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: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"> <!-- 載入配置檔案 --> <context:property-placeholder location="classpath:db.properties"/> <!-- 資料庫連線池 --> <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource" 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"/> </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-config.xml"/> <!-- 掃描entity包 使用別名 --> <property name="typeAliasesPackage" value="com.jimisun.domain"/> <!-- 掃描sql配置檔案:mapper需要的xml檔案 --> <property name="mapperLocations" value="classpath:mapper/*.xml" /> </bean> <!-- 配置掃描Dao介面包,動態實現Dao介面,注入到spring容器中 --> <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"> <!-- 注入sqlSessionFactory --> <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"/> <!-- 給出需要掃描Dao介面包 --> <property name="basePackage" value="com.jimisun.dao"/> </bean> </beans>
Spring和Myabtis整合的有兩個關注點
- Myabtis將SqlSessionFactory交付Spring管理
- Spring將XML對應的介面進行接手管理
<resultMap id="MyDto" type="com.jimisun.domain.dto.MyDto"> <result property="myid" column="id"></result> <result property="myusername" column="username"></result> </resultMap>雖然很多時候我們在開發中並不經常Mybatis的二級快取 , 但是如果針對個別SQL進行優化設定能夠極大提升訪問資料庫效率 . mybatis支援一級快取和二級快取,預設開啟一級快取,一級快取使SqlSession級別的,Session結束快取就清空了,二級快取使Mapper級別的,需要我們手動開啟。
<!--開啟二級快取--> <cache/>針對不需要使用二級快取的方法設定useCache=false
<select id="selectOne" parameterType="java.lang.Integer" resultType="com.jimisun.domain.TestUser" useCache="false"> SELECT * from user where id = #{id} </select>
我們進行簡單的測試 , 觀察Mybatis二級快取是否開啟
public static void main(String[] args) { ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("spring-config.xml"); TestUserMapper testUserMapper = (TestUserMapper) context.getBean("testUserMapper"); /*測試快取:先查詢此時username為jimisun*/ TestUser testUser = testUserMapper.selectOne(1); /*測試快取:修改username為lisi*/ Integer integer = testUserMapper.updateOne(1); /*測試快取:最後查詢檢視是否從資料庫獲取還是從快取獲取*/ TestUser resultUser = testUserMapper.selectOne(1); System.out.println(resultUser.toString()); }
Mybatis其他使用技巧
- 在mapper.xml編寫sql時對於重複的sql我們可以使用<include refid="id">引用程式碼
- 對於Mybatis非空判斷我們建議這樣使用 <if test="param !=null and param != ''">
- 一個Mapper.xml中可以直接引用另一個Mapper.xml的resultMap , 不需要重複定義