Mybatis介紹
MyBatis是一個支持普通SQL查詢,存儲過程和高級映射的優秀持久層框架。MyBatis消除了幾乎所有的JDBC代碼和參數的手工設置以及對結果集的檢索封裝。MyBatis可以使用簡單的XML或註解用於配置和原始映射,將接口和Java的POJO(Plain Old Java Objects,普通的Java對象)映射成數據庫中的記錄。
詳細瞭解請觀看:http://www.mybatis.org/mybatis-3/
通過一個實例我們來了解Mybatis主配置文件及應用
mybatis-3.3.0.jar下載路徑:https://github.com/mybatis/mybatis-3/releases
1.主配置文件batis.xml:
<?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>
<environments default="development">
<environment id="development">
<transactionManager type="JDBC" />
<!-- 數據源配置 -->
<dataSource type="POOLED">
<!-- 數據庫驅動 -->
<property name="driver" value="com.mysql.jdbc.Driver" />
<!-- 數據庫連接URL-->
<property name="url" value="jdbc:mysql://localhost:3306/mysqljdbc" />
<!-- 數據庫用戶名和密碼 -->
<property name="username" value="root" />
<property name="password" value="" />
</dataSource>
</environment>
</environments>
<!-- 配置實體映射文件,用於數據庫CRUD
<mappers>
<mapper resource="org/iflysse/training/ibatis/biz/mapper/familyMapper.xml" />
</mappers>
-->
</configuration>
2.測試類BatisTest :
package org.iflysse.test;
import java.io.IOException;
import java.io.InputStream;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
public class BatisTest {
public static void main(String[] args) {
// 進行MyBatis主配置文件的設置
String resource = "batis.xml";
InputStream inputStream = null;
try {
//javaSE類記載器,讀取MyBatis的配置文件,獲取數據庫的連接信息
//使用MyBatis提供的Resources類加載mybatis的配置文件(它也加載關聯的映射文件)
//1.通常使用流的形式,讀取這個batis.xml
inputStream = Resources.getResourceAsStream(resource);
/*2.文件讀取
File file=new File("src/batis.xml");
Reader reader=new FileReader(file);
SqlSessionFactory sqlSessionfactory = new SqlSessionFactoryBuilder().build(reader);
*/
} catch (IOException e) {
e.printStackTrace();
}
//使用SessionFactory創建映射對象,進行數據查詢
//2-MyBatis正式工作:構建sqlsession工廠 ,類似之前JDBC的連接Connection
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder()
.build(inputStream);
System.out.println("主配置文件已經加載完畢..." + sqlSessionFactory);
//創建會話對象,並使用她來進行數據庫的各項 操作
SqlSession session = sqlSessionFactory.openSession();
System.out.println("創建會議對象" + session);
}
}
3.控制檯輸出結果:
WARNING: An illegal reflective access operation has occurred
WARNING: Illegal reflective access by org.apache.ibatis.reflection.Reflector (file:/D:/Users/Administrator/eclipse-workspace/MyBatis/lib/mybatis-3.3.0.jar) to method java.lang.Class.checkPackageAccess(java.lang.SecurityManager,java.lang.ClassLoader,boolean)
WARNING: Please consider reporting this to the maintainers of org.apache.ibatis.reflection.Reflector
WARNING: Use --illegal-access=warn to enable warnings of further illegal reflective access operations
WARNING: All illegal access operations will be denied in a future release
主配置文件已經加載完畢...org.apache.ibatis.session.defaults.DefaultSqlSessionFactory@40e6dfe1
創建會議對象org.apache.ibatis.session.defaults.DefaultSqlSession@50a7bc6e
主配置文件優化:
其實我們完全可以將數據庫的連接配置信息寫在一個properties文件中,然後在batis.xml文件中引用properties文件,具體做法如下:
1、在src目錄下新建一個db.properties文件,如下圖所示:
在db.properties文件編寫連接數據庫需要使用到的數據庫驅動,連接URL地址,用戶名,密碼,如下:
driver=com.mysql.jdbc.Driver
url=jdbc:mysql://localhost:3306/mysqljdbc
name=root
password=
2、在MyBatis的batis.xml文件中引用db.properties文件,如下:
<?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>
<!-- 引用db.properties配置文件 -->
<properties resource="db.properties"/>
<!--
development : 開發模式
work : 工作模式
-->
<environments default="development">
<environment id="development">
<transactionManager type="JDBC" />
<!-- 配置數據庫連接信息 -->
<dataSource type="POOLED">
<!-- value屬性值引用db.properties配置文件中配置的值 -->
<property name="driver" value="${driver}" />
<property name="url" value="${url}" />
<property name="username" value="${name}" />
<property name="password" value="${password}" />
</dataSource>
</environment>
</environments>
</configuration>