若是您正在爲Java後端龐大的體系所困擾,若是您正在爲各類繁出不窮的技術和各類框架所迷茫,那麼本系列文章將帶您窺探Java龐大的體系。本系列教程但願您能站在上帝的角度去觀察(瞭解)Java體系。使Java的各類後端技術在你心中模塊化;讓你在工做中能將Java各個技術瞭然於心;可以即插即用。本章咱們來一塊兒瞭解ORM(對象關係映射關係)框架之Mybatis(Ibatis)。java
主流ORM框架有Mybatis和Hibernate,本章咱們將對Mybatis的核心要點進行了解。spring
ORM(Object Relational Mapping)對象關係映射,是 一種爲了解決面向對象與關係型數據庫不匹配而出現的技術,使開發者可以用面向對象的方式使用關係型數據庫。sql
/** * @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 resource = "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()); } }
PS:Mybatis支持註解開發,但須要保留空的XML文件,也就是保留空的命名空間 ; 以下所示數據庫
@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整合的有兩個關注點session
在實際項目中咱們經過使用mybatis查詢數據庫常常使用多表查詢,關聯查詢,或者實體的屬性名和數據庫列名不符等狀況...因此查詢的結果存在不定性,咱們能夠自定義Dto類,在mapper.xml文件中自定義<resultMap>標籤便可。mybatis
<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級別的,須要咱們手動開啓。在此我向你們推薦一個架構學習交流羣。交流學習羣號:874811168 裏面會分享一些資深架構師錄製的視頻錄像架構
<!--開啓二級緩存--> <cache/>
針對不須要使用二級緩存的方法設置useCache=falseapp
<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()); }