1. 程式人生 > 其它 >Mybatis的兩個xml配置檔案和test檔案

Mybatis的兩個xml配置檔案和test檔案

技術標籤:mybatismybatis

Dao中的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="org.example.Dao.StudentDao"
> <select id="GetStudentData" resultType="org.example.Entity.Student"> select id,name,email,age from student </select> <!-- select表示查詢操作 id:你要執行的sql語法的唯一標識,mybatis會使用這個id值來找到執行sql語句 可以自定義,但是要求使用介面中的方法名 resultType:表示結果型別的,是sql語句執行後得到的ResultSet,
遍歷這個ResultSet得到java物件的型別 值寫的型別是全限定名稱 --> </mapper> <!-- sql對映檔案:寫sql語句的,mybatis會執行這些sql 1.指定約束檔案 <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd"> mybatis-3-mapper.dtd是約束檔案的名稱,拓展名是dtd的 2.
約束檔案作用:限制,檢查在當前檔案中出現的標籤,屬性必須符合mybatis的要求 3.mapper 是當前檔案的根標籤,必須的。 namespace:叫做名稱空間,唯一的值,可以是自定義的字串 要求使用dao介面的全限定名稱 4.在當前檔案中,可以使用特定的標籤,表示資料庫的特定操作。 <select>:表示執行查詢,select語句 <update>:表示更新資料庫的操作,就是在<update>標籤中寫的是update sql語句 <insert>:表示插入,執行的insert語句 <delete>:表示刪除,執行的delete語句 -->

Resource中的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>
<!--    環境配置:資料庫的連線資訊
        default:必須和某個environment的id值一樣
        告訴Mybatis使用哪個資料庫的連線資訊。也就是訪問哪個資料庫
-->
    <environments default="online">
<!--      environment  一個數據庫資訊的配置
          id:一個唯一值,自定義,表示環境的名稱。
-->
<!--        表示線上的資料庫,是專案真實使用的庫-->
        <environment id="online">
<!--            transactionManager:mybatis的事務型別
                type:JDBC(表示使用jdbc中的Connection物件的commit,rollback做事務處理)
-->
            <transactionManager type="JDBC"/>
<!--        dataSource表示資料來源,連線資料庫的
            type表示資料來源的型別  POOLED表示使用連線池
  -->
            <dataSource type="POOLED">
<!--               driver,url,username,password 是固定的不能自定義-->
<!--                資料庫的驅動類名-->
                <property name="driver" value="com.mysql.cj.jdbc.Driver"/>
<!--                連線資料庫的url字串-->
                <property name="url" value="jdbc:mysql://localhost:3306/syfwfl?"/>
<!--                訪問資料庫的使用者名稱稱-->
                <property name="username" value="root"/>
<!--                密碼-->
                <property name="password" value="19980605"/>
            </dataSource>
        </environment>
    </environments>
<!--    sql mapper(sql對映檔案)的位置-->
    <mappers>
<!--        一個mapper標籤指定一個檔案的位置 從類路徑開始的路徑資訊  target/classes(類路徑) 定位sql語句-->
        <mapper resource="org/example/Dao/StudentDao.xml"/>
    </mappers>
</configuration>
<!--
    mybatis的主配置檔案:主要定義了資料庫的配置資訊,sql對映檔案的位置
    1.約束檔案
    <!DOCTYPE configuration
        PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-config.dtd">
    mybatis-3-config.dtd:約束檔案的名稱
    2.configuration:根標籤  environments為環境配置
-->

測試檔案程式碼,查詢

 //訪問mybatis
        //1.定義mybatis主配置檔名稱,從類路徑根開始(target/classes)
        String config="mybatis.xml";
        //2.讀取這個config表示的檔案
        try {
            InputStream input= Resources.getResourceAsStream(config);
            //3.建立了SqlSessionFactoryBuilder物件
            SqlSessionFactoryBuilder builder=new SqlSessionFactoryBuilder();
            //4.建立SqlSessionFactory
            SqlSessionFactory factory=builder.build(input);
            //5.[重要]獲取SqlSession物件,從SqlSessionFactory中獲取
            SqlSession sqlSession= factory.openSession();
            //6.指定要執行的sql語句標識 sql對映檔案中的namespace+"."+標籤的id值、
            String sqlId="org.example.Dao.StudentDao"+"."+"GetStudentData";
            //7.執行sql語句,通過sqlId找到語句
            List<Student> studentList=sqlSession.selectList(sqlId);
            //8.輸出結果
            studentList.forEach(System.out::println);
            //9.關閉sqlSession物件
            sqlSession.close();
        } catch (IOException e) {
            e.printStackTrace();
        }

插入程式碼(介面有資料傳入)
例如:public int UpdStuData(Student student);

        //訪問mybatis
        //1.定義mybatis主配置檔名稱,從類路徑根開始(target/classes)
        String config="res.xml";
        //2.讀取這個config表示的檔案
        try {
            InputStream input= Resources.getResourceAsStream(config);
            //3.建立了SqlSessionFactoryBuilder物件
            SqlSessionFactoryBuilder builder=new SqlSessionFactoryBuilder();
            //4.建立SqlSessionFactory
            SqlSessionFactory factory=builder.build(input);
            //5.[重要]獲取SqlSession物件,從SqlSessionFactory中獲取
            SqlSession sqlSession= factory.openSession();
            //6.指定要執行的sql語句標識 sql對映檔案中的namespace+"."+標籤的id值、
            String sqlId="Dao.StudentDao"+"."+"UpdStuData";
            //7.執行sql語句,通過sqlId找到語句
            Student student=new Student();
            student.setId(3);
            student.setName("王國熹");
            student.setAge(23);
            student.setEmail("fawfaw");
            int ret=sqlSession.insert(sqlId,student);
            sqlSession.commit();
            //8.輸出結果
            System.out.println("修改成功"+ret+"條資料");
            //9.關閉sqlSession物件
            sqlSession.close();
        } catch (IOException e) {
            e.printStackTrace();
        }

它的介面xml配置

    <update id="UpdStuData">
        insert into student values (#{id},#{name},#{email},#{age})
    </update>