1. 程式人生 > >MyBatis經典入門例項

MyBatis經典入門例項

週末學習了MyBatis開源框架,MyBatis是由原來的iBatis改名而來,目前已近釋出了3.0.1版本。可以在官方網站http://www.mybatis.org下載

MyBatis作為持久層框架,其主要思想是將程式中的大量sql語句剝離出來,配置在配置檔案中,實現sql的靈活配置。這樣做的好處是將sql與程式程式碼分離,可以在不修改程式程式碼的情況下,直接在配置檔案中修改sql。下面給個簡單的入門例子。

下面的例子實現從資料庫中查詢商品表(Goods)中id為1的商品,並打印出商品名稱。

資料庫建表指令碼如下:

DROP TABLE GOODS;

CREATE TABLE GOODS(

ID INT PRIMARY KEY,

CATE_ID INT,

NAME VARCHAR(50),

PRICE DECIMAL(16,2),

DESCRIPTION VARCHAR(100),

ORDER_NO INT,

UPDATE_TIME TIMESTAMP

);

資料庫初始化指令碼:

INSERT INTO GOODS(ID,CATE_ID,NAME,PRICE,DESCRIPTION,ORDER_NO,UPDATE_TIME) VALUES (1,1,'諾基亞N85',3010,'內建RealPlayer播放器',1,CURRENT_TIMESTAMP);

INSERT INTO GOODS(ID,CATE_ID,NAME,PRICE,DESCRIPTION,ORDER_NO,UPDATE_TIME) VALUES (2,1,'金立 A30',2000,'標準鋰電池兩塊',2,CURRENT_TIMESTAMP);

一、configuration.xml配置檔案

首先在工程中匯入mybatis-3.0.1.jar包。然後編寫configuration.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>

<typeAliases>

<typeAlias alias="Goods" type="com.oryx.mybatis.Goods"
/> 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://localhost:3306/test"/> <property name="username" value="root"/> <property name="password" value="root"/> dataSource> environment> environments> <mappers> <mapper resource="com/oryx/mybatis/GoodsMapper.xml"/> mappers> configuration>

二、Mapper.xml配置檔案

接著編寫GoodsMapper.xml配置檔案。Mapper配置檔案主要是實現POJO類和sql之間的對映。

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="com.oryx.mybatis.GoodsMapper">

<select id="selectGoods" parameterType="int" resultType="Goods">

select * from Goods where id = #{id}

select>

mapper>

其中#{id}是需要傳入的引數,parameterType是引數的型別,resultType是查詢返回的結果類。這地方的Goods是一個別名,可以在configuration.xml檔案中找到它對應的具體類。

由此可知查詢結果集將儲存在com.oryx.mybatis.Goods中返回。

三、Goods類

在工程中新建com.oryx.mybatis.Goods.java類。

package com.oryx.mybatis;

import java.sql.Timestamp;

public class Goods {

private String id;

private String cateId;

private String name;

private double price;

private String description;

private int orderNo;

private Timestamp updateTime;

/**

* @return the goodsid

*/

public String getId() {

return id;

}

/**

* @param goodsid the goodsid to set

*/

public void setId(String id) {

this.id = id;

}

/**

* @return the cateId

*/

public String getCateId() {

return cateId;

}

/**

* @param cateId the cateId to set

*/

public void setCateId(String cateId) {

this.cateId = cateId;

}

/**

* @return the name

*/

public String getName() {

return name;

}

/**

* @param name the name to set

*/

public void setName(String name) {

this.name = name;

}

/**

* @return the price

*/

public double getPrice() {

return price;

}

/**

* @param price the price to set

*/

public void setPrice(double price) {

this.price = price;

}

/**

* @return the description

*/

public String getDescription() {

return description;

}

/**

* @param description the description to set

*/

public void setDescription(String description) {

this.description = description;

}

/**

* @return the orderNo

*/

public int getOrderNo() {

return orderNo;

}

/**

* @param orderNo the orderNo to set

*/

public void setOrderNo(int orderNo) {

this.orderNo = orderNo;

}

/**

* @return the updateTime

*/

public Timestamp getUpdateTime() {

return updateTime;

}

/**

* @param updateTime the updateTime to set

*/

public void setUpdateTime(Timestamp updateTime) {

this.updateTime = updateTime;

}

}

四、測試用例

package com.oryx.mybatis;

import java.io.IOException;

import java.io.Reader;

import java.sql.SQLException;

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 TestMyBatis {

public static void main(String[] args) throws SQLException, IOException{

String resource = "com/oryx/mybatis/configuration.xml";

Reader reader = Resources.getResourceAsReader(resource);

SqlSessionFactory sessionFactory = new SqlSessionFactoryBuilder().build(reader);

SqlSession session = sessionFactory.openSession();

try{

Goods goods = (Goods)session.selectOne("com.oryx.mybatis.GoodsMapper.selectGoods",1);

System.out.println("good name:"+goods.getName());

}finally{

session.close();

}

}

}
例項原始碼下載