MyBatis的前身是Apache的一個開源項目iBatis,2010年這個項目由apache software foundation 遷移到了google code,而且更名爲MyBatis。2013年11月遷移到GitHub,所以目前MyBatis是由GitHub維護的。java
一樣做爲持久層框架的Hibernate在前些年很是的火,它在配置了映射文件和數據庫鏈接文件後就能夠經過Session操做,它甚至提供了HQL去操做POJO進而操做數據庫的數據,幾乎可使編程人員脫離sql語言。但是爲何MyBatis卻愈來愈受歡迎呢?咱們稍稍總結以下:mysql
Hibernate:sql
1.不方便的全表映射,好比更新時須要發送全部的字段;數據庫
2.沒法根據不一樣的條件組裝不一樣sql;apache
3.對多表關聯和複製sql查詢支持較差;編程
4.有HQL但性能較差,作不到sql優化;數組
5.不能有效支持存儲過程;緩存
在當今的大型互聯網中,靈活、sql優化,減小數據的傳遞是最基本的優化方法,可是Hibernate卻沒法知足咱們的需求,而MyBatis提供了更靈活、更方便的方法。在MyBatis裏,咱們須要本身編寫sql,雖然比Hibernate配置要多,可是是MyBatis能夠配置動態sql,也能夠優化sql,且支持存儲過程,MyBatis幾乎能作到 JDBC 所能作到的全部事情!憑藉其高度靈活、可優化、易維護等特色,成爲目前大型移動互聯網項目的首選框架。安全
導入mybatis的jar包,若是採用Maven進行部署,只需導入mybatis座標。服務器
導入數據庫驅動jar包或座標。
代碼調試階段可導入日誌工具jar包或座標,如log4j。
導入Junit單元測試座標。
在resources目錄下創建一個名字爲mybatis-config.xml(名稱隨意)配置文件,編寫所需配置信息。
遵循開發規範:
類屬性命名儘可能和表字段保持一致。
實體類實現序列化接口。
實體類屬性使用包裝類型定義,如Integer。
Tips: 有時能夠考慮經過定義時初始化來避免可能的空指針異常!
如:private List<String> list = new ArrayList<>() ;
建立Mapper接口,在內定義CRUD方法。
Tips: 方法名惟一,須要在對應的mapper.xml文件中配置id。
在resources下建立sql映射文件。
Tips: 同對應的Mapper接口保持包結構及命名一致。
如:Mapper接口: cn.dintalk.dao.UserMapper
對應配置文件:cn.dintalk.dao.UserMapper.xml
將映射文件經過引入的方式加入到mybatis的主配置文件中。
Tips: 全部映射文件會隨主配置文件在程序運行時加入內存,任一映射文件出 錯都會致使整個環境報錯!(初學者常常搞混resultType和resultMap)。
在映射文件中編寫sql進行crud操做,在單元測試中,或service層中調用方法!
環境搭建好後開發基本流程爲:
接口定義方法 。
Mapper.xml文件中編寫sql。
單元測試或service調用。
MyBatis的核心組件:
SqlSessionFactoryBuilder(構造器):根據配置信息或代碼生成SqlSessionFactory
SqlSessionFactory(工廠接口):依靠工廠來生成SqlSession(會話)。
SqlSession(會話): 既能夠發生sql去執行並返回結果,也能夠獲取Mapper的接口
SQL Mapper:它是MyBatis新設計的組件,它是由一個java接口和xml文件(或註解)構成的,須要給出對應的sql和映射規則。它負責發送sql去執行,並返回結果。
正確理解並掌握上述核心組件的生命週期可讓咱們寫出高效的程序,還可避免帶來嚴重的併發問題。
其做用就是利用xml或java編碼得到資源來構建SqlSessionFactory對象,構建成功就失去了存在的意義,將其回收。因此它的生命週期只存在於方法的局部。
SqlSessionFactory的做用是建立SqlSession,而SqlSession就是一個會話,至關於JDBC中的Connection對象,每次訪問數據庫都須要經過SqlSessionFactory建立SqlSession,因此SqlSessionFactory應該在MyBatis應用的整個生命週期中。咱們使每個數據庫只對應一個SqlSessionFactory(單例模式)。
SqlSession是一個會話,至關於JDBC的一個Connection對象,它的生命週期應該是在請求數據庫處理事務的過程當中。是一個線程不安全的對象,涉及多線程時格外小心。此外,每次建立的SqlSession都必須及時關閉它。
Mapper是一個接口,沒有任何實現類,其做用是發送sql,返回咱們須要的結果,或者執行sql修改數據庫數據,所以它也因該在一個SqlSession事務方法以內,是一個方法級別的東西。就如同 JDBC中的一條sql語句的執行,它的最大範圍和SqlSession是相同的。
public class MyBatisUtil { private static SqlSessionFactory build =null; static { try { //使用MyBatis的Resources加載資源得到輸入流,構建工廠 InputStream inputStream = Resources.getResourceAsStream("mybatis-config.xml"); build = new SqlSessionFactoryBuilder().build(inputStream); } catch (IOException e) { throw new RuntimeException("資源文件加載失敗!"); } } //使用工廠生產sqlSession public static SqlSession openSession(){ return build.openSession(); } }
SqlSession使用方法
//1.定義sqlSession SqlSession sqlSession = null; try { sqlSession = openSession(); //2.獲取映射器 UserMapper mapper = sqlSession.getMapper(UserMapper.class); //3.some code like 'User u = mapper.findById(id);' //4.sqlSession不提交默認回滾!增刪改需格外注意! sqlSession.commit(); } catch (Exception e) { throw new RuntimeException(e); } finally { if (sqlSession != null) { sqlSession.close(); } }
咱們能夠在映射器中定義哪些元素,以及它們有何做用呢?
元素名稱 | 做用 |
---|---|
insert | 定義插入語句 |
delete | 定義刪除語句 |
update | 定義修改語句 |
select | 定義查詢語句 |
parameterMap | 定義參數映射關係 |
resultMap | 提供從數據庫結果集到POJO映射規則 |
cache | 配置當前命名空間的緩存配置(二級緩存) |
sql | 定義部分sql,各個地方均可引用 |
cache-ref | 引用其餘命名空間的緩存配置 |
在各個元素當中又有至關多的屬性配置項,這裏很少贅述,經過下一節掌握經常使用的內容便可。這裏特別說明sql元素的使用:
<sql id="user_columns"> <!-- 此處定義後,到處使用,尤爲字段多的時候 --> id, name, password </sql> <select ....> select <include refid="user_columns"/> from users where uid=#{id} </select>
<!-- 1.添加用戶 --> <insert id="add" parameterType="User"> INSERT into users (name,password) VALUES (#{name},#{password}); </insert>
<!-- 2.添加用戶,獲取數據庫生成的主鍵值(需數據庫具有主鍵自增功能) --> <insert id="add" parameterType="User" useGeneratedKeys="true" keyProperty="uid"> insert into users (name,password) values(#{name},#{password}); </insert> <!-- 3.添加用戶,獲取數據庫生成的主鍵:數據庫有無自動增加能力均可以--> <insert id="add" parameterType="User"> <selectKey keyColumn="uid" keyProperty="uid" resultType="int" order="AFTER"> select last_insert_id(); </selectKey> insert into users (name,password) values(#{name},#{password}); </insert>
<!-- 4.修改/刪除用戶 --> <update id="update" parameterType="User"> update users set name=#{name},password=#{password} where uid=#{uid}; </update> <delete id="delete" > delete from users where uid=#{uid}; </delete>
<!-- 5.查詢全部的用戶 --> <!-- 定義查詢結果映射規則(結果集映射能夠用別名) --> <resultMap id="userMap" type="User"> <id column="id" property="uid"></id> <result column="姓名" property="name"></result> <result column="password" property="password"></result> </resultMap> <!-- 查詢語句 --> <select id="findAll" resultMap="userMap"> select uid as id,name as '姓名',password from users; </select> <!--6.根據用戶id查詢用戶(單個參數)--> <select id="findById" parameterType="int" resultType="User"> select uid,name,password from users where uid=#{uid}; </select> <!-- 7.根據用戶名和密碼查詢用戶(接口定義方法參數時@Param(" ")進行指定,不然多個參數時 默認使用 arg0,arg1 或param1,param2來映射) --> <select id="findByNamePassword" resultType="User"> select uid,name,password from users where name=#{name} and password=#{password}; </select> <!-- 8.根據用戶實體對象查詢用戶 --> <select id="findByUser" parameterType="User" resultType="User"> select uid,name,password from users where name=#{name}; </select> <!-- 9.根據用戶名進行模糊查詢 --> <select id="findUsersLikeName" resultType="User"> select uid,name,password from users where name like concat("%",#{name},"%"); </select>
MyBatis的動態sql包含這些元素:if | choose(when ohterwise) |trim(where set) | foreach ,經過這些元素來動態組裝sql語句,主要是增改查操做。(實際開發中,嚴禁使用select * 操做。這裏爲了簡便使用select *演示)!
<!-- 1.使用if,實現動態sql,完成查詢操做 --> <select id="findByCondition" parameterType="user" resultType="user"> select uid,name,password,email from users where 1 = 1 <if test="name != null and name !=''"> and name like concat("%",#{name},"%") </if> <if test="password != null and password != '' "> and password = #{password} </if> </select>
<!-- 2.使用if,實現動態sql,完成更新操做 --> <update id="updateUser" parameterType="user"> update users set <if test="name != null and name != '' "> name=#{name}, </if> <if test="password != null and password != '' "> password=#{password}, </if> uid=#{uid} where uid=#{uid}; <!-- 保證任何狀況下的sql完整 --> </update>
<!-- 3.使用if,實現動態sql,完成添加操做 --> <insert id="addUser" parameterType="user"> insert into users (name, <if test="password != null and password != '' "> password, </if>email,phoneNumber,birthday) values(#{name}, <if test="password != null and password != '' "> #{password}, </if>#{email},#{phoneNumber},#{birthday}); </insert>
<!-- 4.使用choose when otherwise,實現動態sql,完成查詢操做 --> <select id="findByCondition" parameterType="user" resultType="user"> select * from users where 1 = 1 <!-- 動態組裝準備 --> <choose> <when test="name != null and name != '' "> and name like concat("%",#{name},"%"); </when> <otherwise> and 1 = 2; <!-- 動態否決 --> </otherwise> </choose> </select>
<!-- 5.使用if和where,實現動態sql,完成查詢操做 --> <select id="findByCondition" resultType="user" parameterType="user"> select * from users <where> <!-- 至少有一個if執行時纔會加上where關鍵字並去掉緊跟後面的and|or關鍵字 --> <if test="name != null and name != '' "> and name like concat("%",#{name},"%") </if> <if test="password != null and password != '' "> and password=#{password} </if> </where> </select>
<!-- 6.使用if和set,實現動態sql,完成更新操做 --> <update id="updateUser" parameterType="user"> update users <set> <!-- set元素會去掉最後一個,號 --> <if test="name != null and name != '' "> name=#{name}, </if> <if test="password != null and password != '' "> password=#{password}, </if> uid=#{uid}, </set> where uid=#{uid}; </update> <!-- trim元素的使用 --> <trim prefix="where" prefixOverrides="and|or"></trim> <!-- 等同與where元素 --> <trim prefix="set" suffixOverrides=","></trim> <!-- 等同與set元素 -->
<!-- 7.使用foreach,實現動態sql,完成根據id list列表的查詢操做 --> <select id="findByIds" resultType="user"> select * from users where uid in <foreach collection="collection" open="(" close=")" separator="," item="uid"> #{uid} </foreach> </select> <!-- foreach中的collection值取決於要遍歷的對象類型(mybatis內部作判斷後默認的) List : 可取 collection或list Set : 取 collection Array: 取 array Map : 取 _parameter (用map無心義,遍歷的依舊是value) 上述默認引用均可以在接口方法中經過@Param(「 」)覆蓋掉! -->
bind元素的做用是經過OGNL表達式自定義一個上下文變量,這樣更方便咱們使用。在進行模糊查詢的時候,若是是MySQL數據庫,咱們經常使用concat函數用「%」和參數鏈接。然而在Oracle數據則是用鏈接符號「||」。這樣sql就須要提供兩種形式去實現。用bind元素,咱們就沒必要使用數據庫語言,只要使用MyBatis的語言便可與所需參數相連。
<select id="findByCondition" parameterType="user" resultType="user"> <!-- 使用bind定義上下文變量 --> <bind name="pattern" value="'%' + name + '%' "/> select uid,name,password,email from users where 1 = 1 <if test="name != null and name !=''"> <!-- and name like concat("%",#{name},"%") --> and name like #{pattern} </if> <if test="password != null and password != '' "> and password = #{password} </if> </select>
<!-- 1.使用多表查詢,完成一對多關聯查詢及映射 --> <!-- user結果集封裝,能夠經過繼承重複使用 --> <resultMap id="userMap" type="user"> <id column="uid" property="uid" /> <result column="name" property="name"/> <result column="password" property="password"/> </resultMap> <!-- loginInfo結果集,繼承user結果集完整數據 --> <resultMap id="userLoginInfoMap" extends="userMap" type="user"> <!-- 使用collection映射多的一方,ofType指定集合中的數據類型 --> <collection property="loginInfos" ofType="loginInfo"> <id column="lid" property="lid"/> <result column="ip" property="ip"/> <result column="loginTime" property="loginTime"/> </collection> </resultMap> <!-- 定義查詢語句 --> <select id="findAllUsers" resultMap="userLoginInfoMap"> select * from users u left join login_infos li on u.uid = li.uid; </select>
<!-- 2.多對一,採用別名映射關聯關係 --> <select id="findAllLoginInfos" resultType="loginInfo"> select li.*, u.uid "user.uid", u.name "user.name", u.password "user.password" from login_infos li,users u where li.uid = u.uid; </select> <!-- 3.多對一,採用resultMap進行結關聯關係的映射 --> <resultMap id="userMap" type="loginInfo"> <id column="uid" property="user.uid"/> <result column="name" property="user.name"/> <result column="password" property="user.password"/> </resultMap> <!-- 這裏的resultMap繼承關係最好和POJO中的關聯關係保持一致,便於理解 這裏不一致,但也能夠運行 --> <resultMap id="userLoginInfoMap" extends="userMap" type="loginInfo"> <id column="lid" property="lid"/> <result column="ip" property="ip"/> <result column="loginTime" property="loginTime"/> </resultMap> <select id="findAllLoginInfos1" resultMap="userLoginInfoMap"> select * from users u,login_infos li where u.uid=li.uid; </select> <!-- 4.多對一,使用resultMap結合association進行關聯關係的映射 --> <resultMap id="loginInfoMap" type="loginInfo"> <id column="lid" property="lid"/> <result column="ip" property="ip"/> <result column="loginTime" property="loginTime"/> </resultMap> <!-- 這裏的resultMap繼承關係和POJO的關聯關係保持了一致,即LoginInfo下有User屬性 --> <resultMap id="loginInfoUserMap" extends="loginInfoMap" type="loginInfo"> <association property="user" javaType="user"> <id column="uid" property="uid"/> <result column="name" property="name"/> <result column="password" property="password"/> </association> </resultMap> <select id="findAllLoginInfos2" resultMap="loginInfoUserMap"> select * from users u,login_infos li where u.uid=li.uid; </select>
<!-- 13.使用多表查詢,完成多對多查詢,查找全部的用戶及其角色 --> <!-- 注意:爲了不冗餘,這裏繼承了2.2中的resultMap --> <resultMap id="userRoleMap" extends="userMap" type="user"> <collection property="roles" ofType="role"> <id column="rid" property="rid"/> <result column="name" property="name"/> <result column="description" property="description"/> </collection> </resultMap> <!-- 多對多主要在sql編寫上,須要藉助中間表 --> <select id="findAllUsers" resultMap="userRoleMap"> select * from users u left join users_roles ur on u.uid=ur.uid inner join roles r on ur.rid=r.rid; </select>
在關聯查詢時,對於關聯的一方是否查詢出來,要根據業務需求而定。不能經過編碼方式進行策略的改變,而應該經過修改配置文件改變加載策略。可使用嵌套查詢(分步查詢)。
<!-- 1.嵌套查詢,使用reultMap結合association使用引用的mapper.xml進行查詢 --> <resultMap id="loginInfoMap" type="loginInfo"> <id column="lid" property="lid"/> <result column="ip" property="ip"/> <result column="loginTime" property="loginTime"/> </resultMap> <resultMap id="loginInfoUserMap" extends="loginInfoMap" type="loginInfo"> <!-- 解決user屬性: property:屬性 column:查詢依據,也是當前查詢表的外鍵 select:指向根據外鍵查詢的xml惟一映射 --> <association property="user" column="uid" select="cn.dintalk.UserMapper.findById"/> </resultMap> <select id="findAllLoginInfos3" resultMap="loginInfoUserMap"> select * from login_infos; </select> <!-- 在association中,select指向的是另外一個Mapper.xml文件中的映射(根據命名空間和id) --> <!-- UserMapper.xml中 14.嵌套查詢之 根據uid查詢用戶 --> <select id="findById" resultType="user"> select * from users where uid = #{uid}; </select>
<!-- 2.使用嵌套查詢,可經過懶加載優化sql,查詢全部的用戶及其日誌信息 --> <!-- 爲了簡便,這裏繼承了上述的id爲userMap的resultMap --> <resultMap id="userLoginInfoMap" extends="userMap" type="user"> <collection property="loginInfos" column="uid" select="cn.dintalk.dao.LoginInfoMapper.findAllLoginInfos"/> </resultMap> <select id="findAllUser" resultMap="userLoginInfoMap1"> select * from users; </select> <!-- 同理,在collection中,select指向的是另外一個Mapper.xml文件的映射 --> <!--LoginInfoMapper.xml中 5.根據用戶id查詢全部的登陸信息 --> <select id="findAllLoginInfos" resultType="loginInfo"> select * from login_infos where uid=#{uid}; </select>
<!-- 2.配置延遲加載,即sql優化 --> <settings> <!-- 啓用懶加載策略 --> <setting name="lazyLoadingEnabled" value="true"/> <!-- 覆蓋掉延遲加載的觸發方法 --> <setting name="lazyLoadTriggerMethods" value=""/> </settings>
啓用延遲加載後,mybatis默認有toString、equals等4個觸發加載的方法。咱們也能夠將其覆蓋掉。
延遲加載,即用到關聯數據的時候再去查,不用就不查。(service層中會有不少方法調用dao方法,根據service層中的實際需求動態調整加載策略,高效利用資源!)
fetchType="lazy"(默認) | eager
lazy : 支持延遲加載
eager : 當即加載
經過 JDBC 能夠手動控制:
Connection.setAutoCommit(false); Connection.commit(); Connection.rollback();// 開啓事務後,未提交會自動回滾!
<transactionManager type="JDBC"></transactionManager>
至關於使用 JDBC 進行事務控制:(增刪改時不提交會自動回滾!)
獲取SqlSession時:SqlSessionFactory.openSession(); // 手動控制事務 ★
SqlSessionFactory.openSession(true); //自動提交事務
mybatis內置了三種數據源:
UNPOOLED :不帶有池(鏈接池)|學習時用
POOLED : 帶有池的 | 實際生產環境使用
JNDI : mybatis提供的JndiDataSourceFactory來獲取數據源
POOLED對應的是PooledDataSource數據源,PooledDataSourceFactory用來生產帶有池的數據源。
UNPOOLED對應的是UnpooledDataSource數據源,UnpooledDataSourceFactory用來生產不帶有池的數據源。
<dependency> <groupId>com.alibaba</groupId> <artifactId>druid</artifactId> <version>1.1.14</version> </dependency>
第二步:編寫工廠類,用來產生Druid的數據源,通常選擇繼承UnpooledDataSourceFactory
public class DataSourceFactory extends UnpooledDataSourceFactory{ public DataSourceFactory(){ this.dataSource = new DruidDataSource();//建立druid數據源 } }
第三步:在mybatis-config.xml中進行配置
<!--1.類別名的配置 --> <typeAliases> <typeAlias type="cn.dintalk.dataSource.DataSourceFactory" alias="DRUID"/> </typeAliases> <environments default="mysql"> <environment id="mysql"> <transactionManager type="JDBC"></transactionManager> <!-- 2.配置druid數據源 --> <dataSource type="DRUID"> <!-- 3.配置數據庫鏈接:name由數據源中的setXXX而定,value是外部配置的key --> <property name="driverClassName" value="${jdbc.driver}"></property> <property name="url" value="${jdbc.url}"></property> <property name="username" value="${jdbc.username}"></property> <property name="password" value="${jdbc.password}"></property> </dataSource> </environment> </environments>
MyBatis對緩存提供支持,可是在沒有配置的默認狀況下,它只開啓一級緩存。
一級緩存只是相對於同一個SqlSession而言的。使用SqlSession第一次查詢後,MyBatis會將其放在緩存中,之後再查詢時,若是沒有聲明須要刷新,且緩存未超時的狀況下,SqlSession都只會取出當前緩存的數據,而不會再次發送Sql到數據庫。
二級緩存是在SqlSessionFactory層面上的,能夠將緩存提供給各個SqlSession對象共享。
<settings> <!-- 二級緩存配置(默認開啓,此行可省略) --> <setting name="cacheEnabled" value="true"/> </settings>
Mapper.xml文件中
<mapper namespace="cn.dintalk.dao.UserMapper"> <cache/> <!-- 開啓二級緩存,使用默認配置 --> </mapper> <!-- 使用默認配置意味着: 映射語句文件中的全部select語句將會被緩存。 映射語句文件中的全部insert、update和delete語句會刷新緩存。 緩存使用默認的Least Recently Used(LRU,最近最少使用的)回收策略。 緩存會存1024個列表集合或對象(不管查詢方法返回什麼) 緩存會被視爲是read/write(可讀可寫)的緩存 -->
Tips:
一級緩存中存放的是對象自己,是同一個對象!
二級緩存中存放的是對象的拷貝,對象所屬類必須實現jav.io.Serializable接口!
<cache evicition="LRU" flushInterval="100000" size="1024" readOnly=true/>
eviction: 表明的是緩存回收策略,目前MyBatis提供如下策略:
(1)LRU, 最近最少使用的,移除最長時間不用的對象。
(2)FIFO, 先進先出,按對象進入緩存的順序來移除它們。
(3)SOFT,軟引用,移除基於垃圾回收器狀態和軟引用規則的對象。
(4)WEAK,弱引用,更積極地移除基於垃圾收集器狀態和弱引用規則的對象。
flushInterval: 刷新間隔時間,單位爲毫秒。
size: 引用數目,表明緩存最多能夠存儲多少個對象。
readOnly: 只讀,意味着緩存數據只讀。
系統緩存是MyBatis應用機器上的本地緩存,咱們也可使用緩存服務器來定製緩存,如比較流行的Redis緩存。咱們須要實現MyBatis爲咱們提供的接口org.apache.ibatis.cache.Cache,緩存接口簡介:
//獲取緩存編號 String getId(); //保存key值緩存對象 void putObject(Object key,Object value); //經過key獲取緩存對象 Object getObject(Object key); //經過key刪除緩存對象 Object removeObject(Object key); //清空緩存 void clear(); //獲取緩存對象大小 int getSize(); //獲取緩存的讀寫鎖 ReadWriterLock getReadWriterLock();
因爲每種緩存都有其不一樣的特色,上面的接口都須要咱們去實現。假設咱們已經有一個實現類:cn.dintalk.MyCache。則配置以下:
<cache type="cn.dintalk.MyCache"/>
完成上述配置,就能使用自定義的緩存了。MyBatis也支持在緩存中定義經常使用的屬性,如:
<cache type="cn.dintalk.MyCache"> <property name="host" value="localhost"/> </cache>
若是咱們在MyCache這個類中增長setHost(String host) 方法,那麼它在初始化的時候就會被調用,這樣咱們能夠對自定義的緩存設置一些外部參數。
Tips: 咱們也可配置Sql層面的緩存規則,來決定它們是否須要刷新或使用緩存。
<insert ...flushCache="true"/> <delete ...flushCache="true"/> <update ...flushCache="true"/> <select ...flushCache="false" useCache="true"/>
<?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> <!-- 1.引入外部的配置文件 --> <properties resource="jdbc.properties"/> <!-- 2.配置延遲加載,即sql優化 --> <settings> <!-- 啓用懶加載策略 --> <setting name="lazyLoadingEnabled" value="true"/> <!-- 覆蓋掉延遲加載的觸發方法 --> <setting name="lazyLoadTriggerMethods" value=""/> <!-- 二級緩存配置(默認開啓,此行可省略) --> <!-- 使用二級緩存,在對應的mapper.xml中加入cache便可 --> <!--<setting name="cacheEnabled" value="true"/>--> </settings> <!-- 3.類別名的配置 --> <typeAliases> <!-- 單個類的配置 --> <!--<typeAlias type="cn.dintalk.domain.User" alias="user"/>--> <!-- 配置druid數據源工廠類別名 --> <typeAlias type="cn.dintalk.dataSource.DataSourceFactory" alias="DRUID"/> <!-- 給包中全部的類配置默認別名, 即類名首字母小寫--> <package name="cn.dintalk.domain"/> </typeAliases> <!-- 4.使用默認的環境配置(能夠是多個) --> <environments default="mysql"> <environment id="mysql"> <!-- 事務管理器,此處配置 爲JDBC --> <transactionManager type="JDBC"></transactionManager> <!-- 數據源配置,此處配置爲 POOLED--> <!--<dataSource type="POOLED">--> <!-- 配置druid數據源 --> <dataSource type="DRUID"> <!-- 配置數據庫鏈接:name由數據源中的setXXX而定,value是外部配置的key --> <property name="driverClassName" value="${jdbc.driver}"></property> <property name="url" value="${jdbc.url}"></property> <property name="username" value="${jdbc.username}"></property> <property name="password" value="${jdbc.password}"></property> </dataSource> </environment> </environments> <!-- 5.註冊映射文件 --> <mappers> <!-- 指定資源文件路徑 --> <!--<mapper resource="cn/dintalk/dao/UserMapper.xml"></mapper>--> <!--<mapper resource="cn/dintalk/dao/LoginInfoMapper.xml"></mapper>--> <!-- 基於Mapper接口的開發:指定類名--> <!--<mapper class="cn.dintalk.dao.UserMapper"/>--> <!-- 指定基於Mapper接口開發的包:(需類名和xml文件名一致,包名一致)--> <package name="cn.dintalk.dao"/> </mappers> </configuration>
<?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="cn.dintalk.dao.UserMapper"> <cache/> <!-- 開啓二級緩存 --> </mapper>