配置mybatis環(huán)境(包含log4j,MySQL驅(qū)動) 使用maven新建項目,pom中添加 <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis</artifactId> <version>3.3.0</version> </dependency> <dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-api</artifactId> <version>1.7.12</version> </dependency> <dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-log4j12</artifactId> <version>1.7.12</version> </dependency> <dependency> <groupId>log4j</groupId> <artifactId>log4j</artifactId> <version>1.2.16</version> </dependency> <dependency> <groupId>cglib</groupId> <artifactId>cglib</artifactId> <version>3.0</version> </dependency> <dependency> <groupId>commons-logging</groupId> <artifactId>commons-logging</artifactId> <version>1.2</version> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>5.1.9</version> <scope>runtime</scope> </dependency>
mybatis核心組件 SqlsessionFactoryBuilder(構(gòu)造器):根據(jù)配置信息或者代碼生成SqlSessionFactory. SqlSessionFactory:依靠生產(chǎn)來生成會話(SqlSession) SqlSession:是一個既能執(zhí)行sql并獲取結(jié)果又能獲取Mapper的接口 SqlMapper:java接口和xml(或注解)組成,需要給出對應(yīng)的SQL和映射規(guī)則。他負(fù)責(zé)發(fā)送Sql去執(zhí)行并返回結(jié)果。 
一,我們創(chuàng)建數(shù)據(jù)庫連接池(PooledDataSource)。載入相關(guān)的驅(qū)動,數(shù)據(jù)庫實例,用戶名,密碼等。這里可以使用讀取jdbc.PRoperties的方式注入相關(guān)信息 二,構(gòu)建數(shù)據(jù)庫事務(wù)方式,Mybatis使用JDBC事務(wù)(JdbcTransactionFactory) 三,創(chuàng)建數(shù)據(jù)庫運行環(huán)境,默認(rèn)開發(fā)環(huán)境(Environment) 四,構(gòu)建連接對象(Configuration),通過configuration對象可以注冊別名(方法getTypeAliasRegistry().registerAlias()),加入映射器(addMapper()) 五,構(gòu)建SqlSessionFactoryBuilder,new SqlSessionFactoryBuilder().build(configuration); mapper映射器默認(rèn)在po的同級目錄下
使用XML獲取SqlSessionFactoryBuilder<?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> <!-- 定義別名 --> <typeAliases> <typeAlias type="com.mybatis.po.Role" alias="role"/> </typeAliases> <!-- 定義數(shù)據(jù)庫信息,默認(rèn)使用development 數(shù)據(jù)庫構(gòu)建環(huán)境 --> <environments default="development"> <environment id="development"> <!-- 采用jdbc事務(wù)管理 --> <transactionManager type="JDBC"/> <!-- 配置數(shù)據(jù)庫鏈接信息 --> <dataSource type="POOLED"> <property name="driver" value="com.mysql.jdbc.Driver"/> <property name="url" value="jdbc:mysql://localhost:3306/mybatis"/> <property name="username" value="root"/> <property name="password" value="123456"/> </dataSource> </environment> </environments> <!-- 定義映射器 --> <mappers> <mapper resource="com/mybatis/mapper/roleMapper.xml"/> </mappers> </configuration>java代碼
String resource = "mybatis-config.xml"; InputStream inputStream = null; try { inputStream = Resources.getResourceAsStream(resource); } catch (IOException e) { // TODO Auto-generated catch block Logger.getLogger(SqlSessionFactoryUtil.class.getName()) .log(Level.SEVERE,null, e); } synchronized (CLASS_LOCK) { if(sqlSessionFactory == null){ sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream); } } return sqlSessionFactory;xml配置后續(xù)博客將重點解析 一,通過inputStream流的方式讀取xml配置 二,SqlSessionFactoryBuilder().build(inputStream); SqlSessionFactoryBuilder顯然需要單例模式,這里采用懶漢式延時獲取。由于懶漢式存在這線程不安全,所以,這里用了類線程鎖來保證同步。
映射器 映射器是java接口和XML(或注解)共同組成,他的作用有:定義參數(shù)類型(parameterType傳入?yún)?shù)類型;resultType返回結(jié)果類型),描述緩存,描述SQL語句,定義查詢結(jié)果和POJO的映射關(guān)系。<select id="getRole" parameterType="long" resultType="role"> select id, role_name as roleName, note from t_role where id=#{id} </select> <insert id="insertRole" parameterType="role"> insert into t_role(role_name,note) values (#{roleName},#{note}) </insert> <delete id="deleteRole" parameterType="long"> delete from t_role where id=#{id} </delete>生命周期 掌握生命周期對于mybatis的正確性和高性能極其重要 SqlSessionFactoryBuilder:構(gòu)建SqlSessionFactory,如果一旦SqlSessionFactory被成功構(gòu)建,我們應(yīng)該毫不猶豫將它回收。所以,它的生命周期應(yīng)該是方法的局部。 SqlSessionFactory:SqlSessionFactory責(zé)任應(yīng)該是唯一的,生產(chǎn)SqlSession。但是又是Mybatis全局的。每次訪問數(shù)據(jù)庫,我們都需要通過他去生成SqlSession.如果多次創(chuàng)建同一個SqlSessionFactory,我們的連接資源很快就會被耗盡。所以,果斷使用單例。 SqlSession:一個會話。在請求數(shù)據(jù)庫處理事務(wù)的過程中,是一個線程不安全的對象,在處理多線程的時候一定要小心。處理事務(wù)的時候需要注意,隔離級別,數(shù)據(jù)庫鎖等高級特性。此外創(chuàng)建的SqlSession需要及時關(guān)閉。我們通過finally里面關(guān)閉它,一般情況。 Mapper:一個接口,沒有任何實現(xiàn)類。作用是發(fā)送sql,返回結(jié)果。它應(yīng)該在一個SqlSession事務(wù)內(nèi)部。是一個方法級別的新聞熱點
疑難解答