天天看點

mybatis自學入門執行個體(一)

mybatis 是什麼?

MyBatis 是支援普通SQL 查詢,存儲過程和進階映射的優秀持久層架構。MyBatis  消除了幾乎所有的JDBC 代碼和參數的手工設定以及結果集的檢索。MyBatis  使用簡單的XML或注解用于配置和原始映射,将接口和Java 的POJOs(Plan  Old  Java  Objects ,普通的Java對象)映射成資料庫中的記錄。
           

入門

每一個 MyBatis 的應用程式都以一個 SqlSessionFactory對象的執行個體為核心。

SqlSessionFactory 對象的執行個體可以通SqlSessionFactoryBuilder對象來獲得。SqlSessionFactoryBuilder 對象可以從XML 配置檔案,或從Configuration 類的習慣準備的執行個體中建構SqlSessionFactory 對象。

從XML 中建構SqlSessionFactory從XML 檔案中構SqlSessionFactory 的執行個體非常簡單。這裡建議你使用類路徑下的資源檔案來配置,但是你可以使用任意的Reader 執行個體,這個執行個體包括由文字形式的檔案路徑或URL 形式的檔案路徑file://來建立。MyBatis 包含了一些工具類,稱作為資源,這些工具

類包含一些方法,這些方法使得從類路徑或其他位置加載資源檔案更加簡單。

String resource = "org/mybatis/example/Configuration.xml";  

Reader reader = Resources.getResourceAsReader(resource);  

sqlMapper = new SqlSessionFactoryBuilder().build(reader);  
           

XML 配置檔案包含對MyBatis 系統的核心設定,包含擷取資料庫連接配接執行個體的資料源和

決定事務範圍和控制的事務管理器。

入門執行個體

## mybatis配置檔案 ##
           
<?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>
    <properties resource="db.properties" /> 
    <environments default="development">
        <environment id="development">
            <transactionManager type="JDBC" />
            <dataSource type="POOLED">
                <property name="driver" value="${driver}" />
                <property name="url" value="${url}" />
                <property name="username" value="${user}" />
                <property name="password" value="${password}" />
            </dataSource>
        </environment>
    </environments>
    <mappers>
        <mapper resource="com/StudentMapper.xml" />//對應的POJO對象的配置檔案配置在這裡
    </mappers>
</configuration>
           
## JDBC配置檔案 ##
           
driver=com.mysql.jdbc.Driver
url=jdbc:mysql://localhost:/testmybaties
user=root
password=root
           
## Student pojo對象類 ##
           
package com;
public class Student {
    private Integer id;
    private String name;
    private Integer age;
    private Integer tid;
    public Student(String name, Integer age, Integer tid) {
        this.name = name;
        this.age = age;
        this.tid = tid;
    }
    public Integer getId() {
        return id;
    }
    public void setId(Integer id) {
        this.id = id;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public Integer getAge() {
        return age;
    }
    public void setAge(Integer age) {
        this.age = age;
    }
    public Integer getTid() {
        return tid;
    }
    public void setTid(Integer tid) {
        this.tid = tid;
    }
    @Override
    public String toString() {
        return "Student [id=" + id + ", name=" + name + ", age=" + age
                + ", tid=" + tid + "]";
    }
}
           
##StudentMapper.xml檔案##
           
<?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.StudentMapper">
    <insert id="insertStudent" parameterType="com.Student">
        <!-- insert into STUDENT values(se_Student.nextval,#{name},#{age},#{tid}) -->
        insert into STUDENT(name,age,tid) values(#{name},#{age},#{tid})
    </insert>
</mapper> 
           
## 測試類Test.java ##
           
package pojo;

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.Student;

public class Test {
    public static void main(String[] args) throws IOException {
        String resource = "sqlConfig.xml";
        Reader reader = Resources.getResourceAsReader(resource);
        SqlSessionFactory sqlMapper = new SqlSessionFactoryBuilder().build(reader);
        SqlSession session = sqlMapper.openSession();
        Student student = new Student("lilei",,);
        session.insert("com.StudentMapper.insertStudent", student);
        session.commit();
    }
}
           

繼續閱讀