MyBatis框架學習(一)——mbatis的介紹
MyBatis介紹
MyBatis本是apache的一個開源專案iBatis,2010年這個專案由apache software foundation遷移到了google code,並且改名為MyBatis。2013年11月遷移到Github。MyBatis是一個優秀的持久層框架,它對jdbc的操作資料庫的過程進行封裝,使開發者只需要關注SQL本身,而不需要花費精力去處理例如註冊驅動、建立connection、建立statement、手動設定引數、結果集檢索等jdbc繁雜的過程程式碼。
Mybatis通過xml或註解的方式將要執行的各種statement(statement、preparedStatemnt、CallableStatement)配置起來,並通過java物件和statement中的sql進行對映生成最終執行的sql語句,最後由mybatis框架執行sql並將結果對映成java物件並返回。
使用JDBC程式設計問題總結
介紹完MyBatis之後,我們再來看看使用傳統的JDBC程式設計所帶來的問題。
建立mysql資料庫
在這兒我使用圖形化工具——Navicat for MySQL來建立一個mybatis的資料庫,步驟如下:
- 新建一個連線——localhost_3306,右鍵,在彈出的下拉框中選中【新建資料庫】
- 在彈出的對話方塊中,輸入資料庫名,選擇字符集
然後將如下sql指令碼檔案——mybatis.sql匯入到mybatis資料庫中:
/*
Navicat MySQL Data Transfer
Source Server : localhost_3306
Source Server Version : 50521
Source Host : localhost:3306
Source Database : mybatis
Target Server Type : MYSQL
Target Server Version : 50521
File Encoding : 65001
Date: 2015-04-09 16:03:53
*/
SET FOREIGN_KEY_CHECKS=0;
-- ----------------------------
-- Table structure for `orders`
-- ----------------------------
DROP TABLE IF EXISTS `orders`;
CREATE TABLE `orders` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`user_id` int(11) NOT NULL COMMENT '下單使用者id',
`number` varchar(32) NOT NULL COMMENT '訂單號' ,
`createtime` datetime NOT NULL COMMENT '建立訂單時間',
`note` varchar(100) DEFAULT NULL COMMENT '備註',
PRIMARY KEY (`id`),
KEY `FK_orders_1` (`user_id`),
CONSTRAINT `FK_orders_id` FOREIGN KEY (`user_id`) REFERENCES `user` (`id`) ON DELETE NO ACTION ON UPDATE NO ACTION
) ENGINE=InnoDB AUTO_INCREMENT=6 DEFAULT CHARSET=utf8;
-- ----------------------------
-- Records of orders
-- ----------------------------
INSERT INTO `orders` VALUES ('3', '1', '1000010', '2015-02-04 13:22:35', null);
INSERT INTO `orders` VALUES ('4', '1', '1000011', '2015-02-03 13:22:41', null);
INSERT INTO `orders` VALUES ('5', '10', '1000012', '2015-02-12 16:13:23', null);
-- ----------------------------
-- Table structure for `user`
-- ----------------------------
DROP TABLE IF EXISTS `user`;
CREATE TABLE `user` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`username` varchar(32) NOT NULL COMMENT '使用者名稱稱',
`birthday` date DEFAULT NULL COMMENT '生日',
`sex` char(1) DEFAULT NULL COMMENT '性別',
`address` varchar(256) DEFAULT NULL COMMENT '地址',
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=27 DEFAULT CHARSET=utf8;
-- ----------------------------
-- Records of user
-- ----------------------------
INSERT INTO `user` VALUES ('1', '王五', null, '2', null);
INSERT INTO `user` VALUES ('10', '張三', '2014-07-10', '1', '北京市');
INSERT INTO `user` VALUES ('16', '張小明', null, '1', '河南鄭州');
INSERT INTO `user` VALUES ('22', '陳小明', null, '1', '河南鄭州');
INSERT INTO `user` VALUES ('24', '張三丰', null, '1', '河南鄭州');
INSERT INTO `user` VALUES ('25', '陳小明', null, '1', '河南鄭州');
INSERT INTO `user` VALUES ('26', '王五', null, null, null);
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
匯入步驟記錄如下:
- 右鍵mybatis資料庫,在彈出的下拉框中選中【執行SQL檔案】
- 在彈出的對話方塊中選中mybatis.sql指令碼檔案,點選【開始】按鈕開始匯入sql指令碼檔案
建立一個普通的Java工程
首先建立一個普通的Java工程,例如mybatis-day01。然後匯入mysql的資料庫驅動Jar包。
編寫JDBC程式
大家還記得JDBC程式設計的步驟嗎?
- 載入資料庫驅動
- 建立並獲取資料庫連線
- 建立JDBC Statement物件
- 設定sql語句
- 設定sql語句中的引數(使用PreparedStatement)
- 通過Statement執行sql並獲取結果
- 對sql執行結果進行解析處理
- 釋放資源(ResultSet、PreparedStatement、Connection)
接下來就要按以上步驟來編寫編寫JDBC程式了,首先在src目錄下新建一個名為cn.itheima.mybatis.jdbc的包,然後在該包下編寫一個JdbcTest類:
public class JdbcTest {
public static void main(String[] args) {
Connection connection = null;
PreparedStatement preparedStatement = null;
ResultSet resultSet = null;
try {
//載入資料庫驅動
Class.forName("com.mysql.jdbc.Driver");
//通過驅動管理類獲取資料庫連結
connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/mybatis?characterEncoding=utf-8", "root", "yezi");
//定義sql語句,?表示佔位符
String sql = "select * from user where username = ?";
//獲取預處理statement
preparedStatement = connection.prepareStatement(sql);
//設定引數,第一個引數為sql語句中引數的序號(從1開始),第二個引數為設定的引數值
preparedStatement.setString(1, "王五");
//向資料庫發出sql執行查詢,查詢出結果集
resultSet = preparedStatement.executeQuery();
//遍歷查詢結果集
while(resultSet.next()){
System.out.println(resultSet.getString("id")+" "+resultSet.getString("username"));
}
} catch (Exception e) {
e.printStackTrace();
}finally{
//釋放資源
if(resultSet!=null){
try {
resultSet.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if(preparedStatement!=null){
try {
preparedStatement.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if(connection!=null){
try {
connection.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
以上使用JDBC的原始方法(未經封裝)實現了查詢資料庫表記錄的操作。
執行以上程式,Eclipse控制檯會列印:
通過觀察以上程式程式碼,使用JDBC程式設計所帶來的問題呼之欲出:
問題總結:
1.資料庫連線問題,使用時就建立,不適用立即釋放,對資料庫進行頻繁連線開啟和關閉
造成資料庫資源浪費,影響資料庫效能。
2 將sql語句硬編碼到java程式碼中,如果sql語句修改,需要重新編譯java程式碼,不利於系統
維護。 設想:將sql語句配置在xml配置檔案中,即使sql變化,不需要對java程式碼進行重新編譯
3 向preparedStatement中設定引數,對佔位符號位置和設定引數值,硬編碼在java中,
不利於維護。
設想:將sql語句及佔位符和引數全部配置在xml中。
4 從resultSet中便利結果集資料時,存在硬編碼,將獲取表的欄位進行硬編碼,不利於烯烴維護。