Mybatis基礎

Mybatis是簡化和實現了 Java 數據持久化層(persistence layer)的開源框架,它抽象了大量的JDBC冗餘代碼,並提供了一個簡單易用的API和數據庫交互。要使用這個框架首先就要導入mybatis的架包,而mybatis底層是封裝JDBC的方法,所以還需要導入Jdbc的架包,當導入了架包,就可以開始mybaits的配置了。
首先是一個mybatis的配置文件,類型爲下
注意:名字必須爲mybatis-config.xml,位置默認在src下

<?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="Student" type="pojo.Student" /> 
              </typeAliases> -->
              <typeAliases>
              <!-- 這個元素表示你要使用的類的別名,有兩種方法表示,一種就是上面alias=****,type=     前者表示別名,後者表示類在工程中的位置。第二種就是直接pack name =包名,直接爲包內所有類起好別名,默認是去掉路徑的類名 -->
                <!--   <package name="pojo"/>--> 
                  <package name="many2many"/>
              </typeAliases>
        <environments default="development"> 
                <environment id="development"> 
                  <transaction Manager type="JDBC" />  指定使用的底層實現轉化工具,這裏使用的是JDBC
                   <dataSource type="POOLED">
                    <property name="driver" value="oracle.jdbc.driver.OracleDriver" /> 
                    <property name="url" value="jdbc:oracle:thin:@127.0.0.1:1521:XE" /> 
                    <property name="username" value="test" /> 
                    <property name="password" value="test" /> 
                  </dataSource> 
                </environment> 
         </environments> 
        <mappers> 
                <!-- 這個表示映射的配置文件位置 -->
                <mapper resource="com/briup/pojo/StudentMapper.xml" /> 
        </mappers> 
</configuration> 

然後就是建立一個pojo類,我這裏是建立一個Student類,添加屬性和get,set方法。然後建立一個接口,接口是來映射pojo類,並通過配置映射接口的xml來表示兩者的關係,mybatis會自動幫我們封裝。

public interface StudentMapper {
/*
 * 描述對象的映射關係
 * 增加學生
 */
    public void addStudent(Student s);
    public List<Student> selectStudentAll();
    public Student selectIDStudent(Integer i);
    public void deleteStudent(Integer i);
    public void updataStudent(Student s);
}

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="mapper.StudentMapper">
   <!-- id值爲接口中表示增加學生的方法名 -->
   <insert id="addStudent" parameterType="Student">
        <!-- 寫一個類似sql的語句,實際是表達映射關係-->
        INSERT INTO JD1709_STUDENT
        VALUES(#{id},#{name},#{birthday},#{phone})
   </insert>
   <resultMap type="Student" id="StudentResult">
                    <id property="id" column="id" />
                    <result property="name" column="name" />
                    <result property="birthday" column="birthday" />

   </resultMap>
   <select id="selectStudentAll" resultMap="StudentResult">
      SELECT * FROM JD1709_STUDENT
   </select>
   <select id="selectIDStudent" parameterType="Integer" resultType="Student">
        SELECT ID,NAME,BIRTHDAY,PHONE FROM JD1709_STUDENT
        WHERE ID=#{id}
   </select>
   <delete id="deleteStudent" parameterType="Integer">
        DELETE FROM JD1709_STUDENT
        WHERE ID=#{id}
   </delete>
   <update id="updataStudent" parameterType="Student">
        UPDATE JD1709_STUDENT
        SET NAME=#{name},BIRTHDAY=#{birthday}
        WHERE ID=#{id}
   </update>
</mapper>

注意,如果對象的屬性與數據庫中表的名字相同,mybatis會自動完成映射,如果不同,需要使用resultMap元素手動映射

完成配置文件和pojo類和映射接口的建立,就可以使用這些東西完成我們想要的插入和刪除等任務,流程如下:

/1.讀取配置文件的輸入流
             InputStream in = Resources.getResourceAsStream("mybatis-config.xml");
             //2.拿到工廠,通過建造者模式拿到
             SqlSessionFactory factory = new SqlSessionFactoryBuilder().build(in);
             //3.拿到SqlSession
              session = factory.openSession();
             //4.通過接口拿到子類對象
              mapper= session.getMapper(StudentMapper.class);

然後就可以使用mapper來直接完成我們的插入指令

Student s = new Student(1,"Tom",new Timestamp(System.currentTimeMillis()),new PhoneNum("153-83467865"));

             mapper.addStudent(s);

這樣就簡單的完成了mybatis的使用,記得在使用的最後完成資源的釋放,commot和close操作。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章