1. 程式人生 > >早期MyBatis開發與接口式Mybatis開發的簡介

早期MyBatis開發與接口式Mybatis開發的簡介

數據庫事務 返回 org develop post eem val result code

早期MyBatis開發與接口式Mybatis開發的簡介

一、早期版本的myBatis使用

導jar包
1、配置mybatis.xml的配置文件
1)、需要加載數據庫配置文檔

    <properties resource="db.properties" />

2)、配置數據源,數據庫連接池、處理事務方式

 <environments default="development">
     <environment id="development"
> <transactionManager type="JDBC"/> <!-- 鏈接數據庫 的數據池 --> <dataSource type="POOLED"> <property name="username" value="${jdbc.username}" /> <property name="password" value="${jdbc.password}" /> <
property name="url" value="${jdbc.url}" /> <property name="driver" value="${jdbc.driver}" /> </dataSource> </environment> </environments>

3)、配置映射文件 即xml中配置的sql語句文件 文件位置需要使用/ 不能使用 .

 <mapper url="file:///var/mappers/AuthorMapper.xml"
/> <mapper resource="org/mybatis/builder/PostMapper.xml"/> <mappers> <mapper resource="com/da/wei/mapper.xml"/> </mappers>

2、配置映射mapper.xml

  <mapper namespace="com.cn.mybatis.mapper.EmployeeMapper">
      <!-- 命名空間使用將要映射執行方法的接口文件  在此處,原始的方法中,命名空間的作用並不明顯-->
      <!-- 查詢所以字段信息 -->
      <select id="selectAllEmployee" resultType="com.cn.mybatis.entity.Employee">   
         SELECT     emp_id AS empId,emp_name AS empName,emp_gender AS empGender,emp_email AS empEmail FROM employee
      </select>
      <!-- 通過id號查詢員工信息 -->
      <select id="selectEmployeeById" resultType="com.cn.mybatis.entity.Employee" parameterType="java.lang.Integer">
         SELECT     emp_id AS empId,emp_name AS empName,emp_gender AS empGender,emp_email AS empEmail
          FROM employee WHERE emp_id = #{empId}
      </select>
  </mapper>

3、執行sql操作
執行步驟:
1、配置資源文件
2、設置輸入流
3、通過輸入流創建會話工廠
new SqlSessionFactoryBuilder().build(iS);
4、通過會話工廠創建會話
sqlSessionFactory.openSession();
5、通過會話執行sql
sqlSession.selectOne("com.cn.mybatis.mapper.EmployeeMapper.selectEmployeeById",1);
其中第一個參數是映射過去的參數,即需要執行的方法體的全限定名
第二個是參數,需要與設置的值類型(方法體的參數類型統一)
這裏可以直接強轉到想獲得的類型,默認為Object
sqlSession.selectList("com.cn.mybatis.mapper.EmployeeMapper.selectAllEmployee",Employee.class);
這裏查詢返回list 可以通過使用類類型設置返回值的類型,可是不能使用參數

   6、關閉會話
   sqlSession.close();

 1   private static SqlSessionFactory getSqlSessionFactory(){
 2       //1、配置資源文件
 3       String source = "mybatis.xml";
 4       //2、設置輸入流
 5       InputStream iS = null;
 6       //3、創建會話工廠
 7       SqlSessionFactory sqlSessionFactory = null;
 8       try {
 9             //4、將配置文件以流的方式讀入
10             iS = Resources.getResourceAsStream(source);
11             //5、根據配置文件創建會話工場
12            sqlSessionFactory = new SqlSessionFactoryBuilder().build(iS);
13            return sqlSessionFactory;
14              } catch (IOException e) {
15               // TODO Auto-generated catch block
16                e.printStackTrace();
17             }
18       return null;
19 }
20 public static void test01(){
21     SqlSessionFactory sqlSessionFactory = null;
22     SqlSession sqlSession = null;
23     //獲取會話工廠
24     sqlSessionFactory = getSqlSessionFactory();
25     if(sqlSessionFactory!=null){
26         //6、由會話工程創建會話
27         sqlSession = sqlSessionFactory.openSession();
28         //7、由會話執行sql
29        Employee employee = sqlSession.selectOne("com.cn.mybatis.mapper.EmployeeMapper.selectEmployeeById",1);
30        System.out.println(employee);
31        System.out.println("==============標準割==============");
32        List<Employee> listEmployee = sqlSession.selectList("com.cn.mybatis.mapper.EmployeeMapper.selectAllEmployee",Employee.class);
33         //    System.out.println(listEmployee);
34         for(Employee emp : listEmployee){
35                 System.out.println(employee);
36           }           //sqlSession.selectList("com.cn.mybatis.mapper.EmployeeMapper.selectAllEmployee", rowBounds)
37          sqlSession.close(); 
38    }
39 }

早期版本的myBatis其執行的過程為,
1、在使用時,通過引用資源mybatis.xml,創建會話工廠
2、業務執行時,加載mybatis的配置文件
在mybatis中加載數據庫的配置文件db.properties文件,進行數據庫數據源配置,同時進行數據連接池,數據庫事務管理的配置。
3、在mybatis中加載sql映射文件mapper.xml
4、會話工廠創建會話,通過創建出來的會話進行功能調用
調用時,其中的statement參數需要執行,執行方法的全限定名。
5、使用過後進行會話的關閉。

二、接口式版本開發

   接口版本配置基本一致

    1、mybatis.xml配置
其中映射使用的mapper.xml需要指定到對應的包下
2、mapper.xml配置
按照開發規範,這裏需要將mapper的名字配置與接口方法同名且在同一包下。
且命名空間此時用有效需要設置,其值是對應的mapper.xml的接口文件全限定名。
3、使用

     在調用時不再是以前的那種statement的配置方式了
通過創建出來的會話,通過獲取映射的方式,獲取mapper方法接口的代理對象,使用其代理對象進行方法體的直接調用
//由會話工程創建會話
sqlSession = sqlSessionFactory.openSession();
//通過會話獲取方法->代理對象
EmployeeMapper employeeMapper = sqlSession.getMapper(com.cn.mybatis.mapper.EmployeeMapper.class);
List<Employee> listEmployee = employeeMapper.selectAllEmployee();
Employee employee = employeeMapper.selectEmployeeById(4);

以上兩種方式的比較:

  1、接口方式的sql語句mapper.xml的需要按照規範,與接口方法放在同包下,且保持同名。
早期方法只需要在mybatis下指定對應的地址即可
接口的方式更加規範和易讀,可以提高開發效率。
2、接口方法在調用時,接口采用通過會話工廠創建代理對象的方式,使用代理對象調用接口中的方法使之實現。
早期方法使用會話直接使用,使用會話的方法指定對應的statement(方法的全限定名)進行sql操作。
接口方式由於直接調用方法,因此可以對參數進行檢驗驗證。

早期MyBatis開發與接口式Mybatis開發的簡介