1. 程式人生 > >MyBatis入門基礎專案搭建

MyBatis入門基礎專案搭建

MyBatis 本是apache的一個開源專案iBatis, 2010年這個專案由apache software foundation 遷移到了google code,並且改名為MyBatis 。
SSM框架是javaweb的基礎結構之一 最近在學習mybatis的專案搭建 總結一下過程 寫下這篇博文
這是專案完成後的結構目錄
專案完成後的結構目錄
首先建立javaweb專案 正常走流程就可以
接下來匯入專案所需要的jar包
mybatis是基於資料庫的操作 所以建立資料庫必不可少
建立資料庫
這裡採用了我原有的資料庫

根據建立的資料庫 欄位 建立相關實體類 此步驟與hibernate的過程類似

package com.l.entity;

public class Student {
    private int pid;
    private String name;
    private int sex;

    public int getPid() {
        return pid;
    }

    public void setPid(int pid) {
        this.pid = pid;
    }

    public String getName() {
        return name;
    }

    public
void setName(String name) { this.name = name; } public int getSex() { return sex; } public void setSex(int sex) { this.sex = sex; } public Student() { super(); // TODO Auto-generated constructor stub } public Student(int pid, String name, int
sex) { super(); this.pid = pid; this.name = name; this.sex = sex; } }

建立實體類對應的對映檔案 student.xml

mybatis把sql語句的編寫放在對映檔案之中 就是與hibernate最大的區別
自己編寫sql語句最大的好處就是能優化sql查詢的效率
但在簡單資料操作中就沒有這種優勢 mybatis的對映檔案的編寫給我一種寫方法的感覺 在DAO層呼叫時就如同呼叫函式一般 既可以傳入引數 也可以直接無參呼叫

<?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="Student">
    <select id="selectStudent" parameterType="int" resultType="Student">
        SELECT * FROM person1 WHERE pid = #{pid}
    </select>
    <select id="selectStudents" resultType="Student">
        SELECT * FROM person1
    </select>
</mapper>

下面編寫mybatis配置檔案 configuration.xml

材料都已經準備好 開始下鍋 選單就是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>
        <!-- 給實體類取別名,方便呼叫;否則在student.xml中的resultType需要寫成com.l.entity.Student-->
        <typeAlias alias="Student" type="com.l.entity.Student"></typeAlias>
    </typeAliases>
    <environments default="development">
        <environment id="development">
            <transactionManager type="JDBC"></transactionManager>
            <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="1234" />
            </dataSource>
        </environment>
    </environments>
    <!-- 載入對映檔案路徑 -->
    <mappers>
        <mapper resource="student.xml" />
    </mappers>
</configuration>

這樣整個框架就基本搭建完成了 現在開始呼叫
建立com.l.dao.StudentDao來完成呼叫

package com.l.dao;

import java.io.IOException;
import java.io.Reader;
import java.util.List;

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

public class StudentDao {
    public static void main(String[] args) throws IOException {
        //載入配置檔案
        String sqlConfig = "configuration.xml";
        Reader reader = Resources.getResourceAsReader(sqlConfig);
        //建立sqlsessionFactoryBuilder來建立SqlSession
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(reader);
        SqlSession sqlSession = sqlSessionFactory.openSession();
        //呼叫對映檔案中的selectStudent
        Student s = sqlSession.selectOne("selectStudent", 1);
        System.out.println(s.getPid() + s.getName());
        List<Student> list = sqlSession.selectList("selectStudents");
        for (Student student : list) {
            System.out.println(student.getPid() + student.getName());
        }
    }
}

最終的結果顯示成功 希望可以幫助到你
最終效果圖