mybatis作為ORM輕量級框架一出現就吸引了無數人的眼球,比hibernate要簡單且入門較容易,下面開始我的第一個mybatis程序。
一、下載mybatis的包
我們知道任何一個框架都會有其包,我們從其官方網站下載其包,官網網址為:http://www.mybatis.org/mybatis-3/,我這里使用的版本為3.3.0。下載完成之后解壓可看到如下的目錄結構:
mybatis-3.3.0.jar是其包,lib目錄下是其依賴包,我們把這些包放到我們的項目中。我這里創建的是javaweb項目,方便以后做web測試,編寫的程序是普通的java程序。
二、配置環境
把mybatis的包放到項目的lib目錄下之后,接下來時配置mybatis的環境,我們知道mybatis做為ORM框架,屬于開發中的DAO層,是和數據庫打交道的,所以我們必須有數據,這里以mysql數據為例,具體的建庫和建表這里不闡述。
在src目錄下創建mybatis的配置文件,文件名稱為:configuratin.xml,文件內容如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
|
<?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 alias= "Message" type= "com.cn.imooc.entity.Message" /> </typeAliases> <environments default = "development" > <environment id= "development" > <transactionManager type= "JDBC" /> <dataSource type= "POOLED" > <property name= "driver" value= "com.mysql.jdbc.Driver" /> <property name= "url" value= "jdbc:mysql://127.0.0.1:3306/weixin?useUnicode=true&characterEncoding=UTF-8" /> <property name= "username" value= "root" /> <property name= "password" value= "123456" /> </dataSource> </environment> </environments> <!--映射文件--> <mappers> <mapper resource= "com/cn/mappers/message.xml" /> </mappers> </configuration> |
在mybatis配置文件中還有很多的配置項這里僅僅使用了這幾個,
<typeAliases> 別名配置,即把實體類做個別名,目的在于在映射文件中使用實體類時不使用全限類名,而是使用別名,起到簡單的作用
<environments> 配置了一些環境 比如數據配置,這里我們配置了數據源
<mappers> 配置映射文件,這里配置了com.cn.mappers包下的message.xml映射文件
下面對Message實體類進行說明,此實體類里是一些屬性,如下,
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
|
package com.cn.imooc.entity; public class Message { private String id; private String command; private String description; private String comment; public String getId() { return id; } public void setId(String id) { this .id = id; } public String getCommand() { return command; } public void setCommand(String command) { this .command = command; } public String getDescription() { return description; } public void setDescription(String description) { this .description = description; } public String getComment() { return comment; } public void setComment(String comment) { this .comment = comment; } @Override public String toString() { return "Message [id=" + id + ", command=" + command + ", description=" + description + ", comment=" + comment + "]" ; } } |
提供了getXXX和setXXX方法,其中setXXX方法很關鍵,我這里的屬性和數據庫的字段名是一致,可以方便使用mybatis查詢出結果之后反射到實體類中,當然也可以和數據庫表字段名不一致,后續會進行說明。
message.xml映射文件如下,
1
2
3
4
5
6
7
8
9
10
11
12
|
<mapper namespace= "com.cn.inter.IMessageOperation" > <select id= "selectUserByID" parameterType= "int" resultType= "com.cn.imooc.entity.Message" > select * from `message` where id = #{id} </select> <select id= "selectMessages" resultType= "Message" > select id, command, description, comment from message; </select> </mapper> |
這是我的mapper映射文件,里邊有兩個方法,一個是:selectUserById 根據id查詢,另一個是selectMessages 查詢所有
好了,到此為止,我們的mybatis的環境搭建完成,下面可以進行測試了。
三、測試
下面是測試代碼,
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
|
package com.cn.test; import java.io.IOException; import java.io.Reader; import org.apache.ibatis.io.Resources; import org.apache.ibatis.session.SqlSession; import org.apache.ibatis.session.SqlSessionFactory; import org.apache.ibatis.session.SqlSessionFactoryBuilder; import com.cn.imooc.entity.Message; public class MyTest { public static void main(String[] args) { // TODO Auto-generated method stub Reader reader; SqlSession sqlSession= null ; try { //1、獲得sqlSessionFactory reader = Resources.getResourceAsReader( "Configuration.xml" ); SqlSessionFactory sqlSessionFactory= new SqlSessionFactoryBuilder().build(reader); //2、獲得sqlSession sqlSession=sqlSessionFactory.openSession(); //3、查詢 Message message=sqlSession.selectOne( "com.cn.inter.IMessageOperation.selectUserByID" , 1 ); System.out.println(message); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } finally { sqlSession.close(); } } } |
從上面可以看出,首先需要一個SqlSessionFactory,然后由sqlSessionFactory獲得sqlSession,由sqlSession執行查詢,使用了selectOne方法,第一個參數是映射文件中的nameSpace+“.”方法名,第二個參數是查詢參數。
以上所述是小編給大家介紹的MyBatis如何使用(一)的全部敘述,希望對大家有所幫助。后續還會介紹別的版本,更多內容敬請關注服務器之家!
原文鏈接:http://www.cnblogs.com/teach/p/5698956.html