天天看點

Mybatis

介紹:

        它是一個插件,是一個持久化插件

        幫我們簡化資料庫的操作

        mybatis的好處就是可以自定義sql讓sq變得很靈活

工程建立: mybatis-config.xml(這裡是檔案名随你起)  是mybatis的資料庫和事務配置檔案

                  資料庫連接配接資訊

                  事務管理 

                  mapper 檔案管理

每多一張表 就需要以下配置:

                                           StudentMapper.xml  是針對student表做出的sql配置xml檔案 //映射檔案配置,資料庫相關操作都在這個檔案中

                                           Student  類 是用來包裝student表格的 POJO類  //對應的是你要操作的表,對應表中的相應資料

                                           StudentMapper 接口 是對應studentMapper.xml 的功能接口 //實作 POJO類的接口

資料庫連接配接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>
    <environments default="test">
        <environment id="test">
            <!--這裡是你要連接配接的資料庫,對應的你要連結的資料庫-->
            <transactionManager type="JDBC"/>
            <!--事務管理方式-->
            <!--在 MyBatis 中有兩種類型的事務管理器(也就是 type=”[JDBC|MANAGED]”):-->
            <!--JDBC – 這個配置就是直接使用了 JDBC 的送出和復原設定,它依賴于從資料源得到的連接配接來管理事務作用域。-->
            <!--MANAGED – 容器來管理事務的整個生命周期-->
            <dataSource type="POOLED">
                <!--資料源 [UNPOOLED|POOLED|JNDI] -->
                <!--UNPOOLED 不用資料連結池  每次通路資料 從新建立connection對象-->
                <!--POOLED 使用連接配接池-->
                <!--JNDI 容器提供的資料源-->
                <property name="driver" value="com.mysql.jdbc.Driver"/>
                <property name="url" value="jdbc:mysql://localhost:3306/test?useUnicode=true&
                characterEncoding=UTF-8&useSSL=false"/>
                <!--這裡是mysq5的連結方案-->
                <property name="username" value="root"/>
                <property name="password" value=""/>
                <!--這裡是資料員配置,要配置相關參數的話就加一列加入相關配置就行-->
            </dataSource>
        </environment>
    </environments>

    <mappers>
        <!--映射檔案就是資料庫的查詢,修改,更新,删除等自定義執行方案-->
        <!--關聯映射檔案  每次多一個映射檔案 就需要寫段-->
        <mapper resource="com/sun/mybatis/dao/MyabtisMapper.xml"/>
    </mappers>
</configuration>           

資料庫連接配接方法包裝:

package com.sun.mybatis;

import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

import java.io.IOException;
import java.io.InputStream;

public class Mybatisconfig {
    public static SqlSessionFactory sqlSessionFactory;

    //這裡用靜态方法的原因是因為,用不了那麼多連結一個就夠了是以設定為靜态類
    static {
        String resource = "com/sun/mybatis/mybatis_config";
        //用靜态塊是因為隻要掉用類,就會啟動執行個體塊中的代碼
        try {
            InputStream inputStream = Resources.getResourceAsStream(resource);
            sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
           

對應資料庫表結構的類

package com.sun.mybatis;
//用get 和set方法包裝即可
public class Student {
    private int sid;
    private String sname;
    private String address;
    private String email;
}
           

mybatis自定義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.sun.mybatis.dao.StudentMappr">
    <!--這裡就是映射檔案-->
    <select id="selectUserAll"  resultType="com.sun.mybatis.Student">
    select * from student
  </select>

<insert id="inster" parameterType="com.sun.mybatis.Student">
    insert into student(sname,address,email) values(#{sname},#{address},#{email})
</insert>
</mapper>           

映射檔案對應的接口實作類:

package com.sun.mybatis.dao;

import com.sun.mybatis.Student;

import java.util.List;

public interface StudentMappr {
    public Student inste(int id);

    public int inster(Student student);
    //這裡傳進來的是對象,因為插入是資料庫相對應的資料
    //這裡注意發揮類型和參數
    public List<Student> selectUserAll();
    //查詢放回值傳回的是對象,因為有資料庫相對應的對象
    //這裡的方法是集合查詢 也就是可以多查
    // 這裡的Student代表傳回出去的是都是Student對象
}
           

資料操作:

package com.sun.mybatis;

import com.sun.mybatis.dao.StudentMappr;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;

import java.util.List;

public class Teat {
    public static void main(String[] args) {
        SqlSessionFactory sqlSessionFactory = Mybatisconfig.sqlSessionFactory;
        //建立sql繪畫工廠對象
        SqlSession sqlSession = sqlSessionFactory.openSession();
        //将會話工廠類給sqlsession工廠
        StudentMappr studentMappr = sqlSession.getMapper(StudentMappr.class);
        //建立動态實作類
        //mybatis的運作過程,應該是
        //1.建立工廠連結
        //2.通過工廠sqlsession工廠類 中的方法在記憶體建立動态實作類
        //3.通過映射檔案找到相對應的實作類
        //4.再通過實作類的名字找到相對應的實作方法,能找見方法是因為配置的映射中的标記名對應着的是實作類的方法名
        List<Student> studentList=studentMappr.selectUserAll();
        //這裡是集合聲明也是,集合相當于進階的數組是以也是同過循環取值
        for (Student student : studentList) {
            System.out.println(student.toString());
        }
    }
}